Electron - Linux - How to setup deep linking from a web browser?

Viewed 613

What i want to achieve?

Entering a URL scheme like this myApp://someData in the browser should open my app and pass the URL (in Linux). All the examples i have found are good for Windows/Mac Os and do not work in Linux

Im using electron-builder to bundle my Electron app so to configure a scheme, i added the following to my package.json

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

Lets assume that works - i have no idea how to test it.

How do i intercept a myApp://someData URL in my electron app? i tried: app.on('open-url',..... but that is for Mac OS only

i tried protocol.registerFileProtocol('myApp', (request, callback) => {...}) which didnt work as well.

Is this thing even possible in Linux machines? If it is, how?

1 Answers

You need to handle this on "second-instance" event of the app -

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);
        }
    })
}
Related