From c8bd39056cd7880c56f3b1e482f94aa877ac8d20 Mon Sep 17 00:00:00 2001 From: Feross Aboukhadijeh Date: Sat, 19 Mar 2016 23:00:42 -0700 Subject: [PATCH] Register .torrent file hander on Windows --- main/register-protocol-handler.js | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/main/register-protocol-handler.js b/main/register-protocol-handler.js index 6e43a03e..e84587c7 100644 --- a/main/register-protocol-handler.js +++ b/main/register-protocol-handler.js @@ -1,6 +1,7 @@ module.exports = function () { if (process.platform === 'win32') { registerProtocolHandler('magnet', 'BitTorrent Magnet URL', process.execPath) + registerFileHandler('.torrent', 'io.webtorrent.torrent', 'BitTorrent Document', process.execPath) } } @@ -44,3 +45,29 @@ function registerProtocolHandler (protocol, name, command) { if (err) console.error(err.message || err) } } + +function registerFileHandler (ext, id, name, command) { + var Registry = require('winreg') + + var extKey = new Registry({ + hive: Registry.HKCU, // HKEY_CURRENT_USER + key: '\\Software\\Classes\\' + ext + }) + extKey.set('', Registry.REG_SZ, id, callback) + + var idKey = new Registry({ + hive: Registry.HKCU, + key: '\\Software\\Classes\\' + id + }) + idKey.set('', Registry.REG_SZ, name, callback) + + var commandKey = new Registry({ + hive: Registry.HKCU, + key: '\\Software\\Classes\\' + id + '\\shell\\open\\command' + }) + commandKey.set('', Registry.REG_SZ, '"' + command + '" "%1"', callback) + + function callback (err) { + if (err) console.error(err.message || err) + } +}