admin管理员组文章数量:1355522
I have developed a chrome extension and it is working absolutely fine.
Part of the manifest.json
looks like this:
"content_scripts":[
{
"js":["js/script.js"],
"css": ["css/style.css"],
"matches": ["http://localhost/*", "https://localhost/*"]
}
],
so the extension injects the content script only when the domain is localhost
, which is also working fine. Now I want a way by which the extension popup can have a enable extension on this domain
or disable extension on this domain
in the so the user can enable/disable the extension according to needs.
I have seen that in multiple ad-blocker plugins, so I guess it should be possible.
I have developed a chrome extension and it is working absolutely fine.
Part of the manifest.json
looks like this:
"content_scripts":[
{
"js":["js/script.js"],
"css": ["css/style.css"],
"matches": ["http://localhost/*", "https://localhost/*"]
}
],
so the extension injects the content script only when the domain is localhost
, which is also working fine. Now I want a way by which the extension popup can have a enable extension on this domain
or disable extension on this domain
in the so the user can enable/disable the extension according to needs.
I have seen that in multiple ad-blocker plugins, so I guess it should be possible.
Share Improve this question edited Nov 16, 2019 at 1:57 Cœur 38.8k25 gold badges205 silver badges277 bronze badges asked Sep 28, 2017 at 19:34 voidvoid 36.7k10 gold badges69 silver badges111 bronze badges 5- Either switch to declarativeContent API with RequestContentScript or simply store the state in chrome.storage and read it in the content script. – woxxom Commented Sep 28, 2017 at 19:44
- @wOxxOm when you are saying store the state in chrome.storage then which state are you referring to? And how exactly will that enable content script on a certain domain? – void Commented Sep 29, 2017 at 4:30
- With the state approach, the content script runs on all sites. – woxxom Commented Sep 30, 2017 at 11:10
- I'd try declarativeContent or manual programmatic injection via tabs.executeScript. – woxxom Commented Sep 30, 2017 at 11:11
-
I highly suggest not using
declarativeContent.RequestContentScript
: it's broken and unsupported. CSS isn't injected and JS can be injected multiple times on the same page. – fregante Commented Aug 3, 2019 at 7:51
2 Answers
Reset to default 4This needs two parts:
Programmatic script injection
Now there's the chrome.scripting.registerContentScripts()
API, which lets you programmatically register content scripts.
chrome.scripting.registerContentScripts([{
id: "a-script",
matches: ['https://your-dynamic-domain.example./*'],
js: ['content.js']
}]);
Acquiring new permissions
By using chrome.permissions.request
you can add new domains on which you can inject content scripts. An example would be:
// In a content script, options page or popup
document.querySelector('button').addEventListener('click', () => {
chrome.permissions.request({
origins: ['https://your-dynamic-domain.example./*']
}, granted => {
if (granted) {
/* Use contentScripts.register */
}
});
});
For this to work, you'll have to allow new origins to be added on demand by adding this in your manifest.json
{
"optional_permissions": [
"http://*/*",
"https://*/*"
]
}
There are also tools to further simplify this for you and for the end user, such as
webext-domain-permission-toggle
and webext-dynamic-content-scripts
, which are used by many GitHub-related extensions to add support for self-hosted GitHub installations.
They will also register your scripts in the next browser launches and allow the user the remove the new permissions and scripts as well.
Google is working on programmatic script registration for extension manifest v3. The new API will be called chrome.scripting.registerContentScript and pretty much matches what Firefox already has, minus the intuitive naming of the API. You can follow the implementation status in this Chrome bug.
本文标签: javascriptEnable content script in chrome extension programmaticallyStack Overflow
版权声明:本文标题:javascript - Enable content script in chrome extension programmatically - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1743937797a2565002.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论