使用 HTML 锚标记在 Electron 中打开外部链接

Opening external links in Electron with the HTML anchor tag

我正在制作一个 Electron 应用程序,我想在我的 index.html 文件中制作一个按钮来打开我的 GitHub 的默认互联网浏览器(例如 Chrome)存储库网站。我已经看到关于此的其他 Whosebug 问题并获得了成功的答案,但它们包含 JavaScript 的片段,我不知道将它们放在哪里。

这个答案引用了问题的早期模棱两可的版本,然后暗示需要打开一个新的电子window。

当你说 "external link" 时,我假设你想打开另一个包含你的 github 存储库的 window。

这可以通过将以下代码添加到您的 main.js:

开头:

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

app.on函数内:

var externalWindow = new BrowserWindow ({
    width: 800,
    height: 600
})

app.on函数后:

ipcMain.on('loadGH', (event, arg) => {
    externalWindow.loadURL(arg);
});

在你的头部 index.html 然后你需要实例化 ICP 模块:

<script>
const ipc = require('electron').ipcRenderer;
</script>

然后在href中使用onclick事件来实际执行新的加载window:

<a HREF="#" onclick="ipc.send('loadGH','http://github.com/yourGitHubName');">Link</a>

使用 shell module:

将以下代码添加到您的 main.js:

开头:

const {ipcMain} = require('electron');
const {shell} = require('electron');

app.on函数后:

ipcMain.on('loadGH', (event, arg) => {
    shell.openExternal(arg);
});

在你的头部 index.html 然后你需要实例化 ICP 模块:

<script>
const ipc = require('electron').ipcRenderer;
</script>

然后使用onclick事件实际执行新的加载window:

<a HREF="#" onclick="ipc.send('loadGH','http://github.com/yourGitHubName');">Link</a>