瀏覽代碼

fix: made "fix missing cfg keys" function also delete extra keys

Sv443 10 月之前
父節點
當前提交
4d2b57c3c7
共有 2 個文件被更改,包括 11 次插入6 次删除
  1. 9 4
      src/config.ts
  2. 2 2
      src/index.ts

+ 9 - 4
src/config.ts

@@ -141,7 +141,7 @@ export async function initConfig() {
 
   // since the config changes so much in development keys need to be fixed in this special way
   if(mode === "development") {
-    await cfgDataStore.setData(fixMissingCfgKeys(data));
+    await cfgDataStore.setData(fixCfgKeys(data));
     data = cfgDataStore.getData();
   }
 
@@ -150,7 +150,7 @@ export async function initConfig() {
     info("  !- Config data was initialized with default values");
   else if(oldFmtVer !== cfgDataStore.formatVersion) {
     try {
-      await cfgDataStore.setData(data = fixMissingCfgKeys(data));
+      await cfgDataStore.setData(data = fixCfgKeys(data));
       info(`  !- Config data was migrated from version ${oldFmtVer} to ${cfgDataStore.formatVersion}`);
     }
     catch(err) {
@@ -165,10 +165,10 @@ export async function initConfig() {
 }
 
 /**
- * Fixes missing keys in the passed config object with their default values and returns a copy of the fixed object.  
+ * Fixes missing keys in the passed config object with their default values or removes extraneous keys and returns a copy of the fixed object.  
  * Returns a copy of the originally passed object if nothing needs to be fixed.
  */
-export function fixMissingCfgKeys(cfg: Partial<FeatureConfig>): FeatureConfig {
+export function fixCfgKeys(cfg: Partial<FeatureConfig>): FeatureConfig {
   const newCfg = { ...cfg };
   const passedKeys = Object.keys(cfg);
   const defaultKeys = Object.keys(defaultData);
@@ -177,6 +177,11 @@ export function fixMissingCfgKeys(cfg: Partial<FeatureConfig>): FeatureConfig {
     for(const key of missingKeys)
       newCfg[key as keyof FeatureConfig] = defaultData[key as keyof FeatureConfig] as never;
   }
+  const extraKeys = passedKeys.filter(k => !defaultKeys.includes(k));
+  if(extraKeys.length > 0) {
+    for(const key of extraKeys)
+      delete newCfg[key as keyof FeatureConfig];
+  }
   return newCfg as FeatureConfig;
 }
 

+ 2 - 2
src/index.ts

@@ -1,6 +1,6 @@
 import { compress, decompress, pauseFor, type Stringifiable } from "@sv443-network/userutils";
 import { addStyleFromResource, domLoaded, warn } from "./utils/index.js";
-import { clearConfig, fixMissingCfgKeys, getFeatures, initConfig, setFeatures } from "./config.js";
+import { clearConfig, fixCfgKeys, getFeatures, initConfig, setFeatures } from "./config.js";
 import { buildNumber, compressionFormat, defaultLogLevel, mode, scriptInfo } from "./constants.js";
 import { error, getDomain, info, getSessionId, log, setLogLevel, initTranslations, setLocale } from "./utils/index.js";
 import { initSiteEvents } from "./siteEvents.js";
@@ -301,7 +301,7 @@ function registerDevMenuCommands() {
 
   GM.registerMenuCommand("Fix missing config values", async () => {
     const oldFeats = JSON.parse(JSON.stringify(getFeatures())) as FeatureConfig;
-    await setFeatures(fixMissingCfgKeys(oldFeats));
+    await setFeatures(fixCfgKeys(oldFeats));
     console.log("Fixed missing config values.\nFrom:", oldFeats, "\n\nTo:", getFeatures());
     if(confirm("All missing or invalid config values were set to their default values.\nReload the page now?"))
       location.reload();