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 * @returns
*/ */
export const apiSyncData = async (url, key, data) => export const apiSyncData = async (url, key, data) =>
fetchPolyfill( fetchPolyfill(url, {
url,
{
headers: { headers: {
"Content-type": "application/json", "Content-type": "application/json",
Authorization: `Bearer ${await sha256(key, KV_SALT_SYNC)}`, Authorization: `Bearer ${await sha256(key, KV_SALT_SYNC)}`,
}, },
method: "POST", method: "POST",
body: JSON.stringify(data), body: JSON.stringify(data),
}, });
{ useUnsafe: true }
);
/** /**
* 谷歌翻译 * 谷歌翻译

View File

@@ -65,7 +65,7 @@ const newCacheReq = async (request) => {
* @param {*} param0 * @param {*} param0
* @returns * @returns
*/ */
const fetchApi = async ({ input, init, useUnsafe, translator, token }) => { const fetchApi = async ({ input, init, translator, token }) => {
if (translator === OPT_TRANS_MICROSOFT) { if (translator === OPT_TRANS_MICROSOFT) {
init.headers["Authorization"] = `Bearer ${token}`; init.headers["Authorization"] = `Bearer ${token}`;
} else if (translator === OPT_TRANS_OPENAI) { } 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 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 fetchGM(input, init);
} }
}
return fetch(input, init); return fetch(input, init);
}; };
@@ -105,7 +110,7 @@ export const fetchPool = taskPool(
export const fetchData = async ( export const fetchData = async (
input, input,
init, init,
{ useCache, usePool, translator, useUnsafe, token } = {} { useCache, usePool, translator, token } = {}
) => { ) => {
const cacheReq = await newCacheReq(new Request(input, init)); const cacheReq = await newCacheReq(new Request(input, init));
const cache = await caches.open(CACHE_NAME); const cache = await caches.open(CACHE_NAME);
@@ -123,9 +128,9 @@ export const fetchData = async (
if (!res) { if (!res) {
// 发送请求 // 发送请求
if (usePool) { if (usePool) {
res = await fetchPool.push({ input, init, useUnsafe, translator, token }); res = await fetchPool.push({ input, init, translator, token });
} else { } else {
res = await fetchApi({ input, init, useUnsafe, translator, token }); res = await fetchApi({ input, init, translator, token });
} }
if (!res?.ok) { if (!res?.ok) {

View File

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