diff --git a/main/index.js b/main/index.js index 20fee9b7..56080042 100644 --- a/main/index.js +++ b/main/index.js @@ -1,10 +1,12 @@ var electron = require('electron') + +var app = electron.app + var ipc = require('./ipc') var menu = require('./menu') var shortcuts = require('./shortcuts') var windows = require('./windows') - -var app = electron.app +var registerProtocolHandler = require('./register-protocol-handler') app.on('open-file', onOpen) app.on('open-url', onOpen) @@ -16,6 +18,7 @@ app.on('ready', function () { menu.init() windows.createMainWindow() shortcuts.init() + registerProtocolHandler() }) app.on('before-quit', function () { diff --git a/main/register-protocol-handler.js b/main/register-protocol-handler.js new file mode 100644 index 00000000..cc167b10 --- /dev/null +++ b/main/register-protocol-handler.js @@ -0,0 +1,26 @@ +module.exports = function () { + if (process.platform === 'win32') { + registerProtocolHandler('magnet', 'URL:BitTorrent Magnet URL', 'WebTorrent.exe') + } +} + +function registerProtocolHandler (protocol, name, command) { + var Registry = require('winreg') + + var protocolKey = new Registry({ + hive: Registry.HKCR, // HKEY_CLASSES_ROOT + key: '\\' + protocol + }) + protocolKey.set('', Registry.REG_SZ, name, callback) + protocolKey.set('URL Protocol', Registry.REG_SZ, '', callback) + + var commandKey = new Registry({ + hive: Registry.HKCR, + key: '\\' + protocol + '\\shell\\open\\command' + }) + commandKey.set('', Registry.REG_SZ, '"' + command + '" "%1"', callback) + + function callback (err) { + if (err) console.error(err.message || err) + } +} diff --git a/package.json b/package.json index e4f60318..976664c7 100644 --- a/package.json +++ b/package.json @@ -25,7 +25,8 @@ "prettier-bytes": "^1.0.1", "upload-element": "^1.0.1", "virtual-dom": "^2.1.1", - "webtorrent": "^0.86.0" + "webtorrent": "^0.86.0", + "winreg": "^1.0.1" }, "devDependencies": { "electron-packager": "^5.0.0",