auto use unsafe fetch

This commit is contained in:
Gabe Yuan
2023-08-21 23:46:42 +08:00
parent 3e9338be0e
commit d3a5d91f01
3 changed files with 19 additions and 18 deletions

View File

@@ -21,18 +21,14 @@ import { sha256 } from "../libs/utils";
* @returns
*/
export const apiSyncData = async (url, key, data) =>
fetchPolyfill(
url,
{
fetchPolyfill(url, {
headers: {
"Content-type": "application/json",
Authorization: `Bearer ${await sha256(key, KV_SALT_SYNC)}`,
},
method: "POST",
body: JSON.stringify(data),
},
{ useUnsafe: true }
);
});
/**
* 谷歌翻译

View File

@@ -65,7 +65,7 @@ const newCacheReq = async (request) => {
* @param {*} param0
* @returns
*/
const fetchApi = async ({ input, init, useUnsafe, translator, token }) => {
const fetchApi = async ({ input, init, translator, token }) => {
if (translator === OPT_TRANS_MICROSOFT) {
init.headers["Authorization"] = `Bearer ${token}`;
} else if (translator === OPT_TRANS_OPENAI) {
@@ -73,9 +73,14 @@ const fetchApi = async ({ input, init, useUnsafe, translator, token }) => {
init.headers["api-key"] = token; // Azure OpenAI
}
if (isGm && !useUnsafe) {
if (isGm) {
const connects = GM?.info?.script?.connects || [];
const url = new URL(input);
const isSafe = connects.find((item) => url.hostname.endsWith(item));
if (isSafe) {
return fetchGM(input, init);
}
}
return fetch(input, init);
};
@@ -105,7 +110,7 @@ export const fetchPool = taskPool(
export const fetchData = async (
input,
init,
{ useCache, usePool, translator, useUnsafe, token } = {}
{ useCache, usePool, translator, token } = {}
) => {
const cacheReq = await newCacheReq(new Request(input, init));
const cache = await caches.open(CACHE_NAME);
@@ -123,9 +128,9 @@ export const fetchData = async (
if (!res) {
// 发送请求
if (usePool) {
res = await fetchPool.push({ input, init, useUnsafe, translator, token });
res = await fetchPool.push({ input, init, translator, token });
} else {
res = await fetchApi({ input, init, useUnsafe, translator, token });
res = await fetchApi({ input, init, translator, token });
}
if (!res?.ok) {

View File

@@ -66,7 +66,7 @@ export const checkRules = (rules) => {
*/
export const rulesCache = {
fetch: async (url) => {
const res = await fetchPolyfill(url, null, { useUnsafe: true });
const res = await fetchPolyfill(url);
const rules = checkRules(res).filter(
(rule) => rule.pattern.replaceAll(GLOBAL_KEY, "") !== ""
);