feat: Merge default items when saving rule
This commit is contained in:
@@ -1,3 +1,5 @@
|
|||||||
import { run } from "./common";
|
import { run } from "./common";
|
||||||
|
|
||||||
run();
|
if (document.documentElement && document.documentElement.tagName === "HTML") {
|
||||||
|
run();
|
||||||
|
}
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ import {
|
|||||||
OPT_LANGS_FROM,
|
OPT_LANGS_FROM,
|
||||||
OPT_LANGS_TO,
|
OPT_LANGS_TO,
|
||||||
// OPT_TIMING_ALL,
|
// OPT_TIMING_ALL,
|
||||||
|
DEFAULT_RULE,
|
||||||
GLOBLA_RULE,
|
GLOBLA_RULE,
|
||||||
DEFAULT_API_TYPE,
|
DEFAULT_API_TYPE,
|
||||||
} from "../config";
|
} from "../config";
|
||||||
@@ -222,16 +223,28 @@ export const checkRules = (rules) => {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* 保存或更新rule
|
* 保存或更新rule
|
||||||
* @param {*} newRule
|
* @param {*} curRule
|
||||||
*/
|
*/
|
||||||
export const saveRule = async (newRule) => {
|
export const saveRule = async (curRule) => {
|
||||||
const rules = await getRulesWithDefault();
|
const rules = await getRulesWithDefault();
|
||||||
const rule = rules.find((item) => isMatch(newRule.pattern, item.pattern));
|
|
||||||
if (rule && rule.pattern !== GLOBAL_KEY) {
|
const index = rules.findIndex(
|
||||||
Object.assign(rule, { ...newRule, pattern: rule.pattern });
|
(item) =>
|
||||||
} else {
|
item.pattern !== GLOBAL_KEY && isMatch(curRule.pattern, item.pattern)
|
||||||
rules.unshift(newRule);
|
);
|
||||||
|
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);
|
await setRules(rules);
|
||||||
|
|
||||||
trySyncRules();
|
trySyncRules();
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -317,3 +317,19 @@ export const scheduleIdle = (cb, timeout = 200) => {
|
|||||||
}
|
}
|
||||||
return setTimeout(cb, timeout);
|
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;
|
||||||
|
};
|
||||||
|
|||||||
@@ -28,13 +28,16 @@ import { sendIframeMsg } from "../../libs/iframe";
|
|||||||
import { saveRule } from "../../libs/rules";
|
import { saveRule } from "../../libs/rules";
|
||||||
import { tryClearCaches } from "../../libs";
|
import { tryClearCaches } from "../../libs";
|
||||||
import { kissLog } from "../../libs/log";
|
import { kissLog } from "../../libs/log";
|
||||||
|
import { parseUrlPattern } from "../../libs/utils";
|
||||||
|
|
||||||
// 插件popup没有参数
|
// 插件popup没有参数
|
||||||
// 网页弹框有
|
// 网页弹框有
|
||||||
export default function Popup({ setShowPopup, translator }) {
|
export default function Popup({ setShowPopup, translator }) {
|
||||||
const i18n = useI18n();
|
const i18n = useI18n();
|
||||||
const [rule, setRule] = useState(translator?.rule);
|
const [rule, setRule] = useState(translator?.rule);
|
||||||
const [transApis, setTransApis] = useState(translator?.setting?.transApis || []);
|
const [transApis, setTransApis] = useState(
|
||||||
|
translator?.setting?.transApis || []
|
||||||
|
);
|
||||||
const [commands, setCommands] = useState({});
|
const [commands, setCommands] = useState({});
|
||||||
|
|
||||||
const handleOpenSetting = () => {
|
const handleOpenSetting = () => {
|
||||||
@@ -85,16 +88,24 @@ export default function Popup({ setShowPopup, translator }) {
|
|||||||
|
|
||||||
const handleSaveRule = async () => {
|
const handleSaveRule = async () => {
|
||||||
try {
|
try {
|
||||||
let href = window.location.href;
|
let href = "";
|
||||||
if (!translator) {
|
if (!translator) {
|
||||||
const tab = await getCurTab();
|
const tab = await getCurTab();
|
||||||
href = tab.url;
|
href = tab.url;
|
||||||
}
|
|
||||||
const newRule = { ...rule, pattern: href.split("/")[2] };
|
|
||||||
if (isExt && translator) {
|
|
||||||
sendBgMsg(MSG_SAVE_RULE, newRule);
|
|
||||||
} else {
|
} else {
|
||||||
saveRule(newRule);
|
href = window.location?.href;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!href || typeof href !== "string") {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const pattern = parseUrlPattern(href);
|
||||||
|
const curRule = { ...rule, pattern };
|
||||||
|
if (isExt && translator) {
|
||||||
|
sendBgMsg(MSG_SAVE_RULE, curRule);
|
||||||
|
} else {
|
||||||
|
saveRule(curRule);
|
||||||
}
|
}
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
kissLog("save rule", err);
|
kissLog("save rule", err);
|
||||||
|
|||||||
Reference in New Issue
Block a user