11

For a Google-Chrome extension, I would like to load a content script on all Google pages. What is the best way to do this?

I tried this, in the manifest.json, but it does not work:

"matches": ["http://www.google.*/*", "https://www.google.*/*"],


This "works" but it is a bit long to write and I do not think it is the best practice:

"matches": ["http://www.google.com/*", "https://www.google.com/*", "http://www.google.fr/*", "https://www.google.fr/*", "http://www.google.de/*", "https://www.google.de/*", etc..."],
Brock Adams
  • 90,639
  • 22
  • 233
  • 295
Paul Fournel
  • 10,807
  • 9
  • 40
  • 68

1 Answers1

23

See Match patterns and globs. Unfortunately, Google-Chrome doesn't have a nice mechanism for top-level-domains (TLD's) in its matches specification. So, http://www.google.*/* throws an error and http://www.google.tld/* (Greasemonkey syntax) is not supported.

To work around this, widen the matches parameter and filter the results with the include_globs parameter.
Like so:

"matches":        ["http://*/*", "https://*/*"],
"include_globs":  ["http://www.google.*/*", "https://www.google.*/*"],
Gangula
  • 5,193
  • 4
  • 30
  • 59
Brock Adams
  • 90,639
  • 22
  • 233
  • 295
  • 3
    Technically, this answer is bad from a security perspective. That's why Google disallowed the obvious route in the first place. – Xan Sep 10 '14 at 08:50
  • 1
    Also, to improve it, `"*://..."` means either http or https – Xan Sep 10 '14 at 08:50
  • @Xan, the security risk is from the question. The answer just showed the OP how to do what he wanted. But, yes, there is a risk that you could get tricked on to a malicious top-level domain, and the extension would try to run. ... Purposely didn't use `"*://..."` so that it wouldn't try to run on `file`, `ftp`, or the once-possible `chrome-extension` and `about` schemes. – Brock Adams Sep 10 '14 at 09:12
  • 3
    That's what I'm saying. "*://..." is, specifically, a shorthand for `http` and `https` and not all protocols. See [docs](https://developer.chrome.com/extensions/match_patterns). _"If the scheme is *, then it matches either http or https, and not file, or ftp."_ – Xan Sep 10 '14 at 09:14
  • @Xan. You're right. Guess I missed a chance for an extra simplification. Consider the existing code "Self documenting" ;) rather than slightly more efficient and noticeably harder to understand at a glance. – Brock Adams Sep 10 '14 at 09:23