【发布时间】:2019-11-01 09:25:47
【问题描述】:
我正在使用 Electron 开发一个应用程序,我需要在这个应用程序中处理一个自定义协议。
我为此使用app.setAsDefaultProtocolClient(PROTOCOL)。
我在 macOS 上使用“open-url”通过我的自定义协议处理 URL,它运行顺利,但我无法在 Windows 上解决。我在 URL 中发送一些数据,所以仅仅打开窗口是行不通的。
我检查了this answer,但这在 2016 年得到了回答,现在不推荐使用 makeSingleInstance 方法。在文档中,它建议使用requestSingleInstanceLock,但它不接受任何回调或返回 URL。
那么如何在 macOS 和 Windows 中启用相同的功能?
代码
index.js
app.on('ready', () => createWindow(`file://${__dirname}/views/welcome.html`));
app.on('activate', () => {
// eslint-disable-next-line no-shadow,global-require
const { mainWindow } = require('./utils/createWindow');
// On OS X it's common to re-create a window in the app when the
// dock icon is clicked and there are no other windows open.
if (mainWindow === null) {
createWindow(`file://${__dirname}/views/welcome.html`);
}
});
app.on('open-url', handleOpenURL);
app.setAsDefaultProtocolClient(PROTOCOL);
handleOpenURL.js
module.exports = (e, data) => {
e.preventDefault();
// Some other Logic
createWindow(URL);
}
【问题讨论】:
-
您能否详细说明问题,或者您可以粘贴示例代码吗?
-
添加基本代码
-
我认为没有可用于 Windows 的教程来处理相同的情况。您可以注册协议。并寻找处理这个问题的过程争论。使用 process.argv .
-
我试过这个例子:electronjs.org/docs/all#apprequestsingleinstancelock。我尝试打印
event, commandLine, workingDirectory,但这个事件永远不会被执行。
标签: javascript windows electron