Skip to content

Conversation

@CodFrm
Copy link
Member

@CodFrm CodFrm commented Jan 16, 2026

概述 Descriptions

close #1141

变更内容 Changes

截图 Screenshots

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 修复了 GM_getValue、GM_setValue 和 GM_getValues API 中的对象引用问题(#1141)。当存储和检索对象类型的值时,原实现会导致返回的对象与内部存储共享引用,使得对返回对象的修改会意外影响存储的数据。

Changes:

  • GM_setValue 中的 JSON.parse(JSON.stringify(value)) 替换为 structuredClone(value) 进行深拷贝
  • GM_getValue 中添加了对象类型检查,返回前使用 structuredClone 创建副本
  • GM_getValues 的数组参数分支中添加了对象深拷贝逻辑
  • 添加了完整的测试用例验证引用隔离行为

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
src/app/service/content/gm_api/gm_api.ts 在 GM_getValue、GM_setValue 和 GM_getValues 中实现对象深拷贝,防止引用泄漏
src/app/service/content/gm_api/gm_api.test.ts 添加测试用例验证对象值的引用隔离,确保修改返回值不影响存储的数据
Comments suppressed due to low confidence (1)

src/app/service/content/gm_api/gm_api.ts:373

  • keysOrDefaults 为 null 或 undefined 时,直接返回 this.scriptRes.value 会暴露内部存储的引用。这与本 PR 修复的引用问题相同。应该对返回的对象进行深拷贝,或者遍历所有键并逐个克隆对象类型的值。
    if (!keysOrDefaults) {
      // Returns all values
      return this.scriptRes.value;
    }

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.

[BUG] GM_getValue 返回的是可变对象引用,而不是拷贝 / GM_getValue returns mutable object references instead of copies

3 participants