[BUG FIX] [v1.3] 修正 GM API 异步声明,正确返回 Promise#1169
[BUG FIX] [v1.3] 修正 GM API 异步声明,正确返回 Promise#1169CodFrm merged 4 commits intoscriptscat:release/v1.3from
Conversation
|
虽然是 bug fix. 但我认为放在 1.3 里就好 之前 #1132 提到的 |
|
|
||
| @PermissionVerify.API({}) | ||
| @PermissionVerify.API({ | ||
| link: ["GM_closeNotification", "GM_updateNotification"], |
There was a problem hiding this comment.
是想着定义 GM_closeNotification、GM_updateNotification 也可以正常使用 GM_notification?
但是 content 没有 depend ?
There was a problem hiding this comment.
忘記了原因
src/app/service/service_worker/gm_api/gm_api.ts 的改動可以退回
| @GMContext.API({ alias: "GM.closeInTab" }) | ||
| public GM_closeInTab(tabid: string) { |
There was a problem hiding this comment.
感觉可以考虑统一 GM. 与 GM_ ,GM_ 必定为同步,GM. 必定为Promise,这里返回的实际是Promise了
不过这样的话,也有不少历史遗留了
这个API竟然没加入文档、定义和example
看见了,是内部依赖的 API,可以考虑修改名字了,这个后续再说
There was a problem hiding this comment.
GM_closeInTab 是SC搞出来的东西。这PR我不改它
|
|
||
| // 用于脚本跨域请求,需要@connect domain指定允许的域名 | ||
| @GMContext.API({ | ||
| depend: ["CAT_fetchBlob", "CAT_createBlobUrl", "CAT_fetchDocument"], |
There was a problem hiding this comment.
保留是为了跟旧SC脚本相容吧
之后要做一个 GM.fetch ( 见 Firemonkey )
There was a problem hiding this comment.
Pull request overview
该 PR 旨在修复部分 GM_* / GM.* 异步 API 的声明与绑定方式,使其能以标准 Promise 形式使用,并补齐 GM_xxx 与 GM.xxx 两种调用风格下的注入/绑定行为。
Changes:
- 调整 content 侧 GM API 的方法声明方式,补充多组
GM.xxxPromise 包装方法与依赖注入关系(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 的行为 |
| 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]; |
There was a problem hiding this comment.
该方法内部抛出的错误信息 "param is failed" 可读性较差,排查问题时难以定位具体哪个参数不合法。建议改成更明确的描述(例如指出 details 不能为空、notificationId 必须是 string 等),并在可能情况下包含参数名/期望类型。
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
修正 GM API 的异步声明,使其正确返回 Promise
问题描述
在之前的实现中,部分 GM_* / GM.* API(尤其是涉及异步操作的接口)在声明时没有正确返回
Promise,导致:.then()来处理异步结果本次变更
核心修复
src/app/service/content/gm_api/gm_api.ts和src/app/service/service_worker/gm_api/gm_api.ts,统一调整异步 GM API 的声明方式,确保它们返回标准的Promise对象。兼容性提升
GM_xxx和GM.xxx两种调用风格测试增强
gm_api.test.ts测试用例@grant声明时 API 的绑定行为bound GM_xxx,确保异步执行后行为一致影响范围
async/await或 Promise 链的现代用户脚本提供更好的支持