修复 install.ps1 在 Windows PowerShell 5.1 下的两个兼容性 bug

- ConvertFrom-Json 不支持 -Depth 参数(该参数只存在于 ConvertTo-Json),
  5.1 下三处调用会直接报错终止,已实测复现并修复。
- 文件缺少 UTF-8 BOM,导致 5.1 用 -File 加载脚本时按系统代码页误读
  中文/emoji 字符串,解析失败;PS7 默认按 UTF-8 解析不受影响。已加 BOM 修复。

已在本机 PowerShell 5.1 和 PowerShell 7 上分别实测通过(脚本解析 +
test-fixtures 三种 profile 场景的 JSON 读写逻辑 + 幂等性)。

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XBPAN7bzFphM8eMDwACmxs
This commit is contained in:
SkyJourney
2026-08-16 13:50:32 +08:00
co-authored by Claude Sonnet 5
parent d90d2ecd03
commit b8f30dcc9f
+7 -5
View File
@@ -1,4 +1,4 @@
# 强制启用 Chrome 中的 GeminiProject Glic(PowerShell 版本) # 强制启用 Chrome 中的 GeminiProject Glic(PowerShell 版本)
# 通过修补 Chrome 的 Local State 配置文件,绕过地区资格限制 # 通过修补 Chrome 的 Local State 配置文件,绕过地区资格限制
# 支持系统:Windows # 支持系统:Windows
# #
@@ -56,8 +56,10 @@ if (-not $chromeVersion) {
Write-Host "⚠️ 未能自动探测 Chrome 版本号,variations_permanent_consistency_country 的版本位会留空" -ForegroundColor Yellow Write-Host "⚠️ 未能自动探测 Chrome 版本号,variations_permanent_consistency_country 的版本位会留空" -ForegroundColor Yellow
} }
# 读取并解析 JSON(-Depth 需要给够,避免嵌套结构在读取阶段被截断) # 读取并解析 JSON
$json = Get-Content -Path $chromeStatePath -Raw -Encoding UTF8 | ConvertFrom-Json -Depth 100 # 注意:ConvertFrom-Json 不接受 -Depth 参数(该参数只存在于 ConvertTo-Json
# Windows PowerShell 5.1 下传了会直接报"找不到与参数名"Depth"匹配的参数"并终止执行)
$json = Get-Content -Path $chromeStatePath -Raw -Encoding UTF8 | ConvertFrom-Json
# 对可能不存在的属性做"存在则改、不存在则插入"的统一封装, # 对可能不存在的属性做"存在则改、不存在则插入"的统一封装,
# 因为 PSCustomObject 对不存在的属性不能直接用 `=` 赋值 # 因为 PSCustomObject 对不存在的属性不能直接用 `=` 赋值
@@ -97,7 +99,7 @@ $tmpPath = "$chromeStatePath.tmp"
# 校验写回的内容仍是合法 JSON,且目标字段确实生效;不合法就回滚、不覆盖原文件 # 校验写回的内容仍是合法 JSON,且目标字段确实生效;不合法就回滚、不覆盖原文件
try { try {
$verify = Get-Content -Path $tmpPath -Raw -Encoding UTF8 | ConvertFrom-Json -Depth 100 $verify = Get-Content -Path $tmpPath -Raw -Encoding UTF8 | ConvertFrom-Json
if ($verify.variations_country -ne "us") { if ($verify.variations_country -ne "us") {
throw "variations_country 未生效" throw "variations_country 未生效"
} }
@@ -111,7 +113,7 @@ try {
# 汇报结果 # 汇报结果
Write-Host "" Write-Host ""
$errors = 0 $errors = 0
$final = Get-Content -Path $chromeStatePath -Raw -Encoding UTF8 | ConvertFrom-Json -Depth 100 $final = Get-Content -Path $chromeStatePath -Raw -Encoding UTF8 | ConvertFrom-Json
if ($final.variations_country -eq "us") { if ($final.variations_country -eq "us") {
Write-Host "✓ variations_country 已设为 us" -ForegroundColor Green Write-Host "✓ variations_country 已设为 us" -ForegroundColor Green
} else { } else {