Skip to content

[BUG FIX] [v1.3] 修正 GM API 异步声明,正确返回 Promise#1169

Merged
CodFrm merged 4 commits intoscriptscat:release/v1.3from
cyfung1031:pr-GM-api-fix05992
Feb 2, 2026
Merged

[BUG FIX] [v1.3] 修正 GM API 异步声明,正确返回 Promise#1169
CodFrm merged 4 commits intoscriptscat:release/v1.3from
cyfung1031:pr-GM-api-fix05992

Conversation

@cyfung1031
Copy link
Collaborator

@cyfung1031 cyfung1031 commented Jan 31, 2026

修正 GM API 的异步声明,使其正确返回 Promise

问题描述

在之前的实现中,部分 GM_* / GM.* API(尤其是涉及异步操作的接口)在声明时没有正确返回 Promise,导致:

  • 开发者无法使用 .then() 来处理异步结果
  • 异步函数的行为不规范,与现代 JavaScript 异步编程习惯不一致
  • 在某些场景下可能出现函数上下文(this)丢失或绑定错误

本次变更

  1. 核心修复

    • 修改 src/app/service/content/gm_api/gm_api.tssrc/app/service/service_worker/gm_api/gm_api.ts,统一调整异步 GM API 的声明方式,确保它们返回标准的 Promise 对象。
  2. 兼容性提升

    • 同时支持 GM_xxxGM.xxx 两种调用风格
    • 修复函数绑定(bound function)问题,保证上下文正确传递
  3. 测试增强

    • 更新 gm_api.test.ts 测试用例
    • 验证有/无 @grant 声明时 API 的绑定行为
    • 明确检查返回函数是否为 bound GM_xxx,确保异步执行后行为一致

影响范围

  • 只影响脚本执行环境中的 GM API 调用
  • 对使用 async/await 或 Promise 链的现代用户脚本提供更好的支持

@cyfung1031 cyfung1031 changed the title 修正 GM API 异步声明,正确返回 Promise [BUG FIX] [v1.3] 修正 GM API 异步声明,正确返回 Promise Jan 31, 2026
@cyfung1031
Copy link
Collaborator Author

cyfung1031 commented Jan 31, 2026

虽然是 bug fix. 但我认为放在 1.3 里就好

之前 #1132 提到的 @grant sync vs async 的与TM不对齐问题。不会在这里处理
这PR只为了修复 Promise result


@PermissionVerify.API({})
@PermissionVerify.API({
link: ["GM_closeNotification", "GM_updateNotification"],
Copy link
Member

Choose a reason for hiding this comment

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

是想着定义 GM_closeNotification、GM_updateNotification 也可以正常使用 GM_notification?

但是 content 没有 depend ?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

忘記了原因
src/app/service/service_worker/gm_api/gm_api.ts 的改動可以退回

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 1320 to 1321
@GMContext.API({ alias: "GM.closeInTab" })
public GM_closeInTab(tabid: string) {
Copy link
Member

@CodFrm CodFrm Feb 1, 2026

Choose a reason for hiding this comment

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

感觉可以考虑统一 GM. 与 GM_ ,GM_ 必定为同步,GM. 必定为Promise,这里返回的实际是Promise了

不过这样的话,也有不少历史遗留了


这个API竟然没加入文档、定义和example


看见了,是内部依赖的 API,可以考虑修改名字了,这个后续再说

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

GM_closeInTab 是SC搞出来的东西。这PR我不改它


// 用于脚本跨域请求,需要@connect domain指定允许的域名
@GMContext.API({
depend: ["CAT_fetchBlob", "CAT_createBlobUrl", "CAT_fetchDocument"],
Copy link
Member

Choose a reason for hiding this comment

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

CAT_fetchDocument 似乎没有地方使用了

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

保留是为了跟旧SC脚本相容吧
之后要做一个 GM.fetch ( 见 Firemonkey )

Copy link
Member

Choose a reason for hiding this comment

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

也可以,先留着

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_* / GM.* 异步 API 的声明与绑定方式,使其能以标准 Promise 形式使用,并补齐 GM_xxx 与 GM.xxx 两种调用风格下的注入/绑定行为。

Changes:

  • 调整 content 侧 GM API 的方法声明方式,补充多组 GM.xxx Promise 包装方法与依赖注入关系(depend)。
  • 调整 service worker 侧通知相关 API 的权限关联(PermissionVerify.link)。
  • 更新 content 侧 GM API 测试用例,覆盖更多 GM_* / GM.* 绑定与无 grant 时的行为。

Reviewed changes

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

File Description
src/app/service/service_worker/gm_api/gm_api.ts 更新通知相关 API 的 PermissionVerify.link 配置
src/app/service/content/gm_api/gm_api.ts 重构多处 GM.xxx 方法声明与 Promise 包装/依赖注入,增强 GM.* 兼容性
src/app/service/content/gm_api/gm_api.test.ts 扩充测试用例,验证 GM_* 与 GM.* 的绑定行为与无 grant 返回 nil 的行为

Comment on lines 1043 to 1045
async GM_notification(request: GMApiRequest<[GMTypes.NotificationDetails, string | undefined]>, sender: IGetSender) {
const details: GMTypes.NotificationDetails = request.params[0];
const notificationId: string | undefined = request.params[1];
Copy link

Copilot AI Feb 1, 2026

Choose a reason for hiding this comment

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

该方法内部抛出的错误信息 "param is failed" 可读性较差,排查问题时难以定位具体哪个参数不合法。建议改成更明确的描述(例如指出 details 不能为空、notificationId 必须是 string 等),并在可能情况下包含参数名/期望类型。

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 and others added 3 commits February 1, 2026 21:41
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
@CodFrm CodFrm merged commit cc2729d into scriptscat:release/v1.3 Feb 2, 2026
3 checks passed
@CodFrm CodFrm mentioned this pull request Feb 4, 2026
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