Skip to content

[v1.4] 更新 Subscribe 相关代码和追加注释#1198

Merged
CodFrm merged 17 commits intoscriptscat:release/v1.4from
cyfung1031:pr-subscribe-update
Mar 29, 2026
Merged

[v1.4] 更新 Subscribe 相关代码和追加注释#1198
CodFrm merged 17 commits intoscriptscat:release/v1.4from
cyfung1031:pr-subscribe-update

Conversation

@cyfung1031
Copy link
Copy Markdown
Collaborator

@cyfung1031 cyfung1031 commented Feb 4, 2026

修正1

修正了 Subscribe 订阅、Script 直接安装,以及多个 Subscribe 之间的冲突问题
脚本的删除行为将只依首次安装方式决定

虽然 Subscribe 会关联脚本的 UUID,但若该 Subscribe 并非首次安装来源,即使删除该 Subscribe,也不会删除脚本。

行为说明如下:

  • 若首次以「连结方式」安装脚本,后续其他 Subscribe 内包含相同 Script URL,只会建立 UUID 关联;删除这些 Subscribe 不会删除脚本。
  • 若首次以 SubscribeA 安装脚本,之后再以连结方式或 SubscribeB 安装,则只有移除 SubscribeA 或直接删除脚本时,脚本才会被删除;删除 SubscribeB 不会影响该脚本。

修正2

  • 加强了错误检查

修正3

  • 加了注释

Script 的单独安装处理有可能提交另一个PR处理。
这只针对 Subscribe本身的处理

@cyfung1031 cyfung1031 changed the title 更新 Subscribe 相关代码和追加注释 [v1.3] 更新 Subscribe 相关代码和追加注释 Feb 4, 2026
Comment on lines +9 to +10
unknown3: 3, // 3 是什么?
unknown4: 4, // 4 是什么?
Copy link
Copy Markdown
Collaborator Author

@cyfung1031 cyfung1031 Feb 4, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@CodFrm
你看看要不要删掉 3 和 4
现在的代码已经没有 3 和 4
可能是你的旧代码留下来的?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

我也不记得了,应该可以删除

// @description 订阅xxx系列脚本
// @version 0.1.0
// @author You
// @connect www.baidu.com
Copy link
Copy Markdown
Collaborator Author

@cyfung1031 cyfung1031 Feb 4, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@CodFrm

UserSubscribe 里面的 @connect 好像没什么作用吧。还是说你希望这个 UserSubscribe 包含的 @connect 会让 Script脚本的 slient update 时跳过检查?也就是即使脚本本身 @connect 有变化, 只要Subscribe里有这个网域,就不视作变化而进行 slience update?

这样的话 update 时要反查 subscribe 然后做处理
这个暂时在SC未有

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

是的,订阅安装的脚本使用订阅的 @connect,我记得我处理过

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

是的,订阅安装的脚本使用订阅的 @connect,我记得我处理过

@cyfung1031
Copy link
Copy Markdown
Collaborator Author

cyfung1031 commented Feb 5, 2026

@CodFrm 我追查了一下,你升级到MV3时已经写反了

if (!checkDisable && subscribe.status === SUBSCRIBE_STATUS_ENABLE) {

这里应该是 disable 不是 enable

@cyfung1031 cyfung1031 changed the title [v1.3] 更新 Subscribe 相关代码和追加注释 [v1.4] 更新 Subscribe 相关代码和追加注释 Feb 24, 2026
@cyfung1031 cyfung1031 changed the base branch from release/v1.3 to release/v1.4 March 15, 2026 05:00
cyfung1031 and others added 8 commits March 22, 2026 04:42
根据文档设计,订阅脚本应始终静默更新,不受「非重要变更静默更新脚本」
开关控制;订阅下脚本的 connect 权限完全由订阅的 connect 覆盖。

- subscribe.ts: 订阅本身更新去掉 toggle 依赖,始终静默(除非 connect 变化)
- script.ts: 订阅下脚本更新始终静默,不检查 toggle 和 connect
- gm_api.ts: 运行时 GM_xmlhttpRequest/GM_cookie 的 connect 权限使用订阅声明的覆盖
- utils.ts: 简化 checkSilenceUpdate,移除不再需要的 subscribeMetadata 参数
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

本 PR 主要针对 Subscribe(user.sub.js)安装/更新过程中,Subscribe 与脚本“首次安装来源”冲突导致的删除/关联行为不一致问题进行修正,并补充若干注释与类型调整,以让 Subscribe 仅作为“关联/同步来源”,脚本删除行为由首次安装来源决定。

Changes:

  • 将 Subscribe 状态常量重构为 SubscribeStatusType(替代旧的 SUBSCRIBE_STATUS_*),并同步更新引用处与测试
  • 调整 Service Worker 的 Subscribe 脚本同步逻辑:新增“已存在脚本仅建立 UUID 关联”的处理,并修正 enable/disable 判断
  • 补充 Subscribe 相关注释,并将若干对象字典类型改为 Record<>

Reviewed changes

Copilot reviewed 7 out of 7 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
src/pkg/utils/script.ts Subscribe 解析/构建逻辑改用 SubscribeStatusType,并补充 user.sub.js 元数据示例注释
src/pkg/backup/struct.ts 备份结构里 scripts 字典类型改为 Record<string, SubscribeScript>
src/pages/options/routes/SubscribeList.tsx 订阅列表 UI 逻辑改用 SubscribeStatusType 做筛选/开关状态
src/pages/install/App.tsx 安装页对 Subscribe 安装路径补充说明注释
src/app/service/service_worker/subscribe.ts Subscribe 安装/更新/同步脚本逻辑调整,处理“已存在脚本仅关联”与状态判断修正,并补充注释
src/app/service/service_worker/gm_api/gm_api_subscribe.test.ts 测试同步更新为 SubscribeStatusType
src/app/repo/subscribe.ts Subscribe 仓储结构:引入 SubscribeStatusType 与 scripts 字典类型改造,移除 findByUrl

Comment on lines +6 to +11
export const SubscribeStatusType = {
enable: 1, // 启动 checkSubscribeUpdate
disable: 2, // 停用 checkSubscribeUpdate
unknown3: 3, // 3 是什么?
unknown4: 4, // 4 是什么?
} as const;
Copy link

Copilot AI Mar 29, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SubscribeStatusType 里引入 unknown3/unknown4 且注释为“3 是什么?/4 是什么?”会把不确定的语义暴露为公共 API,后续维护/排错会非常困难。建议:如果 3/4 只是历史遗留或保留值,用更明确的命名(如 reserved3/reserved4)并说明来源/用途;如果不会出现则不要纳入类型范围,避免代码误用。

Copilot uses AI. Check for mistakes.
Comment on lines +165 to +176
if (script.subscribeUrl === subscribe.url) {
delete subscribe.scripts[url];
// 删除脚本
await this.scriptService.deleteScript(script.uuid);
removedScriptNames.push(name);
} else {
logger.warn("Subscribe Update: skip deletion", {
scriptUUID: script.uuid,
scriptUrl: url,
scriptName: name,
});
}
Copy link

Copilot AI Mar 29, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

在 upsertScript 的移除逻辑里,当脚本不是由当前 Subscribe 安装(script.subscribeUrl !== subscribe.url)时会跳过删除,但同时也没有从 subscribe.scripts 中移除该 url 关联;这会导致 subscribe.scripts 与 metadata.scripturl 长期不一致,并在后续更新中反复进入 removedScripts 分支/重复告警。建议无论是否删除脚本本体,都先删除 subscribe.scripts[url](若脚本确实由该 Subscribe 安装再额外执行 deleteScript)。另外当 scriptDAO.get(item.uuid) 返回空时也应清理该映射。

Copilot uses AI. Check for mistakes.
CodFrm and others added 2 commits March 29, 2026 15:56
- 修复删除分支未清理 subscribe.scripts 关联导致每次更新重复触发的 bug
- 删除未使用的 unknown3/unknown4 状态值
- 优化 upsertScript 中已安装脚本查找,用 Map 索引替代逐个全表扫描
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
@CodFrm
Copy link
Copy Markdown
Member

CodFrm commented Mar 29, 2026

review 了一下代码,发现几个问题,已经提交修复到分支上了(77517bb4):

1. Bug: 删除分支未清理 subscribe.scripts 关联

upsertScriptremovedScripts 循环中,当 script.subscribeUrl !== subscribe.url 跳过删除脚本时,没有执行 delete subscribe.scripts[url]。同样当 script 为 null 时也没有清理。

后果:这个 URL 已经不在 metadata 的 scripturl 里了,但关联没被清除,每次更新都会重复进入这个分支并打 warn 日志。

修复:把 delete subscribe.scripts[url] 提到 if (script) 之前,无论什么情况都清理关联。

2. 删除 unknown3 / unknown4 状态值

代码中没有使用 3 和 4,定义它们会污染 SubscribeStatusType 类型(变成 1 | 2 | 3 | 4)。已删除。

3. 优化 upsertScript 中已安装脚本查找

原来每个 addedScript 都调用 scriptDAO.find() 做全表扫描匹配 downloadUrl / origin,复杂度 O(N×M)。

改为循环外一次性取出所有脚本,建立 downloadUrl / originScript 的 Map,循环内 O(1) 查找。

@CodFrm CodFrm merged commit da16ddf into scriptscat:release/v1.4 Mar 29, 2026
3 of 4 checks passed
@cyfung1031
Copy link
Copy Markdown
Collaborator Author

cyfung1031 commented Mar 29, 2026

当 script.subscribeUrl !== subscribe.url 跳过删除脚本时,没有执行 delete subscribe.scripts[url]。同样当 script 为 null 时也没有清理。

快两个月前写的,我有点忘了
虽然原本的寫法应该不是不对的,但先这样吧。有问题再算

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.

3 participants