Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disable / Enable Chrome Extension Via Browser Action / Icon

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.

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!

like image 568
TyGoss Avatar asked Sep 11 '13 12:09

TyGoss


People also ask

How do I force Chrome to disable extensions?

Open the Chrome browser and click on the three-dot menu icon in the upper right-corner. Click on More tools >Extensions. Simple click the toggle button of then extension you want to disable.

What is browser action icon?

A browser action is a button that your extension adds to the browser's toolbar. The button has an icon, and may optionally have a popup whose content is specified using HTML, CSS, and JavaScript. This key is replaced by action in Manifest V3 extensions.

How do I stop disabling developer mode extensions in Chrome?

Pack your extension: go to chrome://extensions , check Developer mode and click Pack extension. Install the extension by dragging and dropping the . crx file into the chrome://extensions page. You'll get an "Unsupported extensions disabled" popup if you try restarting Chrome at this point.


1 Answers

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

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

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 page 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();
});
like image 117
Andrey Avatar answered Nov 10 '22 03:11

Andrey