【问题标题】:How to match the same domain with multiple extensions in Chrome extension?如何在 Chrome 扩展程序中将同一个域与多个扩展程序匹配?
【发布时间】:2018-08-28 12:34:36
【问题描述】:

因此,我正在开发一个 Chrome 扩展程序,该扩展程序需要在具有域 example.extension 的任何站点上激活,其中 extension 可以是任何东西(.com.de 等)。

我有一个内容脚本,并在manifest.json 中包含了一个又一个列出的所有域名:

"content_scripts": [{
    "js": ["content.js"],
    "matches": ["https://www.example.com/*","https://www.example.de/*"]
  }]

但是我怎么能写出匹配 example.* 的东西而不是枚举它们呢? 请注意,我尝试过这样的事情,但它不起作用:

 "content_scripts": [{
    "js": ["content.js"],
    "matches": ["https://www.example*"]
  }]

【问题讨论】:

标签: google-chrome google-chrome-extension content-script


【解决方案1】:

这是 2 个选项:

第一个选项: 您在清单中使用include_globs,如here所示

    "content_scripts": [{
           "js": ["content.js"],
            "matches": [ "*://*/*" ],
            "include_globs": [
                "*://*.example.*/*",
            ]
        }]

第二个选项:您可以更改内容脚本以检查目标 URL 是否与 www.example.* 匹配:

if (window.location.host.startsWith('www.example')){
   //content script code
}

【讨论】:

    【解决方案2】:

    您可以使用持久性后台脚本并在 url 符合您的要求时注入脚本。例如:

    background.js

    chrome.tabs.onUpdated.addListener(function(tabId, changeInfo, tab) {
        if (changeInfo.status == 'complete' && tab.url.indexOf('https://www.example.') == 0){
            chrome.tabs.executeScript(tabId,{file:'content.js'});
        }
    });
    

    【讨论】:

    • @Csaba wOxxOm 在您的问题中的评论可能会更好。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-05
    • 2013-12-25
    • 2017-02-04
    • 1970-01-01
    • 2011-08-06
    • 2021-08-03
    相关资源
    最近更新 更多