2023-08-30 18:05:37 +08:00
|
|
|
import { matchValue, type, isMatch } from "./utils";
|
2023-08-20 19:27:29 +08:00
|
|
|
import {
|
|
|
|
|
GLOBAL_KEY,
|
|
|
|
|
OPT_TRANS_ALL,
|
|
|
|
|
OPT_STYLE_ALL,
|
|
|
|
|
OPT_LANGS_FROM,
|
|
|
|
|
OPT_LANGS_TO,
|
2023-08-30 18:05:37 +08:00
|
|
|
GLOBLA_RULE,
|
|
|
|
|
DEFAULT_SUBRULES_LIST,
|
2023-08-20 19:27:29 +08:00
|
|
|
} from "../config";
|
2023-08-31 00:18:57 +08:00
|
|
|
import { loadOrFetchSubRules } from "./subRules";
|
2023-08-30 18:05:37 +08:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 根据href匹配规则
|
|
|
|
|
* @param {*} rules
|
|
|
|
|
* @param {string} href
|
|
|
|
|
* @returns
|
|
|
|
|
*/
|
|
|
|
|
export const matchRule = async (
|
|
|
|
|
rules,
|
|
|
|
|
href,
|
|
|
|
|
{ injectRules = true, subrulesList = DEFAULT_SUBRULES_LIST }
|
|
|
|
|
) => {
|
|
|
|
|
rules = [...rules];
|
|
|
|
|
if (injectRules) {
|
|
|
|
|
try {
|
|
|
|
|
const selectedSub = subrulesList.find((item) => item.selected);
|
|
|
|
|
if (selectedSub?.url) {
|
2023-08-31 00:18:57 +08:00
|
|
|
const subRules = await loadOrFetchSubRules(selectedSub.url);
|
2023-08-30 18:05:37 +08:00
|
|
|
rules.splice(-1, 0, ...subRules);
|
|
|
|
|
}
|
|
|
|
|
} catch (err) {
|
|
|
|
|
console.log("[load injectRules]", err);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const rule = rules.find((r) =>
|
|
|
|
|
r.pattern.split(",").some((p) => isMatch(href, p.trim()))
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
const globalRule =
|
|
|
|
|
rules.find((r) => r.pattern.split(",").some((p) => p.trim() === "*")) ||
|
|
|
|
|
GLOBLA_RULE;
|
|
|
|
|
|
|
|
|
|
if (!rule) {
|
|
|
|
|
return globalRule;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
rule.selector =
|
|
|
|
|
rule?.selector?.trim() ||
|
|
|
|
|
globalRule?.selector?.trim() ||
|
|
|
|
|
GLOBLA_RULE.selector;
|
|
|
|
|
|
|
|
|
|
rule.bgColor = rule?.bgColor?.trim() || globalRule?.bgColor?.trim();
|
2023-09-01 15:57:05 +08:00
|
|
|
rule.textDiyStyle = rule?.textDiyStyle?.trim() || globalRule?.textDiyStyle?.trim();
|
2023-08-30 18:05:37 +08:00
|
|
|
|
|
|
|
|
["translator", "fromLang", "toLang", "textStyle", "transOpen"].forEach(
|
|
|
|
|
(key) => {
|
|
|
|
|
if (rule[key] === GLOBAL_KEY) {
|
|
|
|
|
rule[key] = globalRule[key];
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
return rule;
|
|
|
|
|
};
|
2023-08-20 19:27:29 +08:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 检查过滤rules
|
|
|
|
|
* @param {*} rules
|
|
|
|
|
* @returns
|
|
|
|
|
*/
|
|
|
|
|
export const checkRules = (rules) => {
|
|
|
|
|
if (type(rules) === "string") {
|
|
|
|
|
rules = JSON.parse(rules);
|
|
|
|
|
}
|
|
|
|
|
if (type(rules) !== "array") {
|
|
|
|
|
throw new Error("data error");
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-30 18:05:37 +08:00
|
|
|
const fromLangs = OPT_LANGS_FROM.map((item) => item[0]);
|
|
|
|
|
const toLangs = OPT_LANGS_TO.map((item) => item[0]);
|
2023-08-20 19:27:29 +08:00
|
|
|
const patternSet = new Set();
|
|
|
|
|
rules = rules
|
|
|
|
|
.filter((rule) => type(rule) === "object")
|
|
|
|
|
.filter(({ pattern }) => {
|
|
|
|
|
if (type(pattern) !== "string" || patternSet.has(pattern.trim())) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
patternSet.add(pattern.trim());
|
|
|
|
|
return true;
|
|
|
|
|
})
|
|
|
|
|
.map(
|
|
|
|
|
({
|
|
|
|
|
pattern,
|
|
|
|
|
selector,
|
|
|
|
|
translator,
|
|
|
|
|
fromLang,
|
|
|
|
|
toLang,
|
|
|
|
|
textStyle,
|
|
|
|
|
transOpen,
|
|
|
|
|
bgColor,
|
2023-09-01 15:57:05 +08:00
|
|
|
textDiyStyle,
|
2023-08-20 19:27:29 +08:00
|
|
|
}) => ({
|
|
|
|
|
pattern: pattern.trim(),
|
|
|
|
|
selector: type(selector) === "string" ? selector : "",
|
|
|
|
|
bgColor: type(bgColor) === "string" ? bgColor : "",
|
2023-09-01 15:57:05 +08:00
|
|
|
textDiyStyle: type(textDiyStyle) === "string" ? textDiyStyle : "",
|
2023-08-20 19:27:29 +08:00
|
|
|
translator: matchValue([GLOBAL_KEY, ...OPT_TRANS_ALL], translator),
|
|
|
|
|
fromLang: matchValue([GLOBAL_KEY, ...fromLangs], fromLang),
|
|
|
|
|
toLang: matchValue([GLOBAL_KEY, ...toLangs], toLang),
|
|
|
|
|
textStyle: matchValue([GLOBAL_KEY, ...OPT_STYLE_ALL], textStyle),
|
|
|
|
|
transOpen: matchValue([GLOBAL_KEY, "true", "false"], transOpen),
|
|
|
|
|
})
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
return rules;
|
|
|
|
|
};
|