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 Answers 2

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.

8
  • 1
    How do magnet: links work then? I see these in chrome as magnet:xyz and they do work there.
    – user429620
    Dec 30, 2013 at 12:13
  • @Wesley Link to such extension? You can easily take a look in their source code using my Chrome extension source viewer?
    – Rob W
    Dec 30, 2013 at 12:14
  • 1
    it's no extension. Perhaps it's the application (transmission) that does this somehow? Can apps register custom protocols?
    – user429620
    Dec 30, 2013 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 stackoverflow.com/q/19221677 and 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, 2013 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. Dec 30, 2013 at 16:45
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.