You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
API: Register Electron App as Protocol Handler #4857
There's no simple, cross-platform way to register protocol handlers in Electron. Every OS requires a different solution. This seems like something that Electron should smooth over.
For OS X, this seems to require modifying the Info.plist, unless there's some other mechanism to set this at runtime that I haven't discovered yet.
For Windows, this is actually pretty straightforward. I implemented it in a couple dozen lines of code, without even requiring a native dependency. Code here. This uses winreg to modify the Windows registry. It just calls out to the REG command line program with child_process, so it's pure JavaScript.
Winreg has a few issues for things like protocol handlers and file handlers across versions of Windows, which is why my team at Microsoft built this (native) alternative, ensuring the correct ACLs for the operation.
@felixrieseberg Okay, fair enough. Thanks for explaining the potential issue. Right now, our app doesn't have any native dependencies and we're trying to keep it that way as long as possible so it's simpler to package.
For larger apps, or for a built-in Electron API, the C++ approach sounds right.
Nice, good find! Quick question though for you, @paulcbetts - the navigator API wasn't really meant for this and I'm concerned that we're just replacing an existing API with a slightly different function. I'd rather make a new API, maybe somewhere on app. Thoughts?
@felixrieseberg Yeah, I'd implement the same API on app - it's really a per-app Thing, and if in the future Chromium implemented this API, we'd conflict if we tried to patch it in
FWIW, I implemented the real navigator.registerProtocolHandler which gives websites the ability to request registration of a protocol here: brave/muon@3a2f05b
Good examples of this is gmail now prompts to set as default handler for mailto: and coinbase for bitcoin: urls.
Unfortunately it requires KeyedService though which we only have in our branch with extensions support. I'd be happy to upstream one day if Electron's stance changes on not taking in extensions support or if they get the needed support for KeyedService otherwise.
@felixrieseberg I'm looking into using this stuff and can't figure out the details of how you actually catch the event when the OS triggers your protocol.
@felixrieseberg I think I found that it's just executing the CLI - I was hoping for something more like DDE - where it can trigger an event if the process is already started.
@caryfitzhugh thanks for responding so quickly. I initiate the app / mainWindow in main.js and have my logic in render.js where I again access app over remote object. How can I trigger an even on the app?
I figured out how to accomplish this. In main.js I either call mainWindow.webContents.reload() which does the trick, or I use mainWindow.webContents.send( 'ping','wohoo' ) and add the following to render.js
require('electron').ipcRenderer.on('ping', function(event, message) {
console.log(message); // Prints "whoooooooh!"
});
This issue is closed but from what I can tell navigator.registerProtocolHandler still does not work. From what I can tell navigator.registerProtocolHandler succeeds (as long origin of the registered URL has same origin as document performing registration). But then attempt to load registered URL triggers openExternal permission handler. Accepting / Rejecting it seems to make no difference.
I think this issue mentioned navigator.registerProtocolHandler but was really about app.setAsDefaultProtocolClient instead.
Activity
felixrieseberg commentedon Mar 20, 2016
I'd be in favor of that and happy to help with code 👍
feross commentedon Mar 20, 2016
For OS X, this seems to require modifying the
Info.plist, unless there's some other mechanism to set this at runtime that I haven't discovered yet.For Windows, this is actually pretty straightforward. I implemented it in a couple dozen lines of code, without even requiring a native dependency. Code here. This uses
winregto modify the Windows registry. It just calls out to theREGcommand line program withchild_process, so it's pure JavaScript.felixrieseberg commentedon Mar 20, 2016
Winreg has a few issues for things like protocol handlers and file handlers across versions of Windows, which is why my team at Microsoft built this (native) alternative, ensuring the correct ACLs for the operation.
feross commentedon Mar 20, 2016
What potential ACL issues exist with my approach? Isn't
HKEY_CURRENT_USER\Software\Classesalways writable by the current user?felixrieseberg commentedon Mar 20, 2016
It is, but certain environments might not have access to regedit. Our solution just does it directly in c++, removing any dependencies.
This is mostly interesting for Visual Studio Code, Atom, and other apps that have a lot of users, bringing edge cases with them.
feross commentedon Mar 20, 2016
@felixrieseberg Okay, fair enough. Thanks for explaining the potential issue. Right now, our app doesn't have any native dependencies and we're trying to keep it that way as long as possible so it's simpler to package.
For larger apps, or for a built-in Electron API, the C++ approach sounds right.
anaisbetts commentedon Mar 21, 2016
This is correct, many environments inexplicably use Group Policy to block REG.exe but not editing the registry in general. Yes, this is dumb.
Correct, on both OS X and Linux this is not possible to do :(
anaisbetts commentedon Mar 21, 2016
Actually, I'm full of it, @deepak1556 tracked down https://code.google.com/p/chromium/codesearch#chromium/src/chrome/browser/shell_integration.h&q=shell_integration&sq=package:chromium&l=45 which looks to have implementations for all platforms, I'm a 👍 on this
felixrieseberg commentedon Mar 21, 2016
Nice, good find! Quick question though for you, @paulcbetts - the
navigatorAPI wasn't really meant for this and I'm concerned that we're just replacing an existing API with a slightly different function. I'd rather make a new API, maybe somewhere onapp. Thoughts?anaisbetts commentedon Mar 21, 2016
@felixrieseberg Yeah, I'd implement the same API on
app- it's really a per-app Thing, and if in the future Chromium implemented this API, we'd conflict if we tried to patch it infelixrieseberg commentedon Mar 21, 2016
Cool. I'll take a look at this, we'll need this in Ghost, too.
15 remaining items
bbondy commentedon Jul 13, 2016
FWIW, I implemented the real
navigator.registerProtocolHandlerwhich gives websites the ability to request registration of a protocol here: brave/muon@3a2f05bGood examples of this is gmail now prompts to set as default handler for mailto: and coinbase for bitcoin: urls.
Unfortunately it requires KeyedService though which we only have in our branch with extensions support. I'd be happy to upstream one day if Electron's stance changes on not taking in extensions support or if they get the needed support for KeyedService otherwise.
caryfitzhugh commentedon Sep 2, 2016
@felixrieseberg I'm looking into using this stuff and can't figure out the details of how you actually catch the event when the OS triggers your protocol.
In Mac I can catch it with
But the docs say that is mac only.
How are you actually using this ? :)
caryfitzhugh commentedon Sep 2, 2016
@felixrieseberg I think I found that it's just executing the CLI - I was hoping for something more like DDE - where it can trigger an event if the process is already started.
https://msdn.microsoft.com/en-us/library/aa767914(v=vs.85).aspx
https://msdn.microsoft.com/en-us/library/windows/desktop/hh127429(v=vs.85).aspx
If you have any insights, please let me know. :)
caryfitzhugh commentedon Sep 2, 2016
and - I've discovered this gem:
https://github.com/electron/electron/blob/master/docs/api/app.md#appmakesingleinstancecallback
Already in electron, and looks like it will fwd my command line args to the first instance. Perfect.
felixrieseberg commentedon Sep 2, 2016
@caryfitzhugh Seems like you already figured out how to use it ;-)
jamgold commentedon Mar 15, 2017
@caryfitzhugh how do I communicate with the already running app?
caryfitzhugh commentedon Mar 15, 2017
jamgold commentedon Mar 15, 2017
@caryfitzhugh thanks for responding so quickly. I initiate the app / mainWindow in main.js and have my logic in render.js where I again access app over remote object. How can I trigger an even on the app?
jamgold commentedon Mar 15, 2017
I figured out how to accomplish this. In main.js I either call
mainWindow.webContents.reload()which does the trick, or I usemainWindow.webContents.send( 'ping','wohoo' )and add the following to render.jscaryfitzhugh commentedon Mar 15, 2017
feat/linux-protocol: Adds protocol handling functionality via .deskto…
feat/linux-protocol: Adds protocol handling functionality for linux;
feat/linux-protocol: Adds protocol handling functionality for linux;
Gozala commentedon Oct 3, 2018
This issue is closed but from what I can tell
navigator.registerProtocolHandlerstill does not work. From what I can tellnavigator.registerProtocolHandlersucceeds (as long origin of the registered URL has same origin as document performing registration). But then attempt to load registered URL triggersopenExternalpermission handler. Accepting / Rejecting it seems to make no difference.I think this issue mentioned
navigator.registerProtocolHandlerbut was really aboutapp.setAsDefaultProtocolClientinstead.Implement a basic URL scheme. (#308)