Skip to content

Webview preload script not surviving webview src change #1843

Closed

Description

@frankhale

I stated 0.27.2 in the issue topic but for me it broke after 0.23. I need a sanity check here. I'm doing a very basic ping/pong type IPC with my applications main window and a webview. This works perfectly fine for me on version 0.23.0 but breaks at 0.24.0 and all the way up to today with 0.27.2. I've looked at the dev tools for the webview and I can tell that my preload script is running but doing any IPC sends never makes it to the webview.

Same code works fine in 0.23.0 and breaks in 0.24 -> 0.27.2.

I'm doing exactly the same very basic IPC as listed in:

https://github.com/atom/electron/blob/master/docs/api/web-view-tag.md#ipc-message

// In embedder page.
webview.addEventListener('ipc-message', function(event) {
  console.log(event.channel);
  // Prints "pong"
});
webview.send('ping');

.....

// In guest page.
var ipc = require('ipc');
ipc.on('ping', function() {
  ipc.sendToHost('pong');
})

Is anyone else having issues with IPC in webviews?

Activity

  1. anaisbetts commented on Jun 1, 2015

    @anaisbetts
    Contributor

    This is working for us in the Slack app, but one thing we've always had to do is patch the process variable back in via process.nextTick:

      const processRef = global.process;
      process.nextTick(function() { global.process = processRef; });
  2. frankhale commented on Jun 1, 2015

    @frankhale
    ContributorAuthor

    Thanks for that! I was able to get a small sample running with a basic ping/pong. I think my problem is that when setting the src of the webview IPC doesn't respond. This is my theory at the moment, I'm going to try my little test app to see if IPC survives after changing src.

  3. frankhale commented on Jun 1, 2015

    @frankhale
    ContributorAuthor

    Yep, after src is changed IPC stops working. Or rather the code that was set in my preload is no longer there to run.

  4. changed the title [-]Is IPC broken for webviews in 0.27.2?[/-] [+]Webview preload script not surviving webview src change[/+] on Jun 1, 2015
  5. frankhale commented on Jun 1, 2015

    @frankhale
    ContributorAuthor

    Here is a small example to demonstrate the behavior.

    https://github.com/frankhale/electron-webview-ipc-test

    When that example is ran it opens a website in a webview, kicks off ping/pong messages and logs them to the console in the dev tools on the main window and webview. At 10 seconds the src is changed to another website. The webview node context is reset? The preload script code is gone and IPC no longer works.

    I just want to stress that version 0.23 works and IPC survives a webview src change. Versions of Electron 0.24 and newer all fail.

    The following issue may also be related: #1823
    Another possible related issue: #1820
    And another one (dang!) #1754

    I am not using nodeintegration in my particular case. The interesting bit is that changing src is blowing things up. I'm looking into the code now to see if I can see anything that may have changed with webview.

  6. frankhale commented on Jun 3, 2015

    @frankhale
    ContributorAuthor

    Thank you!!

  7. steem commented on Jun 8, 2015

    @steem

    @frankhale Have you tried the latest release binaries(electron v0.27.3)? I found that the preload attribute still doesn't work properly.
    Here are the steps:

    1. Create a webview html element, the src is 'about:blank'. Then change the src to a url. It works.
    2. Create a new webview html element again, the src is 'about:blank'. Then change the src to the url of step1. It doesn't work.
    3. Create a new webview html element again, the src is 'about:blank'. Then change the src to a new url(not same with step1). It doesn't work.

    Also, I have tried those steps:

    1. Create a webview html element, the src is a url. It works.
    2. Create a new webview html element, the src is same with step 1. It doesn't work.
    3. Create a new webview html element, the src is not same with step 1. It doesn't work.

    @zcbenz Please help to check the steps when you are available.

    Thanks!

  8. steem commented on Jun 8, 2015

    @steem

    @zcbenz After I tried more, I found that if you change the src to different site(in other words, different domain), it works. And then, if I change the src to the page in the site again, it doesn't work.

  9. frankhale commented on Jun 8, 2015

    @frankhale
    ContributorAuthor

    @steem, I haven't tried 0.27.3 yet but I did compile from master and was using the fix. I have not seen any weird issues with preload with patch #1850 because I am only creating one webview and reusing it for my entire application life cycle. It seems to work fine for me in that particular case.

  10. steem commented on Jun 9, 2015

    @steem

    @frankhale @zcbenz I have tried one webview today, still doesn't work after changing the src two times(note: the src is same). I also have tried reload method, it only works at the first time.
    My test js code is very simple:

    var site_count = 0;
    function preload_test() {
        console.log('----------------: ' + site_count);
        ++site_count;
    }
    setInterval(preload_test, 3000);

    Using 'console-message' event to echo the message.

  11. zcbenz commented on Jun 9, 2015

    @zcbenz
    Contributor

    @steem Changing src to the same value will be ignored, if you want a refresh you should call reload.

  12. steem commented on Jun 9, 2015

    @steem

    @zcbenz Yes, I use reload, but still doesn't work :(

  13. zcbenz commented on Jun 9, 2015

    @zcbenz
    Contributor

    Can you provide an example? Following code works for me:

    /tmp/test.js:

    require('ipc').sendToHost('test');

    In devtools:

    var w = new WebView;
    w.setAttribute('preload', 'file:///tmp/test.js');
    w.addEventListener('ipc-message', function(event) {
      console.log(event.channel);
    });
    w.src = 'http://google.com';
    document.body.appendChild(w);
  14. steem commented on Jun 9, 2015

    @steem

    @zcbenz Here is the sample code:

    HTML:

    <html>
        <head>
            <meta charset="utf-8">
            <title>Test</title>
        </head>
        <body>
          <input type="button" id="test_refresh" value="refresh"><br>
          <input type="button" id="test_change_src" value="change src">&nbsp;<input type="text" id="new_url" value=""><br>
          <script src="js/deps/jquery-2.0.3.min.js"></script>
          <script>
          function create_web_view (id, url) {
            $(document.body).append('<webview autosize="on" id="' + id + '" src="' + url + '"  preload="op/op_min.js" plugins></webview>');
          }
          create_web_view('test1', 'http://www.baidu.com');
          $('#test_change_src').click(function(){
            document.getElementById('test1').src = $('#new_url').val();
          });
          $('#test_refresh').click(function(){
            document.getElementById('test1').reload();
          });
          document.getElementById('test1').addEventListener('console-message', function(e) {
            console.log('webview message: ' + e.message);
          });
          </script>
        </body>
    </html>
    

    op_min.js

    var site_count = 0;
    function preload_test() {
        console.log('----------------: ' + site_count);
        ++site_count;
    }
    setInterval(preload_test, 3000);
    

    If you click the refresh button, the op_min.js only works at the first two click.

    I found that you use absolute path for preload, I will try it again and give you the result.

    Updates:
    The issue still exists if you use absolute path for preload.

  15. steem commented on Jun 10, 2015

    @steem

    @zcbenz I debug the code and find that the it may be caused by v8 engine. The first call of reload will cause the AtomBrowserClient::AppendExtraCommandLineSwitches be called. But if you call reload again, AtomBrowserClient::AppendExtraCommandLineSwitches won't be called. WebContents::LoadURL will be called each time when calling reload.
    The v8 code is libchromiumcontent, where can I get the code and build it?

    Updates:
    It seems that libchromiumcontent code is here: https://github.com/atom/libchromiumcontent. I will try to build it.

  16. steem commented on Jun 12, 2015

    @steem

    @zcbenz @frankhale Finally, I found that it's caused by the following line:
    app.commandLine.appendSwitch('process-per-site');

    After I delete it, it works. If not, after calling reload one time, it doesn't work. Even if you create a new webview, and initialize the src to any page of a website, it doesn't work after one time.

    So, libchromiumcontent may not call AtomBrowserClient::AppendExtraCommandLineSwitches if in 'process-per-site' mode in some cases. Currently, I don't have time to debug libchromiumcontent. @zcbenz , if you are available, please help to check it.

    Thanks!

  17. zcbenz commented on Jun 12, 2015

    @zcbenz
    Contributor

    @steem Thanks for debugging this! Sorry I didn't have time to look into it.

    In Electron every page is forced to use its own process, which is necessary for the node integration support, so if you pass process-per-site to Chromium some pages will then be forced to share the same process, and node integration won't work for them.

  18. frozeman commented on Jun 12, 2015

    @frozeman

    I'm on 0.26.0 and as soon as i change the src, or navigate to another domain, i lose the preloaded script.
    Is this issue not yet in 0.26.0 resolved?

  19. steem commented on Jun 14, 2015

    @steem

    @zcbenz Ok, I see.

    Chromium process model won't create new process if the max process number is reached. Just worry about that whether the issue may come up when the max process number is reached.

  20. frozeman commented on Jun 15, 2015

    @frozeman

    The problem still persists for me at 0.26.0.

  21. frozeman commented on Jun 16, 2015

    @frozeman

    works on 0.28.0 👍

  22. mschnee commented on Sep 8, 2015

    @mschnee

    This bug appears to have returned as of 0.31.2: specifically when the WebView's location is changed via navigation or reloading in the WebView itself.

    Example project here:
    https://github.com/mschnee/electron-bug-example

  23. zcbenz commented on Sep 9, 2015

    @zcbenz
    Contributor

    @mschnee Which operating system are you using? And can you reproduce on 0.32.0?

  24. mschnee commented on Sep 9, 2015

    @mschnee

    @zcbenz Verified as working in [email protected] on Windows 10, OSX 10.10, and Linux (gentoo x64, vanilla kernel 4.2)

    Apologies for necro'ing a bug, looks like you beat me to it!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions