Files
kiss-translator/src/libs/index.js

113 lines
2.4 KiB
JavaScript
Raw Normal View History

2023-07-20 13:45:41 +08:00
import storage from "./storage";
import {
DEFAULT_SETTING,
STOKEY_SETTING,
STOKEY_RULES,
2023-08-16 22:38:58 +08:00
STOKEY_FAB,
2023-08-08 13:29:15 +08:00
GLOBLA_RULE,
GLOBAL_KEY,
2023-08-20 19:27:29 +08:00
DEFAULT_SUBRULES_LIST,
2023-07-20 13:45:41 +08:00
} from "../config";
2023-08-04 16:48:40 +08:00
import { browser } from "./browser";
2023-08-18 13:16:17 +08:00
import { isMatch } from "./utils";
2023-08-20 19:27:29 +08:00
import { tryLoadRules } from "./rules";
2023-07-20 13:45:41 +08:00
/**
* 获取节点列表并转为数组
* @param {*} selector
* @param {*} el
* @returns
*/
export const queryEls = (selector, el = document) =>
Array.from(el.querySelectorAll(selector));
/**
* 查询storage中的设置
* @returns
*/
export const getSetting = async () => ({
...DEFAULT_SETTING,
...((await storage.getObj(STOKEY_SETTING)) || {}),
});
/**
* 查询规则列表
* @returns
*/
export const getRules = async () => (await storage.getObj(STOKEY_RULES)) || [];
2023-08-16 22:38:58 +08:00
/**
* 查询fab位置信息
* @returns
*/
export const getFab = async () => (await storage.getObj(STOKEY_FAB)) || {};
/**
* 设置fab位置信息
* @returns
*/
export const setFab = async (obj) => await storage.setObj(STOKEY_FAB, obj);
2023-07-20 13:45:41 +08:00
/**
* 根据href匹配规则
* @param {*} rules
* @param {string} href
* @returns
*/
2023-08-20 19:27:29 +08:00
export const matchRule = async (
rules,
href,
{ injectRules, subrulesList = DEFAULT_SUBRULES_LIST }
) => {
2023-08-17 15:55:44 +08:00
if (injectRules) {
2023-08-20 19:27:29 +08:00
try {
const selectedSub = subrulesList.find((item) => item.selected);
if (selectedSub?.url) {
const subRules = await tryLoadRules(selectedSub.url);
rules.splice(-1, 0, ...subRules);
}
} catch (err) {
console.log("[load injectRules]", err);
}
2023-08-17 15:55:44 +08:00
}
2023-08-08 13:29:15 +08:00
const rule = rules.find((rule) =>
2023-08-18 13:16:17 +08:00
rule.pattern.split(",").some((p) => isMatch(href, p.trim()))
2023-08-08 13:29:15 +08:00
);
2023-08-08 18:16:00 +08:00
const globalRule =
rules.find((rule) =>
rule.pattern.split(",").some((p) => p.trim() === "*")
) || GLOBLA_RULE;
2023-08-08 13:29:15 +08:00
if (!rule) {
2023-08-08 18:16:00 +08:00
return globalRule;
2023-08-08 13:29:15 +08:00
}
2023-08-08 18:16:00 +08:00
rule.selector =
rule?.selector?.trim() ||
globalRule?.selector?.trim() ||
GLOBLA_RULE.selector;
2023-08-08 13:29:15 +08:00
2023-08-08 18:16:00 +08:00
rule.bgColor = rule?.bgColor?.trim() || globalRule?.bgColor?.trim();
2023-08-08 16:41:47 +08:00
2023-08-08 13:29:15 +08:00
["translator", "fromLang", "toLang", "textStyle", "transOpen"].forEach(
(key) => {
if (rule[key] === GLOBAL_KEY) {
2023-08-08 18:16:00 +08:00
rule[key] = globalRule[key];
2023-08-08 13:29:15 +08:00
}
}
);
return rule;
};
2023-07-20 13:45:41 +08:00
/**
* 本地语言识别
* @param {*} q
* @returns
*/
export const detectLang = async (q) => {
const res = await browser?.i18n.detectLanguage(q);
return res?.languages?.[0]?.language;
};