Skip to content

Commit 2096137

Browse files
committed
✨ GM_download可以下载blob文件
1 parent a06113c commit 2096137

1 file changed

Lines changed: 13 additions & 9 deletions

File tree

src/apps/grant/background.ts

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1294,13 +1294,16 @@ export class BackgroundGrant {
12941294
public GM_download(grant: Grant, post: IPostMessage): Promise<any> {
12951295
return new Promise(resolve => {
12961296
const config = <GM_Types.DownloadDetails>grant.params[0];
1297-
const headers = new Array<chrome.downloads.HeaderNameValuePair>();
1298-
if (config.headers) {
1299-
for (const key in config.headers) {
1300-
headers.push({
1301-
name: key, value: config.headers[key]
1302-
});
1303-
}
1297+
// blob本地文件直接下载
1298+
if (config.url.startsWith('blob:')) {
1299+
chrome.downloads.download({
1300+
url: config.url,
1301+
saveAs: config.saveAs,
1302+
filename: config.name,
1303+
}, () => {
1304+
resolve({ type: 'onload' });
1305+
});
1306+
return;
13041307
}
13051308
// 使用ajax下载blob,再使用download api创建下载
13061309
const xhr = new XMLHttpRequest();
@@ -1326,12 +1329,13 @@ export class BackgroundGrant {
13261329
xhr.onload = () => {
13271330
deal('onload');
13281331
const url = URL.createObjectURL(xhr.response);
1332+
setTimeout(() => {
1333+
URL.revokeObjectURL(url);
1334+
}, 6000);
13291335
chrome.downloads.download({
1330-
method: config.method,
13311336
url: url,
13321337
saveAs: config.saveAs,
13331338
filename: config.name,
1334-
headers: headers,
13351339
});
13361340
}
13371341
xhr.onerror = () => {

0 commit comments

Comments
 (0)