Skip to content

Commit e66db3a

Browse files
Fix handling of the custom protocol on windows
1 parent ba0a9ba commit e66db3a

File tree

2 files changed

+21
-2
lines changed

2 files changed

+21
-2
lines changed
Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,19 @@
11
const app = require('electron').app
22
const dialog = require('electron').dialog
3+
const path = require('path')
34

4-
app.setAsDefaultProtocolClient('electron-api-demos')
5+
// If we are running a non-packaged version of the app
6+
if (process.defaultApp) {
7+
// If we have the path to our app we set the protocol client to launch electron.exe with the path to our app
8+
if (process.argv.length >= 2) {
9+
app.setAsDefaultProtocolClient('electron-api-demos', process.execPath, [path.resolve(process.argv[1])])
10+
}
11+
} else {
12+
app.setAsDefaultProtocolClient('electron-api-demos')
13+
}
514

15+
// NOTE: See the makeSingleInstance function in main.js for reference as to how
16+
// this event is fired on Windows
617
app.on('open-url', function (event, url) {
718
dialog.showErrorBox('Welcome Back', `You arrived from: ${url}`)
819
})

main.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,10 +73,18 @@ function initialize () {
7373
function makeSingleInstance () {
7474
if (process.mas) return false
7575

76-
return app.makeSingleInstance(function () {
76+
return app.makeSingleInstance(function (argv) {
7777
if (mainWindow) {
7878
if (mainWindow.isMinimized()) mainWindow.restore()
7979
mainWindow.focus()
80+
// On windows we have to check the second instance arguments to emit the open-url event
81+
if (process.platform === 'win32') {
82+
// Check if the second instance was attempting to launch a URL for our protocol client
83+
const url = argv.find(function (arg) {
84+
return /^electron-api-demos:\/\//.test(arg)
85+
})
86+
if (url) app.emit('open-url', null, url.replace(/^electron-api-demos:\/\//, ''))
87+
}
8088
}
8189
})
8290
}

0 commit comments

Comments
 (0)