From 8cb335a9af39c9a40e5b82653da2a3cf52d2ccb0 Mon Sep 17 00:00:00 2001 From: windingwind <33902321+windingwind@users.noreply.github.com> Date: Sun, 10 Sep 2023 16:54:12 +0800 Subject: [PATCH] fix: #110 --- addon/prefs.js | 3 +-- src/utils/rules.ts | 18 +++++++++++------- 2 files changed, 12 insertions(+), 9 deletions(-) diff --git a/addon/prefs.js b/addon/prefs.js index be8f47d..74e6416 100644 --- a/addon/prefs.js +++ b/addon/prefs.js @@ -1,3 +1,2 @@ /* eslint-disable no-undef */ -pref("__prefsPrefix__.enable", true); -pref("__prefsPrefix__.input", "This is input"); +pref("__prefsPrefix__.rulesInit", false); diff --git a/src/utils/rules.ts b/src/utils/rules.ts index 1eb64b2..27c4a32 100644 --- a/src/utils/rules.ts +++ b/src/utils/rules.ts @@ -1,5 +1,6 @@ import { config } from "../../package.json"; import { updateHint } from "./hint"; +import { getPref, setPref } from "./prefs"; const AsyncFunction = Object.getPrototypeOf(async function () {}).constructor; @@ -85,12 +86,15 @@ function initRules() { addon.data.rules.data = new ztoolkit.LargePref( `${config.prefsPrefix}.rules`, `${config.prefsPrefix}.rules.`, - "parser", + "parser" ).asMapLike() as TagRuleMap; - for (const key of defaultRules.keys()) { - if (!Array.from(addon.data.rules.data.keys()).includes(key)) { - addon.data.rules.data.set(key, defaultRules.get(key)!); + if (!getPref("rulesInit")) { + for (const key of defaultRules.keys()) { + if (!Array.from(addon.data.rules.data.keys()).includes(key)) { + addon.data.rules.data.set(key, defaultRules.get(key)!); + } } + setPref("rulesInit", true); } } @@ -122,7 +126,7 @@ async function applyRule(rule: TagRule, data: TagRuleData) { item.removeTag(tag); } message = `Remove tag ${tags.join(",")} from item ${item.getField( - "title", + "title" )}`; break; } @@ -135,7 +139,7 @@ async function applyRule(rule: TagRule, data: TagRuleData) { } } message = `Toggle tag ${tags.join(",")} to item ${item.getField( - "title", + "title" )}`; break; } @@ -144,7 +148,7 @@ async function applyRule(rule: TagRule, data: TagRuleData) { try { const func = new AsyncFunction("item, data, require", script); message = await func(item, data, (module: string) => - ztoolkit.getGlobal(module), + ztoolkit.getGlobal(module) ); } catch (e) { ztoolkit.log("Script Error", e);