From 7aad25b3e0e8dc385a5a0997414811b06f156e82 Mon Sep 17 00:00:00 2001 From: Ken Powers Date: Wed, 18 Dec 2019 14:21:35 -0500 Subject: [PATCH] Fix xdg-open and protocol handler for windows/linux --- main.js | 31 ++++++++++++++++++++++--------- package.json | 6 ++++++ 2 files changed, 28 insertions(+), 9 deletions(-) diff --git a/main.js b/main.js index adb80146e0..91ebc2298e 100644 --- a/main.js +++ b/main.js @@ -113,7 +113,7 @@ if (!process.mas) { console.log('quitting; we are the second instance'); app.exit(); } else { - app.on('second-instance', () => { + app.on('second-instance', (e, argv) => { // Someone tried to run a second instance, we should focus our window if (mainWindow) { if (mainWindow.isMinimized()) { @@ -122,6 +122,12 @@ if (!process.mas) { showWindow(); } + // Are they trying to open a sgnl link? + const incomingUrl = getIncomingUrl(argv); + if (incomingUrl) { + handleSgnlLink(incomingUrl); + } + // Handled return true; }); } @@ -405,11 +411,9 @@ async function readyForUpdates() { isReadyForUpdates = true; // First, install requested sticker pack - if (process.argv.length > 1) { - const [incomingUrl] = process.argv; - if (incomingUrl.startsWith('sgnl://')) { - handleSgnlLink(incomingUrl); - } + const incomingUrl = getIncomingUrl(process.argv); + if (incomingUrl) { + handleSgnlLink(incomingUrl); } // Second, start checking for app updates @@ -969,9 +973,13 @@ app.on('web-contents-created', (createEvent, contents) => { }); app.setAsDefaultProtocolClient('sgnl'); -app.on('open-url', (event, incomingUrl) => { - event.preventDefault(); - handleSgnlLink(incomingUrl); +app.on('will-finish-launching', () => { + // open-url must be set from within will-finish-launching for macOS + // https://stackoverflow.com/a/43949291 + app.on('open-url', (event, incomingUrl) => { + event.preventDefault(); + handleSgnlLink(incomingUrl); + }); }); ipc.on('set-badge-count', (event, count) => { @@ -1162,10 +1170,15 @@ function installSettingsSetter(name) { }); } +function getIncomingUrl(argv) { + return argv.find(arg => arg.startsWith('sgnl://')); +} + function handleSgnlLink(incomingUrl) { const { host: command, query } = url.parse(incomingUrl); const args = qs.parse(query); if (command === 'addstickers' && mainWindow && mainWindow.webContents) { + console.log('Opening sticker pack from sgnl protocol link'); const { pack_id: packId, pack_key: packKeyHex } = args; const packKey = Buffer.from(packKeyHex, 'hex').toString('base64'); mainWindow.webContents.send('show-sticker-pack', { packId, packKey }); diff --git a/package.json b/package.json index 2b8b959b61..6325f2b5aa 100644 --- a/package.json +++ b/package.json @@ -314,6 +314,12 @@ "libxss1" ] }, + "protocols": { + "name": "sgnl-url-scheme", + "schemes": [ + "sgnl" + ] + }, "files": [ "package.json", "config/default.json",