You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Difference between webview.executeJavaScript and webview.getWebContents().executeJavaScript. #9804
I installed electron-quick-start and changed the electron dependency to 1.7.3. I only modified the index.html file:
<html><head><metacharset="UTF-8"><title>Hello World!</title></head><body><webviewid="webview" src='https://github.com'></webview></body><script>varwebview=document.getElementById('webview');webview.addEventListener('dom-ready',()=>{//webview.getWebContents().executeJavaScript(`var a = 'foo'; alert(a); Promise.resolve(a);`)webview.executeJavaScript(`var a = 'foo'; alert(a); Promise.resolve(a);`).then((a)=>console.log(a)).catch((error)=>console.log(error));});</script></html>
Expected behavior
I expected to get an alert foo and fooin the console. I also uncommented the line
webview.getWebContents().executeJavaScript(`var a = 'foo'; alert(a); Promise.resolve(a);`)
and commented out
webview.executeJavaScript(`var a = 'foo'; alert(a); Promise.resolve(a);`)
I expected the same result.
Actual behavior
In the first case, without getWebContents(), I got the alert as expected, but the following error message in the console:
Uncaught TypeError: Cannot read property 'then' of undefined
Whereas, in the second case, with getWebContents(), I got both the the alert foo and foo in the console.
This may actually be only a documentation issue. It just that when I look at the documentation, there is little indication why executeJavaScript on a webview tag Vs on the webContents of this webview, should behave differently.
Reacted by Danny Althoff, Spike O'Carroll, toobigdata and albert-hg
OK, I found the answer. The documentation is very dense and I misunderstood it. These are different methods. In the webview case, no return value is documented, so we have to assume that there is no return value. In the webContents case, the return value is a promise, as documented. I had tried to discuss this issue in a forum before, but the response got lost somewhere. I personally, still find that the documentation should mention explicitly that in the case of webview, the method is executed asynchronously and there is, thus, no return value. Whereas, in the case of webContents, the method synchronously returns a promise. This interesting fact should be made a bit more explicit, but having started to learn Node and Electron a month ago, I am biased.
Activity
Dominic-Mayers commentedon Jun 20, 2017
OK, I found the answer. The documentation is very dense and I misunderstood it. These are different methods. In the webview case, no return value is documented, so we have to assume that there is no return value. In the webContents case, the return value is a promise, as documented. I had tried to discuss this issue in a forum before, but the response got lost somewhere. I personally, still find that the documentation should mention explicitly that in the case of webview, the method is executed asynchronously and there is, thus, no return value. Whereas, in the case of webContents, the method synchronously returns a promise. This interesting fact should be made a bit more explicit, but having started to learn Node and Electron a month ago, I am biased.