Skip to content

Instantly share code, notes, and snippets.

@Xsir0
Created January 29, 2026 15:03
Show Gist options
  • Select an option

  • Save Xsir0/c26e0f517be79658f954c21eccc156e2 to your computer and use it in GitHub Desktop.

Select an option

Save Xsir0/c26e0f517be79658f954c21eccc156e2 to your computer and use it in GitHub Desktop.
Patch Chrome Local State for all local profiles: set is_glic_eligible=true (recursive), force variations_country=us, update variations_permanent_consistency_country. Auto backup + restart Chrome if needed.
#!/usr/bin/env bash
set -euo pipefail
need_cmd() { command -v "$1" >/dev/null 2>&1 || { echo "Missing: $1" >&2; exit 1; }; }
need_cmd jq
OS="$(uname -s)"
if [[ "$OS" != "Darwin" ]]; then
echo "This script is macOS-only (Darwin). Current: $OS" >&2
exit 1
fi
# -------- paths --------
declare -a LABELS=()
declare -a PATHS=()
add_path_if_exists() {
local label="$1" path="$2"
path="${path/#\~/$HOME}"
if [[ -d "$path" ]]; then
LABELS+=("$label")
PATHS+=("$path")
fi
}
get_user_data_paths() {
LABELS=(); PATHS=()
add_path_if_exists "stable" "$HOME/Library/Application Support/Google/Chrome"
add_path_if_exists "canary" "$HOME/Library/Application Support/Google/Chrome Canary"
add_path_if_exists "dev" "$HOME/Library/Application Support/Google/Chrome Dev"
add_path_if_exists "beta" "$HOME/Library/Application Support/Google/Chrome Beta"
if [[ ${#PATHS[@]} -eq 0 ]]; then
echo "No Chrome user data dir found." >&2
exit 1
fi
}
# -------- chrome running state --------
is_chrome_running_any() {
pgrep -f 'Google Chrome' >/dev/null 2>&1
}
shutdown_chrome_graceful() {
# 优雅退出(包含 Canary/Dev/Beta)
osascript >/dev/null 2>&1 <<'OSA' || true
tell application "Google Chrome" to quit
tell application "Google Chrome Canary" to quit
tell application "Google Chrome Dev" to quit
tell application "Google Chrome Beta" to quit
OSA
# 等待最多 5 秒退出
for _ in {1..50}; do
if ! is_chrome_running_any; then
return 0
fi
sleep 0.1
done
# 兜底强杀
pkill -f 'Google Chrome' >/dev/null 2>&1 || true
}
get_last_version() {
local user_data_path="$1"
local f="$user_data_path/Last Version"
[[ -f "$f" ]] || return 1
tr -d '\r\n' < "$f"
}
patch_local_state() {
local user_data_path="$1" last_version="$2"
local f="$user_data_path/Local State"
if [[ ! -f "$f" ]]; then
echo "Failed to patch Local State. Not found: $f"
return 0
fi
cp -f "$f" "$f.bak.$(date +%Y%m%d%H%M%S)"
local tmp
tmp="$(mktemp)"
jq --arg last_version "$last_version" '
def patch_glic:
walk(
if type == "object" and has("is_glic_eligible") and (.is_glic_eligible != true)
then .is_glic_eligible = true
else .
end
);
.
| patch_glic
| .variations_country = "us"
| (if (.variations_permanent_consistency_country | type) == "array"
and (.variations_permanent_consistency_country | length) >= 2
then .variations_permanent_consistency_country[0] = $last_version
| .variations_permanent_consistency_country[1] = "us"
else .
end)
' "$f" > "$tmp"
mv -f "$tmp" "$f"
echo "Patched: $f"
}
restart_chrome_for_detected_variants() {
# 只重启“存在用户数据目录”的那些版本
local label
for label in "${LABELS[@]}"; do
case "$label" in
stable) open -a "Google Chrome" >/dev/null 2>&1 || true ;;
canary) open -a "Google Chrome Canary" >/dev/null 2>&1 || true ;;
dev) open -a "Google Chrome Dev" >/dev/null 2>&1 || true ;;
beta) open -a "Google Chrome Beta" >/dev/null 2>&1 || true ;;
esac
done
}
main() {
get_user_data_paths
local was_running=0
if is_chrome_running_any; then
was_running=1
echo "Shutdown Chrome..."
shutdown_chrome_graceful
fi
local i
for i in "${!PATHS[@]}"; do
local label="${LABELS[$i]}"
local user_data_path="${PATHS[$i]}"
local last_version
if ! last_version="$(get_last_version "$user_data_path")"; then
echo "Failed to get version. Not found: $user_data_path/Last Version"
continue
fi
echo "Patching Chrome $label $last_version \"$user_data_path\""
patch_local_state "$user_data_path" "$last_version"
done
if [[ "$was_running" -eq 1 ]]; then
echo "Restart Chrome..."
restart_chrome_for_detected_variants
else
echo "Chrome was not running, skip restart."
fi
read -r -p "Enter to continue... " _
}
main "$@"
@lcandy2
Copy link

lcandy2 commented Jan 30, 2026

@Xsir0
Copy link
Author

Xsir0 commented Jan 30, 2026

研究方法请注明来源: https://github.com/lcandy2/enable-chrome-ai Credit source: https://github.com/lcandy2/enable-chrome-ai

感谢指正,我也是昨天看到小互发后,直接用AI 转写后就发出来了,并没有关注这些

@imoling
Copy link

imoling commented Jan 30, 2026

需要美国网吗?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment