Skip to content

Commit 1ae5096

Browse files
committed
⚡️ 删除GM_getCookieStore使用GM_Cookie('store')操作
1 parent fa9dcbc commit 1ae5096

8 files changed

Lines changed: 136 additions & 132 deletions

File tree

src/apps/grant/background.ts

Lines changed: 42 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ export class BackgroundGrant {
6868
let isScriptcat = false;
6969
const requestHeaders: chrome.webRequest.HttpHeader[] = [];
7070
const unsafeHeader: { [key: string]: string } = {};
71-
data.requestHeaders?.forEach((val, key) => {
71+
data.requestHeaders?.forEach((val) => {
7272
const lowerCase = val.name.toLowerCase();
7373
switch (lowerCase) {
7474
case 'x-cat-' + this.rand + '-cookie': {
@@ -387,7 +387,7 @@ export class BackgroundGrant {
387387

388388
}
389389

390-
protected dealXhr(config: GM_Types.XHRDetails, xhr: XMLHttpRequest): GM_Types.XHRResponse {
390+
protected dealXhr(config: GMSend.XHRDetails, xhr: XMLHttpRequest): GM_Types.XHRResponse {
391391
const removeXCat = new RegExp('x-cat-' + this.rand + '-', 'g');
392392
const respond: GM_Types.XHRResponse = {
393393
finalUrl: xhr.responseURL || config.url,
@@ -405,7 +405,7 @@ export class BackgroundGrant {
405405
respond.response = URL.createObjectURL(xhr.response);
406406
}
407407
setTimeout(() => {
408-
URL.revokeObjectURL(respond.response);
408+
URL.revokeObjectURL(<string>respond.response);
409409
}, 60e3)
410410
} else if (config.responseType == 'json') {
411411
try {
@@ -472,26 +472,25 @@ export class BackgroundGrant {
472472
if (config.responseType != 'json') {
473473
xhr.responseType = config.responseType || '';
474474
}
475-
const _this = this;
476475

477-
function deal(event: string) {
478-
const respond = _this.dealXhr(config, xhr);
476+
const deal = (event: string) => {
477+
const respond = this.dealXhr(config, xhr);
479478
grant.data = { type: event, data: respond };
480479
post.postMessage(grant);
481480
}
482-
xhr.onload = (event) => {
481+
xhr.onload = () => {
483482
deal('load');
484483
}
485-
xhr.onloadstart = (event) => {
484+
xhr.onloadstart = () => {
486485
deal('onloadstart');
487486
}
488-
xhr.onloadend = (event) => {
487+
xhr.onloadend = () => {
489488
deal('onloadstart');
490489
}
491-
xhr.onabort = (event) => {
490+
xhr.onabort = () => {
492491
deal('onabort');
493492
}
494-
xhr.onerror = (event) => {
493+
xhr.onerror = () => {
495494
deal('onerror');
496495
}
497496
xhr.onprogress = (event) => {
@@ -505,7 +504,7 @@ export class BackgroundGrant {
505504
grant.data = { type: 'onprogress', data: respond };
506505
post.postMessage(grant);
507506
}
508-
xhr.onreadystatechange = (event) => {
507+
xhr.onreadystatechange = () => {
509508
deal('onreadystatechange');
510509
}
511510
xhr.ontimeout = () => {
@@ -545,61 +544,41 @@ export class BackgroundGrant {
545544
if (config.overrideMimeType) {
546545
xhr.overrideMimeType(config.overrideMimeType);
547546
}
548-
if ((<any>config).dataType && (<any>config).dataType == 'FormData') {
547+
if (config.dataType == 'FormData') {
549548
const data = new FormData();
550-
for (const key in config.data) {
551-
const val = config.data[key];
552-
if (val.type == 'file') {
553-
data.append(val.key, base64ToBlob(val.val), val.filename);
554-
} else {
555-
data.append(val.key, val.val);
556-
}
549+
if (config.data && config.data instanceof Array) {
550+
config.data.forEach((val: GMSend.XHRFormData) => {
551+
if (val.type == 'file') {
552+
data.append(val.key, base64ToBlob(val.val), val.filename);
553+
} else {
554+
data.append(val.key, val.val);
555+
}
556+
});
557+
xhr.send(data);
557558
}
558-
xhr.send(data);
559559
} else {
560-
xhr.send(config.data);
560+
xhr.send(<string>config.data);
561561
}
562562
return resolve(undefined);
563563
});
564564
}
565565

566-
@BackgroundGrant.GMFunction({
567-
background: true
568-
})
569-
protected GM_getCookieStore(grant: Grant, post: IPostMessage): Promise<any> {
570-
return new Promise((resolve, reject) => {
571-
const tabid = grant.params[0];
572-
if (!tabid) {
573-
return reject('tabis is null');
574-
}
575-
chrome.cookies.getAllCookieStores(s => {
576-
for (let i = 0; i < s.length; i++) {
577-
for (let n = 0; n < s[i].tabIds.length; n++) {
578-
if (s[i].tabIds[n] == tabid) {
579-
grant.data = { type: 'done', data: s[i].id };
580-
post.postMessage(grant);
581-
return resolve(undefined);
582-
}
583-
}
584-
}
585-
reject('not found');
586-
});
587-
});
588-
}
589-
590566
@BackgroundGrant.GMFunction({
591567
confirm: (grant: Grant, script: Script) => {
592568
return new Promise((resolve, reject) => {
569+
if (grant.params[0] == 'store') {
570+
resolve(true);
571+
}
593572
const detail = <GM_Types.CookieDetails>grant.params[1];
594573
if ((!detail.url && !detail.domain)) {
595574
return reject('there must be one of url or domain');
596575
}
597-
let url: any = {};
576+
let url: URL = <URL>{};
598577
if (detail.url) {
599578
url = new URL(detail.url);
600579
} else {
601-
url.host = detail.domain;
602-
url.hostname = detail.domain;
580+
url.host = detail.domain || '';
581+
url.hostname = detail.domain || '';
603582
}
604583
let flag = false;
605584
if (script.metadata['connect']) {
@@ -635,19 +614,28 @@ export class BackgroundGrant {
635614
if (param.length != 2) {
636615
return reject('there must be two parameters');
637616
}
617+
const detail = <GM_Types.CookieDetails>grant.params[1];
638618
if (param[0] == 'store') {
639619
chrome.cookies.getAllCookieStores(res => {
640620
const data: any[] = [];
641621
res.forEach(val => {
642-
data.push({ storeId: val.id });
622+
if (detail.tabId) {
623+
for (let n = 0; n < val.tabIds.length; n++) {
624+
if (val.tabIds[n] == detail.tabId) {
625+
data.push({ storeId: val.id });
626+
break;
627+
}
628+
}
629+
} else {
630+
data.push({ storeId: val.id });
631+
}
643632
});
644633
grant.data = { type: 'done', data: data };
645634
post.postMessage(grant);
646635
});
647636
return;
648637
}
649-
const detail = <GM_Types.CookieDetails>grant.params[1];
650-
// url或者域名不能为空,且必须有name
638+
// url或者域名不能为空
651639
if (detail.url) {
652640
detail.url = detail.url.trim();
653641
}
@@ -674,7 +662,7 @@ export class BackgroundGrant {
674662
break;
675663
}
676664
case 'delete': {
677-
if (!detail.url) {
665+
if (!detail.url || !detail.name) {
678666
return reject('delete operation must have url and name');
679667
}
680668
chrome.cookies.remove({
@@ -688,7 +676,7 @@ export class BackgroundGrant {
688676
break;
689677
}
690678
case 'set': {
691-
if (!detail.url) {
679+
if (!detail.url || !detail.name) {
692680
return reject('set operation must have url or domain, and the name must exist');
693681
}
694682
chrome.cookies.set({

src/apps/grant/frontend.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ export class FrontendGrant implements ScriptContext {
205205

206206
this.postRequest('GM_xmlhttpRequest', [param], (grant: Grant) => {
207207
if (grant.error) {
208-
details.onerror && details.onerror(grant.errorMsg);
208+
details.onerror && details.onerror(grant.errorMsg || '');
209209
return;
210210
}
211211
switch (grant.data.type) {
@@ -228,7 +228,7 @@ export class FrontendGrant implements ScriptContext {
228228
details.ontimeout && details.ontimeout();
229229
break;
230230
case 'onerror':
231-
details.onerror && details.onerror();
231+
details.onerror && details.onerror('');
232232
break;
233233
case 'onabort':
234234
details.onabort && details.onabort();
@@ -298,7 +298,7 @@ export class FrontendGrant implements ScriptContext {
298298
}
299299

300300
@FrontendGrant.GMFunction()
301-
public GM_log(message: string, level?: GM_Types.LOGGER_LEVEL): void {
301+
public GM_log(message: string, level?: GM_Types.LoggerLevel): void {
302302
this.postRequest('GM_log', [message, level]);
303303
}
304304

src/apps/script/controller.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -510,7 +510,7 @@ export class ScriptController {
510510
ret.grantMap = {};
511511

512512
ret.metadata['grant']?.forEach((val: string) => {
513-
ret.grantMap![val] = 'ok';
513+
ret.grantMap[val] = 'ok';
514514
});
515515

516516
resolve(ret);

src/model/do/script.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ export interface Config {
3939
export type UserConfig = { [key: string]: { [key: string]: Config } };
4040

4141
export interface ScriptCache extends Script {
42-
grantMap?: { [key: string]: string }
42+
grantMap: { [key: string]: string }
4343
value: { [key: string]: Value }
4444
flag?: string
4545
resource: { [key: string]: Resource }

src/types/main.d.ts

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
declare let sandbox: any;
22

3-
declare module "@App/tampermonkey.d.ts";
4-
declare module "*.tpl";
3+
declare module '@App/tampermonkey.d.ts';
4+
declare module '*.tpl';
55

66
interface ITabItem {
77
tabKey: string | number;
@@ -15,7 +15,7 @@ interface ITabItem {
1515
message?: string;
1616
beforeChange?: (tabPane: TabPane) => Promise<boolean>;
1717
beforeRemove?: (tabPane: TabPane) => Promise<boolean>;
18-
template?: "normal" | "crontab" | "background";
18+
template?: 'normal' | 'crontab' | 'background';
1919
}
2020

2121
interface IChangeTitle {
@@ -32,13 +32,13 @@ interface IEditScript {
3232

3333

3434
interface ICreateScript {
35-
35+
//
3636
}
3737

3838
interface INewScript {
3939
scriptId: number;
4040
tabKey: string | number;
41-
template?: "normal" | "crontab" | "background";
41+
template?: 'normal' | 'crontab' | 'background';
4242
}
4343

4444
interface IUpdateMeta {
@@ -62,25 +62,25 @@ interface ICodeChange {
6262

6363
declare const ScriptFlag;
6464

65-
declare module chrome {
66-
declare module clipboard {
65+
declare namespace chrome {
66+
declare namespace clipboard {
6767
declare function setImageData(
6868
imageData: ArrayBuffer,
6969
type: ImageType,
7070
additionalItems: AdditionalDataItem[],
7171
callback: function,
7272
);
7373

74-
type DataItemType = "textPlain" | "textHtml";
75-
type ImageType = "png" | "jpeg";
74+
type DataItemType = 'textPlain' | 'textHtml';
75+
type ImageType = 'png' | 'jpeg';
7676
declare interface AdditionalDataItem {
7777
data: string;
7878
type: DataItemType;
7979
}
8080
}
8181
}
8282

83-
declare var top: Window;
83+
declare const top: Window;
8484

8585
interface Userinfo {
8686
id: number;
@@ -90,20 +90,29 @@ interface Userinfo {
9090

9191
declare namespace GMSend {
9292
interface XHRDetails {
93-
method?: "GET" | "HEAD" | "POST" | "PUT" | "DELETE" | "PATCH" | "OPTIONS"
93+
method?: 'GET' | 'HEAD' | 'POST' | 'PUT' | 'DELETE' | 'PATCH' | 'OPTIONS'
9494
url: string
9595
headers?: { [key: string]: string }
96-
data?: string | Array
96+
data?: string | Array<XHRFormData>
9797
cookie?: string
9898
binary?: boolean
9999
timeout?: number
100100
context?: CONTEXT_TYPE
101-
responseType?: "arraybuffer" | "blob" | "json"
101+
responseType?: 'arraybuffer' | 'blob' | 'json'
102102
overrideMimeType?: string,
103103
anonymous?: boolean,
104104
fetch?: boolean,
105105
user?: string,
106106
password?: string,
107107
nocache?: boolean
108+
dataType?: 'FormData'
109+
}
110+
111+
interface XHRFormData {
112+
type?: 'file'
113+
key: string
114+
val: string
115+
filename?: string
108116
}
109117
}
118+

0 commit comments

Comments
 (0)