Search code examples
google-chromegoogle-chrome-extensioncontent-script

How to match the same domain with multiple extensions in Chrome extension?


So, I am working on a Chrome extension that needs to be activated on any site with the domain example.extension where extension can be anything(.com, .de etc.).

I have a content script and in the manifest.json I included all the domain names listed one after another:

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

But how could I write something that matches example.* instead of enumerating all of them? Note that I tried something like this and it does not work:

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

Solution

  • These are 2 options:

    First option: You make use of the include_globs in the manifest as shown here

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

    2nd Option: You can change your content script to check if the target URL matches www.example.*:

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