Chrome Extension : Custom Protocol?

Viewed 6444

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

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.

Related