user1649740 user1649740 - 1 year ago 64
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 Source

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

Use 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"]


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

    To get the IDs by short extension names:

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

    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
    chrome.webNavigation.onBeforeNavigate.addListener(beforeNavigate, {
        url: [{hostEquals: ''}, {urlContains: 'something'}]
    function beforeNavigate(details) {
        if (details.frameId === 0) {
    function setState(newState) {'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('://') > 0) {
    // In the actual code you may want to track chrome.tabs.onUpdated too
    // in order to detect in-tab navigation from to another site

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