✨ Add @site-access opt-in scripts to the popup#1597
Draft
cyfung1031 wants to merge 5 commits into
Draft
Conversation
2 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Checklist / 检查清单
背景
脚本作者需要声明“默认不在所有站点运行,但允许用户明确选择站点”的脚本。
现有
@match、@include、@exclude规则适合描述脚本的原始匹配范围,但不能表达用户主动选择的 opt-in 站点,也不能安全地把 opt-in 规则直接混入原始匹配规则。因此,本次增加独立的
@site-accessopt-in 规则:其中:
opt-in表示脚本需要额外的站点访问许可;+开头的规则表示作者声明的初始 / 默认站点;@site-access +<pattern>元数据。本次改动
@site-access opt-in以及@site-access +<pattern>默认站点。+*://<host>/*时显示移除许可操作。@early-start和@unwrap脚本都在执行边界检查原始 URL 规则与 site-access 规则的交集。chrome.userScripts.update/register。实现考虑
1. 不修改原始
@match/@include/@exclude语义opt-in 站点许可不会覆盖脚本原始 URL 规则。
脚本是否执行必须同时满足:
@match/@include/@exclude规则允许当前 URL;@site-access的作者默认规则或用户自定义规则允许当前 URL;这样可以避免把用户的 opt-in 设置误认为脚本作者扩大了脚本原始匹配范围。
2. 在浏览器注册和执行载荷两侧同时限制访问范围
仅在页面加载请求中检查 site-access 不足以保护以下路径:
@early-start脚本可能在页面请求之前执行;@unwrap脚本可能直接执行注册载荷;因此实现同时处理:
chrome.userScripts注册范围;@early-start预注入载荷;@unwrap直接执行载荷;被拒绝的页面不会执行脚本,也不会暴露可直接调用的完整脚本函数。
3. 用户规则使用 additive custom metablock
用户通过 Popup 加入当前站点时,实际保存的是:
作者声明的
@site-access内容不会被覆盖,用户规则可以单独编辑、删除或重置。Popup 的“移除当前站点”操作只删除它自己生成的精确 host 规则。更宽泛的规则需要在设置页面中明确编辑,避免删除操作显示成功但脚本仍因其他规则继续运行。
4. Popup reload 必须等待注册完成
加入或移除站点会触发脚本注册信息更新。
消息队列原有的普通 publish 是异步通知,不等待订阅者完成,因此 Popup 如果立即 reload,可能使用旧的注册规则。现在 Popup RPC 会等待当前 Service Worker 中的异步 install handler 完成,确认注册更新结束后才 reload 当前标签页。
5. 保持 issue 范围清晰
本 PR 实现的是作者控制的
@site-access opt-in设计。原始
@match/@include不匹配当前页面的脚本仍不会因为本功能自动出现在 Popup 中。面向所有未匹配脚本的发现、展示和双向管理属于更广泛的后续范围,本 PR 不改变现有脚本匹配边界。已知限制
1. 不会自动发现原始规则完全不匹配的脚本
如果脚本的原始
@match/@include规则完全不匹配当前页面,即使作者声明了@site-access opt-in,该脚本也不会被本 PR 自动加入 Popup 的候选列表。2. Popup 只处理精确 host 许可
Popup 的快捷移除操作只处理
+*://<host>/*形式的用户规则。以下规则不会通过 Popup 的快捷按钮删除:
这些规则需要在脚本设置页面中编辑或删除。
3. 作者默认站点不能通过“移除用户许可”关闭
如果作者默认规则已经允许当前站点,用户删除自己的同站点规则不会阻止脚本运行。因此这种情况下 Popup 不显示误导性的移除许可操作。
建议审查重点
@site-access opt-in是否始终与原始 URL 规则取交集;@early-start、@unwrap执行载荷是否都执行 site-access 检查;chrome.userScripts.update/register完成;关联
Related to #1590
验证
pnpm exec vitest run --no-coverage --reporter=dot --reporter.summary=false:303 个文件,3,285 个测试通过pnpm typecheck:通过pnpm build:通过;仅保留现有 Rspack asset-size 和 Monaco 警告