Chrome扩展程序:自定义协议?

时间:2013-12-30 09:16:41

标签: google-chrome google-chrome-extension

是否有方法可以像在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; 

我想要协议:

XYZ:

不是xyz://

这可能吗?

2 个答案:

答案 0 :(得分:5)

Chrome不提供为xyz:方案设置自定义处理程序的方法。

有一些方法可以模仿这种行为:

  • 使用内容脚本为指向xyz:...的链接点击设置事件监听器。
  • 使用webRequest API拦截并将默认搜索提供商的所有请求重定向到自定义网址。我正在使用此方法来捕获通配符搜索关键字,但它也可用于支持虚假方案。不幸的是,扩展程序将非常特定于用户的搜索设置,因为它会执行以下操作:

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

在这两种情况下,您都不会在多功能框中看到xyz:whatever

navigator.registerProtocolHandler应该是注册xyz:处理程序的最佳方式。不幸的是,目前它非常有限。自定义协议必须以web+为前缀。另请查看此API的list of open bugs

答案 1 :(得分:0)

新方法是使用 declarativeNetRequest

创建一个执行重定向的规则 rules.json

[
  {
    "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"]
    }
  }
]

然后在您的 redirect.html 脚本中处理它,您可以使用 chrome.runtime.sendMessage 对其进行操作。

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()

这就是您可以使用虚假 URL 链接在扩展程序中进行应用深度链接的方法。

相关问题