Skip to content

Conversation

@cyfung1031
Copy link
Collaborator

概述 Descriptions

统一使用 makeBlobURL 来处理 URL.createObjectURL

变更内容 Changes

截图 Screenshots

@cyfung1031 cyfung1031 changed the base branch from main to release/v1.3 November 8, 2025 13:33
@cyfung1031 cyfung1031 changed the title Pr ff create object url x01 URL.createObjectURL 共通化、兼容 FF Nov 8, 2025
@CodFrm CodFrm requested a review from Copilot November 8, 2025 14:02
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将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 并改进变量命名 (fileszipOutput)
src/app/service/service_worker/synchronize.ts 使用 makeBlobURL 的降级回调机制,并改进变量命名 (zipjszip, fileszipOutput)
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 逻辑

Comment on lines 269 to 272
export const makeBlobURL = <T extends { blob: Blob; persistence: boolean }>(
params: T,
fallbackFn?: (params: T) => any
): Promise<string> | string => {
Copy link

Copilot AI Nov 8, 2025

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),这样可以简化调用方的类型处理并提供一致的异步接口。

Copilot uses AI. Check for mistakes.
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

故意的。不用

Comment on lines +36 to +37
export function createObjectURL(msgSender: MessageSend, params: { blob: Blob; persistence: boolean }) {
return sendMessage(msgSender, "offscreen/createObjectURL", params);
Copy link

Copilot AI Nov 8, 2025

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。建议确认是否所有调用点都已更新,或考虑保持向后兼容性。

Suggested 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,
});

Copilot uses AI. Check for mistakes.
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

内部用API不用理会签名改变问题

Comment on lines +308 to +311
url: makeBlobURL({
blob: new Blob([e.getValue()], { type: "text/javascript" }),
persistence: false,
}) as string,
Copy link

Copilot AI Nov 8, 2025

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 返回一致的类型。

Copilot uses AI. Check for mistakes.
Copy link
Collaborator Author

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
Copy link

Copilot AI Nov 8, 2025

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 返回一致的类型。

Suggested change
const url = makeBlobURL({ blob, persistence: false }) as string; // 生成一个临时的URL
const url = await makeBlobURL({ blob, persistence: false }); // 生成一个临时的URL

Copilot uses AI. Check for mistakes.
Copy link
Collaborator Author

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;
Copy link

Copilot AI Nov 8, 2025

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 返回一致的类型。

Copilot uses AI. Check for mistakes.
Copy link
Collaborator Author

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;
Copy link

Copilot AI Nov 8, 2025

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 返回一致的类型。

Suggested change
const url = makeBlobURL({ blob: zipOutput, persistence: false }) as string;
const url = await makeBlobURL({ blob: zipOutput, persistence: false });

Copilot uses AI. Check for mistakes.
Copy link
Collaborator Author

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;
Copy link

Copilot AI Nov 8, 2025

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 返回一致的类型。

Copilot uses AI. Check for mistakes.
Copy link
Collaborator Author

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;
Copy link

Copilot AI Nov 8, 2025

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 确保总是返回字符串值。

Suggested change
return makeBlobURL(params) as string;
return await makeBlobURL(params);

Copilot uses AI. Check for mistakes.
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

沒有fallbackFn. 不用

@CodFrm CodFrm merged commit 54ad4de into scriptscat:release/v1.3 Nov 8, 2025
2 of 3 checks passed
@cyfung1031 cyfung1031 deleted the pr-ff-createObjectURL-x01 branch December 21, 2025 09:38
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.

2 participants