-
Notifications
You must be signed in to change notification settings - Fork 310
URL.createObjectURL 共通化、兼容 FF #929
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
URL.createObjectURL 共通化、兼容 FF #929
Conversation
There was a problem hiding this 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将Blob URL的创建逻辑重构为一个统一的 makeBlobURL 工具函数,以消除代码重复并提供一致的资源管理。
主要变更:
- 在
utils.ts中新增makeBlobURL函数,封装URL.createObjectURL的调用和自动释放逻辑 - 在多个文件中用
makeBlobURL替换直接调用URL.createObjectURL的重复代码 - 统一变量命名,将
zip/files重命名为更具描述性的jszip/zipOutput
Reviewed Changes
Copilot reviewed 11 out of 11 changed files in this pull request and generated 9 comments.
Show a summary per file
| File | Description |
|---|---|
| src/pkg/utils/utils.ts | 新增 makeBlobURL 工具函数,支持 Service Worker 降级处理和自动资源释放 |
| src/pages/options/routes/script/ScriptEditor.tsx | 使用 makeBlobURL 替换手动创建 Blob URL |
| src/pages/components/layout/MainLayout.tsx | 使用 makeBlobURL 替换手动创建 Blob URL |
| src/pages/components/ScriptResource/index.tsx | 使用 makeBlobURL 替换手动创建 Blob URL 和手动 revoke 逻辑 |
| src/pages/components/CloudScriptPlan/index.tsx | 使用 makeBlobURL 并改进变量命名 (files → zipOutput) |
| src/app/service/service_worker/synchronize.ts | 使用 makeBlobURL 的降级回调机制,并改进变量命名 (zip → jszip, files → zipOutput) |
| src/app/service/service_worker/gm_api.ts | 使用 makeBlobURL 的降级回调机制 |
| src/app/service/service_worker/client.ts | 使用 makeBlobURL 替换手动创建 Blob URL |
| src/app/service/offscreen/index.ts | 简化 createObjectURL 处理器逻辑,委托给 makeBlobURL |
| src/app/service/offscreen/client.ts | 更新 createObjectURL 函数签名以匹配统一的参数结构 |
| src/app/service/content/content.ts | 使用 makeBlobURL 替换手动创建 Blob URL 和手动 revoke 逻辑 |
| export const makeBlobURL = <T extends { blob: Blob; persistence: boolean }>( | ||
| params: T, | ||
| fallbackFn?: (params: T) => any | ||
| ): Promise<string> | string => { |
Copilot
AI
Nov 8, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
返回类型 Promise<string> | string 是不确定的联合类型,会给调用方带来额外的类型检查负担。建议统一返回 Promise<string>,在没有 fallbackFn 的情况下返回 Promise.resolve(url),这样可以简化调用方的类型处理并提供一致的异步接口。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
故意的。不用
| export function createObjectURL(msgSender: MessageSend, params: { blob: Blob; persistence: boolean }) { | ||
| return sendMessage(msgSender, "offscreen/createObjectURL", params); |
Copilot
AI
Nov 8, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
函数签名发生了破坏性变更。旧签名是 createObjectURL(msgSender, data, persistence),新签名是 createObjectURL(msgSender, params)。虽然这与 makeBlobURL 保持一致,但这是一个 API 的 breaking change。建议确认是否所有调用点都已更新,或考虑保持向后兼容性。
| export function createObjectURL(msgSender: MessageSend, params: { blob: Blob; persistence: boolean }) { | |
| return sendMessage(msgSender, "offscreen/createObjectURL", params); | |
| /** | |
| * 兼容新旧签名的 createObjectURL | |
| * 旧签名: createObjectURL(msgSender, data, persistence) | |
| * 新签名: createObjectURL(msgSender, { blob, persistence }) | |
| */ | |
| export function createObjectURL( | |
| msgSender: MessageSend, | |
| dataOrParams: { blob: Blob; persistence: boolean } | Blob, | |
| persistence?: boolean | |
| ) { | |
| // 新签名: 第二个参数为对象且有 blob 字段 | |
| if ( | |
| typeof dataOrParams === "object" && | |
| dataOrParams !== null && | |
| "blob" in dataOrParams | |
| ) { | |
| return sendMessage(msgSender, "offscreen/createObjectURL", dataOrParams); | |
| } | |
| // 旧签名: 第二个参数为 Blob,第三个参数为 persistence | |
| return sendMessage(msgSender, "offscreen/createObjectURL", { | |
| blob: dataOrParams as Blob, | |
| persistence: !!persistence, | |
| }); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
内部用API不用理会签名改变问题
| url: makeBlobURL({ | ||
| blob: new Blob([e.getValue()], { type: "text/javascript" }), | ||
| persistence: false, | ||
| }) as string, |
Copilot
AI
Nov 8, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
使用 as string 类型断言来处理 Promise<string> | string 联合类型是不安全的。如果 makeBlobURL 返回 Promise,这里的断言会导致将 Promise 对象传递给 chrome.downloads.download 的 url 参数,从而引发运行时错误。建议使用 await 或修改 makeBlobURL 返回一致的类型。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
不用
| (document.getElementById("import-local") as HTMLInputElement).value = ""; | ||
| const blob = new Blob([aFile], { type: "application/javascript" }); | ||
| const url = URL.createObjectURL(blob); // 生成一个临时的URL | ||
| const url = makeBlobURL({ blob, persistence: false }) as string; // 生成一个临时的URL |
Copilot
AI
Nov 8, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
使用 as string 类型断言来处理 Promise<string> | string 联合类型是不安全的。如果 makeBlobURL 返回 Promise,这里的断言会导致将 Promise 对象传递给后续的 scriptClient.importByUrl(url),从而引发运行时错误。建议使用 await 或修改 makeBlobURL 返回一致的类型。
| const url = makeBlobURL({ blob, persistence: false }) as string; // 生成一个临时的URL | |
| const url = await makeBlobURL({ blob, persistence: false }); // 生成一个临时的URL |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
不用
| setTimeout(() => { | ||
| URL.revokeObjectURL(url); | ||
| }, 60 * 1000); | ||
| const url = makeBlobURL({ blob: base64ToBlob(value.base64), persistence: false }) as string; |
Copilot
AI
Nov 8, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
使用 as string 类型断言来处理 Promise<string> | string 联合类型是不安全的。如果 makeBlobURL 返回 Promise,这里的断言会导致将 Promise 对象传递给 chrome.downloads.download 的 url 参数,从而引发运行时错误。建议使用 await 或修改 makeBlobURL 返回一致的类型。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
不用
| setTimeout(() => { | ||
| URL.revokeObjectURL(url); | ||
| }, 30 * 1000); | ||
| const url = makeBlobURL({ blob: zipOutput, persistence: false }) as string; |
Copilot
AI
Nov 8, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
使用 as string 类型断言来处理 Promise<string> | string 联合类型是不安全的。如果 makeBlobURL 返回 Promise,这里的断言会导致将 Promise 对象传递给 chrome.downloads.download 的 url 参数,从而引发运行时错误。建议使用 await 或修改 makeBlobURL 返回一致的类型。
| const url = makeBlobURL({ blob: zipOutput, persistence: false }) as string; | |
| const url = await makeBlobURL({ blob: zipOutput, persistence: false }); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
不用
| setTimeout(() => { | ||
| URL.revokeObjectURL(url); | ||
| }, 60 * 1000); | ||
| const url = makeBlobURL({ blob: file, persistence: false }) as string; |
Copilot
AI
Nov 8, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
使用 as string 类型断言来处理 Promise<string> | string 联合类型是不安全的。如果 makeBlobURL 返回 Promise,这里的断言会导致将 Promise 对象作为字符串返回,从而引发运行时错误。建议使用 await 或修改 makeBlobURL 返回一致的类型。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
不用
| } | ||
| return url; | ||
| this.windowServer.on("createObjectURL", async (params: { blob: Blob; persistence: boolean }) => { | ||
| return makeBlobURL(params) as string; |
Copilot
AI
Nov 8, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
使用 as string 类型断言来处理 Promise<string> | string 联合类型是不安全的。虽然在 offscreen 环境中 URL.createObjectURL 应该可用,但如果由于某种原因需要 fallback,这里的断言可能会导致返回 Promise 而不是 string。建议使用 await 确保总是返回字符串值。
| return makeBlobURL(params) as string; | |
| return await makeBlobURL(params); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
沒有fallbackFn. 不用
概述 Descriptions
统一使用 makeBlobURL 来处理
URL.createObjectURL变更内容 Changes
截图 Screenshots