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

Setting window.opener to null doesn't work in Electron #2220

Closed
codebykat opened this issue Nov 15, 2020 · 1 comment
Closed

Setting window.opener to null doesn't work in Electron #2220

codebykat opened this issue Nov 15, 2020 · 1 comment
Assignees
Labels
Milestone

Comments

@codebykat
Copy link

@codebykat codebykat commented Nov 15, 2020

TL;DR: I just resorted to spoofing the User-Agent in Electron to get window.open working with external windows. 🤦‍♀️

Similar issue to: #628

Electron uses its own version of window.open, which always returns null (does not return a handle to the window). There's now an option (nativeWindowOpen) to use the native window.open, which gets external links to work (opening a new Electron window), but if you need to open external links in the default browser instead, there's no way to get the URL Monaco is attempting to open. The initial window.open() request will trigger new-window, but without the url in that call, it's a dead end.

The upshot is, if you're using the common pattern of hooking to the new-window event to trap external links and open them in the default browser, "Follow Link" will silently fail (or, on Windows, open a dialog saying you have to choose an app to open "this about link", presumably because it doesn't have a default handler registered for "about:blank").

I unfortunately found this extremely hard to work around, because there's just no way to get the requested URL if the Opener receives a null in response to window.open. At one point I resorted to adding a click event to the editor DOM and checking to see if the click took place on a detected-link-active. 😰 I also tried the hack of allowing the window to open and listening for will-navigate events on it, then closing it and going to the requested URL; but since there's no way to modify the options on the initial window.open, it can't be loaded as a hidden window, so the user would see it open and immediately close before opening the browser window.

Adding "Electron" to the user-agent / platform detection and another boolean here would fix the problem.

Alternatively, maybe we could use some kind of option to toggle this behavior back to the plain window.open(url).

My terrible hacks and eventual user-agent "fix" are here: Automattic/simplenote-electron#2470

n.b. there are apparently Electron changes in the pipeline that will make this easier to work around, but it would still be good not to have to hack around it. electron/electron#24517

monaco-editor version: 0.20.0
Browser: Electron 9.1.0 (tested in 10 and 11-beta as well)
OS: OSX Catalina, also reproduced on Windows 10
Playground code that reproduces the issue:

( Any that contains a detectable link )

Electron external window setup:

    mainWindow.webContents.on('new-window', function (event, linkUrl) {
      event.preventDefault();
      shell.openExternal(linkUrl);
    });
@codebykat
Copy link
Author

@codebykat codebykat commented Jan 6, 2021

alexdima closed this in microsoft/vscode@d746f2d 8 days ago

@alexdima and @nrayburn-tech Thank you for the fix! 👏

@github-actions github-actions bot locked and limited conversation to collaborators Jan 28, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

2 participants