소스 검색

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
   // since the config changes so much in development keys need to be fixed in this special way
   if(mode === "development") {
   if(mode === "development") {
-    await cfgDataStore.setData(fixMissingCfgKeys(data));
+    await cfgDataStore.setData(fixCfgKeys(data));
     data = cfgDataStore.getData();
     data = cfgDataStore.getData();
   }
   }
 
 
@@ -150,7 +150,7 @@ export async function initConfig() {
     info("  !- Config data was initialized with default values");
     info("  !- Config data was initialized with default values");
   else if(oldFmtVer !== cfgDataStore.formatVersion) {
   else if(oldFmtVer !== cfgDataStore.formatVersion) {
     try {
     try {
-      await cfgDataStore.setData(data = fixMissingCfgKeys(data));
+      await cfgDataStore.setData(data = fixCfgKeys(data));
       info(`  !- Config data was migrated from version ${oldFmtVer} to ${cfgDataStore.formatVersion}`);
       info(`  !- Config data was migrated from version ${oldFmtVer} to ${cfgDataStore.formatVersion}`);
     }
     }
     catch(err) {
     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.
  * 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 newCfg = { ...cfg };
   const passedKeys = Object.keys(cfg);
   const passedKeys = Object.keys(cfg);
   const defaultKeys = Object.keys(defaultData);
   const defaultKeys = Object.keys(defaultData);
@@ -177,6 +177,11 @@ export function fixMissingCfgKeys(cfg: Partial<FeatureConfig>): FeatureConfig {
     for(const key of missingKeys)
     for(const key of missingKeys)
       newCfg[key as keyof FeatureConfig] = defaultData[key as keyof FeatureConfig] as never;
       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;
   return newCfg as FeatureConfig;
 }
 }
 
 

+ 2 - 2
src/index.ts

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