javascriptgoogle-chromecustom-protocolprotocol-handler

Where to call navigator.registerProtocolHandler?


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?


Solution

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