简体   繁体   English

以编程方式在 chrome 扩展中启用内容脚本

[英]Enable content script in chrome extension programmatically

I have developed a chrome extension and it is working absolutely fine.我开发了一个 chrome 扩展,它工作得非常好。

Part of the manifest.json looks like this: manifest.json如下所示:

"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.所以扩展只在域是localhost时才注入内容脚本,这也工作正常。 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 .现在我想要一种方式,扩展弹出窗口可以enable extension on this domaindisable extension on this domain上的disable extension on this domain以便用户可以根据需要启用/禁用扩展

I have seen that in multiple ad-blocker plugins, so I guess it should be possible.我在多个广告拦截器插件中看到过,所以我想这应该是可能的。

This needs two parts:这需要两部分:

Programmatic script injection程序化脚本注入

Now there's the contentScripts.register() API , which lets you programmatically register content scripts.现在有contentScripts.register() API ,它允许您以编程方式注册内容脚本。

browser.contentScripts.register({
    matches: ['https://your-dynamic-domain.example.com/*'],
    js: [{file: 'content.js'}]
});

This API is only available in Firefox but there's a Chrome polyfill you can use.此 API 仅在 Firefox 中可用,但您可以使用Chrome polyfill In the future there will also be chrome.scripting.registerContentScript but it's not yet implemented .将来还会有chrome.scripting.registerContentScript尚未实现

Acquiring new permissions获取新权限

By using chrome.permissions.request you can add new domains on which you can inject content scripts.通过使用chrome.permissions.request您可以添加可以注入内容脚本的新域。 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.com/*']
    }, 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为此,您必须通过在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.还有一些工具可以为您和最终用户进一步简化此操作,例如webext-domain-permission-togglewebext-dynamic-content-scripts ,许多与 GitHub 相关的扩展都使用它们来添加对自托管的支持GitHub 安装。

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. Google 正在为扩展清单 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.新的 API 将被称为chrome.scripting.registerContentScript并且几乎与 Firefox 已有的相匹配, 只是去掉了 API 的直观命名。 You can follow the implementation status in this Chrome bug .您可以关注此Chrome 错误中的实施状态。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM