Skip to content

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

Closed
@Dominic-Mayers

Description

@Dominic-Mayers
  • 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.

Activity

Dominic-Mayers

Dominic-Mayers commented on Jun 20, 2017

@Dominic-Mayers
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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @Dominic-Mayers

        Issue actions

          Difference between webview.executeJavaScript and webview.getWebContents().executeJavaScript. · Issue #9804 · electron/electron