mirror of
https://notabug.org/SuperSaltyGamer/ame
synced 2026-01-15 19:52:55 -03:00
35 lines
918 B
TypeScript
35 lines
918 B
TypeScript
export function fetchCors(input: RequestInfo | URL, init?: RequestInit): Promise<Response> {
|
|
return new Promise<Response>((resolve, reject) => {
|
|
const fetchReq = new Request(input, init);
|
|
|
|
GM.xmlHttpRequest({
|
|
method: fetchReq.method as any,
|
|
url: fetchReq.url,
|
|
headers: Object.fromEntries(Array.from(fetchReq.headers)),
|
|
responseType: 'blob',
|
|
onload(res) {
|
|
const headers = res.responseHeaders
|
|
.split('\r\n')
|
|
.slice(0, -1)
|
|
.map(line => line.split(': '));
|
|
|
|
const fetchRes = new Response(res.response, {
|
|
headers: Object.fromEntries(headers),
|
|
status: res.status,
|
|
statusText: res.statusText
|
|
});
|
|
|
|
Object.defineProperty(fetchRes, 'url', { value: fetchReq.url });
|
|
|
|
resolve(fetchRes);
|
|
},
|
|
onerror() {
|
|
reject(new TypeError('Network request errored.'));
|
|
},
|
|
ontimeout() {
|
|
reject(new TypeError('Network request timed out.'));
|
|
}
|
|
});
|
|
});
|
|
}
|