UserScript: Fix fetch().res.ok

This commit is contained in:
V 2023-05-15 02:49:34 +02:00
parent 53ff2532f4
commit 2815509c00
No known key found for this signature in database
GPG key ID: A1DC0CFB5615D905
2 changed files with 20 additions and 55 deletions

View file

@ -16,20 +16,6 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>. * along with this program. If not, see <https://www.gnu.org/licenses/>.
*/ */
function fetchOptions(url) {
return new Promise((resolve, reject) => {
const opt = {
method: "OPTIONS",
url: url,
};
opt.onload = resp => resolve(resp.responseHeaders);
opt.ontimeout = () => reject("fetch timeout");
opt.onerror = () => reject("fetch error");
opt.onabort = () => reject("fetch abort");
GM_xmlhttpRequest(opt);
});
}
function parseHeaders(headers) { function parseHeaders(headers) {
if (!headers) if (!headers)
return {}; return {};
@ -52,21 +38,6 @@ function parseHeaders(headers) {
return result; return result;
} }
// returns true if CORS permits request
async function checkCors(url, method) {
const headers = parseHeaders(await fetchOptions(url));
const origin = headers["access-control-allow-origin"];
if (origin !== "*" && origin !== window.location.origin) return false;
const methods = headers["access-control-allow-methods"]?.toLowerCase()
.split(",")
.map(s => s.trim());
if (methods && !methods.includes(method.toLowerCase())) return false;
return true;
}
function blobTo(to, blob) { function blobTo(to, blob) {
if (to === "arrayBuffer" && blob.arrayBuffer) return blob.arrayBuffer(); if (to === "arrayBuffer" && blob.arrayBuffer) return blob.arrayBuffer();
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
@ -80,9 +51,6 @@ function blobTo(to, blob) {
function GM_fetch(url, opt) { function GM_fetch(url, opt) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
checkCors(url, opt?.method || "GET")
.then(can => {
if (can) {
// https://www.tampermonkey.net/documentation.php?ext=dhdg#GM_xmlhttpRequest // https://www.tampermonkey.net/documentation.php?ext=dhdg#GM_xmlhttpRequest
const options = opt || {}; const options = opt || {};
options.url = url; options.url = url;
@ -95,16 +63,13 @@ function GM_fetch(url, opt) {
resp.text = () => blobTo("text", blob); resp.text = () => blobTo("text", blob);
resp.json = async () => JSON.parse(await blobTo("text", blob)); resp.json = async () => JSON.parse(await blobTo("text", blob));
resp.headers = new Headers(parseHeaders(resp.responseHeaders)); resp.headers = new Headers(parseHeaders(resp.responseHeaders));
resp.ok = resp.status >= 200 && resp.status < 300;
resolve(resp); resolve(resp);
}; };
options.ontimeout = () => reject("fetch timeout"); options.ontimeout = () => reject("fetch timeout");
options.onerror = () => reject("fetch error"); options.onerror = () => reject("fetch error");
options.onabort = () => reject("fetch abort"); options.onabort = () => reject("fetch abort");
GM_xmlhttpRequest(options); GM_xmlhttpRequest(options);
} else {
reject("CORS issue");
}
});
}); });
} }
export const fetch = GM_fetch; export const fetch = GM_fetch;

View file

@ -59,7 +59,7 @@ export async function translate(kind: "received" | "sent", text: string): Promis
const res = await fetch(url); const res = await fetch(url);
if (!res.ok) if (!res.ok)
throw new Error( throw new Error(
`Failed to translate "${text}" (${sourceLang} -> ${targetLang}` `Failed to translate "${text}" (${sourceLang} -> ${targetLang})`
+ `\n${res.status} ${res.statusText}` + `\n${res.status} ${res.statusText}`
); );