34

Is there a way to limit a Chrome extension to only run on certain urls?

Say I want the extension to run only if the domain is Amazon.com. I realize you can put a check at the top of the scripts, then execute the rest of the script only if the check matches.

However, can you prevent the extension from running at all?

amp343
  • 1,016
  • 1
  • 8
  • 10

3 Answers3

26

Welp, it figures I would find the answer to this right after posting the question, even though I have been stuck for a while...

Archive of version of docs originally linked in answer: http://code.google.com/chrome/extensions/content_scripts.html

Analog page for Manifest V2 (though see also V3): https://developer.chrome.com/docs/extensions/mv2/content_scripts/

In manifest.json:

  "content_scripts": [
    {
      "matches": ["http://*.nytimes.com/*"],
      "exclude_matches": ["*://*/*business*"],
      "js": ["contentScript.js"]
    }
  ],
ruffin
  • 16,507
  • 9
  • 88
  • 138
amp343
  • 1,016
  • 1
  • 8
  • 10
  • 8
    FYI This doesn't prevent the script from running, it prevents chrome from injecting it into pages that don't match. – Joseph Yaduvanshi May 08 '12 at 18:44
  • 1
    True, I guess that was the nature of my original question, should have worded it more carefully, Thanks. – amp343 May 08 '12 at 18:58
  • Side note: The URL’s path part after the domain name, e.g. the `/watch/` part in `"matches": [ "https://www.netflix.com/watch/*" ],` is not ignored, even though it is confusingly omitted in the extension details view: `https://www.netflix.com/*` – Aaron Thoma Mar 12 '20 at 20:26
  • 1
    Can the matches be dynamic like taking input from user? – Blue Rose Apr 07 '21 at 06:41
9

As a user, with Chrome 71 (or maybe even before) with chrome://flags/#extension-active-script-permission (you may need to enable User consent for extension scripts flag) allows you to right click extension icons and select "This can read and change site data" then you can choose:

  • When you click the extension
  • On current-domain-name.com
  • On all sites (default)

enter image description here

This way you can limit an extension to only run on certain domains very quickly.

Wernight
  • 36,122
  • 25
  • 118
  • 131
  • 2
    Limit as a _user_, or as a _developer?_ – Xan Nov 15 '18 at 11:27
  • @Xan You're right the question isn't very clear either, so I added "as a user". – Wernight Nov 19 '18 at 13:56
  • 3
    I believe that from the context: 1) It's a question on StackOverflow (and not, say, Super User), 2) The original author self-responded with "how to do it through an extension manifest", the intent here is a developer question. Which makes it important to at least note that **developers have no control over this mechanism.** Though it's not fully off-topic as developers need to be aware of this, and may need to inform the users of this. – Xan Nov 19 '18 at 14:02
  • This flag is available from Chrome version 37.0.1991.0 – Krzysztof Gapski Dec 14 '18 at 08:53
  • What about blacklist URLs? Black lists for all extension that never runs? – mathema Mar 06 '19 at 19:11
  • Does that change turn the plugin off? Or just block it? I want to turn it off, to reduce memory footprint, as some plugins are dedicated to a single website. – s.meijer Mar 23 '19 at 11:44
-1

There's a Chrome extension for this: Extension Automation. I had gone the route of modifying manifests before, but this is easier.

Joe Fusion
  • 41
  • 5