javascript 通过浏览器操作/图标禁用/启用 Chrome 扩展

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18740932/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 13:02:50  来源:igfitidea点击:

Disable / Enable Chrome Extension Via Browser Action / Icon

javascriptgoogle-chromegoogle-chrome-extension

提问by TyGoss

The chrome extension I am developing inserts content scripts and css onto every page of a website. However, the user may have a certain page or pages he or she does not want the extension to run on, so it would be great if I could set up the browser action as basically a toggle on / off.

我正在开发的 chrome 扩展将内容脚本和 css 插入网站的每个页面。但是,用户可能有他或她不希望扩展程序运行的某个或多个页面,因此如果我可以将浏览器操作设置为基本上打开/关闭切换,那就太好了。

What I'd like to do is something like this:

我想做的是这样的:

chrome.browserAction.onClicked.addListener(function(tab) {

    //IF ENABLED THEN DISABLE

    //IF DISABLED THEN ENABLE

} 

Any help would be greatly appreciated!

任何帮助将不胜感激!

采纳答案by Andrey

Such API is not provided. But two possible workarounds exist:

不提供此类 API。但是存在两种可能的解决方法:

I. You can use the "disabled" flag variable and update it from your background page.

I. 您可以使用“禁用”标志变量并从您的后台页面更新它。

Background page:

后台页面:

function disableExtension(disabled)
{
    chrome.windows.getAll({populate : true}, function (window_list)
    {
        for (var i = 0; i < window_list.length; ++i)
        {
            var window = window_list[i];
            for (var j = 0; j < window.tabs.length; ++j)
            {
                var tab = window.tabs[j];
                if (checkContentScriptExists(tab))
                {
                    chrome.tabs.executeScript(tab.id, {code : "disabled = " + disabled + ";"}, allTabs: true) 
                }
            }
        }
        // No matching url found. Open it in the new tab
        chrome.tabs.create({ url : url, selected: true });
    });
}

And content script should check the condition before the run

并且内容脚本应该在运行前检查条件

if (!disabled) doSomething();

II. A controversial approach to save disable variable within background age content

二、在背景年龄内容中保存禁用变量的有争议的方法

Background page:

后台页面:

function disableExtension(disabled)
{
    global.disabled = disabled;
}
chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) {
    if (request.msg == "getDisabled") {
        sendResponse({disabled: global.disabled});
        return true;
    }
});

and the content script should query currently disabled status before execution

并且内容脚本应在执行前查询当前禁用状态

chrome.runtime.sendMessage({msg: "getDisabled"}, function(response) {
   if (!response.disabled) doSomething();
});

回答by Rajat Sharma

Chrome extensions have APIs of their own. You can invoke them via content scripts. Not sure if you can invoke them via any third party JS. Refer this ink

Chrome 扩展程序有自己的 API。您可以通过内容脚本调用它们。不确定您是否可以通过任何第三方 JS 调用它们。参考这个墨水

Hope it helps.

希望能帮助到你。