I want my content script to match all google domains, and a specific page. I know this is not possible.
Manifest.json
"content_scripts": [{
"matches": [
,"*://www.google.*"
,"*://www.youtube.com/*"
,"*://readthedocs.org/*"]
,
....
Is there another way to do this? Just wanted to make sure before I list all domains Google has :)
Listing all Google domains is not that difficult, because Google has published a list of all public Google domains at http://www.google.com/supported_domains. Prefix every item in this list with "*://* and add the ", suffix to every item. Then copy-paste the result to your manifest file.
An alternative option is to use the "include_globs" field (this is applied after "matches"):
{
....
"content_scripts": [{
"matches": [ "*://*/*" ],
"include_globs": [
"*://*.google.*/*",
"*://*.youtube.com/*",
"*://readthedocs.org/*"
],
"js": [ "contentscript.js" ]
}]
}
(I've replaced "*://www.google.com/*" with "*://*.google.com/*, because of subdomains like https://encrypted.google.com/)
Related
I am trying to make my own Chrome extension. I want to apply changes to any link. I have:
"content_scripts": [{
"matches": ["https://*"],
"css": ["main.css"]
}]
What should I write in "matches"?
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*"]
}]
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
}
You can use a persistent background script and inject the script when the url matches your requirement. For example:
background.js
chrome.tabs.onUpdated.addListener(function(tabId, changeInfo, tab) {
if (changeInfo.status == 'complete' && tab.url.indexOf('https://www.example.') == 0){
chrome.tabs.executeScript(tabId,{file:'content.js'});
}
});
I have Chrome web store package with zip file ready. But the problem is that manifest.json i need to allow two domain + all subdomains.
This is the reference:
https://developer.chrome.com/extensions/manifest
https://developer.chrome.com/extensions/content_scripts
Where its saying: "matches": ["http://www.google.com/*"]
But how do i say in matches to allow two domain with all there subdomains? example:
*.stackoverflow.com and *.centos.org?
{
"name": "My extension",
...
"content_scripts": [
{
"matches": ["http://www.google.com/*"],
"css": ["mystyles.css"],
"js": ["jquery.js", "myscript.js"]
}
],
...
}
You're looking at wrong parts of the documentation.
Match Patterns
Match patterns and globs
Note that "matches" property is an array.
"content_scripts": [
{
"matches": ["*://*.stackoverflow.com/*", "*://*.centos.org/*"],
"css": ["mystyles.css"],
"js": ["jquery.js", "myscript.js"]
}
],
The above will match all pages on stackoverflow.com, centos.org and all of their subdomains. Important to note that just stackoverflow.com will be matched.
* in place of http will match exactly either http or https.
I want to insert a javascript file into the original new tab page of chrome to be executed,
I tried this but error in matching when I'm trying to load the extension.
"content_scripts": [
{
"matches": ["chrome://newtab/*"],
"js": ["jquery.js", "myscript.js"]
}
]
You cannot do that, you need to use override pages to do this.
{
"name": "My extension",
...
"chrome_url_overrides" : {
"newtab": "myPage.html"
},
...
}
Add this to your matches: "matches": ["*://*.google.com/_/chrome/newtab*"]
That url looks like an implementation detail, so no guarantees it won't change, but for now it works.
I looked at the google-chrome-extension labs page and I don't see any example where you can use different content scripts for different sites. They have a page showing how to have multiple content scripts on one site, but not the reverse. Is there any way to have different content scripts for different sites?
{
"name": "My extension",
...
"content_scripts": [
{
"matches": ["http://www.google.com/*"],
"js": ["script1.js"]
},{
"matches": ["http://www.yahoo.com/*"],
"js": ["script2.js"]
}
],
...
}