Skip to content

Commit 71a039f

Browse files
committed
✨ GM_xmlhttpRequest支持blob数据 #29
1 parent 757fe2c commit 71a039f

5 files changed

Lines changed: 94 additions & 56 deletions

File tree

src/apps/grant/background.ts

Lines changed: 73 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -422,19 +422,22 @@ export class BackgroundGrant {
422422
if (api == undefined) {
423423
return resolve(undefined);
424424
}
425-
api.apply(this, [grant, postMessage]).then((result: any) => {
426-
if (grant.value == 'CAT_runComplete' || (grant.value == 'CAT_setRunError' && grant.params[0])) {
427-
//执行完毕,释放资源
428-
BackgroundGrant.freedCallback.forEach(v => {
429-
v(grant.id, grant.tabId);
430-
});
431-
}
432-
resolve(result);
433-
}).catch((e: string) => {
434-
grant.error = 'GM_ERROR';
435-
grant.errorMsg = e;
436-
resolve(grant);
437-
});
425+
const ret = api.apply(this, [grant, postMessage]);
426+
if (ret instanceof Promise) {
427+
ret.then((result: any) => {
428+
if (grant.value == 'CAT_runComplete' || (grant.value == 'CAT_setRunError' && grant.params[0])) {
429+
//后台脚本执行完毕,释放资源
430+
BackgroundGrant.freedCallback.forEach(v => {
431+
v(grant.id, grant.tabId);
432+
});
433+
}
434+
resolve(result);
435+
}).catch((e: string) => {
436+
grant.error = 'GM_ERROR';
437+
grant.errorMsg = e;
438+
resolve(grant);
439+
});
440+
}
438441
});
439442
});
440443

@@ -592,6 +595,15 @@ export class BackgroundGrant {
592595
});
593596
xhr.send(data);
594597
}
598+
} else if (config.dataType == 'Blob') {
599+
const handler = async () => {
600+
if (!config.data) {
601+
return reject('data is null');
602+
}
603+
const resp = await (await fetch(<string>config.data)).blob();
604+
xhr.send(resp);
605+
}
606+
void handler();
595607
} else {
596608
xhr.send(<string>config.data);
597609
}
@@ -741,7 +753,7 @@ export class BackgroundGrant {
741753
@BackgroundGrant.GMFunction({
742754
listener: () => {
743755
chrome.tabs.onRemoved.addListener(tabId => {
744-
const tab = BackgroundGrant.tabMap.get(tabId);
756+
const tab = <[Grant, IPostMessage]>BackgroundGrant.tabMap.get(tabId);
745757
if (tab) {
746758
tab[0].data = { type: 'close' }
747759
tab[1].postMessage(tab[0]);
@@ -775,36 +787,45 @@ export class BackgroundGrant {
775787

776788
@BackgroundGrant.GMFunction({
777789
listener: () => {
778-
chrome.notifications.onClosed.addListener(async (id, user) => {
779-
const ret = await App.Cache.get('GM_notification:' + id);
780-
if (ret) {
781-
const [grant, post] = ret;
782-
grant.data = { type: 'done', id: id, user: user };
783-
post.postMessage(grant);
784-
App.Cache.del('GM_notification:' + id);
790+
chrome.notifications.onClosed.addListener((id, user) => {
791+
const handler = async () => {
792+
const ret = await App.Cache.get('GM_notification:' + id);
793+
if (ret) {
794+
const [grant, post] = <[Grant, IPostMessage]>ret;
795+
grant.data = { type: 'done', id: id, user: user };
796+
post.postMessage(grant);
797+
void App.Cache.del('GM_notification:' + id);
798+
}
785799
}
800+
void handler();
786801
});
787-
chrome.notifications.onClicked.addListener(async (id) => {
788-
const ret = await App.Cache.get('GM_notification:' + id);
789-
if (ret) {
790-
const [grant, post] = ret;
791-
grant.data = { type: 'click', id: id, index: undefined };
792-
post.postMessage(grant);
793-
grant.data = { type: 'done', id: id, user: true };
794-
post.postMessage(grant);
795-
App.Cache.del('GM_notification:' + id);
802+
chrome.notifications.onClicked.addListener((id) => {
803+
const handler = async () => {
804+
const ret = await App.Cache.get('GM_notification:' + id);
805+
if (ret) {
806+
const [grant, post] = <[Grant, IPostMessage]>ret;
807+
grant.data = { type: 'click', id: id, index: undefined };
808+
post.postMessage(grant);
809+
grant.data = { type: 'done', id: id, user: true };
810+
post.postMessage(grant);
811+
void App.Cache.del('GM_notification:' + id);
812+
}
796813
}
814+
void handler();
797815
});
798-
chrome.notifications.onButtonClicked.addListener(async (id, buttonIndex) => {
799-
const ret = await App.Cache.get('GM_notification:' + id);
800-
if (ret) {
801-
const [grant, post] = ret;
802-
grant.data = { type: 'click', id: id, index: buttonIndex };
803-
post.postMessage(grant);
804-
grant.data = { type: 'done', id: id, user: true };
805-
post.postMessage(grant);
806-
App.Cache.del('GM_notification:' + id);
816+
chrome.notifications.onButtonClicked.addListener((id, buttonIndex) => {
817+
const handler = async () => {
818+
const ret = await App.Cache.get('GM_notification:' + id);
819+
if (ret) {
820+
const [grant, post] = <[Grant, IPostMessage]>ret;
821+
grant.data = { type: 'click', id: id, index: buttonIndex };
822+
post.postMessage(grant);
823+
grant.data = { type: 'done', id: id, user: true };
824+
post.postMessage(grant);
825+
void App.Cache.del('GM_notification:' + id);
826+
}
807827
}
828+
void handler();
808829
});
809830
}
810831
})
@@ -827,15 +848,15 @@ export class BackgroundGrant {
827848
}
828849

829850
chrome.notifications.create(options, (notificationId) => {
830-
App.Cache.set('GM_notification:' + notificationId, [grant, post]);
851+
void App.Cache.set('GM_notification:' + notificationId, [grant, post]);
831852
grant.data = { type: 'create', id: notificationId };
832853
post.postMessage(grant);
833854
if (details.timeout) {
834855
setTimeout(() => {
835856
chrome.notifications.clear(notificationId);
836857
grant.data = { type: 'done', id: notificationId, user: false };
837858
post.postMessage(grant);
838-
App.Cache.del('GM_notification:' + notificationId);
859+
void App.Cache.del('GM_notification:' + notificationId);
839860
}, details.timeout);
840861
}
841862
});
@@ -845,18 +866,17 @@ export class BackgroundGrant {
845866

846867
@BackgroundGrant.GMFunction()
847868
protected GM_closeNotification(grant: Grant): Promise<any> {
848-
return new Promise(async resolve => {
849-
chrome.notifications.clear(grant.params[0]);
850-
851-
const ret = await App.Cache.get('GM_notification:' + grant.params[0]);
852-
if (ret) {
853-
const [grant, post] = ret;
854-
grant.data = { type: 'done', id: grant.params[0], user: false };
855-
post.postMessage(grant);
856-
App.Cache.del('GM_notification:' + grant.params[0]);
857-
}
858-
859-
return resolve(undefined);
869+
return new Promise(resolve => {
870+
chrome.notifications.clear(<string>grant.params[0]);
871+
void App.Cache.get('GM_notification:' + <string>grant.params[0]).then(ret => {
872+
if (ret) {
873+
const [grant, post] = <[Grant, IPostMessage]>ret;
874+
grant.data = { type: 'done', id: grant.params[0], user: false };
875+
post.postMessage(grant);
876+
void App.Cache.del('GM_notification:' + <string>grant.params[0]);
877+
}
878+
return resolve(undefined);
879+
});
860880
});
861881
}
862882

src/apps/grant/frontend.ts

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,16 @@ export class FrontendGrant implements ScriptContext {
138138
});
139139
}
140140

141-
@FrontendGrant.GMFunction({ depend: ['CAT_fetchBlob'] })
141+
@FrontendGrant.GMFunction()
142+
public CAT_createBlobUrl(blob: Blob): Promise<string> {
143+
return new Promise(resolve => {
144+
this.postRequest('CAT_createBlobUrl', [blob], (grant: Grant) => {
145+
resolve(<string>grant.data);
146+
});
147+
});
148+
}
149+
150+
@FrontendGrant.GMFunction({ depend: ['CAT_fetchBlob', 'CAT_createBlobUrl'] })
142151
public async GM_xmlhttpRequest(details: GM_Types.XHRDetails) {
143152
const u = new URL(details.url, window.location.href);
144153
if (details.headers) {
@@ -197,6 +206,9 @@ export class FrontendGrant implements ScriptContext {
197206
}
198207
}
199208
param.data = data;
209+
} else if (details.data instanceof Blob) {
210+
param.dataType = 'Blob';
211+
param.data = await this.CAT_createBlobUrl(details.data);
200212
} else {
201213
param.data = details.data;
202214
}
@@ -250,6 +262,8 @@ export class FrontendGrant implements ScriptContext {
250262
});
251263
}
252264

265+
266+
253267
public GM_notification(text: string, title: string, image: string, onclick?: GM_Types.NotificationOnClick): void
254268

255269
@FrontendGrant.GMFunction()

src/content.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ chrome.runtime.sendMessage('runScript', (event: unknown) => {
1818
(<{ cloneInto: (detail: any, view: any) => any }><unknown>global).cloneInto(resp, document.defaultView) : resp;
1919
browserMsg.send(msg.flag, msg);
2020
break;
21+
case 'CAT_createBlobUrl':
22+
msg.data = URL.createObjectURL(msg.params[0]);
23+
browserMsg.send(msg.flag, msg);
24+
break;
2125
default:
2226
// NOTE: 好像没处理释放问题
2327
MsgCenter.connect(ScriptGrant, msg).addListener((msg: Grant) => {

src/types/main.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ declare namespace GMSend {
111111
user?: string,
112112
password?: string,
113113
nocache?: boolean
114-
dataType?: 'FormData'
114+
dataType?: 'FormData' | 'Blob'
115115
}
116116

117117
interface XHRFormData {

src/types/scriptcat.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ declare namespace GM_Types {
197197
method?: 'GET' | 'HEAD' | 'POST' | 'PUT' | 'DELETE' | 'PATCH' | 'OPTIONS'
198198
url: string
199199
headers?: { [key: string]: string }
200-
data?: string | FormData
200+
data?: string | FormData | Blob
201201
cookie?: string
202202
binary?: boolean
203203
timeout?: number

0 commit comments

Comments
 (0)