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

Commit 0b01414

Browse files
committed
Merge pull request #1438 from OpenBazaar/add-win-uri2
Windows URI Handling for External Links
2 parents e17acca + 6e630b2 commit 0b01414

File tree

2 files changed

+102
-4
lines changed

2 files changed

+102
-4
lines changed

main.js

Lines changed: 99 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
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
@@ -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+
});

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,11 @@
3939
"sortablejs": "^1.4.2",
4040
"taggle": "1.6.1",
4141
"underscore": "1.8.3",
42-
"yargs": "^4.1.0"
42+
"yargs": "^4.1.0",
43+
"winreg": "^1.1.1"
4344
},
4445
"devDependencies": {
45-
"electron-prebuilt": "0.37.5",
46+
"electron-prebuilt": "0.37.7",
4647
"eslint": "1.9.0",
4748
"pre-commit": "^1.1.2",
4849
"valiquire": "0.3.0"

0 commit comments

Comments
 (0)