2023-08-05 18:15:01 +08:00
|
|
|
import { isExt, isGm } from "./browser";
|
2023-07-20 13:45:41 +08:00
|
|
|
import { sendMsg } from "./msg";
|
|
|
|
|
import {
|
|
|
|
|
MSG_FETCH,
|
|
|
|
|
CACHE_NAME,
|
|
|
|
|
OPT_TRANS_MICROSOFT,
|
|
|
|
|
OPT_TRANS_OPENAI,
|
|
|
|
|
} from "../config";
|
|
|
|
|
|
|
|
|
|
/**
|
2023-08-05 18:15:01 +08:00
|
|
|
* 油猴脚本的请求封装
|
|
|
|
|
* @param {*} input
|
|
|
|
|
* @param {*} init
|
|
|
|
|
* @returns
|
|
|
|
|
*/
|
2023-08-05 15:32:51 +08:00
|
|
|
const fetchGM = async (input, { method = "GET", headers, body } = {}) =>
|
2023-08-05 18:15:01 +08:00
|
|
|
new Promise((resolve, reject) => {
|
|
|
|
|
try {
|
2023-08-05 16:32:49 +08:00
|
|
|
(window.GM_xmlhttpRequest || window.GM.xmlhttpRequest)({
|
2023-08-05 18:15:01 +08:00
|
|
|
method,
|
|
|
|
|
url: input,
|
|
|
|
|
headers,
|
|
|
|
|
data: body,
|
|
|
|
|
onload: (response) => {
|
2023-08-05 15:32:51 +08:00
|
|
|
if (response.status === 200) {
|
|
|
|
|
const headers = new Headers();
|
|
|
|
|
response.responseHeaders.split("\n").forEach((line) => {
|
|
|
|
|
let [name, value] = line.split(":").map((item) => item.trim());
|
|
|
|
|
if (name && value) {
|
|
|
|
|
headers.append(name, value);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
resolve(new Response(response.response, { headers }));
|
|
|
|
|
} else {
|
|
|
|
|
reject(new Error(`[${response.status}] ${response.responseText}`));
|
|
|
|
|
}
|
2023-08-05 18:15:01 +08:00
|
|
|
},
|
2023-08-05 15:32:51 +08:00
|
|
|
onerror: reject,
|
2023-08-05 18:15:01 +08:00
|
|
|
});
|
|
|
|
|
} catch (error) {
|
|
|
|
|
reject(error);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 构造缓存 request
|
2023-07-20 13:45:41 +08:00
|
|
|
* @param {*} request
|
|
|
|
|
* @returns
|
|
|
|
|
*/
|
2023-08-04 16:05:14 +08:00
|
|
|
const newCacheReq = async (request, translator) => {
|
2023-07-20 13:45:41 +08:00
|
|
|
if (translator === OPT_TRANS_MICROSOFT) {
|
2023-08-04 16:05:14 +08:00
|
|
|
request.headers.delete("Authorization");
|
2023-07-20 13:45:41 +08:00
|
|
|
} else if (translator === OPT_TRANS_OPENAI) {
|
2023-08-04 16:05:14 +08:00
|
|
|
request.headers.delete("Authorization");
|
|
|
|
|
request.headers.delete("api-key");
|
2023-07-20 13:45:41 +08:00
|
|
|
}
|
|
|
|
|
|
2023-08-04 16:05:14 +08:00
|
|
|
if (request.method !== "GET") {
|
|
|
|
|
const body = await request.text();
|
|
|
|
|
const cacheUrl = new URL(request.url);
|
|
|
|
|
cacheUrl.pathname += body;
|
|
|
|
|
request = new Request(cacheUrl.toString(), { method: "GET" });
|
|
|
|
|
}
|
2023-07-20 13:45:41 +08:00
|
|
|
|
2023-08-04 16:05:14 +08:00
|
|
|
return request;
|
2023-07-20 13:45:41 +08:00
|
|
|
};
|
|
|
|
|
|
2023-08-04 17:56:47 +08:00
|
|
|
/**
|
|
|
|
|
* 请求数据
|
|
|
|
|
* @param {*} input
|
|
|
|
|
* @param {*} init
|
|
|
|
|
* @param {*} opts
|
2023-07-20 13:45:41 +08:00
|
|
|
* @returns
|
|
|
|
|
*/
|
2023-08-09 10:33:00 +08:00
|
|
|
export const fetchData = async (
|
|
|
|
|
input,
|
|
|
|
|
init,
|
|
|
|
|
{ useCache, translator, useUnsafe } = {}
|
|
|
|
|
) => {
|
2023-08-04 17:56:47 +08:00
|
|
|
const cacheReq = await newCacheReq(new Request(input, init), translator);
|
2023-07-20 13:45:41 +08:00
|
|
|
const cache = await caches.open(CACHE_NAME);
|
|
|
|
|
let res;
|
|
|
|
|
|
|
|
|
|
// 查询缓存
|
|
|
|
|
if (useCache) {
|
|
|
|
|
try {
|
|
|
|
|
res = await cache.match(cacheReq);
|
|
|
|
|
} catch (err) {
|
|
|
|
|
console.log("[cache match]", err);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 发送请求
|
|
|
|
|
if (!res) {
|
2023-08-09 16:30:08 +08:00
|
|
|
if (isGm && !useUnsafe) {
|
|
|
|
|
res = await fetchGM(input, init);
|
2023-08-05 18:15:01 +08:00
|
|
|
} else {
|
|
|
|
|
res = await fetch(input, init);
|
|
|
|
|
}
|
2023-07-20 13:45:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!res?.ok) {
|
|
|
|
|
throw new Error(`response: ${res.statusText}`);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 插入缓存
|
|
|
|
|
if (useCache) {
|
|
|
|
|
try {
|
|
|
|
|
await cache.put(cacheReq, res.clone());
|
|
|
|
|
} catch (err) {
|
|
|
|
|
console.log("[cache put]", err);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const contentType = res.headers.get("Content-Type");
|
|
|
|
|
if (contentType?.includes("json")) {
|
|
|
|
|
return await res.json();
|
|
|
|
|
}
|
|
|
|
|
return await res.text();
|
|
|
|
|
};
|
2023-08-05 18:15:01 +08:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* fetch 兼容性封装
|
|
|
|
|
* @param {*} input
|
|
|
|
|
* @param {*} init
|
|
|
|
|
* @param {*} opts
|
|
|
|
|
* @returns
|
|
|
|
|
*/
|
|
|
|
|
export const fetchPolyfill = async (input, init, opts) => {
|
|
|
|
|
// 插件
|
|
|
|
|
if (isExt) {
|
|
|
|
|
const res = await sendMsg(MSG_FETCH, { input, init, opts });
|
|
|
|
|
if (res.error) {
|
|
|
|
|
throw new Error(res.error);
|
|
|
|
|
}
|
|
|
|
|
return res.data;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 油猴/网页
|
|
|
|
|
return await fetchData(input, init, opts);
|
|
|
|
|
};
|