Skip to content
This repository was archived by the owner on May 16, 2019. It is now read-only.

Commit 605de2e

Browse files
committed
Add windows URI handling
1 parent 6efc742 commit 605de2e

File tree

1 file changed

+101
-4
lines changed

1 file changed

+101
-4
lines changed

main.js

Lines changed: 101 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ var start_local_server = function() {
108108

109109
var random_port = Math.floor((Math.random() * 10000) + 30000);
110110

111-
subpy = require('child_process').spawn(serverPath + daemon, ['start', '--loglevel', 'debug', '-p', random_port], {
111+
subpy = require('child_process').spawn(serverPath + daemon, ['start', '--loglevel', 'debug', '-p', random_port, '-t'], {
112112
detach: false,
113113
cwd: __dirname + path.sep + '..' + path.sep + 'OpenBazaar-Server'
114114
});
@@ -154,6 +154,99 @@ if(fs.existsSync(__dirname + path.sep + ".." + path.sep + "OpenBazaar-Server" +
154154
// be closed automatically when the JavaScript object is GCed.
155155
var mainWindow = null;
156156

157+
if (process.platform === "win32") {
158+
initWin32();
159+
}
160+
161+
function initWin32() {
162+
var Registry = require('winreg');
163+
164+
var iconPath = path.join(process.env.LOCALAPPDATA, "app.ico");
165+
registerProtocolHandlerWin32('ob', 'URL:OpenBazaar URL', iconPath, process.execPath);
166+
167+
/**
168+
* To add a protocol handler, the following keys must be added to the Windows registry:
169+
*
170+
* HKEY_CLASSES_ROOT
171+
* $PROTOCOL
172+
* (Default) = "$NAME"
173+
* URL Protocol = ""
174+
* DefaultIcon
175+
* (Default) = "$ICON"
176+
* shell
177+
* open
178+
* command
179+
* (Default) = "$COMMAND" "%1"
180+
*
181+
* Source: https://msdn.microsoft.com/en-us/library/aa767914.aspx
182+
*
183+
* However, the "HKEY_CLASSES_ROOT" key can only be written by the Administrator user.
184+
* So, we instead write to "HKEY_CURRENT_USER\Software\Classes", which is inherited by
185+
* "HKEY_CLASSES_ROOT" anyway, and can be written by unprivileged users.
186+
*/
187+
188+
function registerProtocolHandlerWin32 (protocol, name, icon, command) {
189+
var protocolKey = new Registry({
190+
hive: Registry.HKCU, // HKEY_CURRENT_USER
191+
key: '\\Software\\Classes\\' + protocol
192+
})
193+
194+
setProtocol()
195+
196+
function setProtocol (err) {
197+
if (err) log.error(err.message)
198+
console.log(protocolKey);
199+
protocolKey.set('', Registry.REG_SZ, name, setURLProtocol)
200+
}
201+
202+
function setURLProtocol (err) {
203+
if (err) log.error(err.message)
204+
console.log(protocolKey);
205+
protocolKey.set('URL Protocol', Registry.REG_SZ, '', setIcon)
206+
}
207+
208+
function setIcon (err) {
209+
if (err) log.error(err.message)
210+
211+
var iconKey = new Registry({
212+
hive: Registry.HKCU,
213+
key: '\\Software\\Classes\\' + protocol + '\\DefaultIcon'
214+
})
215+
iconKey.set('', Registry.REG_SZ, icon, setCommand)
216+
}
217+
218+
function setCommand (err) {
219+
if (err) log.error(err.message)
220+
221+
var commandKey = new Registry({
222+
hive: Registry.HKCU,
223+
key: '\\Software\\Classes\\' + protocol + '\\shell\\open\\command'
224+
})
225+
commandKey.set('', Registry.REG_SZ, '"' + command + '" "%1"', done)
226+
}
227+
228+
function done (err) {
229+
if (err) log.error(err.message)
230+
}
231+
}
232+
}
233+
234+
var iShouldQuit = app.makeSingleInstance(function(commandLine, workingDirectory) {
235+
var uri = "";
236+
if (commandLine.length == 2) {
237+
uri = commandLine[1];
238+
openURL(uri);
239+
}
240+
mainWindow.webContents.executeJavaScript("console.log("+uri+")");
241+
if (mainWindow) {
242+
if (mainWindow.isMinimized()) mainWindow.restore();
243+
mainWindow.show();
244+
mainWindow.focus();
245+
}
246+
return true;
247+
});
248+
if(iShouldQuit){app.quit();return;}
249+
157250
// Quit when all windows are closed.
158251
app.on('window-all-closed', function() {
159252
// On OS X it is common for applications and their menu bar
@@ -348,7 +441,7 @@ app.on('ready', function() {
348441

349442
if(subpy) {
350443
if(platform == "mac" || platform == "linux") {
351-
subpy.kill('SIGINT');
444+
subpy.kill('SIGHUP');
352445
} else {
353446
require('child_process').spawn("taskkill", ["/pid", subpy.pid, '/f', '/t']);
354447
}
@@ -398,7 +491,7 @@ ipcMain.on('set-badge', function(event, text) {
398491
app.dock.setBadge(String(text));
399492
});
400493

401-
app.on('open-url', function(event, uri) {
494+
function openURL(uri) {
402495
var split_uri = uri.split('://');
403496
uri = split_uri[1];
404497

@@ -410,5 +503,9 @@ app.on('open-url', function(event, uri) {
410503
mainWindow.webContents.send('external-route', uri);
411504
}
412505

506+
}
507+
508+
app.on('open-url', function(event, uri) {
509+
openURL(uri);
413510
event.preventDefault();
414-
});
511+
});

0 commit comments

Comments
 (0)