From 6466860eb886a140d77a0649872c3950deab964c Mon Sep 17 00:00:00 2001 From: Colin Fleming Date: Fri, 16 Oct 2020 02:33:25 +1300 Subject: [PATCH] Fix freetube:// protocol handling on macOS. (#669) 1. Handle open-url events. 2. Make protocol handling more robust on Win/Linux based on SO post. 3. Change 'ping' message to more descriptive 'openUrl' message. 4. Remove freetube:// protocol in main, and unify URL handling logic. --- src/main/index.js | 58 +++++++++++++++++++++++++++++++++++++++++---- src/renderer/App.js | 8 +++---- 2 files changed, 56 insertions(+), 10 deletions(-) diff --git a/src/main/index.js b/src/main/index.js index e7dc6029..f07a5b96 100644 --- a/src/main/index.js +++ b/src/main/index.js @@ -25,13 +25,25 @@ const path = require('path') const isDev = process.env.NODE_ENV === 'development' const isDebug = process.argv.includes('--debug') let mainWindow +let startupUrl // CORS somehow gets re-enabled in Electron v9.0.4 // This line disables it. // This line can possible be removed if the issue is fixed upstream app.commandLine.appendSwitch('disable-features', 'OutOfBlinkCors') -app.setAsDefaultProtocolClient('freetube') +// See: https://stackoverflow.com/questions/45570589/electron-protocol-handler-not-working-on-windows +// remove so we can register each time as we run the app. +app.removeAsDefaultProtocolClient('freetube') + +// If we are running a non-packaged version of the app && on windows +if (isDev && process.platform === 'win32') { + // Set the path of electron.exe and your app. + // These two additional parameters are only available on windows. + app.setAsDefaultProtocolClient('freetube', process.execPath, [path.resolve(process.argv[1])]) +} else { + app.setAsDefaultProtocolClient('freetube') +} // TODO: Uncomment if needed // only allow single instance of application @@ -45,7 +57,10 @@ if (!isDev) { if (mainWindow.isMinimized()) mainWindow.restore() mainWindow.focus() - mainWindow.webContents.send('ping', commandLine) + const url = getLinkUrl(commandLine) + if (url) { + mainWindow.webContents.send('openUrl', url) + } } }) @@ -231,9 +246,8 @@ function createWindow () { }) ipcMain.on('appReady', () => { - const param = process.argv[1] - if (typeof (param) !== 'undefined' && param !== null) { - mainWindow.webContents.send('ping', process.argv) + if (startupUrl) { + mainWindow.webContents.send('openUrl', startupUrl) } }) @@ -276,6 +290,40 @@ app.on('activate', () => { } }) +/* + * Callback when processing a freetube:// link (macOS) + */ +app.on('open-url', (event, url) => { + event.preventDefault() + + if (mainWindow && mainWindow.webContents) { + mainWindow.webContents.send('openUrl', baseUrl(url)) + } else { + startupUrl = baseUrl(url) + } +}) + +/* + * Check if we were passed a freetube:// URL on process startup (linux/win) + */ +const url = getLinkUrl(process.argv) +if (url) { + startupUrl = url +} + +function baseUrl(arg) { + return arg.replace('freetube://', '') +} + +function getLinkUrl(argv) { + for (const arg of argv) { + if (arg.indexOf('freetube://') !== -1) { + return baseUrl(arg) + } + } + return null +} + /** * Auto Updater * diff --git a/src/renderer/App.js b/src/renderer/App.js index 0563ba90..7cd3f9d6 100644 --- a/src/renderer/App.js +++ b/src/renderer/App.js @@ -93,7 +93,7 @@ export default Vue.extend({ console.log('User is using Electron') this.activateKeyboardShortcuts() this.openAllLinksExternally() - this.enableCliPing() + this.enableOpenUrl() this.setBoundsOnClose() } @@ -262,12 +262,10 @@ export default Vue.extend({ }) }, - enableCliPing: function () { + enableOpenUrl: function () { const v = this - electron.ipcRenderer.on('ping', function (event, message) { - let url = message[message.length - 1] + electron.ipcRenderer.on('openUrl', function (event, url) { if (url) { - url = url.replace('freetube://', '') v.$store.dispatch('getVideoIdFromUrl', url).then((result) => { if (result) { v.$router.push({