♻️ Offscreen 与 ServiceWorker 通信统一走 postMessage 通道#1299
Merged
CodFrm merged 4 commits intorelease/v1.4from Mar 21, 2026
Merged
Conversation
Offscreen 转发 GM API 请求到 ServiceWorker 时,原先走 ExtensionMessage (chrome.runtime, JSON 序列化),导致 Blob 等结构化数据丢失。 改为通过 postMessage 通道(结构化克隆)双向传输,所有 GM API 自动受益。
所有 Offscreen→SW 通信统一走 ServiceWorkerClientMessage(postMessage), 不再需要 ExtensionMessage(chrome.runtime)。
扩展 offscreen 页面的 controller 通常为 null,改用 navigator.serviceWorker.ready 获取 registration.active 作为 SW 引用。
Collaborator
|
AI的提议吗?不错 |
Member
Author
也不太算,开发过程中发现的问题 😄 |
Contributor
There was a problem hiding this comment.
Pull request overview
该 PR 旨在将 Offscreen 与 Service Worker 的通信统一切换到 postMessage(结构化克隆)通道,以避免 chrome.runtime 消息的 JSON 序列化导致 Blob/ArrayBuffer 等数据丢失,并保持现有消息系统(Server/Client/forwardMessage)调用方式不变。
Changes:
- Service Worker 侧同时接收
ExtensionMessage(chrome.runtime)与ServiceWorkerMessageSend(postMessage)两种通道的消息,并复用同一个swMessage实例。 - Offscreen 侧移除对
ExtensionMessage的依赖,改用ServiceWorkerClientMessage通过navigator.serviceWorker向 SW 发送消息。 ServiceWorkerMessageSend升级实现完整Message接口,并新增ServiceWorkerClientMessage用于 Offscreen→SW 的 postMessage 发送。
Reviewed changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated 1 comment.
Show a summary per file
| File | Description |
|---|---|
| src/service_worker.ts | 引入并复用 ServiceWorkerMessageSend,Server 同时监听 runtime 与 postMessage 两通道 |
| src/offscreen.ts | Offscreen 端改用 ServiceWorkerClientMessage 向 SW 发送消息(支持结构化克隆) |
| src/app/service/offscreen/script.ts | 构造参数重命名与发送器替换为通用 msgSender |
| src/app/service/offscreen/index.ts | 移除 ServiceWorkerClient 依赖,统一用 sendMessage(msgSender, ...) 与 forwardMessage |
| packages/message/window_message.ts | ServiceWorkerMessageSend 扩展为完整 Message;新增 ServiceWorkerClientMessage |
修复 messageHandle 处理 sendMessage 时未传 sender 导致 SenderRuntime 访问 undefined 属性崩溃的问题,传入空对象表示后台脚本来源。 新增 11 个单元测试覆盖双向 postMessage 通道的核心链路。
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.
Summary
ServiceWorkerMessageSend升级为完整Message接口,支持双向 postMessage 通信ServiceWorkerClientMessage,Offscreen 端通过navigator.serviceWorker.controller.postMessage()发送消息Test plan