11

Clicking a mailto: link will open my default mail client. In a similar manner, I would like to launch an Electron app with my-app:. What is the best way to achieve this and gracefully fallback to a standard http link if the app isn't installed?

Furthermore, I would also like to be able to pass through some extra details my-app:foo/bar. How would this be intercepted inside of Electron when it launches?

I have read some docs on what I think might be relevant stuff: http://electron.atom.io/docs/v0.36.0/api/protocol/ however as a frontend dev there's some gaps in my understanding of how the overarching process works. Any help much appreciated!

Merlin Mason
  • 1,607
  • 2
  • 12
  • 14

2 Answers2

11

Electron has evolved quite a bit since this question was first posted.

You no longer have to dive quite as deep and can skip the Electron protocol API. Instead, use the app.setAsDefaultProtocolClient(protocol[, path, args]) interface and its siblings app.removeAsDefaultProtocolClient(protocol[, path, args]) and app.isDefaultProtocolClient(protocol[, path, args]).

These enable you to register a protocol identifier your-protocol:// and receive arguments:

The whole link, including protocol, will be passed to your application as a parameter. Electron API docs

Jens Habegger
  • 5,266
  • 41
  • 57
  • 1
    do you have an example of this working? I tried it and it's not working for me on linux and chrome. – Shining Love Star Nov 29 '18 at 01:46
  • do you know if is there a way to add two protocols like that? I mean, that electron-app would open both on your-protocol_0:// and your-protocol_1:// ? – Artur Takoev Mar 30 '21 at 08:42
  • I haven't actually tried registering multiple protocols for an app, however, I also didn't find any indication of why it shouldn't work. – Jens Habegger Mar 30 '21 at 12:25
0

I'm not sure if it is possible to do what you want to do. Depending on whether you want to launch your Electron app from an actual browser window or simply from another Electron instance.

I found this other Stack Overflow post link that shows a workaround (though I'm afraid it won't graciously default to anything) and explains how it could be dangerous to launch programs directly from the browser.

If you want to launch your Electron app from another Electron app however you might want to check this out link.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Azmo
  • 144
  • 1
  • 5