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

External URLs should open in system browser #34

Closed
simonw opened this issue Sep 1, 2021 · 3 comments
Closed

External URLs should open in system browser #34

simonw opened this issue Sep 1, 2021 · 3 comments
Labels
electron-wrapper Features that go in the Node.js/Electron code

Comments

@simonw
Copy link
Owner

simonw commented Sep 1, 2021

For example the "powered by Datasette" link, or URLs in tables that get converted into links.

@simonw simonw added the electron-wrapper Features that go in the Node.js/Electron code label Sep 1, 2021
@simonw simonw added this to the First public installer release milestone Sep 1, 2021
@simonw
Copy link
Owner Author

simonw commented Sep 1, 2021

Relevant documentation: https://www.electronjs.org/docs/api/window-open

@simonw
Copy link
Owner Author

simonw commented Sep 1, 2021

This recipe looks good: https://stackoverflow.com/a/67409223

mainWindow.webContents.setWindowOpenHandler(({ url }) => {
  shell.openExternal(url);
  return { action: 'deny' };
});

@simonw
Copy link
Owner Author

simonw commented Sep 1, 2021

setWindowOpenHandler() only fires for target="_blank" or window.open() - I need to fire for all navigated links. This recipe seems to work:

function postConfigure(mainWindow) {
  mainWindow.webContents.on("will-navigate", function (event, reqUrl) {
    let requestedHost = new url.URL(reqUrl).host;
    let currentHost = new url.URL(mainWindow.webContents.getURL()).host;
    if (requestedHost && requestedHost != currentHost) {
      event.preventDefault();
      shell.openExternal(reqUrl);
    }
  });
}

@simonw simonw closed this as completed in d19ffec Sep 1, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
electron-wrapper Features that go in the Node.js/Electron code
Projects
None yet
Development

No branches or pull requests

1 participant