Reputation: 327
So Basically i want to call
masslinker://123/456
and it's supposed to goto show id 123 episode number 456. How do i get those inputs?
I Currently have this line of code
app.setAsDefaultProtocolClient('masslinker');
Upvotes: 2
Views: 1020
Reputation: 3517
Citing from the corresponding docs:
Once registered, all links with your-protocol:// will be opened with the current executable. The whole link, including protocol, will be passed to your application as a parameter.
That means that a new instance of your app will be launched whenever a URL of that type is requested.
You should therefore check for arguments on app startup and if there is one, you can handle it:
if (process.argv.length >= 3) {
const url_to_open = process.argv[2];
console.log("Received: " + url_to_open);
// should print:
// Received: masslinker://123/456
// now take URL apart using string operations ..
}
The exact number of parameters may change depending on how you launch your app and/or when you go into production. You may work around that by checking if the last argument matches your protocol prefix.
Note that if you don't take further measures, this will open a new instance of your app for each URL you open. You can counteract that using app.requestSingleInstanceLock()
and the second-instance
event. The event handler will get as second parameter all the command line parameters of the new instance, so that you can handle them in the first instance.
Further note that setting up a protocol handler seems to be quite dependent on the operating system and may be part of the installation procedure of your app. However, once the handler is set up, processing incoming calls should work as above regardless of the operating system.
Upvotes: 1