Skip to content

Commit

Permalink
fix: responseType blob (#40)
Browse files Browse the repository at this point in the history
close #40
  • Loading branch information
lisonge committed Nov 11, 2024
1 parent fb055de commit c4ff2e8
Showing 1 changed file with 14 additions and 3 deletions.
17 changes: 14 additions & 3 deletions src/utils/gm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,12 @@ export const GM_fetch = async (
const method = request.method.toUpperCase();
const url = fixUrl(request.url);

// temporarily fix tampermonkey bug
let responseType: 'blob' | undefined = undefined;
if (url.startsWith('https://github.com/user-attachments/files/')) {
responseType = 'blob';
}

// headers
const sendHeaders = new Headers(request.headers);
new Headers(init.headers).forEach((value, key) => {
Expand Down Expand Up @@ -122,19 +128,24 @@ export const GM_fetch = async (
headers: headers2obj(sendHeaders),
data,
binary,
responseType,
async onload(e) {
await delay();
const resp = new Response(e.response || e.responseText || null, {
let useNull = false;
if (e.response instanceof Blob) {
useNull = e.response.size === 0;
}
const resp = new Response((useNull ? null : e.response) || null, {
status: e.status,
statusText: e.statusText,
headers: parseHeaders(e.responseHeaders),
});
Object.defineProperty(resp, 'url', { value: e.finalUrl });
resolve(resp);
},
async onerror() {
async onerror(e) {
await delay();
reject(new TypeError('Network request onerror failed'));
reject(new TypeError('Network request onerror failed', { cause: e }));
},
async ontimeout() {
await delay();
Expand Down

0 comments on commit c4ff2e8

Please sign in to comment.