diff --git a/src/apis/index.js b/src/apis/index.js index 847d80a..c2c8bb7 100644 --- a/src/apis/index.js +++ b/src/apis/index.js @@ -21,18 +21,14 @@ import { sha256 } from "../libs/utils"; * @returns */ export const apiSyncData = async (url, key, data) => - fetchPolyfill( - url, - { - headers: { - "Content-type": "application/json", - Authorization: `Bearer ${await sha256(key, KV_SALT_SYNC)}`, - }, - method: "POST", - body: JSON.stringify(data), + fetchPolyfill(url, { + headers: { + "Content-type": "application/json", + Authorization: `Bearer ${await sha256(key, KV_SALT_SYNC)}`, }, - { useUnsafe: true } - ); + method: "POST", + body: JSON.stringify(data), + }); /** * 谷歌翻译 diff --git a/src/libs/fetch.js b/src/libs/fetch.js index 4325720..c1945e8 100644 --- a/src/libs/fetch.js +++ b/src/libs/fetch.js @@ -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,8 +73,13 @@ const fetchApi = async ({ input, init, useUnsafe, translator, token }) => { init.headers["api-key"] = token; // Azure OpenAI } - if (isGm && !useUnsafe) { - return fetchGM(input, init); + 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) { diff --git a/src/libs/rules.js b/src/libs/rules.js index 8737ffe..c3611bf 100644 --- a/src/libs/rules.js +++ b/src/libs/rules.js @@ -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, "") !== "" );