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

window.opener null with nativewindowopen set to true #17869

Closed
3 tasks done
pushkin- opened this issue Apr 18, 2019 · 3 comments
Closed
3 tasks done

window.opener null with nativewindowopen set to true #17869

pushkin- opened this issue Apr 18, 2019 · 3 comments

Comments

@pushkin-
Copy link

pushkin- commented Apr 18, 2019

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.

@maxerbox
Copy link

maxerbox commented Apr 19, 2019

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)}}};

@pushkin-
Copy link
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!

@codebytere
Copy link
Member

Closed by #18173

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

No branches or pull requests

3 participants