Webview.executeJavaScript does not always work. Bug?


#1

I’m programming a kind of web site crawler.

There is a main page loading the site in a webview: when the page is loaded in the webview, an event is sent from the webview to the host, and the host calls executeJavascript on the webview to perform some action. The problem is that sometimes the javascript code is executed in the webview, sometimes not.

Here is the actual code of my program in the host page:

function log(message) {
  console.log(counter + ' ' + message);
  counter = counter + 1;
}

// Called by <body onload='init()'> in the host web page
function init() {
    webview = document.querySelector('webview');
    // Handling webview messages
    webview.addEventListener('ipc-message', function(e) {
      log('Event catched: ' + e.channel);
      switch (e.channel) {
        case 'WebComponentsReady': {
          webview.executeJavaScript('dumpSource();');
          log('dumpSource called');
          break;
        }
        // Some other code here...
        // and then...
        default : {
          log("Bouhhhhhh ...");
        }
      }
    });

After an apparently random number of successfull calls of executeJavascript, the dumpSource function is not executed in the webview, but the message “dumpSource called” is visible in the console.

Maybe the call of executeJavascript is too ‘temporally close’ from the call of the ipc.sendToHost in webview that sends the ipc-message to the host?

Maybe it’s a bug?

I know I could replace the call to executeJavascript with a call to webview.send, but I want to understand what’s happening here.(EDIT: done this. Works perfectly. executeJavascript is the problem)

Any idea?