Skip to content

🐛 修复其他扩展注入 chrome.runtime 导致环境误判的问题 #1280#1281

Open
CodFrm wants to merge 3 commits intomainfrom
fix/isContent-env-detection
Open

🐛 修复其他扩展注入 chrome.runtime 导致环境误判的问题 #1280#1281
CodFrm wants to merge 3 commits intomainfrom
fix/isContent-env-detection

Conversation

@CodFrm
Copy link
Member

@CodFrm CodFrm commented Mar 11, 2026

Summary

  • 移除 isContent 运行时检测(typeof chrome.runtime.sendMessage),改为通过 CustomEventMessage.envTag 由入口脚本显式指定环境
  • 修复其他扩展(如大学搜题酱)向页面注入 chrome.runtime 对象,导致 inject 环境被误判为 content 环境,脚本全部失效的问题
  • 版本升级至 1.3.1

改动说明

  • CustomEventMessage 构造函数改为接收 (eventFlag, isInbound, envTag?) 三个参数,内部拼接 messageFlag
  • content.ts 硬编码 ScriptEnvTag.contentinject.ts 硬编码 ScriptEnvTag.inject
  • gm_api.ts 中的 isContent 改为从 message.envTag 读取

Closes #1280

Test plan

  • 全部 781 个测试用例通过
  • 生产打包成功
  • 安装大学搜题酱扩展,验证脚本猫脚本正常运行

移除 isContent 运行时检测,改为通过 CustomEventMessage.envTag 在构建入口确定环境,
避免其他扩展(如大学搜题酱)向页面注入 chrome.runtime 对象导致 inject 环境被误判为 content 环境。
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 通过让入口脚本显式标注运行环境(content / inject),避免因第三方扩展向页面注入 chrome.runtime 而导致环境误判,从而修复脚本在部分浏览器/扩展组合下全部失效的问题,并同步升级版本号。

Changes:

  • CustomEventMessage 构造函数改为接收 eventFlag + envTag(由内部拼接 messageFlag),不再依赖运行时探测。
  • content.ts / inject.ts 入口硬编码各自的 ScriptEnvTag,移除 isContent 判断。
  • GMApi 中将 isContent 来源改为消息的 envTag;版本号升级至 1.3.1

Reviewed changes

Copilot reviewed 8 out of 8 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
src/scripting.ts 使用新的 CustomEventMessage(eventFlag, ..., envTag) 来分别建立 content/inject 通道
src/inject.ts inject 入口显式指定 ScriptEnvTag.inject 并适配新构造函数
src/content.ts content 入口显式指定 ScriptEnvTag.content 并适配新构造函数
src/app/service/content/gm_api/gm_api.ts CAT_fetchDocument 改为根据 message.envTag 判定来源环境
packages/message/custom_event_message.ts CustomEventMessage 构造函数新增 envTag 参数并内部拼接 messageFlag
packages/message/common.ts 移除 isContent 的运行时检测常量
src/manifest.json 版本号升级至 1.3.1
package.json 版本号升级至 1.3.1

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
@cyfung1031 cyfung1031 marked this pull request as draft March 11, 2026 13:09
@cyfung1031 cyfung1031 self-assigned this Mar 11, 2026
@cyfung1031 cyfung1031 marked this pull request as ready for review March 11, 2026 13:20
@cyfung1031
Copy link
Collaborator

个人认为不需要改 CustomEventMessage
不过你喜欢吧

@cyfung1031 cyfung1031 removed their assignment Mar 11, 2026
Copy link
Member Author

@CodFrm CodFrm left a comment

Choose a reason for hiding this comment

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

个人认为不需要改 CustomEventMessage

我也想了用setIsContent这个办法,纠结了一下还是改动 CustomEventMessage 了,这样避免全局变量,而是依赖注入的形式,也方便单测

Comment on lines +11 to +16
if (typeof chrome === "object" && typeof chrome?.runtime?.sendMessage === "function") {
setIsContent();
} else {
console.error("chrome.runtime.sendMessage is not a function in content.js");
}

Copy link
Member Author

Choose a reason for hiding this comment

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

为什么要这样?这个环境应该是会知道是在content环境吧,也只有content环境会使用这个,而且已经没有地方用isContent了

Copy link
Collaborator

Choose a reason for hiding this comment

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

在 common 保留 isContent 來使用。
isContent 只在 content.ts 設置
當然你也可以什麼都不管直接 setIsContent();

Copy link
Member Author

Choose a reason for hiding this comment

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

但是没地方使用了,有需要再说吧,我删除了

Copy link
Collaborator

Choose a reason for hiding this comment

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

ok

@cyfung1031
Copy link
Collaborator

个人认为不需要改 CustomEventMessage

我也想了用setIsContent这个办法,纠结了一下还是改动 CustomEventMessage 了,这样避免全局变量,而是依赖注入的形式,也方便单测

content.js 跟 inject.js 是兩個環境跑。很難在vitest 模擬出來的吧

vitest 可以執行 "setIsContent" 改環境設置
但實際上只有 content.js 會執行

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.

[BUG] 脚本失效

3 participants