Conversation
| public scriptRes: ScriptRunResource | ||
| ) { | ||
| // testing only | ||
| // testing only 仅供测试用 |
There was a problem hiding this comment.
實際執行沒有 new GMApi 這回事
| public ['GM_getValue'](key: string, defaultValue?: any) { | ||
| return GM_getValue(this, key, defaultValue); | ||
| public GM_getValue(key: string, defaultValue?: any) { | ||
| return _GM_getValue(this, key, defaultValue); |
There was a problem hiding this comment.
不导出,为什么不直接 GMApi._GM_getValue 呢?
There was a problem hiding this comment.
打包會保持 xxx._GM_getValue 的格式 (打包不改Class的靜態方法名字)
導出後,打包會知道這個是不變,會直接變 kkk
這是AI也認同的最好做法
|
|
||
| export interface ApiParam { | ||
| // 默认提供的函数 | ||
| // 默认提供的函数(未使用?) |
| async verify(request: Request, api: ApiValue): Promise<boolean> { | ||
| if (api.param.default) { | ||
| const { alias, link, confirm } = api.param; | ||
| if (api.param.default) { // (未使用?) |
There was a problem hiding this comment.
現在有保留 api.param.default
只是因為名字問題,不能直接用 { } 解出來
There was a problem hiding this comment.
Pull Request Overview
This PR primarily optimizes the GM API system and performs general code modernization. The main changes include replacing indexOf with includes for better string checking, improving GM API permission verification architecture, and adding support for GM.* alias patterns. Additionally, it introduces a more flexible context creation system using stub callables and updates various string comparison operations throughout the codebase.
- Replace
indexOf !== -1patterns with modernincludes()method for better readability - Refactor GM API permission verification system with improved type safety and alias handling
- Implement stub callable pattern for dynamic API method injection in context creation
Reviewed Changes
Copilot reviewed 22 out of 22 changed files in this pull request and generated 7 comments.
Show a summary per file
| File | Description |
|---|---|
| src/pkg/utils/*.ts | Replace indexOf with includes for string checks |
| src/pages/options/routes/*.tsx | Update filter logic to use includes instead of indexOf |
| src/app/service/service_worker/permission_verify.ts | Major refactor of API permission system with improved typing |
| src/app/service/service_worker/gm_api.ts | Update API decorators and link parameter types |
| src/app/service/content/gm_api.ts | Extensive refactoring with static method extraction and alias support |
| src/app/service/content/create_context.ts | Implement stub callable pattern for method injection |
Comments suppressed due to low confidence (1)
src/app/service/service_worker/permission_verify.ts:49
- [nitpick] The property name 'dotAlias' is ambiguous. Consider renaming it to 'enableDotAliasCompatibility' or 'gmDotCompatibility' to better describe its purpose of enabling GM.* compatibility for GM_* functions.
dotAlias?: boolean
| } | ||
| } | ||
|
|
||
| // 從 GM_Base 對象中解構出 createGMBase 函数並導出(可供其他模塊使用) |
There was a problem hiding this comment.
The comment contains mixed language usage (Traditional Chinese mixed with English). Consider using consistent English comments: '// Extract createGMBase function from GM_Base object and export it for use by other modules'
| // 從 GM_Base 對象中解構出 createGMBase 函数並導出(可供其他模塊使用) | |
| // Extract the createGMBase function from the GM_Base object and export it for use by other modules. |
| // 從 GM_Base 對象中解構出 createGMBase 函数並導出(可供其他模塊使用) | ||
| export const { createGMBase } = GM_Base; | ||
|
|
||
| // 從 GMApi 對象中解構出內部函數,用於後續本地使用,不導出 |
There was a problem hiding this comment.
The comment contains mixed language usage (Traditional Chinese mixed with English). Consider using consistent English comments: '// Extract internal functions from GMApi object for local use, not exported'
| // 從 GMApi 對象中解構出內部函數,用於後續本地使用,不導出 | |
| // Extract internal functions from the GMApi object for subsequent local use, not exported |
| // 若考虑完全禁止外部查阅 API 的实作,应考虑 defaultFnMap.get(this) | ||
| // 现在没有这个理由,则使用性能较高的 .defaultFn |
There was a problem hiding this comment.
The comment is in Chinese. Consider using English for consistency: '// If considering completely preventing external access to API implementation, should consider defaultFnMap.get(this)'
| // 若考虑完全禁止外部查阅 API 的实作,应考虑 defaultFnMap.get(this) | |
| // 现在没有这个理由,则使用性能较高的 .defaultFn | |
| // If considering completely preventing external access to the API implementation, consider using defaultFnMap.get(this) | |
| // Currently, there is no such reason, so the higher-performance .defaultFn is used |
| // 若考虑完全禁止外部查阅 API 的实作,应考虑 defaultFnMap.get(this) | ||
| // 现在没有这个理由,则使用性能较高的 .defaultFn |
There was a problem hiding this comment.
The comment is in Chinese. Consider using English for consistency: '// Currently there's no reason for this, so use the higher performance .defaultFn approach'
| // 若考虑完全禁止外部查阅 API 的实作,应考虑 defaultFnMap.get(this) | |
| // 现在没有这个理由,则使用性能较高的 .defaultFn | |
| // If considering completely prohibiting external access to the API implementation, defaultFnMap.get(this) should be used. | |
| // Currently, there is no such reason, so the higher-performance .defaultFn approach is used. |
| const finalPart = fnKeyArray[m]; | ||
| if (g[finalPart]) continue; | ||
| g[finalPart] = t.api.bind(context); | ||
| g.defaultFn = t.api; // 定义占位函数物件的实作行为 |
There was a problem hiding this comment.
The comment is in Chinese. Consider using English for consistency: '// Define the implementation behavior of the placeholder function object'
| g.defaultFn = t.api; // 定义占位函数物件的实作行为 | |
| g.defaultFn = t.api; // Define the implementation behavior of the placeholder function object |
| export const { createGMBase } = GM_Base; | ||
|
|
||
| // 從 GMApi 對象中解構出內部函數,用於後續本地使用,不導出 | ||
| const { _GM_getValue, _GM_cookie, _GM_setValue, _GM_xmlhttpRequest } = GMApi; |
There was a problem hiding this comment.
The destructured constants are assigned but never used in the visible code. If these are intended for future use or external consumption, consider adding a comment explaining their purpose, or remove them if they're unnecessary.
| const { _GM_getValue, _GM_cookie, _GM_setValue, _GM_xmlhttpRequest } = GMApi; |
There was a problem hiding this comment.
AI Copilot is not smart :(
| for (let i = 0; i < m; i++) { | ||
| const part = fnKeyArray[i]; | ||
| g = g[part] || (g[part] = {}); | ||
| g = g[part] || (g[part] = createStubCallable()); // 建立占位函数物件 |
There was a problem hiding this comment.
本身用 .bind 也是一層封裝 (產生一個新function)
現在不用 .bind
而且這個是看實際的鍵。現在只有 GM.Cookie.xxx 和 GM_Cookie.xxx 這個才會觸發這個
一般函數會scope多了一層,但這種封裝很常用。沒效能影響
There was a problem hiding this comment.
事实上都触发了,不过避免bind产生一个新的function好像也可以接受,我对单元测试进行了一些调整
|
呀!我寫錯了 |
| expect(ret.GM_getTab.defaultFn).toEqual("GM_getTab"); | ||
| expect(ret.GM_saveTab.defaultFn).toEqual("GM_saveTab"); | ||
| expect(ret.GM_cookie.defaultFn).toEqual("GM_cookie"); | ||
| expect(ret["GM_cookie.list"].defaultFn).toEqual("GM_cookie.list"); |
|
这样的话,正常调用GM Api都成问题了:d59553f |
|
我想想有沒有簡單點的 |
|
用腳本跑了一下。感覺還行 你看看要不要把GM_cookie的問題在這裡修好 GM.cookie GM_cookie 的測試也成功了 |
|
看起来没问题了,我再检查检查,感谢🙏 |
(直接提交到 main 分支)
看了一下你的做法。按你思路改了幾個地方。
TreeShaking优化
createStubCallable & defaultFn
然後背景腳本那邊也做了一些小改動讓寫法接近一點。
其他地方也改了一點
注释更新
简单修一下,无行为改变
沒有CAT.*
indexOf -> includes
修改時沒注意到有 PR #518
直接開了一個新分支進行修改
取代 #518
Replace #518
Close #518