Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I enable/disable google chrome extensions via console

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

like image 224
Jon Avatar asked Jun 09 '11 01:06

Jon


1 Answers

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)
like image 94
Fred Nurk Avatar answered Sep 30 '22 23:09

Fred Nurk