【问题标题】:Run Chrome extension on every domain except one?在除一个域之外的每个域上运行 Chrome 扩展程序?
【发布时间】:2021-08-03 17:05:03
【问题描述】:

我需要一个 Chrome 扩展程序才能在除一个域之外的每个域上运行。它到处运行:

  "content_scripts": [
    {
      "matches": ["http://*/*", "https://*/*"],
      "run_at": "document_start",
      "js": ["contentScript.js"]
    }
  ]

但是添加exclude_matches似乎没有效果:

  "content_scripts": [
    {
      "matches": ["http://*/*", "https://*/*"],
      "exclude_matches": ["http://*.mysite.com/"],
      "run_at": "document_start",
      "js": ["contentScript.js"]
    }
  ]

我尝试使用这些文档中的匹配模式,但扩展仍然加载在该页面上:

https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/Match_patterns

  "content_scripts": [
    {
      "matches": ["http://*/*", "https://*/*"],
      "exclude_matches": ["*://mozilla.org/"],
      "run_at": "document_start",
      "js": ["contentScript.js"]
    }
  ],

在官方文档中,该示例用于排除域的特定页面,因此可能无法以我尝试的方式使用?

https://developer.chrome.com/docs/extensions/mv2/content_scripts/

{
  "name": "My extension",
  ...
  "content_scripts": [
    {
      "matches": ["http://*.nytimes.com/*"],
      "exclude_matches": ["*://*/*business*"],
      "js": ["contentScript.js"]
    }
  ],
  ...
}

【问题讨论】:

  • 您的第一次尝试使用了http,所以难怪它不起作用,因为现在大多数网站都是https。在第二个尝试排除带有任何子域前缀的父域 *. 并添加 * 后缀以匹配任何路径,如 *://*.mozilla.org/* (它显示在 MDN btw 中)并且还有 exclude_globs 以获得更大的灵活性.
  • 如果我想排除某个域的所有 TLD,例如 google.org、google.com、google.anything 等,是否需要 chrome.webRequest? developer.chrome.com/docs/extensions/reference/webRequest
  • 见this example。

标签: google-chrome-extension


【解决方案1】:

要在“google”域以外的任何地方运行,请使用exclude_globs。在manifest.json:

  "content_scripts": [
    {
      "matches": ["http://*/*", "https://*/*"],
      "exclude_globs": ["*://*.google.*/*"],
      "run_at": "document_start",
      "js": ["contentScript.js"]
    }
  ]

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-20
    • 1970-01-01
    • 1970-01-01
    • 2016-08-28
    • 2012-11-14
    • 2014-11-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多