How do I enable/disable google chrome extensions via console

24,821

Chrome stores extension settings in a JSON file named Preferences in your profile directory (here it is ~/.config/google-chrome/Default/Preferences). The enabled/disabled flag is the "state" key for each extension, with 1 for enabled and 0 for disabled. You could write a script that modified this file before you start Chrome. You could set this script to run on log-in, and even to launch Chrome at the end, if you wanted to auto-start Chrome. Store a list of extensions you want to explicitly disable pre-launch to select only some of them.

I would make certain you don't update Preferences while Chrome is running.

This works for me, and is likely to work on any *nix-like system. Porting to Windows should be fairly straight-forward: chrome_dir and the check for whether Chrome is running or not may be the only changes required.

#!/usr/bin/env python2.6

import datetime
import json
import os
import sys
from os import path

chrome_dir = path.expanduser("~/.config/google-chrome")
if path.lexists(chrome_dir + "/SingletonLock"):
  # there may be a better and portable way to determine if chrome is running
  sys.exit("chrome already running")

prefs_file = chrome_dir + "/Default/Preferences"
now = datetime.datetime.now()
prefs_backup_file = prefs_file + now.strftime("-%Y%m%d-%H%M%S")

enable_keys = [
  # list hash keys, you can find from URL given on chrome://extensions
  "aeoigbhkilbllfomkmmilbfochhlgdmh",
  ]
disable_keys = [
  "hash-like key here",
  ]

default_state = 0
# 1 to enable, 0 to disable, None to leave alone

with open(prefs_file) as f:
  prefs = json.load(f)
os.rename(prefs_file, prefs_backup_file)

for key, ext in prefs["extensions"]["settings"].iteritems():
  if not ext.has_key("state"):
    # may be blacklisted
    continue

  if key in enable_keys:
    ext["state"] = 1
  elif key in disable_keys:
    ext["state"] = 0
  elif default_state is not None:
    ext["state"] = default_state

with open(prefs_file, "w") as f:
  json.dump(prefs, f)
Share:
24,821
Jon
Author by

Jon

Updated on December 31, 2020

Comments

  • Jon
    Jon over 3 years

    Today, while opening Google Chrome, I realized that there is no easy way to enable or disable an extension without going to one of the following locations:

    1. chrome://extensions
    2. clicking on Tools>Extensions>Enable/Disable

    The reason why this is so important, is because of the resources it takes up.

    For example: I will be starting up my computer, and I immediately want to open Google Chrome quickly. Let's say, for instance, that I am running 100 processes before I open Chrome. However, once I open Chrome, that number jumps to 160 because of all the extensions that load when it starts.

    Here is what I am looking to achieve and the current limitations:

    Desired Outcome: Easily enable/disable/uninstall an extension using the console

    Limitations: There is no way to group many extensions, so that they can easily be enabled/disabled

    Please let me know if this portion of the question is not allowed/off topic

  • Jon
    Jon almost 13 years
    Hey @Fred Nurk, I appreciate you taking the time to write a script in Linux, but how do I convert it to Windows, such as VBScript or Powershell?
  • Fred Nurk
    Fred Nurk almost 13 years
    @Jon: I have no idea about VB and Powershell. This is a Python script, and Python runs on Windows. I listed what I believe to be the only changes needed for Windows, but I don't have it here to check.
  • Jon
    Jon almost 13 years
    I will look into this and post any of my findings.
  • Fred Nurk
    Fred Nurk almost 13 years
    @Jon: Setting default_state to None and only listing problematic (resource hungry) extensions in disable_keys may be your best bet, if you don't like disabling all of them. FWIW I have enjoyed Chrome for a quite a while, but I've gone back to Opera because of exactly this problem you have: for the functionality I want, Opera runs better. YMMV, as always.
  • Jon
    Jon almost 13 years
    I will run it using Iron Python if that is possible.
  • Fred Nurk
    Fred Nurk almost 13 years
    @Jon: I haven't tried IronPython, but this or something very like it (maybe a minor syntax change) should work fine. You can also get CPython for Windows from python.org – name the file *.pyw for CPython/Windows to run it without showing a terminal (the script will only have output if there is a problem).
  • Jon
    Jon almost 13 years
    Ok, here is an update: The location where the Preferences file is %localappdata%\Google\Chrome\User Data\Default\Preferences
  • Fred Nurk
    Fred Nurk almost 13 years
    @Jon: os.getenv("localappdata") will get that variable, but are you sure it's all lowercase? It might be os.getenv("LocalAppData") or os.getenv("LOCALAPPDATA"). So: chrome_dir = path.join(os.getenv("LOCALAPPDATA"), "Google/Chrome/User Data"). You'll still need to figure out how to determine if Chrome is currently running.
  • Jon
    Jon almost 13 years
    I think that the syntax should be waitpid whatever chrome's process Id is. Maybe a regular expression for a window containing Chrome AND Google would suffice.
  • Fred Nurk
    Fred Nurk almost 13 years
    @Jon: Where does Chrome store its PID on Windows? That sounds much better than searching window titles, which would produce many false positives including (except for capitalization) the title for a browser window showing this very page.
  • Jon
    Jon almost 13 years
    Here is what I found, but I don't know if it will work for Chrome bytes.com/topic/unix/answers/…