Skip to content

Commit

Permalink
✨ GM_xmlhttpRequest支持blob数据 #29
Browse files Browse the repository at this point in the history
  • Loading branch information
CodFrm committed Jan 11, 2022
1 parent 757fe2c commit 71a039f
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 56 deletions.
126 changes: 73 additions & 53 deletions src/apps/grant/background.ts
Original file line number Diff line number Diff line change
Expand Up @@ -422,19 +422,22 @@ export class BackgroundGrant {
if (api == undefined) {
return resolve(undefined);
}
api.apply(this, [grant, postMessage]).then((result: any) => {
if (grant.value == 'CAT_runComplete' || (grant.value == 'CAT_setRunError' && grant.params[0])) {
//执行完毕,释放资源
BackgroundGrant.freedCallback.forEach(v => {
v(grant.id, grant.tabId);
});
}
resolve(result);
}).catch((e: string) => {
grant.error = 'GM_ERROR';
grant.errorMsg = e;
resolve(grant);
});
const ret = api.apply(this, [grant, postMessage]);
if (ret instanceof Promise) {
ret.then((result: any) => {
if (grant.value == 'CAT_runComplete' || (grant.value == 'CAT_setRunError' && grant.params[0])) {
//后台脚本执行完毕,释放资源
BackgroundGrant.freedCallback.forEach(v => {
v(grant.id, grant.tabId);
});
}
resolve(result);
}).catch((e: string) => {
grant.error = 'GM_ERROR';
grant.errorMsg = e;
resolve(grant);
});
}
});
});

Expand Down Expand Up @@ -592,6 +595,15 @@ export class BackgroundGrant {
});
xhr.send(data);
}
} else if (config.dataType == 'Blob') {
const handler = async () => {
if (!config.data) {
return reject('data is null');
}
const resp = await (await fetch(<string>config.data)).blob();
xhr.send(resp);
}
void handler();
} else {
xhr.send(<string>config.data);
}
Expand Down Expand Up @@ -741,7 +753,7 @@ export class BackgroundGrant {
@BackgroundGrant.GMFunction({
listener: () => {
chrome.tabs.onRemoved.addListener(tabId => {
const tab = BackgroundGrant.tabMap.get(tabId);
const tab = <[Grant, IPostMessage]>BackgroundGrant.tabMap.get(tabId);
if (tab) {
tab[0].data = { type: 'close' }
tab[1].postMessage(tab[0]);
Expand Down Expand Up @@ -775,36 +787,45 @@ export class BackgroundGrant {

@BackgroundGrant.GMFunction({
listener: () => {
chrome.notifications.onClosed.addListener(async (id, user) => {
const ret = await App.Cache.get('GM_notification:' + id);
if (ret) {
const [grant, post] = ret;
grant.data = { type: 'done', id: id, user: user };
post.postMessage(grant);
App.Cache.del('GM_notification:' + id);
chrome.notifications.onClosed.addListener((id, user) => {
const handler = async () => {
const ret = await App.Cache.get('GM_notification:' + id);
if (ret) {
const [grant, post] = <[Grant, IPostMessage]>ret;
grant.data = { type: 'done', id: id, user: user };
post.postMessage(grant);
void App.Cache.del('GM_notification:' + id);
}
}
void handler();
});
chrome.notifications.onClicked.addListener(async (id) => {
const ret = await App.Cache.get('GM_notification:' + id);
if (ret) {
const [grant, post] = ret;
grant.data = { type: 'click', id: id, index: undefined };
post.postMessage(grant);
grant.data = { type: 'done', id: id, user: true };
post.postMessage(grant);
App.Cache.del('GM_notification:' + id);
chrome.notifications.onClicked.addListener((id) => {
const handler = async () => {
const ret = await App.Cache.get('GM_notification:' + id);
if (ret) {
const [grant, post] = <[Grant, IPostMessage]>ret;
grant.data = { type: 'click', id: id, index: undefined };
post.postMessage(grant);
grant.data = { type: 'done', id: id, user: true };
post.postMessage(grant);
void App.Cache.del('GM_notification:' + id);
}
}
void handler();
});
chrome.notifications.onButtonClicked.addListener(async (id, buttonIndex) => {
const ret = await App.Cache.get('GM_notification:' + id);
if (ret) {
const [grant, post] = ret;
grant.data = { type: 'click', id: id, index: buttonIndex };
post.postMessage(grant);
grant.data = { type: 'done', id: id, user: true };
post.postMessage(grant);
App.Cache.del('GM_notification:' + id);
chrome.notifications.onButtonClicked.addListener((id, buttonIndex) => {
const handler = async () => {
const ret = await App.Cache.get('GM_notification:' + id);
if (ret) {
const [grant, post] = <[Grant, IPostMessage]>ret;
grant.data = { type: 'click', id: id, index: buttonIndex };
post.postMessage(grant);
grant.data = { type: 'done', id: id, user: true };
post.postMessage(grant);
void App.Cache.del('GM_notification:' + id);
}
}
void handler();
});
}
})
Expand All @@ -827,15 +848,15 @@ export class BackgroundGrant {
}

chrome.notifications.create(options, (notificationId) => {
App.Cache.set('GM_notification:' + notificationId, [grant, post]);
void App.Cache.set('GM_notification:' + notificationId, [grant, post]);
grant.data = { type: 'create', id: notificationId };
post.postMessage(grant);
if (details.timeout) {
setTimeout(() => {
chrome.notifications.clear(notificationId);
grant.data = { type: 'done', id: notificationId, user: false };
post.postMessage(grant);
App.Cache.del('GM_notification:' + notificationId);
void App.Cache.del('GM_notification:' + notificationId);
}, details.timeout);
}
});
Expand All @@ -845,18 +866,17 @@ export class BackgroundGrant {

@BackgroundGrant.GMFunction()
protected GM_closeNotification(grant: Grant): Promise<any> {
return new Promise(async resolve => {
chrome.notifications.clear(grant.params[0]);

const ret = await App.Cache.get('GM_notification:' + grant.params[0]);
if (ret) {
const [grant, post] = ret;
grant.data = { type: 'done', id: grant.params[0], user: false };
post.postMessage(grant);
App.Cache.del('GM_notification:' + grant.params[0]);
}

return resolve(undefined);
return new Promise(resolve => {
chrome.notifications.clear(<string>grant.params[0]);
void App.Cache.get('GM_notification:' + <string>grant.params[0]).then(ret => {
if (ret) {
const [grant, post] = <[Grant, IPostMessage]>ret;
grant.data = { type: 'done', id: grant.params[0], user: false };
post.postMessage(grant);
void App.Cache.del('GM_notification:' + <string>grant.params[0]);
}
return resolve(undefined);
});
});
}

Expand Down
16 changes: 15 additions & 1 deletion src/apps/grant/frontend.ts
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,16 @@ export class FrontendGrant implements ScriptContext {
});
}

@FrontendGrant.GMFunction({ depend: ['CAT_fetchBlob'] })
@FrontendGrant.GMFunction()
public CAT_createBlobUrl(blob: Blob): Promise<string> {
return new Promise(resolve => {
this.postRequest('CAT_createBlobUrl', [blob], (grant: Grant) => {
resolve(<string>grant.data);
});
});
}

@FrontendGrant.GMFunction({ depend: ['CAT_fetchBlob', 'CAT_createBlobUrl'] })
public async GM_xmlhttpRequest(details: GM_Types.XHRDetails) {
const u = new URL(details.url, window.location.href);
if (details.headers) {
Expand Down Expand Up @@ -197,6 +206,9 @@ export class FrontendGrant implements ScriptContext {
}
}
param.data = data;
} else if (details.data instanceof Blob) {
param.dataType = 'Blob';
param.data = await this.CAT_createBlobUrl(details.data);
} else {
param.data = details.data;
}
Expand Down Expand Up @@ -250,6 +262,8 @@ export class FrontendGrant implements ScriptContext {
});
}



public GM_notification(text: string, title: string, image: string, onclick?: GM_Types.NotificationOnClick): void

@FrontendGrant.GMFunction()
Expand Down
4 changes: 4 additions & 0 deletions src/content.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ chrome.runtime.sendMessage('runScript', (event: unknown) => {
(<{ cloneInto: (detail: any, view: any) => any }><unknown>global).cloneInto(resp, document.defaultView) : resp;
browserMsg.send(msg.flag, msg);
break;
case 'CAT_createBlobUrl':
msg.data = URL.createObjectURL(msg.params[0]);
browserMsg.send(msg.flag, msg);
break;
default:
// NOTE: 好像没处理释放问题
MsgCenter.connect(ScriptGrant, msg).addListener((msg: Grant) => {
Expand Down
2 changes: 1 addition & 1 deletion src/types/main.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ declare namespace GMSend {
user?: string,
password?: string,
nocache?: boolean
dataType?: 'FormData'
dataType?: 'FormData' | 'Blob'
}

interface XHRFormData {
Expand Down
2 changes: 1 addition & 1 deletion src/types/scriptcat.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ declare namespace GM_Types {
method?: 'GET' | 'HEAD' | 'POST' | 'PUT' | 'DELETE' | 'PATCH' | 'OPTIONS'
url: string
headers?: { [key: string]: string }
data?: string | FormData
data?: string | FormData | Blob
cookie?: string
binary?: boolean
timeout?: number
Expand Down

0 comments on commit 71a039f

Please sign in to comment.