最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Force open link in external browser in ElectronJS application - Stack Overflow

programmeradmin2浏览0评论

I put a webapp into ElectronJS framework. In the webapp (actually webpage) there are some links target=_blank. However when I click the link, it will open another Electron browser window. Expectedly I want the link to be shown in default web browser.

What I can think about is to trap such navigation event and then send a message to the main browser window and let it spawn a default browser instance to show the link.

I think this is a bit plicated. I'm seeking an easier way to do this.

I put a webapp into ElectronJS framework. In the webapp (actually webpage) there are some links target=_blank. However when I click the link, it will open another Electron browser window. Expectedly I want the link to be shown in default web browser.

What I can think about is to trap such navigation event and then send a message to the main browser window and let it spawn a default browser instance to show the link.

I think this is a bit plicated. I'm seeking an easier way to do this.

Share Improve this question asked Nov 2, 2016 at 0:49 stanleyxu2005stanleyxu2005 8,27115 gold badges61 silver badges99 bronze badges 1
  • Does this answer your question? How can I force external links from browser-window to open in a default browser from Electron? – icc97 Commented Aug 24, 2023 at 13:19
Add a ment  | 

1 Answer 1

Reset to default 9

You've got the right idea, you need to listen to the new-window event and forward the URL to the default browser. You'd implement it in the main process like so:

import { shell } from 'electron'

mainWindow.webContents.on('new-window', (event, url) => {
  // stop Electron from opening another BrowserWindow
  event.preventDefault()
  // open the url in the default system browser
  shell.openExternal(url)
})

Note that the new-window event actually provides some additional information about the window to be opened (not just the URL), it may make sense for you to take some of that info into account to figure out if the URL should be forwarded to the default browser or not.

发布评论

评论列表(0)

  1. 暂无评论