如何强制从浏览器窗口打开外部链接以在Electron的默认浏览器中打开? [英] How can I force external links from browser-window to open in a default browser from Electron?

查看:1255
本文介绍了如何强制从浏览器窗口打开外部链接以在Electron的默认浏览器中打开?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 BrowserWindow 来显示应用程序并我想强制在默认浏览器中打开外部链接.甚至有可能还是我必须采取不同的方法?

I'm using the BrowserWindow to display an app and I would like to force the external links to be opened in the default browser. Is that even possible or I have to approach this differently?

推荐答案

在检查了上一个答案的解决方案后,我想到了这一点.

I came up with this, after checking the solution from the previous answer.

mainWindow.webContents.on('new-window', function(e, url) {
  e.preventDefault();
  require('electron').shell.openExternal(url);
});

根据电子规范,点击外部链接时会触发new-window.

According to the electron spec, new-window is fired when external links are clicked.

注意:要求您在锚标记上使用target="_blank".

这篇关于如何强制从浏览器窗口打开外部链接以在Electron的默认浏览器中打开?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆