Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Difference between webview.executeJavaScript and webview.getWebContents().executeJavaScript. #9804

Closed
Dominic-Mayers opened this issue Jun 19, 2017 · 1 comment

Comments

@Dominic-Mayers
Copy link

  • 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 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.

@Dominic-Mayers
Copy link
Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant