feat: Merge default items when saving rule

This commit is contained in:
Gabe
2025-09-26 12:40:19 +08:00
parent 14f74b76bb
commit 4935abcf33
4 changed files with 57 additions and 15 deletions

View File

@@ -6,6 +6,7 @@ import {
OPT_LANGS_FROM,
OPT_LANGS_TO,
// OPT_TIMING_ALL,
DEFAULT_RULE,
GLOBLA_RULE,
DEFAULT_API_TYPE,
} from "../config";
@@ -222,16 +223,28 @@ export const checkRules = (rules) => {
/**
* 保存或更新rule
* @param {*} newRule
* @param {*} curRule
*/
export const saveRule = async (newRule) => {
export const saveRule = async (curRule) => {
const rules = await getRulesWithDefault();
const rule = rules.find((item) => isMatch(newRule.pattern, item.pattern));
if (rule && rule.pattern !== GLOBAL_KEY) {
Object.assign(rule, { ...newRule, pattern: rule.pattern });
} else {
rules.unshift(newRule);
const index = rules.findIndex(
(item) =>
item.pattern !== GLOBAL_KEY && isMatch(curRule.pattern, item.pattern)
);
if (index !== -1) {
const rule = rules.splice(index, 1)[0];
curRule = { ...rule, ...curRule, pattern: rule.pattern };
}
const newRule = {};
Object.entries(GLOBLA_RULE).forEach(([key, val]) => {
newRule[key] =
!curRule[key] || curRule[key] === val ? DEFAULT_RULE[key] : curRule[key];
});
rules.unshift(newRule);
await setRules(rules);
trySyncRules();
};

View File

@@ -317,3 +317,19 @@ export const scheduleIdle = (cb, timeout = 200) => {
}
return setTimeout(cb, timeout);
};
/**
* 截取url部分
* @param {*} href
* @returns
*/
export const parseUrlPattern = (href) => {
if (href.startsWith("file")) {
const filename = href.substring(href.lastIndexOf("/") + 1);
return filename;
} else if (href.startsWith("http")) {
const url = new URL(href);
return url.host;
}
return href;
};