Skip to content

window.opener null with nativewindowopen set to true #17869

Closed

Description

@pushkin-

Preflight Checklist

  • I have read the Contributing Guidelines for this project.
  • I agree to follow the Code of Conduct that this project adheres to.
  • I have searched the issue tracker for an issue that matches the one I want to file, without success.

Issue Details

  • Electron Version: 4.0.4
  • Operating System: Windows 10
  • Last Known Working Electron version:: 1.8.8

Expected Behavior

I create a BrowserWindow with nativeWindowOpen set to true. It points to a page with a webview that is navigated to a site. I call window.open("somesite") from that page, and the new window's window.opener property is set.

Actual Behavior

window.opener is null

To Reproduce

main.js:

const { app, BrowserWindow } = require('electron')

function createWindow() {
  const mainWindow = new BrowserWindow({
        webPreferences: {
                 nodeIntegration: true,
                 nativeWindowOpen: true
        }
    });

  mainWindow.webContents.once("did-finish-load", () => {
    mainWindow.webContents.send("loadUrl");
  });

  mainWindow.loadFile('index.html')

  app.on("browser-window-created", (event, window) => {
    window.webContents.on("did-finish-load", () => {
      window.webContents.openDevTools();
    });
  });
}

app.on('ready', createWindow)
app.on('window-all-closed', app.quit);

index.html:

<body>
  <webview id="w" src="about:blank;" allowpopups></webview>
  <script>
    require('./renderer.js');
  </script>
</body>

renderer.js:

const { ipcRenderer } = require("electron");

const webview = document.getElementById("w");
ipcRenderer.once("loadUrl", () => {
        webview.openDevTools();
	webview.loadURL("https://google.com");
});

However, if I load the URL on a setTimeout:

ipcRenderer.once("loadUrl", () => {
        webview.openDevTools();
        setTimeout(() => {
        	webview.loadURL("https://google.com");
        }, 1000);
});

window.opener is set correctly when I open windows...

What is going on here? Why does navigating the webview inside of a setTimeout work, but not when navigating it when the webcontents have finished loading. It looks like Electron needs a little more time to set everything up correctly.

I'll also note that logging win.opener from the window that opens the new window shows an opener. Logging it from the new window doesn't:

Main window:

var w = window.open("https://google.com");
w.opener // correct

Window I just launched:

window.opener // null

Additional info:

Replacing webview with BrowserView appears to resolve the issue. However, there is a peculiar inconsistency it seems between BrowserView and regular Chrome. If I navigate my BrowserView to an http google.com, and open an https google.com window, window.opener is null, but I can't reproduce that behavior from Chrome when I open an https site from an http one.

If I navigate my BrowserView to an https site and open an https window from there, window.opener is set.

Side note: As can be seen from the Last Working Electron version bullet point, this used to work for me (when I was on 1.7; tested on 1.8.8 and works there too). I believe I started seeing this issue when I cleared the cache.

Activity

  1. maxerbox commented on Apr 19, 2019

    @maxerbox

    There is another issue (maybe related?) with window.open, it is returning a null object. I went through this with:

    window.originalopen=window.open,window.open=function(){return arguments.length>0?window.originalopen.apply(this,arguments):{set location(n){window.originalopen(n)}}};
  2. pushkin- commented on Apr 19, 2019

    @pushkin-
    Author

    Huh, looks like setting the src of the webview to about:blank; is causing the issue. Rather, initially setting it to a non-https site breaks this.

    1. If I set webview.src to https://google.com
    2. And then loadURL("https://google.com") (without the setTimeout)
    3. window.opener is correctly set!
  3. codebytere commented on Jun 7, 2019

    @codebytere
    Member

    Closed by #18173

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