UserScript: Fix fetch().res.ok
This commit is contained in:
parent
53ff2532f4
commit
2815509c00
@ -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,31 +51,25 @@ 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")
|
// https://www.tampermonkey.net/documentation.php?ext=dhdg#GM_xmlhttpRequest
|
||||||
.then(can => {
|
const options = opt || {};
|
||||||
if (can) {
|
options.url = url;
|
||||||
// https://www.tampermonkey.net/documentation.php?ext=dhdg#GM_xmlhttpRequest
|
options.data = options.body;
|
||||||
const options = opt || {};
|
options.responseType = "blob";
|
||||||
options.url = url;
|
options.onload = resp => {
|
||||||
options.data = options.body;
|
var blob = resp.response;
|
||||||
options.responseType = "blob";
|
resp.blob = () => Promise.resolve(blob);
|
||||||
options.onload = resp => {
|
resp.arrayBuffer = () => blobTo("arrayBuffer", blob);
|
||||||
var blob = resp.response;
|
resp.text = () => blobTo("text", blob);
|
||||||
resp.blob = () => Promise.resolve(blob);
|
resp.json = async () => JSON.parse(await blobTo("text", blob));
|
||||||
resp.arrayBuffer = () => blobTo("arrayBuffer", blob);
|
resp.headers = new Headers(parseHeaders(resp.responseHeaders));
|
||||||
resp.text = () => blobTo("text", blob);
|
resp.ok = resp.status >= 200 && resp.status < 300;
|
||||||
resp.json = async () => JSON.parse(await blobTo("text", blob));
|
resolve(resp);
|
||||||
resp.headers = new Headers(parseHeaders(resp.responseHeaders));
|
};
|
||||||
resolve(resp);
|
options.ontimeout = () => reject("fetch timeout");
|
||||||
};
|
options.onerror = () => reject("fetch error");
|
||||||
options.ontimeout = () => reject("fetch timeout");
|
options.onabort = () => reject("fetch abort");
|
||||||
options.onerror = () => reject("fetch error");
|
GM_xmlhttpRequest(options);
|
||||||
options.onabort = () => reject("fetch abort");
|
|
||||||
GM_xmlhttpRequest(options);
|
|
||||||
} else {
|
|
||||||
reject("CORS issue");
|
|
||||||
}
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
export const fetch = GM_fetch;
|
export const fetch = GM_fetch;
|
||||||
|
@ -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}`
|
||||||
);
|
);
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user