user1649740 user1649740 - 3 months ago 11
jQuery Question

Automating function of a Chrome Extension?

I have some Chrome extensions installed that need to be toggled on/off when using. The default state for all of them is off. However, when I access some particular webpages, I need to turn them all on (there is no option to filter pages in these extensions).

Since this is a chore, I am trying to create a Chrome extension that, on click, will simply toggle all of them on/off. Firstly, is it possible to do this? And, if yes, how do I go about doing this?

As an example, one of the extensions I want to automate is the TunnelBear VPN (Toggle on through my extension when required)

Answer

I'm using such an extension toggler myself, so I've copied some parts of it here.

Use chrome.management API to enable/disable the extensions.

  1. Enable the extensions manually by a hotkey using chrome.commands API:

    manifest.json, relevant parts:

    "commands": {
        "toggle": {
            "suggested_key": {
                "default": "Alt+T"
            },
            "description": "Toggle extensions"
        }
    },
    "background": {
        "scripts": ["background.js"],
        "persistent": false
    },
    "permissions": ["commands", "management"]
    

    background.js:

    var IDs = ['aasdkfjhkjdfhdfjkhdkfjhdkjfh'];
    chrome.commands.onCommand.addListener(function(command) {
        IDs.forEach(function(ID) {
            chrome.management.get(ID, function(oldState) {
                chrome.management.setEnabled(ID, !oldState.enabled);
            });
        });
    });
    

    To get the IDs by short extension names:

    var IDs;
    chrome.management.getAll(function(info) {
        IDs = info.filter(function(extension) {
            return extension.shortName.match(/Name1|Name2|Name3/);
        }).map(function(extension) {
            return extension.id;
        });
    );
    

    Alternatively, you can define two hotkeys to enable and disable the extension.

  2. Enable the extensions automatically upon navigation to specified URLs

    Some of the extensions may want to inject their content script on "document_start" so I guess we'll need chrome.webNavigation.onBeforeNavigate. Even though this event doesn't guarantee an actual navigation will occur, it's probably the only way to enable the extensions in time.

    Use event filters to specify the URLs to activate, the possible criteria are listed in documentation.

    // Enable the extension upon navigation to example.com
    
    chrome.webNavigation.onBeforeNavigate.addListener(beforeNavigate, {
        url: [{hostEquals: 'example.com'}, {urlContains: 'something'}]
    });
    
    function beforeNavigate(details) {
        if (details.frameId === 0) {
            setState(true);
        }
    }
    
    function setState(newState) {
        chrome.management.setEnabled('dhgfhdgfjgjhdgfjdfhdjhfdjhf', newState);
    }
    
    // And let's disable the extensions when that site is closed
    
    chrome.tabs.onRemoved.addListener(function(tabId, info) {
        chrome.tabs.get(tabId, function(tab) {
            if (tab.url.indexOf('://example.com') > 0) {
                setState(false);
            }
        });
    });
    
    // In the actual code you may want to track chrome.tabs.onUpdated too
    // in order to detect in-tab navigation from example.com to another site
    

    Required permissions: "webNavigation", "tabs"
    Background page declaration can be the same as in #1.