Javascript 在浏览器中打开来自 Electron 的链接

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31749625/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 07:07:46  来源:igfitidea点击:

Make a link from Electron open in browser

javascriptnode.jselectron

提问by saadel

Is there any (simple/built-in way) to open a new browser (I mean default OS browser) window for a link from Electron instead of visiting that link inside your Electron app ?

是否有任何(简单/内置的方式)打开一个新的浏览器(我的意思是默认操作系统浏览器)窗口以获取来自 Electron 的链接,而不是访问您的 Electron 应用程序中的该链接?

回答by zianwar

You can simply use :

您可以简单地使用:

require("shell").openExternal("http://www.google.com")

回答by niieani

There's a much better and simpler way, than what @Marcelo proposed, yet easier to implement for all links at once to what @zianwar proposed.

有一种比@Marcelo 提议的更好、更简单的方法,但更容易同时实现所有链接到@zianwar 提议的方法。

const shell = require('electron').shell;

// assuming $ is jQuery
$(document).on('click', 'a[href^="http"]', function(event) {
    event.preventDefault();
    shell.openExternal(this.href);
});

NOTE: Requires jQuery.

注意:需要 jQuery。

回答by Marcelo Lazaroni

To make all Electron links to open externally in the default OS browser you will have to add an onclickproperty to them and change the hrefproperty so it doesn't load anything in the Electron app.

要使所有 Electron 链接在默认操作系统浏览器中从外部打开,您必须onclick向它们添加一个属性并更改该href属性,以便它不会在 Electron 应用程序中加载任何内容。

You could use something like this:

你可以使用这样的东西:

aTags = document.getElementsByTagName("a");
for (var i = 0; i < aTags.length; i++) {
  aTags[i].setAttribute("onclick","require('shell').openExternal('" + aTags[i].href + "')");
  aTags[i].href = "#";
}

But make sure the entire document has loaded beforedoingthis otherwise it is not going to work. A more robust implementation would look like this:

但是请确保执行此操作之前已加载整个文档,否则它将无法工作。更健壮的实现如下所示:

if (document.readyState != "complete") {
  document.addEventListener('DOMContentLoaded', function() {
    prepareTags()
  }, false);
} else {
  prepareTags();
}

function prepareTags(){
  aTags = document.getElementsByTagName("a");
  for (var i = 0; i < aTags.length; i++) {
    aTags[i].setAttribute("onclick","require('shell').openExternal('" + aTags[i].href + "')");
    aTags[i].href = "#";
  }
  return false;
}

Remember that if you load external files you will have to make them go through this process as well afterthey are fully loaded.

请记住,如果您加载外部文件,则必须它们完全加载也使它们经历此过程。

回答by Arjun Kava

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

Requires that you use target="_blank" on your anchor tags.

要求您在锚标记上使用 target="_blank"。

回答by wani geoffrey

To run an Electron project in your actual browser (Chrome, Mozilla, etc), add this to your script are external script:

要在您的实际浏览器(Chrome、Mozilla 等)中运行 Electron 项目,请将以下内容添加到您的脚本中:

aTags = document.getElementsByTagName("a");
for (var i = 0; i < aTags.length; i++) {
     aTags[i].setAttribute("onclick","require('shell').openExternal('" + aTags[i].href + "')");
     aTags[i].href = "#";
}