问题描述
今天,在打开谷歌浏览器的同时,我意识到没有简单的方法来启用或禁用扩展程序,而无需转到以下任一位置:
- chrome:// extensions
- 点击工具>扩展程序>启用/禁用
之所以如此重要,是因为它占用的资源。
例如:我将启动计算机,并立即打算快速打开Google Chrome。比方说,我在打开Chrome之前运行了100个进程。但是,一旦我打开Chrome浏览器,该数字会跳到160,因为它启动时会加载所有的扩展名。
这是我期望实现的目标和当前限制:
预期结果:
使用控制台轻松启用/禁用/卸载扩展程序
限制:
无法将多个扩展分组,以便轻松启用/禁用
请让我知道,如果这部分问题不允许/关闭主题
Chrome将扩展设置存储在JSON中文件命名首选项在您的个人资料目录中(这里是〜/ .config / google-chrome / Default / Preferences)。启用/禁用标志是每个分机的状态键,其中1表示启用,0表示禁用。在启动Chrome之前,您可以编写一个修改此文件的脚本。如果您想自动启动Chrome,可以将此脚本设置为在登录时运行,甚至可以在最后启动Chrome。存储一份您希望显式禁用预启动的扩展程序列表,以便只选择其中的一部分。
我会确定您在Chrome运行时不更新首选项。
这适用于我,可能适用于任何类似nix的系统。移植到Windows应该是相当直接的:chrome_dir和检查Chrome是否正在运行可能是唯一需要的更改。
#!/ usr / bin / env python2.6
导入日期时间
导入json
导入os
导入sys $ b $从os导入路径
chrome_dir = path.expanduser(〜/ .config / google-chrome)
如果path.lexists(chrome_dir +/ SingletonLock):
#可能有一个更好和便携的方式来确定铬是否正在运行
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 = [
#列表哈希键,你可以在chrome上找到URL:// extensions
aeoigbhkilbllfomkmmilbfochhlgdmh,
]
disable_keys = [
hash-like key here,
]
default_state = 0
#1启用,0禁用,无转向e单独
以open(prefs_file)作为f:
prefs = json.load(f)
os.rename(prefs_file,prefs_backup_file)
如果不是ext.has_key(state):
#可能被列入黑名单
continue
如果键入enable_keys:
ext [state] = 1
elif键入disable_keys:
ext [state] = 0
elif default_state不是None:
ext [state] = default_state
with open(prefs_file,w)as f:
json.dump(prefs,f)
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:
- chrome://extensions
- 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
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)
这篇关于如何通过控制台启用/禁用谷歌浏览器扩展的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!