dev......

This commit is contained in:
Gabe Yuan
2023-08-31 00:18:57 +08:00
parent c46fe7d1c6
commit aa795e2731
21 changed files with 228 additions and 449 deletions

View File

@@ -1,5 +1,5 @@
import storage from "./storage";
import { STOKEY_MSAUTH, URL_MICROSOFT_AUTH } from "../config";
import { getMsauth, setMsauth } from "./storage";
import { URL_MICROSOFT_AUTH } from "../config";
import { fetchData } from "./fetch";
const parseMSToken = (token) => {
@@ -26,9 +26,9 @@ const _msAuth = () => {
}
// 查询storage缓存
const res = (await storage.getObj(STOKEY_MSAUTH)) || {};
token = res.token;
exp = res.exp;
const res = await getMsauth();
token = res?.token;
exp = res?.exp;
if (token && exp * 1000 > now + 1000) {
return [token, exp];
}
@@ -36,7 +36,7 @@ const _msAuth = () => {
// 缓存没有或失效,查询接口
token = await fetchData(URL_MICROSOFT_AUTH);
exp = parseMSToken(token);
await storage.setObj(STOKEY_MSAUTH, { token, exp });
await setMsauth({ token, exp });
return [token, exp];
};
};

View File

@@ -13,21 +13,3 @@ function _browser() {
}
export const browser = _browser();
// export const client = process.env.REACT_APP_CLIENT;
// export const isExt = CLIENT_EXTS.includes(client);
// export const isGm = client === CLIENT_USERSCRIPT;
// export const isWeb = client === CLIENT_WEB;
/**
* 本地语言识别
* @param {*} q
* @returns
*/
export const detectLang = async (q) => {
try {
const res = await browser?.i18n?.detectLanguage(q);
return res?.languages?.[0]?.language;
} catch (err) {
console.log("[detect lang]", err);
}
};

View File

@@ -1,4 +1,5 @@
import { CACHE_NAME } from "../config";
import { browser } from "./browser";
/**
* 清除缓存数据
@@ -10,3 +11,17 @@ export const tryClearCaches = async () => {
console.log("[clean caches]", err.message);
}
};
/**
* 本地语言识别
* @param {*} q
* @returns
*/
export const detectLang = async (q) => {
try {
const res = await browser?.i18n?.detectLanguage(q);
return res?.languages?.[0]?.language;
} catch (err) {
console.log("[detect lang]", err);
}
};

View File

@@ -1,12 +1,3 @@
import {
getSyncWithDefault,
updateSync,
getSubRulesWithDefault,
getSubRules,
delSubRules,
setSubRules,
} from "./storage";
import { fetchPolyfill } from "./fetch";
import { matchValue, type, isMatch } from "./utils";
import {
GLOBAL_KEY,
@@ -17,8 +8,7 @@ import {
GLOBLA_RULE,
DEFAULT_SUBRULES_LIST,
} from "../config";
// import { syncOpt } from "./sync";
import { loadOrFetchSubRules } from "./subRules";
/**
* 根据href匹配规则
@@ -36,7 +26,7 @@ export const matchRule = async (
try {
const selectedSub = subrulesList.find((item) => item.selected);
if (selectedSub?.url) {
const subRules = await loadSubRules(selectedSub.url);
const subRules = await loadOrFetchSubRules(selectedSub.url);
rules.splice(-1, 0, ...subRules);
}
} catch (err) {
@@ -123,19 +113,3 @@ export const checkRules = (rules) => {
return rules;
};
// /**
// * 订阅规则的本地缓存
// */
// export const rulesCache = {
// fetch: async (url, isBg = false) => {
// const res = await fetchPolyfill(url, { isBg });
// const rules = checkRules(res).filter(
// (rule) => rule.pattern.replaceAll(GLOBAL_KEY, "") !== ""
// );
// return rules;
// },
// set: (url, rules) => setSubRules(url, rules),
// get: (url) => getSubRulesWithDefault(url),
// del: (url) => delSubRules(url),
// };

View File

@@ -10,32 +10,16 @@ import {
DEFAULT_SYNC,
BUILTIN_RULES,
} from "../config";
import { browser, isExt, isGm } from "./client";
// import { APP_NAME } from "../config/app";
import { isExt, isGm } from "./client";
import { browser } from "./browser";
async function set(key, val) {
if (isExt) {
await browser.storage.local.set({ [key]: val });
} else if (isGm) {
// const oldValue = await (window.KISS_GM || GM).getValue(key);
await (window.KISS_GM || GM).setValue(key, val);
// window.dispatchEvent(
// new StorageEvent("storage", {
// key,
// oldValue,
// newValue: val,
// })
// );
} else {
// const oldValue = window.localStorage.getItem(key);
window.localStorage.setItem(key, val);
// window.dispatchEvent(
// new StorageEvent("storage", {
// key,
// oldValue,
// newValue: val,
// })
// );
}
}
@@ -54,25 +38,9 @@ async function del(key) {
if (isExt) {
await browser.storage.local.remove([key]);
} else if (isGm) {
// const oldValue = await (window.KISS_GM || GM).getValue(key);
await (window.KISS_GM || GM).deleteValue(key);
// window.dispatchEvent(
// new StorageEvent("storage", {
// key,
// oldValue,
// newValue: null,
// })
// );
} else {
// const oldValue = window.localStorage.getItem(key);
window.localStorage.removeItem(key);
// window.dispatchEvent(
// new StorageEvent("storage", {
// key,
// oldValue,
// newValue: null,
// })
// );
}
}
@@ -96,18 +64,6 @@ async function putObj(key, obj) {
await setObj(key, { ...cur, ...obj });
}
// /**
// * 监听storage事件
// * @param {*} handleChanged
// */
// function onChanged(handleChanged) {
// if (isExt) {
// browser.storage.onChanged.addListener(handleChanged);
// } else {
// window.addEventListener("storage", handleChanged);
// }
// }
/**
* 对storage的封装
*/

View File

@@ -1,5 +1,12 @@
import { getSyncWithDefault, updateSync } from "./storage";
import { GLOBAL_KEY } from "../config";
import {
getSyncWithDefault,
updateSync,
setSubRules,
getSubRules,
} from "./storage";
import { apiFetchRules } from "../apis";
import { checkRules } from "./rules";
/**
* 同步订阅规则
@@ -7,9 +14,12 @@ import { apiFetchRules } from "../apis";
* @returns
*/
export const syncSubRules = async (url, isBg = false) => {
const rules = await apiFetchRules(url, isBg);
const res = await apiFetchRules(url, isBg);
const rules = checkRules(res).filter(
(rule) => rule.pattern.replaceAll(GLOBAL_KEY, "") !== ""
);
if (rules.length > 0) {
await rulesCache.set(url, rules);
await setSubRules(url, rules);
}
return rules;
};
@@ -54,7 +64,7 @@ export const trySyncAllSubRules = async ({ subrulesList }, isBg = false) => {
* @returns
*/
export const loadOrFetchSubRules = async (url) => {
const rules = await apiFetchRules(url);
const rules = await getSubRules(url);
if (rules?.length) {
return rules;
}

View File

@@ -1,28 +1,20 @@
import {
STOKEY_SYNC,
DEFAULT_SYNC,
KV_SETTING_KEY,
KV_RULES_KEY,
KV_RULES_SHARE_KEY,
STOKEY_SETTING,
STOKEY_RULES,
KV_SALT_SHARE,
} from "../config";
import { storage, getSyncWithDefault, updateSync } from "../libs/storage";
import { getSetting, getRules } from ".";
import {
getSyncWithDefault,
updateSync,
getSettingWithDefault,
getRulesWithDefault,
setSetting,
setRules,
} from "./storage";
import { apiSyncData } from "../apis";
import { sha256 } from "./utils";
// /**
// * 同步相关数据
// */
// export const syncOpt = {
// load: async () => (await storage.getObj(STOKEY_SYNC)) || DEFAULT_SYNC,
// update: async (obj) => {
// await storage.putObj(STOKEY_SYNC, obj);
// },
// };
/**
* 同步设置
* @returns
@@ -33,7 +25,7 @@ const syncSetting = async (isBg = false) => {
return;
}
const setting = await getSetting();
const setting = await getSettingWithDefault();
const res = await apiSyncData(
syncUrl,
syncKey,
@@ -50,7 +42,7 @@ const syncSetting = async (isBg = false) => {
settingUpdateAt: res.updateAt,
settingSyncAt: res.updateAt,
});
await storage.setObj(STOKEY_SETTING, res.value);
await setSetting(res.value);
} else {
await updateSync({ settingSyncAt: res.updateAt });
}
@@ -74,7 +66,7 @@ const syncRules = async (isBg = false) => {
return;
}
const rules = await getRules();
const rules = await getRulesWithDefault();
const res = await apiSyncData(
syncUrl,
syncKey,
@@ -91,7 +83,7 @@ const syncRules = async (isBg = false) => {
rulesUpdateAt: res.updateAt,
rulesSyncAt: res.updateAt,
});
await storage.setObj(STOKEY_RULES, res.value);
await setRules(res.value);
} else {
await updateSync({ rulesSyncAt: res.updateAt });
}

View File

@@ -12,7 +12,6 @@ import {
import Content from "../views/Content";
import { fetchUpdate, fetchClear } from "./fetch";
import { debounce } from "./utils";
import { isExt } from "./client";
/**
* 翻译类
@@ -102,6 +101,10 @@ export class Translator {
}
}
get setting() {
return this._setting;
}
get rule() {
// console.log("get rule", this._rule);
return this._rule;