Skip to content

GM优化 及其他更新#519

Merged
CodFrm merged 33 commits intoscriptscat:mainfrom
cyfung1031:fix_gm4
Jul 11, 2025
Merged

GM优化 及其他更新#519
CodFrm merged 33 commits intoscriptscat:mainfrom
cyfung1031:fix_gm4

Conversation

@cyfung1031
Copy link
Collaborator

@cyfung1031 cyfung1031 commented Jul 10, 2025

(直接提交到 main 分支)

看了一下你的做法。按你思路改了幾個地方。

TreeShaking优化
createStubCallable & defaultFn

然後背景腳本那邊也做了一些小改動讓寫法接近一點。
其他地方也改了一點

注释更新
简单修一下,无行为改变
沒有CAT.*
indexOf -> includes


修改時沒注意到有 PR #518
直接開了一個新分支進行修改

取代 #518
Replace #518
Close #518

public scriptRes: ScriptRunResource
) {
// testing only
// testing only 仅供测试用
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
Collaborator Author

Choose a reason for hiding this comment

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

實際執行沒有 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);
Copy link
Member

Choose a reason for hiding this comment

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

不导出,为什么不直接 GMApi._GM_getValue 呢?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

打包會保持 xxx._GM_getValue 的格式 (打包不改Class的靜態方法名字)
導出後,打包會知道這個是不變,會直接變 kkk
這是AI也認同的最好做法


export interface ApiParam {
// 默认提供的函数
// 默认提供的函数(未使用?)
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
Collaborator Author

Choose a reason for hiding this comment

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

你看看要不要保留吧

async verify(request: Request, api: ApiValue): Promise<boolean> {
if (api.param.default) {
const { alias, link, confirm } = api.param;
if (api.param.default) { // (未使用?)
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
Collaborator Author

Choose a reason for hiding this comment

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

現在有保留 api.param.default
只是因為名字問題,不能直接用 { } 解出來

@CodFrm CodFrm requested a review from Copilot July 11, 2025 02:38
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

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 !== -1 patterns with modern includes() 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 函数並導出(可供其他模塊使用)
Copy link

Copilot AI Jul 11, 2025

Choose a reason for hiding this comment

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

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'

Suggested change
// GM_Base 對象中解構出 createGMBase 函数並導出(可供其他模塊使用)
// Extract the createGMBase function from the GM_Base object and export it for use by other modules.

Copilot uses AI. Check for mistakes.
// 從 GM_Base 對象中解構出 createGMBase 函数並導出(可供其他模塊使用)
export const { createGMBase } = GM_Base;

// 從 GMApi 對象中解構出內部函數,用於後續本地使用,不導出
Copy link

Copilot AI Jul 11, 2025

Choose a reason for hiding this comment

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

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'

Suggested change
// GMApi 對象中解構出內部函數,用於後續本地使用,不導出
// Extract internal functions from the GMApi object for subsequent local use, not exported

Copilot uses AI. Check for mistakes.
Comment on lines 34 to 35
// 若考虑完全禁止外部查阅 API 的实作,应考虑 defaultFnMap.get(this)
// 现在没有这个理由,则使用性能较高的 .defaultFn
Copy link

Copilot AI Jul 11, 2025

Choose a reason for hiding this comment

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

The comment is in Chinese. Consider using English for consistency: '// If considering completely preventing external access to API implementation, should consider defaultFnMap.get(this)'

Suggested change
// 若考虑完全禁止外部查阅 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

Copilot uses AI. Check for mistakes.
Comment on lines 34 to 35
// 若考虑完全禁止外部查阅 API 的实作,应考虑 defaultFnMap.get(this)
// 现在没有这个理由,则使用性能较高的 .defaultFn
Copy link

Copilot AI Jul 11, 2025

Choose a reason for hiding this comment

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

The comment is in Chinese. Consider using English for consistency: '// Currently there's no reason for this, so use the higher performance .defaultFn approach'

Suggested change
// 若考虑完全禁止外部查阅 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.

Copilot uses AI. Check for mistakes.
const finalPart = fnKeyArray[m];
if (g[finalPart]) continue;
g[finalPart] = t.api.bind(context);
g.defaultFn = t.api; // 定义占位函数物件的实作行为
Copy link

Copilot AI Jul 11, 2025

Choose a reason for hiding this comment

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

The comment is in Chinese. Consider using English for consistency: '// Define the implementation behavior of the placeholder function object'

Suggested change
g.defaultFn = t.api; // 定义占位函数物件的实作行为
g.defaultFn = t.api; // Define the implementation behavior of the placeholder function object

Copilot uses AI. Check for mistakes.
export const { createGMBase } = GM_Base;

// 從 GMApi 對象中解構出內部函數,用於後續本地使用,不導出
const { _GM_getValue, _GM_cookie, _GM_setValue, _GM_xmlhttpRequest } = GMApi;
Copy link

Copilot AI Jul 11, 2025

Choose a reason for hiding this comment

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

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.

Suggested change
const { _GM_getValue, _GM_cookie, _GM_setValue, _GM_xmlhttpRequest } = GMApi;

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.

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()); // 建立占位函数物件
Copy link
Member

Choose a reason for hiding this comment

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

为什么要这样做呢?直接返回api更好吧,这里还多了一层封装

Copy link
Collaborator Author

@cyfung1031 cyfung1031 Jul 11, 2025

Choose a reason for hiding this comment

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

本身用 .bind 也是一層封裝 (產生一個新function)
現在不用 .bind

而且這個是看實際的鍵。現在只有 GM.Cookie.xxxGM_Cookie.xxx 這個才會觸發這個

一般函數會scope多了一層,但這種封裝很常用。沒效能影響

Copy link
Collaborator Author

@cyfung1031 cyfung1031 Jul 11, 2025

Choose a reason for hiding this comment

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

這個寫法也比較簡潔好看

Copy link
Member

Choose a reason for hiding this comment

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

事实上都触发了,不过避免bind产生一个新的function好像也可以接受,我对单元测试进行了一些调整

@cyfung1031
Copy link
Collaborator Author

呀!我寫錯了
等一等

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");
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
Member

Choose a reason for hiding this comment

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

这个单元测试建议还是修改回来

@CodFrm
Copy link
Member

CodFrm commented Jul 11, 2025

这样的话,正常调用GM Api都成问题了:d59553f

@cyfung1031
Copy link
Collaborator Author

我想想有沒有簡單點的

@cyfung1031
Copy link
Collaborator Author

cyfung1031 commented Jul 11, 2025

用腳本跑了一下。感覺還行
#520

你看看要不要把GM_cookie的問題在這裡修好


GM.cookie GM_cookie 的測試也成功了

@CodFrm
Copy link
Member

CodFrm commented Jul 11, 2025

看起来没问题了,我再检查检查,感谢🙏

@CodFrm CodFrm merged commit 3d3be2c into scriptscat:main Jul 11, 2025
2 checks passed
@cyfung1031 cyfung1031 deleted the fix_gm4 branch July 11, 2025 08:41
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