-
Notifications
You must be signed in to change notification settings - Fork 289
兼容 FF: GM_setClipboard #928
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
base: release/v1.3
Are you sure you want to change the base?
兼容 FF: GM_setClipboard #928
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 将剪贴板相关功能重构为独立模块,以支持 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) { |
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.
[nitpick] 不必要的 setData 存在性检查。ClipboardEvent.clipboardData.setData 是标准 API,始终存在。建议简化条件判断:
if (!customClipboardData || !e.clipboardData) {
return;
}| if (!customClipboardData || !e?.clipboardData?.setData) { | |
| if (!customClipboardData || !e.clipboardData) { |
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.
怕有萬一
| e.preventDefault(); | ||
| const { type, data } = customClipboardData; | ||
| customClipboardData = undefined; | ||
| e.clipboardData.setData(type || "text/plain", data); |
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.
类型处理错误。参数 type 可能是字符串或对象(GMTypes.GMClipboardInfo = string | { type?: string; mimetype?: string })。当 type 是对象时,应该提取其 type 或 mimetype 属性,而不是直接将对象传递给 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);| 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); |
| @@ -0,0 +1,39 @@ | |||
| let textareaDOM: HTMLTextAreaElement | undefined; | |||
| let customClipboardData: any; | |||
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.
类型定义不够严格。变量 customClipboardData 使用了 any 类型,应该定义明确的类型以提高类型安全。建议定义为:
let customClipboardData: { type: string; data: string } | undefined;| let customClipboardData: any; | |
| let customClipboardData: { type: string; data: string } | undefined; |
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.
确实少用any的好
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.
这个 type 也不是 string...
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.
resolved in 25e60d0
| "mightPrepareSetClipboard shall be only called in either Chrome offscreen or FF background script." | ||
| ); | ||
| } | ||
| textareaDOM = document.createElement("textarea") as HTMLTextAreaElement; |
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.
[nitpick] 类型转换是多余的。document.createElement("textarea") 已经返回 HTMLTextAreaElement 类型,不需要显式类型断言。建议简化为:
textareaDOM = document.createElement("textarea");| textareaDOM = document.createElement("textarea") as HTMLTextAreaElement; | |
| textareaDOM = document.createElement("textarea"); |
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.
保留
|
ScriptCat 未处理 GMClipboardInfo object情况 -> 先改成 draft PR |
fixed in 22a63ab |
概述 Descriptions
暫先保留原有寫法。不改用 browser clipboard API
变更内容 Changes
截图 Screenshots