admin管理员组文章数量:1316829
I'm writing a coupon style chrome extension and i want it to run only on specific websites (over 300 sites). I've read about specifying the sites url in content_scripts but that doesn't seem practical especially if i needed to update it. Here is my script:
Manifest.json
{
"name": "Example",
"description": "description",
"version": "1.0",
"manifest_version": 2,
"background":
{
"scripts": ["js/background.js", "js/eventPage.js", "js/jquery-
3.2.1.min.js"],
"persistent":false
},
"page_action": {
"default_icon":"img/32icon.png"
},
"content_scripts": [
{
"matches": ["https://*/*"],
"js": ["js/sites_cs.js", "js/jquery-3.2.1.min.js"]
}
],
"permissions": [
"tabs",
"http://*/*",
"https://*/*",
"activeTab"
]
}
I'm writing a coupon style chrome extension and i want it to run only on specific websites (over 300 sites). I've read about specifying the sites url in content_scripts but that doesn't seem practical especially if i needed to update it. Here is my script:
Manifest.json
{
"name": "Example",
"description": "description",
"version": "1.0",
"manifest_version": 2,
"background":
{
"scripts": ["js/background.js", "js/eventPage.js", "js/jquery-
3.2.1.min.js"],
"persistent":false
},
"page_action": {
"default_icon":"img/32icon.png"
},
"content_scripts": [
{
"matches": ["https://*/*"],
"js": ["js/sites_cs.js", "js/jquery-3.2.1.min.js"]
}
],
"permissions": [
"tabs",
"http://*/*",
"https://*/*",
"activeTab"
]
}
Share
Improve this question
asked Oct 31, 2018 at 21:17
MattiMatti
211 silver badge5 bronze badges
11
-
1
If you don't want to list all the sites in the
matches:
option, how do you want to decide whether to run it? – Barmar Commented Oct 31, 2018 at 21:31 - I want the extension to detect specific websites. When detected, if the user clicks the extension it brings them to a website for coupons – Matti Commented Oct 31, 2018 at 21:42
-
So list all the websites in
matches:
. If the list changes, update the list. I don't see any other option. – Barmar Commented Oct 31, 2018 at 21:43 -
When I look at code from other coupon extensions they have
"matches": ["https://*/*"]
in there content_scripts. so there must be a way to do it in the js files – Matti Commented Oct 31, 2018 at 21:47 -
The JS may do some kind of pattern match on
window.location
– Barmar Commented Oct 31, 2018 at 21:48
1 Answer
Reset to default 8You can have an array with the URLs you want to match and programmatically inject your content scripts to the matching webpages only. For example, remove the content_scripts
entry of manifest.json
file and include this code in the background script:
background.js
// myURLs contains the websites where you want your content script to run
const myURLs = ['www.example1.','www.anotherone.','www.athird.one'];
chrome.tabs.onUpdated.addListener((tabId, changeInfo, tab) => {
if (changeInfo.status == 'plete' && myURLs.some(url => tab.url.includes(url))) {
chrome.tabs.executeScript(tabId,{file:'js/jquery-3.2.1.min.js'},()=>{
chrome.tabs.executeScript(tabId,{file:'js/sites_cs.js'});
});
}
});
This way you just need to keep the variable myURLs
updated with the desired URLs and your content scripts will be injected only on those sites.
本文标签: javascriptRun a chrome extension on specific websitesStack Overflow
版权声明:本文标题:javascript - Run a chrome extension on specific websites - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742010154a2412736.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论