diff --git a/src/config/i18n.js b/src/config/i18n.js
index a54b71a..2f8c939 100644
--- a/src/config/i18n.js
+++ b/src/config/i18n.js
@@ -537,4 +537,8 @@ export const I18N = {
zh: `隐藏`,
en: `"Hide`,
},
+ save_rule: {
+ zh: `保存规则`,
+ en: `"Save Rule`,
+ },
};
diff --git a/src/libs/msg.js b/src/libs/msg.js
index eb055c0..46dba20 100644
--- a/src/libs/msg.js
+++ b/src/libs/msg.js
@@ -19,3 +19,12 @@ export const sendTabMsg = async (action, args) => {
const tabs = await browser.tabs.query({ active: true, currentWindow: true });
return browser.tabs.sendMessage(tabs[0].id, { action, args });
};
+
+/**
+ * 获取当前tab信息
+ * @returns
+ */
+export const getTabInfo = async () => {
+ const tabs = await browser.tabs.query({ active: true, currentWindow: true });
+ return tabs[0];
+};
diff --git a/src/libs/rules.js b/src/libs/rules.js
index 0bf58b2..b78bb49 100644
--- a/src/libs/rules.js
+++ b/src/libs/rules.js
@@ -11,6 +11,8 @@ import {
DEFAULT_OW_RULE,
} from "../config";
import { loadOrFetchSubRules } from "./subRules";
+import { getRulesWithDefault, setRules } from "./storage";
+import { trySyncRules } from "./sync";
/**
* 根据href匹配规则
@@ -134,3 +136,19 @@ export const checkRules = (rules) => {
return rules;
};
+
+/**
+ * 保存或更新rule
+ * @param {*} newRule
+ */
+export const saveRule = async (newRule) => {
+ const rules = await getRulesWithDefault();
+ const rule = rules.find((item) => isMatch(item.pattern, newRule.pattern));
+ if (rule) {
+ Object.assign(rule, { ...newRule, pattern: rule.pattern });
+ } else {
+ rules.unshift(newRule);
+ }
+ await setRules(rules);
+ trySyncRules();
+};
diff --git a/src/views/Popup/index.js b/src/views/Popup/index.js
index 870c5ff..2bc1841 100644
--- a/src/views/Popup/index.js
+++ b/src/views/Popup/index.js
@@ -5,7 +5,7 @@ import MenuItem from "@mui/material/MenuItem";
import FormControlLabel from "@mui/material/FormControlLabel";
import Switch from "@mui/material/Switch";
import Button from "@mui/material/Button";
-import { sendTabMsg } from "../../libs/msg";
+import { sendTabMsg, getTabInfo } from "../../libs/msg";
import { browser } from "../../libs/browser";
import { isExt } from "../../libs/client";
import { useI18n } from "../../hooks/I18n";
@@ -24,6 +24,7 @@ import {
CACHE_NAME,
} from "../../config";
import { sendIframeMsg } from "../../libs/iframe";
+import { saveRule } from "../../libs/rules";
export default function Popup({ setShowPopup, translator: tran }) {
const i18n = useI18n();
@@ -77,6 +78,21 @@ export default function Popup({ setShowPopup, translator: tran }) {
}
};
+ const handleSaveRule = async () => {
+ try {
+ let pattern = window.location.host;
+ if (isExt) {
+ const tab = await getTabInfo();
+ const url = new URL(tab.url);
+ pattern = url.host;
+ }
+
+ saveRule({ ...rule, pattern });
+ } catch (err) {
+ console.log("[save rule]", err);
+ }
+ };
+
useEffect(() => {
if (!isExt) {
return;
@@ -218,9 +234,19 @@ export default function Popup({ setShowPopup, translator: tran }) {
/>
)}
-
+
+
+
+
);