nw.js icon indicating copy to clipboard operation
nw.js copied to clipboard

setTimeout not run in webview's Node vm

Open ViccRondo opened this issue 9 years ago • 7 comments

nw ver 0.19.5

It doesn't work when i use setTimeout in a sandbox with node vm module, in webview.

But it works in the normal page.

here is the key code

// index.html
<webview id="sandbox" allownw src="about:blank"></webview>
<script>
  const sandbox = document.getElementById('sandbox')
  setTimeout(function() {
    sandbox.showDevTools(true)
    sandbox.setAttribute('src', 'http://127.0.0.1:8080/sandbox.html')
  }, 1000)
</script>

<script>
  const vm = require('vm')
  vm.runInNewContext('setTimeout(function(){console.log("outer sandbox")}, 10)', {
    console: console,
    setTimeout: setTimeout,
  })
  // can console log outer sandbox
</script>
// sandbox.html
<script>
  const vm = require('vm')
  vm.runInNewContext('setTimeout(function(){console.log("in sandbox")}, 10)', {
    console: console,
    setTimeout: setTimeout,
  })
  // can not console log in sandbox
</script>

ViccRondo avatar Jan 10 '17 07:01 ViccRondo

The timeout callback was fired, but the function call is blocked by Blink because it was defined with the previous window context before navigation thus invalid.

The fix will be available in the next nightly build.

rogerwang avatar Jan 10 '17 10:01 rogerwang

http://dl.nwjs.io/live-build/01-11-2017/nw19-e1d60bf-5e25421-287bf0e-78766fc/v0.19.6/

rogerwang avatar Jan 11 '17 02:01 rogerwang

From his comments, it looks like the final setTimeout isn't working, which will only come into the picture after navigation.

RohitSharma27 avatar Jan 11 '17 02:01 RohitSharma27

@RohitSharma27 It's the Node context which is initialized in the previous window. Then when vm.runInNewContext() is called after navigation, it's executed in the Node context.

rogerwang avatar Jan 11 '17 03:01 rogerwang

So, when we say that webview runs in a separate process, does it mean that it will have it's own node process or will it have it's own separate node context running in the background process or will it share the node context with already created one inside the background page? In case it shares it with previously created node context then I don't see the problem. And, if a new node context is created for the webview then, how come it was initialized in the previous window. I mean this script will only be executed when the new node context is already in place (since this script is inside the sandbox.html which is rendered in the webview). I am sorry. I am just trying to make sense here. There must be something that I am missing.

RohitSharma27 avatar Jan 11 '17 07:01 RohitSharma27

Node always runs in the same (renderer) process with DOM. So as webview runs in an additional separate process, there is a Node running inside of it.

In OP's sample, the webview is loaded with 'about:blank' first, then it was navigated to the remote url. Before the fix, Node was initialized within the previous context (about:blank), after the navigation, it's still there and reused.

rogerwang avatar Jan 11 '17 13:01 rogerwang

@rogerwang This totally makes sense now. Thanks for the efforts.

RohitSharma27 avatar Jan 11 '17 14:01 RohitSharma27