Skip to content

Conversation

@cyfung1031
Copy link
Collaborator

@cyfung1031 cyfung1031 commented Nov 8, 2025

概述 Descriptions

暫先保留原有寫法。不改用 browser clipboard API

变更内容 Changes

截图 Screenshots

@CodFrm CodFrm changed the base branch from main to release/v1.3 November 8, 2025 14:22
@CodFrm CodFrm requested a review from Copilot November 8, 2025 15:20
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 将剪贴板相关功能重构为独立模块,以支持 Firefox background script 环境,并修正了一些代码注释中的繁体字错误。

  • 提取 clipboard.ts 模块统一处理 Chrome offscreen 和 Firefox background script 的剪贴板操作
  • 重构 GM_setClipboard 以支持 Firefox background script 直接调用
  • 改进 compileScriptCodeByResource 中 require 内容的连接方式,使用分号分隔以提高健壮性

Reviewed Changes

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

File Description
src/app/service/service_worker/clipboard.ts 新增剪贴板功能模块,提供统一的 DOM 操作和事件处理
src/app/service/service_worker/gm_api.ts 集成剪贴板模块,为 Firefox background script 添加直接调用支持
src/app/service/offscreen/gm_api.ts 重构以使用共享的剪贴板模块,移除重复代码
src/app/service/content/utils.ts 优化 require 代码连接方式并修正注释中的繁体字

textareaDOM.style.display = "none";
document.documentElement.appendChild(textareaDOM);
document.addEventListener("copy", (e: ClipboardEvent) => {
if (!customClipboardData || !e?.clipboardData?.setData) {
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.

[nitpick] 不必要的 setData 存在性检查。ClipboardEvent.clipboardData.setData 是标准 API,始终存在。建议简化条件判断:

if (!customClipboardData || !e.clipboardData) {
  return;
}
Suggested change
if (!customClipboardData || !e?.clipboardData?.setData) {
if (!customClipboardData || !e.clipboardData) {

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.

怕有萬一

e.preventDefault();
const { type, data } = customClipboardData;
customClipboardData = undefined;
e.clipboardData.setData(type || "text/plain", data);
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.

类型处理错误。参数 type 可能是字符串或对象(GMTypes.GMClipboardInfo = string | { type?: string; mimetype?: string })。当 type 是对象时,应该提取其 typemimetype 属性,而不是直接将对象传递给 setData。建议修改为:

const { type, data } = customClipboardData;
customClipboardData = undefined;
let mimeType = "text/plain";
if (typeof type === "string") {
  mimeType = type;
} else if (typeof type === "object" && type) {
  mimeType = type.type || type.mimetype || "text/plain";
}
e.clipboardData.setData(mimeType, data);
Suggested change
e.clipboardData.setData(type || "text/plain", data);
let mimeType = "text/plain";
if (typeof type === "string") {
mimeType = type;
} else if (typeof type === "object" && type) {
mimeType = type.type || type.mimetype || "text/plain";
}
e.clipboardData.setData(mimeType, data);

Copilot uses AI. Check for mistakes.
@@ -0,0 +1,39 @@
let textareaDOM: HTMLTextAreaElement | undefined;
let customClipboardData: any;
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.

类型定义不够严格。变量 customClipboardData 使用了 any 类型,应该定义明确的类型以提高类型安全。建议定义为:

let customClipboardData: { type: string; data: string } | undefined;
Suggested change
let customClipboardData: any;
let customClipboardData: { type: string; data: string } | undefined;

Copilot uses AI. Check for mistakes.
Copy link
Member

Choose a reason for hiding this comment

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

确实少用any的好

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

这个 type 也不是 string...

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

resolved in 25e60d0

"mightPrepareSetClipboard shall be only called in either Chrome offscreen or FF background script."
);
}
textareaDOM = document.createElement("textarea") as HTMLTextAreaElement;
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.

[nitpick] 类型转换是多余的。document.createElement("textarea") 已经返回 HTMLTextAreaElement 类型,不需要显式类型断言。建议简化为:

textareaDOM = document.createElement("textarea");
Suggested change
textareaDOM = document.createElement("textarea") as HTMLTextAreaElement;
textareaDOM = document.createElement("textarea");

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.

保留

@cyfung1031 cyfung1031 marked this pull request as draft November 8, 2025 15:31
@cyfung1031
Copy link
Collaborator Author

ScriptCat 未处理 GMClipboardInfo object情况 -> 先改成 draft PR

@cyfung1031 cyfung1031 marked this pull request as ready for review November 8, 2025 16:00
@cyfung1031
Copy link
Collaborator Author

ScriptCat 未处理 GMClipboardInfo object情况 -> 先改成 draft PR

fixed in 22a63ab

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