瀏覽代碼

feat: limit getFeatures access

Sv443 1 年之前
父節點
當前提交
9ad8a479bb
共有 5 個文件被更改,包括 27 次插入2 次删除
  1. 1 0
      contributing.md
  2. 1 0
      src/features/index.ts
  3. 15 1
      src/interface.ts
  4. 8 1
      src/menu/menu_old.ts
  5. 2 0
      src/types.ts

+ 1 - 0
contributing.md

@@ -553,6 +553,7 @@ The usage and example blocks on each are written in TypeScript but can be used i
 > Description:  
 > Returns the current feature configuration object synchronously from memory.  
 > To see the structure of the object, check out the type `FeatureConfig` in the file [`src/types.ts`](src/types.ts)  
+> If features are set to be hidden using `valueHidden: true`, their value will always be `undefined` in the returned object.  
 >   
 > <details><summary><b>Example <i>(click to expand)</i></b></summary>
 > 

+ 1 - 0
src/features/index.ts

@@ -267,6 +267,7 @@ export const featInfo = {
   },
   geniUrlToken: {
     type: "text",
+    valueHidden: true,
     category: "lyrics",
     default: "",
     normalize: (val: string) => val.trim(),

+ 15 - 1
src/interface.ts

@@ -3,8 +3,10 @@ import { mode, branch, host, buildNumber, compressionFormat, scriptInfo } from "
 import { getResourceUrl, getSessionId, getVideoTime, log, setLocale, getLocale, hasKey, hasKeyFor, t, tp, type TrLocale } from "./utils";
 import { addSelectorListener } from "./observers";
 import { getFeatures, saveFeatures } from "./config";
+import { featInfo } from "./features";
 import { fetchLyricsUrlTop, getLyricsCacheEntry, sanitizeArtists, sanitizeSong } from "./features/lyrics";
 import type { SiteEventsMap } from "./siteEvents";
+import type { FeatureConfig, FeatureInfo } from "./types";
 
 const { getUnsafeWindow } = UserUtils;
 
@@ -37,7 +39,7 @@ const globalFuncs = {
   hasKeyFor,
   t,
   tp,
-  getFeatures,
+  getFeatures: getFeaturesInterface,
   saveFeatures,
   fetchLyricsUrlTop,
   getLyricsCacheEntry,
@@ -89,3 +91,15 @@ export function emitInterface<
 ) {
   getUnsafeWindow().dispatchEvent(new CustomEvent(type, { detail: data[0] }));
 }
+
+//#MARKER proxy functions
+
+function getFeaturesInterface() {
+  const features = getFeatures();
+  for(const [key] of Object.entries(features)) {
+    const info = featInfo[key as keyof typeof featInfo] as FeatureInfo[keyof FeatureInfo];
+    if(info && info.valueHidden) // @ts-ignore
+      features[key as keyof typeof features] = undefined;
+  }
+  return features as FeatureConfig;
+}

+ 8 - 1
src/menu/menu_old.ts

@@ -398,12 +398,15 @@ async function addCfgMenu() {
           if(typeof ftInfo.min !== "undefined")// @ts-ignore
             inputElem.min = ftInfo.min;
           // @ts-ignore
-          if(ftInfo.max !== "undefined") // @ts-ignore
+          if(typeof ftInfo.max !== "undefined") // @ts-ignore
             inputElem.max = ftInfo.max;
 
           if(typeof initialVal !== "undefined")
             inputElem.value = String(initialVal);
 
+          if(type === "text" && ftInfo.valueHidden)
+            inputElem.value = String(initialVal).replace(/./g, "•");
+
           if(type === "number" || type === "slider" && step)
             inputElem.step = String(step);
 
@@ -458,6 +461,10 @@ async function addCfgMenu() {
               if(typeof initialVal !== "undefined")
                 confChanged(featKey as keyof FeatureConfig, initialVal, v);
             };
+            const unsub = siteEvents.on("cfgMenuClosed", () => {
+              unsub();
+              textInputUpdate();
+            });
             inputElem.addEventListener("blur", () => textInputUpdate());
             inputElem.addEventListener("keydown", (e) => e.key === "Tab" && textInputUpdate());
           }

+ 2 - 0
src/types.ts

@@ -189,6 +189,8 @@ export type FeatureInfo = Record<
      * Specifying a function is useful for pluralizing or inserting values into the translation at runtime
      */
     helpText?: string | (() => string);
+    /** Whether the value should be hidden in the config menu and from plugins */
+    valueHidden?: boolean;
     /**
      * HTML string that is appended to the end of a feature's text description
      * @deprecated TODO:FIXME: To be removed or changed in the big menu rework