Skip to content

🐛 Chrome 拒绝操作时说清原因:file:// 访问开关、无效网址、书签越界、Cookie 规则与休眠标签页 - #19

Merged
CodFrm merged 2 commits into
mainfrom
fix/file-url-access
Nov 6, 2026
Merged

CodFrm merged 2 commits into
mainfrom
fix/file-url-access

Conversation

@CodFrm

@CodFrm CodFrm commented Nov 6, 2026 •

Copy link
Copy Markdown
Member

背景

用户反馈 sctl 操作不了 file:// 页面。排查后发现,原因是从 Chrome 应用商店 / Edge 加载项安装的 sctl Browser 默认关着「允许访问文件网址」(开发者模式加载的扩展默认是开的),而报错完全看不出这一点。顺着这个问题,又在真机上批量探测了各命令被 Chrome 拒绝时的报错,修掉其中吞掉原因或说错原因的几处。

改动

情况 原来 现在
开关关着时,已打开的 file:// 标签页上跑 page * / debug * / cdp send PAGE_NOT_AUTOMATABLE: Cannot attach to this target. PAGE_NOT_AUTOMATABLE,消息说明开关以及 chrome://extensions/?id=<id>
开关关着时 tabs open / windows open file:// INTERNAL_ERROR: internal error INVALID_REQUEST,同上的提示
开关关着时 page goto file:// INVALID_REQUEST: {"code":-32000,"message":"Navigating to local URL is not allowed"} INVALID_REQUEST,同上的提示
tabs open / windows open 给 notaurl 这类非绝对网址 成功,打开的是扩展自己的 404 页面 INVALID_REQUEST: notaurl is not a valid URL,与书签、下载、历史一致
tabs open / windows open 给 javascript: 等 Chrome 不肯打开的网址 INTERNAL_ERROR: internal error INVALID_REQUEST,带 Chrome 原话
不给 --folder 时 bookmarks add / mkdir 的 --index 越界 INTERNAL_ERROR: internal error INVALID_REQUEST: Index out of bounds.
cookies set --same-site no_restriction 不带 --secure;--domain 不覆盖 URL 的主机 Failed to parse or set cookie named "a". INVALID_REQUEST,说明是哪条规则
被浏览器休眠(discard)的标签页上跑页面命令 PAGE_UNRESPONSIVE,只猜是遗留的 JS 弹框 说明里补上休眠这一原因;page reload / goto 照旧能恢复
  • 开关的判断在 extension/src/handlers/fileAccess.ts 里:只在 Chrome 已经拒绝、且网址是 file:// 时才查 chrome.extension.isAllowedFileSchemeAccess()。开关打开时,Chrome 拒绝仍保留原话。
  • 休眠标签页只改 daemon 的报错文字:daemon 靠在临时附加上发 Page.reload 来恢复,如果扩展抢先拒绝附加,page reload / goto 的恢复就会失效。
  • 错误码都是已有的,protocol.json 不变。错误码有变化的只有 tabs.open、windows.open、书签 create:从 INTERNAL_ERROR 变成 INVALID_REQUEST。
  • 三份重复的网址校验合并为 targets.requireValidUrl;errorMessage 移到 registry 共用。
  • docs/protocol.md、sctl debug 帮助、skills/sctl(SKILL.md、references/page.md)已同步。

验证

  • 扩展:vitest 643 个测试通过(新增 15 个,除 1 个对照外改前都失败),typecheck / lint / format 通过。
  • Go:go build、go vet 通过,go test ./... -race exit 0,golangci-lint run ./cmd/... ./internal/... 0 issues。TestManagerAttachUnresponsive 新增断言:报错要提到 discarded。
  • 真机(按 docs/verification.md):Chrome for Testing 125,隔离配置,只加载本分支 extension/dist,配对隔离 daemon(127.0.0.1:18691)。用 developerPrivate 开关文件网址访问,用 chrome.tabs.discard 制造休眠标签页。修复前复现了上表「原来」一列的全部报错;修复后得到「现在」一列。开关打开时,同样的 file:// 命令全部 exit 0;休眠标签页上 page reload / page goto / tabs reload 后命令照常成功。

没改的

  • 开关关着时,Playwright 经 sctl cdp endpoint 能连上,file:// 标签页不出现在页面列表里,连接不失败。
  • view-source: 页面附加失败时,Chrome 的原话是 "Extension manifest must request permission to access this host",容易误解,但仍能看出是不能访问该页面。
  • downloads start chrome://settings 当场返回成功,下载会在之后失败。
  • 其余报错都已经清楚,例如跨窗口建组、未知 session id、阅读列表只收 http(s)、对 chrome:// / file:// 网址设置 Cookie。

从商店安装的扩展默认关着「允许访问文件网址」,这时 Chrome 拒绝 file:// 只给出
"Cannot attach to this target."、"internal error" 或 CDP 原话,看不出原因。
Chrome 拒绝涉及 file:// 网址的操作后检查 chrome.extension.isAllowedFileSchemeAccess(),
确实是开关关着就换成说明原因与 chrome://extensions/?id=<id> 的错误,错误码沿用:

- 附加调试器(page / debug / cdp send):PAGE_NOT_AUTOMATABLE
- tabs.open、debugger.open、url 参数为 file:// 的 debugger.send(page goto):INVALID_REQUEST
  (tabs.open 原为 INTERNAL_ERROR)

开关打开时的拒绝仍带 Chrome 原话。protocol.md 与 sctl 技能的 PAGE_NOT_AUTOMATABLE 说明同步补上。
真机探测各命令在 Chrome 拒绝时的报错,修正其中吞掉原因或说错原因的几处:

- tabs open / windows open:不是绝对网址时直接 INVALID_REQUEST(原先 "notaurl" 被当成相对地址,
  打开扩展自己的 404 页面);Chrome 拒绝打开(javascript: 等)时带着 Chrome 原话作为 INVALID_REQUEST,
  原为 INTERNAL_ERROR "internal error"。windows open 的 file:// 同样给出文件网址开关的提示,debugger.open 同理。
- bookmarks add / mkdir 不给 --folder 时越界的 --index:带 Chrome 原话作为 INVALID_REQUEST,原为 INTERNAL_ERROR。
- cookies set:SameSite no_restriction 没有 secure、domain 不覆盖 URL 的主机时说明规则,
  原先只有 Chrome 的 "Failed to parse or set cookie named …"。
- 浏览器休眠(discard)的标签页上的页面命令:PAGE_UNRESPONSIVE 的说明补上休眠这一原因,
  原先只猜是遗留的 JS 弹框;page reload / goto 照旧能恢复它。

三处重复的网址校验合并为 targets.requireValidUrl,errorMessage 移到 registry 共用。
protocol.md、CLI 帮助与 sctl 技能同步。
@CodFrm CodFrm changed the title 🐛 sctl Browser 没有文件网址访问权限时,file:// 操作直接说明原因和开关位置 🐛 Chrome 拒绝操作时说清原因:file:// 访问开关、无效网址、书签越界、Cookie 规则与休眠标签页 Nov 6, 2026
@CodFrm
CodFrm merged commit cba6195 into main Nov 6, 2026
4 checks passed
@CodFrm
CodFrm deleted the fix/file-url-access branch November 6, 2026 03:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant