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'});
}
});
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"?
i want to make a chrome extension that would change the content of chrome://newtab.
But when i put it in the matches selector, it gives an invalid scheme error. How could i fix this?
{
... //name
... //version
... //manifest version
"content_scripts": [
{
"matches": ["chrome://newtab"],
"css": ...
"js": ...
}
]
}
I'm trying to create a custom new tab page for Chrome. I have it in my manifest.json so that: "chrome_url_overrides": {
"newtab": "home.html"
}
It won't, however, load the scripts that I load in my home.html. I found out that you need to use content_scripts, so now I also have
"content_scripts": [
{
"matches": [""],
"css": ["style.css"],
"js": ["logic.js", "macros.js"]
}
]
What do I put in the "matches"? It doesn't allow "home.html" or "chrome://newtab".
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.