-
Notifications
You must be signed in to change notification settings - Fork 332
✨ 实现异步GM函数 #492
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
✨ 实现异步GM函数 #492
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -194,15 +194,47 @@ export default class GMApi { | |
| } | ||
| } | ||
|
|
||
| @GMContext.API({ depend: ["GM_setValue"] }) | ||
| public GMDotSetValue(key: string, value: any): Promise<void> { | ||
| // Asynchronous wrapper for GM_setValue to support GM.setValue | ||
| return new Promise((resolve) => { | ||
| this.GM_setValue(key, value); | ||
| resolve(); | ||
| }); | ||
| } | ||
|
|
||
| @GMContext.API({ depend: ["GM_setValue"] }) | ||
| public GM_deleteValue(name: string): void { | ||
| this.GM_setValue(name, undefined); | ||
| } | ||
|
|
||
| @GMContext.API({ depend: ["GM_deleteValue"] }) | ||
| public GMDotDeleteValue(name: string): Promise<void> { | ||
| // Asynchronous wrapper for GM_deleteValue to support GM.deleteValue | ||
| return new Promise((resolve) => { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 同上 |
||
| this.GM_deleteValue(name); | ||
| resolve(); | ||
| }); | ||
| } | ||
|
|
||
| @GMContext.API() | ||
| public GM_listValues(): string[] { | ||
| return Object.keys(this.scriptRes.value); | ||
| } | ||
|
|
||
| @GMContext.API({ depend: ["GM_listValues"] }) | ||
| public GMDotListValues(): Promise<string[]> { | ||
| // Asynchronous wrapper for GM_listValues to support GM.listValues | ||
| return new Promise((resolve) => { | ||
| const ret = this.GM_listValues(); | ||
| resolve(ret); | ||
| }); | ||
| } | ||
|
|
||
| @GMContext.API({ depend: ["GM_setValue"] }) | ||
| public GM_setValues(values: { [key: string]: any }) { | ||
| if (values == null) { | ||
| throw new Error("GM_ setValues: values must not be null or undefined"); | ||
| throw new Error("GM_setValues: values must not be null or undefined"); | ||
| } | ||
| if (typeof values !== "object") { | ||
| throw new Error("GM_setValues: values must be an object"); | ||
|
|
@@ -216,12 +248,13 @@ export default class GMApi { | |
| @GMContext.API({ depend: ["GM_getValue"] }) | ||
| public GM_getValues(keysOrDefaults: { [key: string]: any } | string[] | null | undefined) { | ||
| if (keysOrDefaults == null) { | ||
| // returns all | ||
| // Returns all values | ||
| return this.scriptRes.value; | ||
| } | ||
| let result = <{ [key: string]: any }>{}; | ||
| let result: { [key: string]: any } = {}; | ||
| if (Array.isArray(keysOrDefaults)) { | ||
| // 键名数组 | ||
| // Handle array of keys (e.g., ['foo', 'bar']) | ||
| for (let index = 0; index < keysOrDefaults.length; index++) { | ||
| const key = keysOrDefaults[index]; | ||
| if (key in this.scriptRes.value) { | ||
|
|
@@ -230,15 +263,24 @@ export default class GMApi { | |
| } | ||
| } else { | ||
| // 对象 键: 默认值 | ||
| // Handle object with default values (e.g., { foo: 1, bar: 2, baz: 3 }) | ||
| Object.keys(keysOrDefaults).forEach((key) => { | ||
| let defaultValue = keysOrDefaults[key]; | ||
| result[key] = this.GM_getValue(key, defaultValue); | ||
| }); | ||
| } | ||
|
|
||
| return result; | ||
| } | ||
|
|
||
| // Asynchronous wrapper for GM.getValues | ||
| @GMContext.API({ depend: ["GM_getValues"] }) | ||
| public GMDotGetValues(keysOrDefaults: { [key: string]: any } | string[] | null | undefined): Promise<{ [key: string]: any }> { | ||
| return new Promise((resolve) => { | ||
| const ret = this.GM_getValues(keysOrDefaults); | ||
| resolve(ret); | ||
| }); | ||
| } | ||
|
|
||
| @GMContext.API({ depend: ["GM_deleteValue"] }) | ||
| public GM_deleteValues(keys: string[]) { | ||
| if (!Array.isArray(keys)) { | ||
|
|
@@ -250,6 +292,15 @@ export default class GMApi { | |
| }); | ||
| } | ||
|
|
||
| // Asynchronous wrapper for GM.deleteValues | ||
| @GMContext.API({ depend: ["GM_deleteValues"] }) | ||
| public GMDotDeleteValues(keys: string[]): Promise<void> { | ||
| return new Promise((resolve) => { | ||
| this.GM_deleteValues(keys); | ||
| resolve(); | ||
| }); | ||
| } | ||
|
|
||
| eventId: number = 0; | ||
|
|
||
| menuMap: Map<number, string> | undefined; | ||
|
|
@@ -268,11 +319,6 @@ export default class GMApi { | |
| this.valueChangeListener.delete(listenerId); | ||
| } | ||
|
|
||
| @GMContext.API() | ||
| public GM_listValues(): string[] { | ||
| return Object.keys(this.scriptRes.value); | ||
| } | ||
|
|
||
| @GMContext.API() | ||
| GM_log(message: string, level: GMTypes.LoggerLevel = "info", labels?: GMTypes.LoggerLabel) { | ||
| if (typeof message !== "string") { | ||
|
|
@@ -971,6 +1017,15 @@ export default class GMApi { | |
| return undefined; | ||
| } | ||
|
|
||
| @GMContext.API({ depend: ["GM_getResourceText"] }) | ||
| public GMDotGetResourceText(name: string): Promise<string | undefined> { | ||
| // Asynchronous wrapper for GM_getResourceText to support GM.getResourceText | ||
| return new Promise((resolve) => { | ||
| const ret = this.GM_getResourceText(name); | ||
| resolve(ret); | ||
| }); | ||
| } | ||
|
|
||
| @GMContext.API() | ||
| GM_getResourceURL(name: string, isBlobUrl?: boolean): string | undefined { | ||
| if (!this.scriptRes.resource) { | ||
|
|
@@ -988,17 +1043,12 @@ export default class GMApi { | |
|
|
||
| // GM_getResourceURL的异步版本,用来兼容GM.getResourceUrl | ||
| @GMContext.API({ depend: ["GM_getResourceURL"] }) | ||
| async GMDotGetResourceUrl(name: string, isBlobUrl?: boolean): Promise<string | undefined> { | ||
| if (this.scriptRes.resource) { | ||
| const r = this.scriptRes.resource[name]; | ||
| if (r) { | ||
| if (isBlobUrl) { | ||
| return URL.createObjectURL(base64ToBlob(r.base64)); | ||
| } | ||
| return r.base64; | ||
| } | ||
| } | ||
| return undefined; | ||
| public GMDotGetResourceUrl(name: string, isBlobUrl?: boolean): Promise<string | undefined> { | ||
| // Asynchronous wrapper for GM_getResourceURL to support GM.getResourceURL | ||
| return new Promise((resolve) => { | ||
| const ret = this.GM_getResourceURL(name); | ||
| resolve(ret); | ||
| }); | ||
| } | ||
|
|
||
| @GMContext.API() | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
GM_setValue的实现返回值是一个Promise,这里应该可以直接返回,或者不用专门的实现GMDotSetValue,部分函数同理例如(GM_deleteValue,也可以直接做一个返回)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
思考了一下,毕竟
GM_setValue是专门作为同步函数存在的,这样处理一下避免以后又进行了改动,也挺好的There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
倒回來 GM_setValue 才是寫錯吧。 按照TM,GM_setValue 不傳回任何東西
還有 GM_setValue的值undefined 不等如刪除 才對
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
是如此,可以修改掉