diff options
author | V <vendicated@riseup.net> | 2023-05-15 02:49:34 +0200 |
---|---|---|
committer | V <vendicated@riseup.net> | 2023-05-15 02:49:34 +0200 |
commit | 2815509c00bf5865f8fe9559f7e456d6455299ff (patch) | |
tree | 6003b5186bdf88a85b3fcf579fa89264e1e530e7 | |
parent | 53ff2532f486d9542dda739f195d2601f4589e6b (diff) | |
download | Vencord-2815509c00bf5865f8fe9559f7e456d6455299ff.tar.gz Vencord-2815509c00bf5865f8fe9559f7e456d6455299ff.tar.bz2 Vencord-2815509c00bf5865f8fe9559f7e456d6455299ff.zip |
UserScript: Fix fetch().res.ok
-rw-r--r-- | browser/GMPolyfill.js | 73 | ||||
-rw-r--r-- | src/plugins/translate/utils.ts | 2 |
2 files changed, 20 insertions, 55 deletions
diff --git a/browser/GMPolyfill.js b/browser/GMPolyfill.js index 1f74a7c..f880155 100644 --- a/browser/GMPolyfill.js +++ b/browser/GMPolyfill.js @@ -16,20 +16,6 @@ * 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) { if (!headers) return {}; @@ -52,21 +38,6 @@ function parseHeaders(headers) { 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) { if (to === "arrayBuffer" && blob.arrayBuffer) return blob.arrayBuffer(); return new Promise((resolve, reject) => { @@ -80,31 +51,25 @@ function blobTo(to, blob) { function GM_fetch(url, opt) { return new Promise((resolve, reject) => { - checkCors(url, opt?.method || "GET") - .then(can => { - if (can) { - // https://www.tampermonkey.net/documentation.php?ext=dhdg#GM_xmlhttpRequest - const options = opt || {}; - options.url = url; - options.data = options.body; - options.responseType = "blob"; - options.onload = resp => { - var blob = resp.response; - resp.blob = () => Promise.resolve(blob); - resp.arrayBuffer = () => blobTo("arrayBuffer", blob); - resp.text = () => blobTo("text", blob); - resp.json = async () => JSON.parse(await blobTo("text", blob)); - resp.headers = new Headers(parseHeaders(resp.responseHeaders)); - resolve(resp); - }; - options.ontimeout = () => reject("fetch timeout"); - options.onerror = () => reject("fetch error"); - options.onabort = () => reject("fetch abort"); - GM_xmlhttpRequest(options); - } else { - reject("CORS issue"); - } - }); + // https://www.tampermonkey.net/documentation.php?ext=dhdg#GM_xmlhttpRequest + const options = opt || {}; + options.url = url; + options.data = options.body; + options.responseType = "blob"; + options.onload = resp => { + var blob = resp.response; + resp.blob = () => Promise.resolve(blob); + resp.arrayBuffer = () => blobTo("arrayBuffer", blob); + resp.text = () => blobTo("text", blob); + resp.json = async () => JSON.parse(await blobTo("text", blob)); + resp.headers = new Headers(parseHeaders(resp.responseHeaders)); + resp.ok = resp.status >= 200 && resp.status < 300; + resolve(resp); + }; + options.ontimeout = () => reject("fetch timeout"); + options.onerror = () => reject("fetch error"); + options.onabort = () => reject("fetch abort"); + GM_xmlhttpRequest(options); }); } export const fetch = GM_fetch; diff --git a/src/plugins/translate/utils.ts b/src/plugins/translate/utils.ts index 784ec25..493fb2c 100644 --- a/src/plugins/translate/utils.ts +++ b/src/plugins/translate/utils.ts @@ -59,7 +59,7 @@ export async function translate(kind: "received" | "sent", text: string): Promis const res = await fetch(url); if (!res.ok) throw new Error( - `Failed to translate "${text}" (${sourceLang} -> ${targetLang}` + `Failed to translate "${text}" (${sourceLang} -> ${targetLang})` + `\n${res.status} ${res.statusText}` ); |