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

BrowserWindow.fromWebContents returns undefined for windows created using window.open #21608

Closed
3 tasks done
pushkin- opened this issue Dec 23, 2019 · 8 comments · Fixed by #33257
Closed
3 tasks done
Labels

Comments

@pushkin-
Copy link

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: 5.0.7
  • Operating System: Windows 10

Expected Behavior

I expect that calling BrowserWindow.fromWebContents(win.webContents) returns win.

Actual Behavior

The API returns undefined.

To Reproduce

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

async function createWindow() {
  var mainWindow = new BrowserWindow({
    webPreferences: {
      nativeWindowOpen: true
    }
  });
  await mainWindow.loadURL("https://google.com");
  mainWindow.webContents.openDevTools();
  console.log(BrowserWindow.fromWebContents(mainWindow.webContents)); // correctly returns mainWindow

  app.on("browser-window-created", (e, window) => {
    console.log(BrowserWindow.fromWebContents(window.webContents)); // returns undefined
    window.once("will-move", (e) => {
      console.log(BrowserWindow.fromWebContents(e.sender)); // extra test; also returns undefined
    });
  });
}

app.on('ready', createWindow)

Run npm start and open a window from devtools (e.g. window.open("https://google.com")). Notice that fromWebContents can't find the browser window for the new window.

@pushkin-
Copy link
Author

I wonder if this will be fixed by this PR

@electron-triage
Copy link

Thank you for taking the time to report this issue and helping to make Electron better.

The version of Electron you reported this on has been superseded by newer releases.

If you're still experiencing this issue in Electron 6.x.y or later, please add a comment specifying the version you're testing with and any other new information that a maintainer trying to reproduce the issue should know.

I'm setting the blocked/need-info label for the above reasons. This issue will be closed 7 days from now if there is no response.

Thanks in advance! Your help is appreciated.

@electron-triage electron-triage added the blocked/need-info ❌ Cannot proceed without more information label Feb 15, 2020
@pushkin-
Copy link
Author

This is still an issue in Electron 8.

@pushkin-
Copy link
Author

Still a problem in Electron 10.1.2

@pushkin-
Copy link
Author

Still repros with Electron 12.0.1.

Though the call to BrowserWindow.fromWebContents(e.sender) now fails with:

image

@nornagon nornagon added platform/all status/confirmed A maintainer reproduced the bug or agreed with the feature 18-x-y and removed platform/windows labels Mar 11, 2022
@nornagon
Copy link
Member

Here's a bugbot-compatible gist: https://gist.github.com/decc0f7190ce719279b5f792711c7e6c

@nornagon
Copy link
Member

@nornagon
Copy link
Member

Fixed with #33257. The will-move handler in your example is incorrect as it passes a BrowserWindow object to BrowserWindow.fromWebContents, and the latter requires a WebContents as its argument.

Sorry it took so long to get to this! At least we fixed it eventually 😅

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

Successfully merging a pull request may close this issue.

4 participants