-
Notifications
You must be signed in to change notification settings - Fork 312
修复 1.2.5 structuredClone错误 #1192
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
Merged
+161
−18
Merged
Changes from all commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
50e457c
修复 1.2.5 structuredClone错误
cyfung1031 731c03a
Update global.ts
cyfung1031 d054c18
lint
cyfung1031 47facf1
update
cyfung1031 0949f4f
Update gm_api.ts
cyfung1031 d2d1ca4
Update global.ts
cyfung1031 57ca956
Update global.ts
cyfung1031 72b25fb
lint
cyfung1031 b02e561
lint
cyfung1031 61bc124
修改命名方式
CodFrm 91cc533
添加单元测试
CodFrm 07ba143
修改名字
CodFrm 83325cf
兼容TM,失败设置undefined
CodFrm 982a4ed
修复setValues的兼容性问题
CodFrm 9ec3df1
TM一致
CodFrm d7bb5fd
typescript
cyfung1031 41b1fdd
不建议这样做但先这样吧
cyfung1031 d3e9a1e
修改成 sendingValues 避免直接改变参数物件
cyfung1031 fa6cf0c
修复单测
CodFrm 6f8fb9c
release v1.2.6
CodFrm 66a3153
添加单测
CodFrm File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,36 @@ | ||
| // 避免在全局页面环境中,内置处理函数被篡改或重写 | ||
| const unsupportedAPI = () => { | ||
| throw "unsupportedAPI"; | ||
| }; | ||
|
|
||
| export const Native = { | ||
| structuredClone: typeof structuredClone === "function" ? structuredClone : unsupportedAPI, | ||
| jsonStringify: JSON.stringify.bind(JSON), | ||
| jsonParse: JSON.parse.bind(JSON), | ||
| } as const; | ||
|
|
||
| export const customClone = (o: any) => { | ||
| // 非对象类型直接返回(包含 Symbol、undefined、基本类型等) | ||
| // 接受参数:阵列、物件、null | ||
| if (typeof o !== "object") return o; | ||
|
|
||
| try { | ||
| // 优先使用 structuredClone,支持大多数可克隆对象 | ||
| return Native.structuredClone(o); | ||
| } catch { | ||
| // 例如:被 Proxy 包装的对象(如 Vue 等框架处理过的 reactive 对象) | ||
| // structuredClone 可能会失败,忽略错误继续尝试其他方式 | ||
| } | ||
|
|
||
| try { | ||
| // 退而求其次,使用 JSON 序列化方式进行深拷贝 | ||
| // 仅适用于可被 JSON 表示的普通对象 | ||
| return Native.jsonParse(Native.jsonStringify(o)); | ||
| } catch { | ||
| // 序列化失败,忽略错误 | ||
| } | ||
|
|
||
| // 其他无法克隆的非法对象,例如 window、document 等 | ||
| console.error("customClone failed"); | ||
| return undefined; | ||
| }; |
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,4 @@ | ||
| import { customClone, Native } from "../global"; | ||
| import type { Message, MessageConnect } from "@Packages/message/types"; | ||
| import type { CustomEventMessage } from "@Packages/message/custom_event_message"; | ||
| import type { | ||
|
|
@@ -236,7 +237,7 @@ export default class GMApi extends GM_Base { | |
| const ret = a.scriptRes.value[key]; | ||
| if (ret !== undefined) { | ||
| if (ret && typeof ret === "object") { | ||
| return structuredClone(ret); | ||
| return customClone(ret)!; | ||
| } | ||
| return ret; | ||
| } | ||
|
|
@@ -275,10 +276,15 @@ export default class GMApi extends GM_Base { | |
| } else { | ||
| // 对object的value进行一次转化 | ||
| if (value && typeof value === "object") { | ||
| value = structuredClone(value); | ||
| value = customClone(value); | ||
| } | ||
| // customClone 可能返回 undefined | ||
| a.scriptRes.value[key] = value; | ||
| a.sendMessage("GM_setValue", [id, key, value]); | ||
| if (value === undefined) { | ||
| a.sendMessage("GM_setValue", [id, key]); | ||
| } else { | ||
| a.sendMessage("GM_setValue", [id, key, value]); | ||
| } | ||
| } | ||
| return id; | ||
| } | ||
|
|
@@ -295,20 +301,23 @@ export default class GMApi extends GM_Base { | |
| valueChangePromiseMap.set(id, promise); | ||
| } | ||
| const valueStore = a.scriptRes.value; | ||
| const sendingValues = {} as Record<string, any>; | ||
| for (const [key, value] of Object.entries(values)) { | ||
| let value_ = value; | ||
| if (value_ === undefined) { | ||
| if (valueStore[key]) delete valueStore[key]; | ||
| } else { | ||
| // 对object的value进行一次转化 | ||
| if (value_ && typeof value_ === "object") { | ||
| value_ = structuredClone(value_); | ||
| value_ = customClone(value_); | ||
| } | ||
| // customClone 可能返回 undefined | ||
| valueStore[key] = value_; | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 上面有 你要不要统一一下呀? |
||
| } | ||
| sendingValues[key] = value_; | ||
| } | ||
| // 避免undefined 等空值流失,先进行映射处理 | ||
| const valuesNew = encodeMessage(values); | ||
| const valuesNew = encodeMessage(sendingValues); | ||
| a.sendMessage("GM_setValues", [id, valuesNew]); | ||
| return id; | ||
| } | ||
|
|
@@ -369,7 +378,7 @@ export default class GMApi extends GM_Base { | |
| if (!this.scriptRes) return {}; | ||
| if (!keysOrDefaults) { | ||
| // Returns all values | ||
| return structuredClone(this.scriptRes.value); | ||
| return customClone(this.scriptRes.value)!; | ||
| } | ||
| const result: TGMKeyValue = {}; | ||
| if (Array.isArray(keysOrDefaults)) { | ||
|
|
@@ -381,7 +390,7 @@ export default class GMApi extends GM_Base { | |
| // 对object的value进行一次转化 | ||
| let value = this.scriptRes.value[key]; | ||
| if (value && typeof value === "object") { | ||
| value = structuredClone(value); | ||
| value = customClone(value)!; | ||
| } | ||
| result[key] = value; | ||
| } | ||
|
|
@@ -465,7 +474,7 @@ export default class GMApi extends GM_Base { | |
| GM_log(message: string, level: GMTypes.LoggerLevel = "info", ...labels: GMTypes.LoggerLabel[]) { | ||
| if (this.isInvalidContext()) return; | ||
| if (typeof message !== "string") { | ||
| message = JSON.stringify(message); | ||
| message = Native.jsonStringify(message); | ||
| } | ||
| this.sendMessage("GM_log", [message, level, labels]); | ||
| } | ||
|
|
@@ -1258,7 +1267,7 @@ export default class GMApi extends GM_Base { | |
| GM_saveTab(obj: object) { | ||
| if (this.isInvalidContext()) return; | ||
| if (typeof obj === "object") { | ||
| obj = JSON.parse(JSON.stringify(obj)); | ||
| obj = customClone(obj); | ||
| } | ||
| this.sendMessage("GM_saveTab", [obj]); | ||
| } | ||
|
|
||
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
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
Oops, something went wrong.
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.
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.
上面的
a.sendMessage("GM_setValue", [id, key]);会有delete a.scriptRes.value[key];但这个是a.scriptRes.value[key] = undefined;你要不要统一一下呀?
Uh oh!
There was an error while loading. Please reload this page.
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.
故意为之,兼容TM,删除了key,但是这一次通过 GM_getValues 可以获取到 key,但是刷新页面就没有这个key了