【问题标题】:Electron - Linux - How to setup deep linking from a web browser?Electron - Linux - 如何从 Web 浏览器设置深度链接?
【发布时间】:2020-10-21 22:08:53
【问题描述】:

我想达到什么目的?

在浏览器中输入像 myApp://someData 这样的 URL 方案应该会打开我的应用程序并传递 URL (在 Linux 中)。 我发现的所有示例都适用于 Windows/Mac Os,但不适用于 Linux

我正在使用 electron-builder 捆绑我的 Electron 应用程序以便配置方案,我将以下内容添加到我的 package.json 中

"protocols": [
        {
            "name": "myApp",
            "role": "Viewer",
            "schemes": [
                "myApp"
            ]
        }
    ]

让我们假设它有效 - 我不知道如何测试它。

如何在我的电子应用程序中截获myApp://someData URL? 我试过了: app.on('open-url',..... 但这仅适用于 Mac OS

我试过protocol.registerFileProtocol('myApp', (request, callback) => {...}) 这也不起作用。

这在 Linux 机器上是否可行?如果是,如何?

【问题讨论】:

    标签: electron electron-builder


    【解决方案1】:

    您需要在应用程序的“第二实例”事件中处理此问题 -

    const gotTheLock = app.requestSingleInstanceLock();
    
    app.commandLine.appendSwitch('autoplay-policy', 'no-user-gesture-required');
    
    if (!gotTheLock) {
        console.warn("Two app instances running");
        app.quit();
    } else {
        app.on("second-instance", (event, argv, workingDirectory) => {
            let url;
            // Protocol handler for win and linux
            // argv: An array of the second instance’s (command line / deep linked) arguments
            if (process.platform == 'win32' || process.platform === "linux") {
                // Keep only command line / deep linked arguments
                url = argv.slice(1);
                console.info("Args = " + url);
            }
        })
    }
    

    【讨论】:

      猜你喜欢
      • 2021-04-24
      • 2015-07-05
      • 2021-05-30
      • 1970-01-01
      • 1970-01-01
      • 2017-01-10
      • 2017-07-05
      • 2012-08-11
      • 1970-01-01
      相关资源
      最近更新 更多