Registers protocol like myapp:// to open your nodejs app from browsers.
This is meant to be used in command-line tools and scripts, not in the browser.
- Actively maintained.
- Handles Cross Platform.
- Supports WSL paths to Windows apps.
$ npm install protocol-registry
const path = require('path');
const ProtocolRegistry = require('protocol-registry');
console.log('Registering...');
// Registers the Protocol
ProtocolRegistry.register({
protocol: 'testproto', // set your app for testproto://**
command: `node ${path.join(__dirname, './tester.js')}`, // this will be executed with a extra argument %url from which it was initiated
override: true, // Use this with caution as it will destroy all previous Registrations on this protocol
terminal: true // Use this to run your command inside a terminal
}).then(async () => {
console.log('Successfully registered');
});
At present it supports :
Options are mentioned in the above example. If a valid callback is provided then it returns cb(err) Otherwise it returns a promise.
const path = require('path');
const ProtocolRegistry = require('protocol-registry');
// Registers the Protocol
ProtocolRegistry.register({
protocol: 'testproto',
command: `node ${path.join(__dirname, './tester.js')}`,
terminal: true
}).then(()=>{
// do something
}).catch((e)=>{
// do something
})
// Above will run tester.js when testproto://** is called as
// node .../tester.js testproto://**
// you can further parse the url to run in different modes
// As overide is not passed true it will throw an error is protocol already exists
ProtocolRegistry.register({
protocol: 'testproto',
command: `node ${path.join(__dirname, './tester.js')}`,
terminal: true
},(err)=>{
if(err){
// do something
}
})
// Example with callback
ProtocolRegistry.register({
protocol: 'testproto',
command: `node ${path.join(__dirname, './tester.js')}`,
terminal: false // Terminal is set to false
},(err)=>{
if(err){
// do something
}
})
// The above code will run your command in background
// You wont be able to see any logs
// But if your program launches any UI / webpage / file will be visible
Checks if the provided protocol already exists or not. Returns a Promise which resolves in true or false.
const path = require('path');
const ProtocolRegistry = require('protocol-registry');
// Registers the Protocol
ProtocolRegistry.checkifExists('testproto').then((res)=>{
console.log(res) // true or false
// do something
}).catch((e)=>{
// do something
})
// Above snippet will check it already some app uses the given protocol or not
|