Skip to content

API: Register Electron App as Protocol Handler #4857

Closed

Description

@feross

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.

There's already a W3C API for this: Navigator.registerProtocolHandler. Can Electron implement it?

Activity

  1. felixrieseberg commented on Mar 20, 2016

    @felixrieseberg
    Member

    I'd be in favor of that and happy to help with code 👍

  2. feross commented on Mar 20, 2016

    @feross
    ContributorAuthor

    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.

  3. felixrieseberg commented on Mar 20, 2016

    @felixrieseberg
    Member

    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.

  4. feross commented on Mar 20, 2016

    @feross
    ContributorAuthor

    What potential ACL issues exist with my approach? Isn't HKEY_CURRENT_USER\Software\Classes always writable by the current user?

  5. felixrieseberg commented on Mar 20, 2016

    @felixrieseberg
    Member

    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.

  6. feross commented on Mar 20, 2016

    @feross
    ContributorAuthor

    @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.

  7. anaisbetts commented on Mar 21, 2016

    @anaisbetts
    Contributor

    It is, but certain environments might not have access to regedit.

    This is correct, many environments inexplicably use Group Policy to block REG.exe but not editing the registry in general. Yes, this is dumb.

    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.

    Correct, on both OS X and Linux this is not possible to do :(

  8. anaisbetts commented on Mar 21, 2016

    @anaisbetts
    Contributor
  9. felixrieseberg commented on Mar 21, 2016

    @felixrieseberg
    Member

    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?

  10. anaisbetts commented on Mar 21, 2016

    @anaisbetts
    Contributor

    @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

  11. felixrieseberg commented on Mar 21, 2016

    @felixrieseberg
    Member

    Cool. I'll take a look at this, we'll need this in Ghost, too.

  12. 15 remaining items

  13. bbondy commented on Jul 13, 2016

    @bbondy
    Contributor

    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.

  14. caryfitzhugh commented on Sep 2, 2016

    @caryfitzhugh

    @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

    app.on('open-url', ....
    

    But the docs say that is mac only.

    How are you actually using this ? :)

  15. caryfitzhugh commented on Sep 2, 2016

    @caryfitzhugh

    @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. :)

  16. caryfitzhugh commented on Sep 2, 2016

    @caryfitzhugh

    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.

  17. felixrieseberg commented on Sep 2, 2016

    @felixrieseberg
    Member

    @caryfitzhugh Seems like you already figured out how to use it ;-)

  18. jamgold commented on Mar 15, 2017

    @jamgold

    @caryfitzhugh how do I communicate with the already running app?

  19. caryfitzhugh commented on Mar 15, 2017

    @caryfitzhugh
  20. jamgold commented on Mar 15, 2017

    @jamgold

    @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?

  21. jamgold commented on Mar 15, 2017

    @jamgold

    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!"
    });
    
  22. caryfitzhugh commented on Mar 15, 2017

    @caryfitzhugh
  23. Gozala commented on Oct 3, 2018

    @Gozala

    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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions