12

How and where do I use navigator.registerProtocolHandler to make it work?

.

This topic suggests you can call the following function to add the custom protocol handler:

navigator.registerProtocolHandler('web+custom', 'http://example.com/rph?q=%s', 'My App');

When I call it from the console, I get the

Uncaught DOMException: Failed to execute 'registerProtocolHandler' on 'Navigator': 
Can only register custom handler in the document's origin.

This also happens if I include it as a part of a content script as suggested here

install_protocol.html

<head>
  <title>Web Protocol Handler Sample - Register</title>
  <script type="text/javascript">
    navigator.registerProtocolHandler("burger",
                              "http://www.google.co.uk/?uri=%s",
                              "Burger handler");
  </script>
</head>

I also tried calling it in my background.js script, where it didn't give the error, however the protocol doesn't seem to respond.

.

Could you explain how to go through the process of registering and using a custom protocol in Chrome?

ashleedawg
  • 20,365
  • 9
  • 72
  • 105
Voy
  • 5,286
  • 1
  • 49
  • 59

1 Answers1

13

If you want to register handler with URL http://www.google.co.uk… then your script (the one, that calls navigator.registerProtocolHanlder) must be loaded from the same domain, that is www.google.co.uk.

Kuba Wyrostek
  • 6,163
  • 1
  • 22
  • 40
  • I tried running the following line in the browser address bar javascript:navigator.registerProtocolHandler("web+custom","https: //www.google.co.uk", "myApp") and I got a following syntax error: Uncaught SyntaxError: Failed to execute 'registerProtocolHandler' on 'Navigator': The url provided ('https://www.google.co.uk') does not contain '%s' – Voy Jun 04 '15 at 12:42
  • @Voy the %s is the substitution parameter, it will inject the bit after the colon e.g 'whatever' from customhandler:whatever into the url – HaveAGuess Jan 17 '18 at 17:56
  • This is correct, but to clarify further, to make this work - to add the handler it has to be run from the console while you are looking at the url which will be handling - http://www.google.co.uk in this case. – jethar Aug 10 '18 at 07:23
  • U are the hero of my day. Thanks man, saved me a lot of work ✌ – chainstair Jan 18 '20 at 00:30