问题
Say I have two content scripts, codeA.js
and codeB.js
. I am wondering if it possible to run codeA.js
on http://www.example.com and have codeB.js
run on http://www.domain.com.
Would it be possible to set my content_scripts
in the manifest.json
file to:
"content_scripts": [
{
"matches": ["http://www.example.com", "http:www.domain.com"],
"js": ["codeA.js", "codeB.js"]
}
]
and then have each script check to see which URL the page currently sits at? If so, how would I go about doing this?
UPDATE:
I have tried using the following code
if(document.location == "http://*.google.com/*" || "https://*.google.com/*") {
alert("you are using google");
} else if(document.location == "http://*.yahoo.com/*" || "https://*.yahoo.com") {
alert("you are using yahoo!");
}
but I kept getting you are using google
.
回答1:
Note that content_scripts
is an array:
"content_scripts": [
{
"matches": ["http://www.example.com/*"],
"js": ["codeA.js"]
},
{
"matches": ["http://www.domain.com/*"],
"js": ["codeB.js"]
}
]
回答2:
Make a single script file and check for the domain name like below
if(document.location == domain1){
// load script file for domain1
} else {
// load script for other domains
}
AFAIK you cannot mention different content script files for different domains in manifest.json
file.
As per the edit of your method, that code does not work. You must check it using regular expression not just ==
as your domain contains *
in between. And script Web Accessible Resources before hand.
来源:https://stackoverflow.com/questions/25634572/is-it-possible-to-have-two-content-scripts-run-on-two-separate-pages