Skip to content

[Feature request] window.opener full support #1865

Closed

Description

@felicienfrancois

when opening a new window using window.open, we currently get a BrowserWindowProxy that only support posting messages.

Some websites, webapps and libraries uses window.opener to access properties and objects of the opener window. Thoose are broken in Electron.
That's the case for example of Chromium devtools, that use window.open / window.opener in dockable/undockable mode.

Activity

  1. zcbenz commented on Jun 9, 2015

    @zcbenz
    Contributor

    In Electron every page is forced to run in its own process, however current architecture requires the pages to run in the same process if you want to access one page's contentWindow from the other, so it is not possible to implement it for now.

    Chrome is now working on implementing out of process iframe, when it is done we might be able to implement this.

  2. zcbenz commented on Sep 10, 2015

    @zcbenz
    Contributor

    Seems that windows opened by window.open can run in separated process in Chrome 45, having full window.opener support becomes possible now.

  3. self-assigned this
    on Sep 10, 2015
  4. felicienfrancois commented on Sep 10, 2015

    @felicienfrancois
    Author

    @zcbenz Good news !

  5. zcbenz commented on Sep 22, 2015

    @zcbenz
    Contributor

    After a closer look into the problem, Chromium only allows certain operations to be sent across renderer processes, and from the issue tracking that (https://crbug.com/128768), they doesn't have plan to support all operations.

    So since Electron has a strong requirement that each page runs on a separate process, I'm afraid this is not something that we are able to fix under current architecture.

    I'm still keeping this open in case someone has an idea on how to fix this.

  6. 8 remaining items

  7. hivaga commented on Sep 13, 2016

    @hivaga

    Hi, I am working on a multi-windows application and I need to share a object instances from the main application process with the other windows of the application. Currently in browser you can reference window.opener to achieve this. In browser window.opener (in child window) will give you the instance of the Window object form which window.opent(myURL) was called.
    In Electron when I open window via (window.opent(myURL)) window.opener is replaced with BrowserWindowProxy (Electron API) which does not expose the same window (Window object instance) from which the (window.open(myURL)) was called. Is there a way to access window.opener the same way it works in Browser. I need some hint do you plan to support this in the future and when ?

    Using remote.sharedObject (Electron API) is not an option since it only serialize/deserialize data and can be used for passing data from one window to others but does not give access to same object instances across windows.

  8. sltam commented on Sep 22, 2016

    @sltam

    @Mossop I posted an minimal example at brave/browser-laptop#3270 (comment) of what needs to happen on the app side to use this brave/electron hack. Basically everything needs to be inside a <webview>, and these webviews are uniquely identified by their guest IDs, which allow them to be moved from one BrowserWindow to another.

  9. tarruda commented on Sep 27, 2016

    @tarruda
    Contributor

    This is now supported in electron through the sandbox flag passed to webPreferences when creating a new BrowserWindow.

    I have not documented yet because I still need to push a some extra PRs(tracked by #6712) before the feature is fully complete(should be considered experimental), but it should already possible to use the native window.opener API as long as you can live with a few of limitations:

    • node.js/electron modules are not available in the renderer with one exception: require('electron').ipcRenderer still works on preload scripts. This means you can indirectly use all node.js/electron APIs as long as you set the proper IPC infrastructure in the browser process.
    • spell checking, plugins and <webview> and some extra functionality implemented in normal electron windows might not be available. This is because the sandboxed renderer inherits directly from chromium content renderer, so most of what was implemented by electron on renderer side is not available(this will be fixed in future PRs)

    Some examples of how to use it are available in https://github.com/electron/electron/blob/master/spec/api-browser-window-spec.js

    So far the feature is only available master branch(no prebuilt binaries), but I think it will be on the next released version(cc @zcbenz)

    Any help in testing this flag is appreciated, especially on OSX/windows which are not my native OSes.

  10. tarruda commented on Sep 27, 2016

    @tarruda
    Contributor

    Another limitation about the sandbox flag: The preload script does not have a commonjs environment yet. Instead, the preload code is executed in the context of a function that has require in its scope, but only require('electron').ipcRenderer works for now.

    You can still split your renderer code into multiple modules by using browserify to generate a preload bundle that delegates require('electron') to the enclosing funciton: browserify renderer.js -x electron -o preload.js

  11. lijunle commented on Nov 23, 2016

    @lijunle

    @zcbenz @tarruda It has passed 2 months after the feature is merged into master? Is it available in any version?

  12. tarruda commented on Nov 25, 2016

    @tarruda
    Contributor

    @lijunle it is available on electron 1.4.2 and up

  13. davidrevmakx commented on Nov 28, 2016

    @davidrevmakx

    @tarruda I just tried to do a google auth on Asana.com / trello It doesn't seem to work. I used 1.4.8

  14. davidrevmakx commented on Nov 29, 2016

    @davidrevmakx

    main.js
    app.on('ready', () => { console.log("ready"); const win = new BrowserWindow({webPreferences: { sandbox: true }}) win.loadURL('http://localhost:8888/index.html'); })

    index.html
    <a href="javascript:window.open('https://app.asana.com')">Click here</a>

    When I try accessing window.opener from index.html it says null. Am I doing anything wrong?
    Platform: MacOS Sierra Electron: v1.4.10

  15. lijunle commented on Nov 29, 2016

    @lijunle

    @davidrevmakx The window.opener is available in https://app.asana.com page, not the index.html one.

  16. tarruda commented on Nov 29, 2016

    @tarruda
    Contributor

    @davidrevmakx There's a spec for this scenario, but maybe it is not the correct behavior. Should window.opener be set in children from a different domain? If yes, then please file an issue.

    In the spec the opener is null because that code runs in a separate page, and it is not possible to send a window proxy across IPC. Can you confirm that opener is null in the https://app.asan.com and not in index.html as @lijunle commented?

  17. davidrevmakx commented on Nov 30, 2016

    @davidrevmakx

    @lijunle @tarruda sorry for the confusion. Yes opener is null in https://app.asana.com page.

    @tarruda You were right it isn't working when the children are from a different domain. When I changed my index.html to this

    <a href="javascript:window.open('http://localhost:8888/test.html')">Click here</a>

    test.html has a window.opener object.

    #8100 have created an issue here.

  18. Gvozd commented on Jul 4, 2018

    @Gvozd

    To correct window.opener, and work facebook(and others) logins

    You need use webPreferences.nativeWindowOpen=true, and set same webPreferences.affinity for mainWindow, and opened windows(hook on mainWindow.webContents.on('new-window'))
    https://gist.github.com/Gvozd/2cec0c8c510a707854e439fb15c561b0

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

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions