Search code examples
google-chrome-extension

Run Chrome extension on every domain except one?


I need a Chrome extension to run on every domain except for one. It runs everywhere with this:

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

However adding exclude_matches seems to have no effect:

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

I tried using a match pattern from these docs but the extension still loads on that page:

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"]
    }
  ],

In the official docs the example is for excluding specific pages for a domain, so maybe it can't be used in the way that I'm trying?

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

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

Solution

  • To run everywhere except "google" domains use exclude_globs. In manifest.json:

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