Skip to content

Comments

[v1.3] 修复 87c0a19dea9cad2e9ebdce13bb4d998ea386ad74 重构代码时不正确的修改#1217

Merged
CodFrm merged 4 commits intoscriptscat:release/v1.3from
cyfung1031:pr-fix-external-001
Feb 7, 2026
Merged

[v1.3] 修复 87c0a19dea9cad2e9ebdce13bb4d998ea386ad74 重构代码时不正确的修改#1217
CodFrm merged 4 commits intoscriptscat:release/v1.3from
cyfung1031:pr-fix-external-001

Conversation

@cyfung1031
Copy link
Collaborator

@CodFrm 你搞的重构把我写好测试好的东西都忽略掉

Screenshot 2026-02-07 at 18 58 24

87c0a19

@cyfung1031 cyfung1031 added the hotfix 需要尽快更新到扩展商店 label Feb 7, 2026
@cyfung1031 cyfung1031 marked this pull request as draft February 7, 2026 10:07
@cyfung1031 cyfung1031 marked this pull request as ready for review February 7, 2026 10:15
@CodFrm
Copy link
Member

CodFrm commented Feb 7, 2026

哎呀,尴尬了

@CodFrm CodFrm requested a review from Copilot February 7, 2026 11:59
@CodFrm CodFrm merged commit b76f253 into scriptscat:release/v1.3 Feb 7, 2026
8 of 9 checks passed
Copy link
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 主要用于修复此前通讯机制重构过程中,对“页面侧 external 注入接口(Scriptcat/Tampermonkey isInstalled)”相关逻辑的不正确改动,并将注入逻辑独立成模块,便于后续维护。

Changes:

  • window.external 注入与 TM 兼容补丁逻辑从 script_runtime.ts 抽离到新文件 external.ts
  • ScriptRuntime.externalMessage() 改为调用抽离后的 onInjectPageLoaded
  • isInstalled 的消息发送路径调整为走 scripting 路由(scripting/script/isInstalled

Reviewed changes

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

File Description
src/app/service/content/script_runtime.ts 移除内联 external 注入实现,改为委托到 onInjectPageLoaded
src/app/service/content/external.ts 新增 external 注入/白名单判断/TM isInstalled 回退逻辑的独立实现

return false;
};

// inject 环境 pageLoad 后执行:按白名单对页面注入 external 接口
Copy link

Copilot AI Feb 7, 2026

Choose a reason for hiding this comment

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

注释“inject 环境 pageLoad 后执行”与实际调用点不一致:inject.ts 里是在初始化后立即调用 runtime.externalMessage(),并不等待 pageLoad。建议更新这段注释,避免误导后续维护者对执行时机的判断。

Suggested change
// inject 环境 pageLoad 后执行:按白名单对页面注入 external 接口
// inject 环境初始化时由 inject.ts 直接调用:按白名单对页面注入 external 接口(不依赖 pageLoad)

Copilot uses AI. Check for mistakes.
Comment on lines +66 to +86
export const onInjectPageLoaded = (msg: Message) => {
const hostname = window.location.hostname;

// 不在白名单则不对外暴露接口
if (!isExternalWhitelisted(hostname)) return;

// 确保 external 存在
const external: External = (window.external || (window.external = {} as External)) as External;

// 创建 Scriptcat 暴露对象
const scriptExpose = createScriptcatExpose(msg);

// 尝试设置 external.Scriptcat
safeSetExternal(external, "Scriptcat", scriptExpose);

// 如果页面已有 Tampermonkey,则做兼容补丁;否则将 Tampermonkey 也指向 Scriptcat 接口
const patched = patchTampermonkeyIsInstalled(external, scriptExpose);
if (!patched) {
safeSetExternal(external, "Tampermonkey", scriptExpose);
}
};
Copy link

Copilot AI Feb 7, 2026

Choose a reason for hiding this comment

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

onInjectPageLoaded 新增了白名单判断、external 注入、以及 Tampermonkey 兼容补丁等关键行为,但当前没有对应单元测试覆盖(例如:命中/未命中白名单时的注入行为、TM 存在时 isInstalled 回退逻辑、以及写入 external 失败时的容错)。建议补充 vitest 用例,避免后续通讯机制/注入时机调整时再次回归。

Copilot generated this review using guidance from repository custom instructions.
const createScriptcatExpose = (msg: Message) => {
const scriptExpose: App.ExternalScriptCat = {
isInstalled(name: string, namespace: string, callback: (res: App.IsInstalledResponse | undefined) => unknown) {
sendMessage<App.IsInstalledResponse>(msg, "scripting/script/isInstalled", { name, namespace }).then(callback);
Copy link

Copilot AI Feb 7, 2026

Choose a reason for hiding this comment

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

isInstalled 通过 sendMessage(...).then(callback) 触发回调,但当消息通道异常/目标 API 不存在时 sendMessage 会抛错并导致 Promise rejection:页面侧回调永远不会被调用,同时可能产生未处理的拒绝。建议在这里补充 .catch(...),保证失败时也以 callback(undefined)(或明确的失败结果)结束,并避免未处理异常外溢到页面。

Suggested change
sendMessage<App.IsInstalledResponse>(msg, "scripting/script/isInstalled", { name, namespace }).then(callback);
sendMessage<App.IsInstalledResponse>(msg, "scripting/script/isInstalled", { name, namespace })
.then(callback)
.catch(() => callback(undefined));

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

hotfix 需要尽快更新到扩展商店

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants