Description
- Electron version: 1.7.3
- Operating system: Ubuntu 16.10
I installed electron-quick-start and changed the electron dependency to 1.7.3. I only modified the index.html file:
<html>
<head>
<meta charset="UTF-8">
<title>Hello World!</title>
</head>
<body>
<webview id="webview" src='https://github.com'></webview>
</body>
<script>
var webview = 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 foo
in 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.
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.