0

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)

fwx
  • 333
  • 1
  • 5
  • 14
  • How about adding another `person` in Chrome user manager with those extensions enabled? When you click the user manager and switch a person it opens a new window where you can do your [dubious] deeds without touching the main profile. – wOxxOm Sep 01 '16 at 19:52
  • Although this is perfectly workable, I like the solution you listed below better. – fwx Sep 01 '16 at 20:38

2 Answers2

2

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.

wOxxOm
  • 65,848
  • 11
  • 132
  • 136
  • The hotkey toggling works perfect (I wasn't looking for the URL-based solution since I want manual control on this functionality). Is it possible to use the background.js to automate the form filling on predefined URLs as well? I'm asking because that's the entire reason I need to toggle these extensions - so that the forms on certain pages show up properly - if I can bundle the form filling, a single hotkey combination will do everything for me. – fwx Sep 01 '16 at 20:31
  • You'll need a [content script](http://stackoverflow.com/a/4532567/3959875) - it runs in the context of the webpage so you can do all the usual js stuff. – wOxxOm Sep 01 '16 at 20:40
  • I just noticed something - if the hotkey combination has been previously assigned to another extension, it can't be overloaded for a new operation. Is there any workaround to this? One of the extensions I use needs to be toggled by clicking to open it and then toggling. A hotkey has already been assigned to do this without requiring to click on the extension - it would be great if I could overload that hotkey combination for disabling everything else. – fwx Sep 01 '16 at 20:55
  • http://www.howtogeek.com/127162/how-to-create-custom-keyboard-shortcuts-for-browser-actions-and-extensions-in-google-chrome/ – wOxxOm Sep 01 '16 at 20:59
  • I've actually already seen that link. But it's not very useful in figuring out how to overload hotkeys for extensions in Chrome. I want to, for eg, use Ctrl+Shift+S for toggling options in multiple extensions when this combination is already assigned to something beforehand. – fwx Sep 01 '16 at 21:14
  • Sorry to pile on this comment thread, but how can I add functionality to run background.js on clicking this new extension as well? The commands argument in manifest.json only enables keyboard listening. – fwx Sep 01 '16 at 21:29
  • I see what you wanted with the hotkeys but it's not possible. As for your second question, I didn't understand it but I guess you mean [browserAction icon](https://developer.chrome.com/extensions/browserAction). I think it's covered in the extension [overview](https://developer.chrome.com/extensions/overview). – wOxxOm Sep 01 '16 at 21:34
  • Thanks! I managed to figure out everything I needed from this thread! – fwx Sep 02 '16 at 04:27
1

You could try to use chrome.management API to disable/enable the extensions.

However, if the function you need from an extension depends on some trigger, such as a click on the extension, you can't replicate that.

Xan
  • 74,770
  • 16
  • 179
  • 206
  • I just wrote a script that uses the management API and it works just fine in detecting which extensions are installed. However, all of the extensions do require me to click on them and then toggle the switch. Why is it not possible to replicate this with code? – fwx Sep 01 '16 at 19:46
  • Because you cannot perform the "click on interface button" action or inject code into another extension's UI. It's not supported by the API. – Xan Sep 01 '16 at 19:47