Ver código fonte

feat: more plugin stuff

Sv443 11 meses atrás
pai
commit
e579cb85e2
3 arquivos alterados com 24 adições e 14 exclusões
  1. 7 2
      src/config.ts
  2. 6 6
      src/features/lyrics.ts
  3. 11 6
      src/interface.ts

+ 7 - 2
src/config.ts

@@ -3,7 +3,7 @@ import { featInfo } from "./features/index";
 import { compressionSupported, info, log } from "./utils";
 import { emitSiteEvent } from "./siteEvents";
 import { compressionFormat } from "./constants";
-import type { FeatureConfig } from "./types";
+import type { FeatureConfig, FeatureKey } from "./types";
 import { emitInterface, getFeaturesInterface } from "./interface";
 
 /** If this number is incremented, the features object data will be migrated to the new format */
@@ -106,10 +106,15 @@ export async function initConfig() {
 }
 
 /** Returns the current feature config from the in-memory cache as a copy */
-export function getFeatures() {
+export function getFeatures(): FeatureConfig {
   return bytmCfgStore.getData();
 }
 
+/** Returns the value of the feature with the given key from the in-memory cache, as a copy */
+export function getFeature<TKey extends FeatureKey>(key: TKey): FeatureConfig[TKey] {
+  return bytmCfgStore.getData()[key];
+}
+
 /** Saves the feature config synchronously to the in-memory cache and asynchronously to the persistent storage */
 export function setFeatures(featureConf: FeatureConfig) {
   const res = bytmCfgStore.setData(featureConf);

+ 6 - 6
src/features/lyrics.ts

@@ -3,7 +3,7 @@ import Fuse from "fuse.js";
 import { constructUrlString, error, getResourceUrl, info, log, warn, t, tp, currentMediaType } from "../utils";
 import { emitInterface } from "../interface";
 import { mode, scriptInfo } from "../constants";
-import { getFeatures } from "../config";
+import { getFeature } from "../config";
 import { addLyricsCacheEntryBest, addLyricsCacheEntryPenalized, getLyricsCacheEntry } from "./lyricsCache";
 import type { LyricsCacheEntry } from "../types";
 import { addSelectorListener } from "src/observers";
@@ -221,7 +221,7 @@ export async function fetchLyricsUrls(artist: string, song: string): Promise<Omi
     }
 
     const startTs = Date.now();
-    const fetchUrl = constructUrlString(`${getFeatures().geniUrlBase}/search`, {
+    const fetchUrl = constructUrlString(`${getFeature("geniUrlBase")}/search`, {
       disableFuzzy: null,
       utm_source: scriptInfo.name,
       utm_content: `v${scriptInfo.version}${mode === "development" ? "-dev" : ""}`,
@@ -231,11 +231,11 @@ export async function fetchLyricsUrls(artist: string, song: string): Promise<Omi
 
     log(`Requesting URLs from geniURL at '${fetchUrl}'`);
 
-    const { geniUrlToken } = getFeatures();
+    const token = getFeature("geniUrlToken");
     const fetchRes = await fetchAdvanced(fetchUrl, {
-      ...(geniUrlToken ? {
+      ...(token ? {
         headers: {
-          Authorization: `Bearer ${geniUrlToken}`,
+          Authorization: `Bearer ${token}`,
         },
       } : {}),
     });
@@ -285,7 +285,7 @@ export async function fetchLyricsUrls(artist: string, song: string): Promise<Omi
         url,
       }));
 
-    if(!getFeatures().advancedLyricsFilter) {
+    if(!getFeature("advancedLyricsFilter")) {
       const topRes = allResultsSan[0];
       topRes && addLyricsCacheEntryBest(topRes.meta.artists, topRes.meta.title, topRes.url);
 

+ 11 - 6
src/interface.ts

@@ -6,7 +6,7 @@ import { addSelectorListener } from "./observers";
 import { getFeatures, setFeatures } from "./config";
 import { compareVersionArrays, compareVersions, featInfo, fetchLyricsUrlTop, getLyricsCacheEntry, sanitizeArtists, sanitizeSong, type LyricsCache } from "./features";
 import { allSiteEvents, siteEvents, type SiteEventsMap } from "./siteEvents";
-import { LogLevel, type FeatureConfig, type FeatureInfo, type LyricsCacheEntry, type PluginDef, type PluginInfo, type PluginRegisterResult, type PluginDefResolvable, type PluginEventMap, type PluginItem } from "./types";
+import { LogLevel, type FeatureConfig, type FeatureInfo, type LyricsCacheEntry, type PluginDef, type PluginInfo, type PluginRegisterResult, type PluginDefResolvable, type PluginEventMap, type PluginItem, type BytmObject } from "./types";
 import { BytmDialog, createHotkeyInput, createToggleInput } from "./components";
 
 const { getUnsafeWindow } = UserUtils;
@@ -112,7 +112,7 @@ export function setGlobalProp<
   const win = getUnsafeWindow();
 
   if(typeof win.BYTM !== "object")
-    win.BYTM = {} as typeof window.BYTM;
+    win.BYTM = {} as BytmObject;
 
   win.BYTM[key] = value;
 }
@@ -141,9 +141,14 @@ export function initPlugins() {
   // TODO(v1.3): check perms and ask user for initial activation
 
   for(const [key, { def, events }] of pluginQueue) {
-    pluginMap.set(key, { def, events });
-    pluginQueue.delete(key);
-    emitOnPlugins("pluginRegistered", (d) => sameDef(d, def), pluginDefToInfo(def)!);
+    try {
+      pluginMap.set(key, { def, events });
+      pluginQueue.delete(key);
+      emitOnPlugins("pluginRegistered", (d) => sameDef(d, def), pluginDefToInfo(def)!);
+    }
+    catch(err) {
+      error(`Failed to initialize plugin '${getPluginKey(def)}':`, err);
+    }
   }
 
   for(const evt of allSiteEvents) // @ts-ignore
@@ -166,7 +171,7 @@ function pluginDefToInfo(plugin?: PluginDef): PluginInfo | undefined {
   };
 }
 
-/** Checks whether two plugin definitions are the same */
+/** Checks whether two plugins are the same, given their resolvable definition objects */
 function sameDef(def1: PluginDefResolvable, def2: PluginDefResolvable) {
   return getPluginKey(def1) === getPluginKey(def2);
 }