admin管理员组文章数量:1022956
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
1 Answer
Reset to default 9You'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.
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
1 Answer
Reset to default 9You'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.
本文标签: javascriptForce open link in external browser in ElectronJS applicationStack Overflow
版权声明:本文标题:javascript - Force open link in external browser in ElectronJS application - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745593610a2158030.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论