Fix xdg-open and protocol handler for windows/linux

This commit is contained in:
Ken Powers 2019-12-18 14:21:35 -05:00 committed by Scott Nonnenberg
parent 1c5ea63410
commit 7aad25b3e0
2 changed files with 28 additions and 9 deletions

31
main.js
View File

@ -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 });

View File

@ -314,6 +314,12 @@
"libxss1"
]
},
"protocols": {
"name": "sgnl-url-scheme",
"schemes": [
"sgnl"
]
},
"files": [
"package.json",
"config/default.json",