如何强制从浏览器窗口打开外部链接以在Electron的默认浏览器中打开?

如何强制从浏览器窗口打开外部链接以在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".