6

Are there methods to register a custom protocol with a google chrome extension like you can in firefox :

const kSIMPLEURI_CONTRACTID = "@mozilla.org/network/simple-uri;1"; 
const kIOSERVICE_CONTRACTID = "@mozilla.org/network/io-service;1"; 
const nsISupports = Components.interfaces.nsISupports; 
const nsIIOService = Components.interfaces.nsIIOService; 
const nsIProtocolHandler = Components.interfaces.nsIProtocolHandler; 
const nsIURI = Components.interfaces.nsIURI; 

I want the protocol:

xyz:

Not xyz://

Is this possible?

2 Answers2

5

Chrome does not offer a way to set custom handlers for the xyz: scheme.

There are some ways to emulate the behavior though:

  • Use Content scripts to set up an event listener for clicks on links which point to xyz:....
  • Use the webRequest API to intercept and redirect all requests from the default search provider to a custom URL. I'm using this method for catching wildcard search keywords, but it can also be used for supporting fake schemes. Unfortunately, the extension would be quite specific to the user's search settings, because it would do something like this:

    Redirect http://google.com/search?q=xyz%3Awhatever
          to chrome-extension://.../whatever
    

in both cases, you won't see xyz:whatever in the omnibox, though.

navigator.registerProtocolHandler should be the best way to register a xyz: handler. Unfortunately, it is quite limited at the moment. Custom protocols must be prefixed with web+. Also take a look at the list of open bugs for this API.

Rob W
  • 341,306
  • 83
  • 791
  • 678
  • 1
    How do magnet: links work then? I see these in chrome as magnet:xyz and they do work there. –  Dec 30 '13 at 12:13
  • @Wesley Link to such extension? You can easily take a look in their source code using my [Chrome extension source viewer](https://chrome.google.com/webstore/detail/chrome-extension-source-v/jifpbeccnghkjeaalbbjmodiffmgedin)? – Rob W Dec 30 '13 at 12:14
  • 1
    it's no extension. Perhaps it's the application (transmission) that does this somehow? Can apps register custom protocols? –  Dec 30 '13 at 12:15
  • @Wesley Yes. On Windows, I believe that this can be done through the registry. I have never tried such a thing, so go research the answers yourself. A quick Google shows http://stackoverflow.com/q/19221677 and http://msdn.microsoft.com/en-us/library/aa767914(VS.85).aspx, I'm sure that there are many other references that will be useful to you. – Rob W Dec 30 '13 at 12:19
  • 2
    @Wesley: Any links that browsers don't recognize will be delegated to the operating system. `magnet:` has a protocol handler registered by some application that is installed on your computer - this application essentially asked to be started whenever such link is clicked. The details of registering a protocol handler this way depend on the operating system. – Wladimir Palant Dec 30 '13 at 16:45
  • @Wesley `magnet` along with `mailto` and [some others](https://html.spec.whatwg.org/multipage/webappapis.html#custom-handlers) are white listed. – 0xcaff Oct 28 '15 at 21:04
  • @RobW, Is webRequest even triggered by such visits? I did [`navigator.registerProtocolHandler("burger", "https://www.google.co.uk/?uri=%s", "Burger handler");`](https://archive.is/2U3y1#selection-1163.0-1201.1) in bg.js . But visiting [⨕ by clicking on the dropdown second row](https://i.stack.imgur.com/zIMYy.png) didn't log any webRequests. Did you manage to get it to work? – Pacerier Oct 13 '17 at 03:25
  • @Pacerier The webRequest API is triggered for the handler URL (google.co.uk in your case), not the requested one (web+burger). – Rob W Oct 13 '17 at 09:08
0

The new way would be using declarativeNetRequest

Create a rule rules.json that does a redirect.

[
  {
    "id": 1,
    "priority": 1,
    "action": {
      "type": "redirect",
      "redirect": { 
        "regexSubstitution": "chrome-extension://xxxxxxxxxxxxxxxx/redirect.html#\\1"
      }
    },
    "condition": { 
      "regexFilter": "^https://mohamedmansour.com/join/([\\w]+)", 
      "resourceTypes": ["main_frame"]
    }
  }
]

Then within your redirect.html script, handle it, you can use chrome.runtime.sendMessage to act upon it.

function closeCurrentTab() {
  chrome.tabs.getCurrent(tab => chrome.tabs.remove(tab.id, () => {}))
}

if (location.hash)
  chrome.runtime.sendMessage({ command: 'AppLink', data: location.hash }, () => closeCurrentTab())
else
  closeCurrentTab()

That is how you can do App Deep Linking within Extensions with a fake URL link.

Mohamed Mansour
  • 39,445
  • 10
  • 116
  • 90