Skip to content

Commit fa9dcbc

Browse files
committed
🐛 修复GM_setValue设置object实例时的错误
1 parent 74981dc commit fa9dcbc

File tree

4 files changed

+28
-4
lines changed

4 files changed

+28
-4
lines changed

src/apps/grant/background.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -464,7 +464,7 @@ export class BackgroundGrant {
464464
//错误
465465
return reject('param is null');
466466
}
467-
const config = <GM_Types.XHRDetails>grant.params[0];
467+
const config = <GMSend.XHRDetails>grant.params[0];
468468

469469
const xhr = new XMLHttpRequest();
470470
xhr.open(config.method || 'GET', config.url, true, config.user || '', config.password || '');

src/apps/grant/frontend.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ export class FrontendGrant implements ScriptContext {
143143
}
144144
}
145145
}
146-
let param: GM_Types.XHRDetails = {
146+
let param: GMSend.XHRDetails = {
147147
method: details.method,
148148
timeout: details.timeout,
149149
url: u.href,
@@ -313,6 +313,10 @@ export class FrontendGrant implements ScriptContext {
313313

314314
@FrontendGrant.GMFunction()
315315
public GM_setValue(name: string, value: any): void {
316+
// 对object的value进行一次转化
317+
if (typeof value === "object") {
318+
value = JSON.parse(JSON.stringify(value));
319+
}
316320
let ret = this.script.value![name];
317321
if (ret) {
318322
ret.value = value;

src/types/main.d.ts

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,4 +86,24 @@ interface Userinfo {
8686
id: number;
8787
username: string;
8888
avatar?: string;
89-
}
89+
}
90+
91+
declare namespace GMSend {
92+
interface XHRDetails {
93+
method?: "GET" | "HEAD" | "POST" | "PUT" | "DELETE" | "PATCH" | "OPTIONS"
94+
url: string
95+
headers?: { [key: string]: string }
96+
data?: string | Array
97+
cookie?: string
98+
binary?: boolean
99+
timeout?: number
100+
context?: CONTEXT_TYPE
101+
responseType?: "arraybuffer" | "blob" | "json"
102+
overrideMimeType?: string,
103+
anonymous?: boolean,
104+
fetch?: boolean,
105+
user?: string,
106+
password?: string,
107+
nocache?: boolean
108+
}
109+
}

src/types/tampermonkey.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ declare namespace GM_Types {
193193
method?: "GET" | "HEAD" | "POST" | "PUT" | "DELETE" | "PATCH" | "OPTIONS"
194194
url: string
195195
headers?: { [key: string]: string }
196-
data?: any
196+
data?: string | FormData
197197
cookie?: string
198198
binary?: boolean
199199
timeout?: number

0 commit comments

Comments
 (0)