Ver código fonte

ref: random stuff that I found

Sv443 1 ano atrás
pai
commit
7f459e4554

+ 44 - 50
dist/BetterYTM.user.js

@@ -17,7 +17,7 @@
 // @license           AGPL-3.0-only
 // @author            Sv443
 // @copyright         Sv443 (https://github.com/Sv443)
-// @icon              https://raw.githubusercontent.com/Sv443/BetterYTM/develop/assets/images/logo/logo_48.png?b=75ea0ef
+// @icon              https://raw.githubusercontent.com/Sv443/BetterYTM/develop/assets/images/logo/logo_48.png?b=7ed0cd6
 // @match             https://music.youtube.com/*
 // @match             https://www.youtube.com/*
 // @run-at            document-start
@@ -35,37 +35,37 @@
 // @grant             GM.openInTab
 // @grant             unsafeWindow
 // @noframes
-// @resource          css-anchor_improvements https://raw.githubusercontent.com/Sv443/BetterYTM/develop/assets/style/anchorImprovements.css?b=75ea0ef
-// @resource          css-fix_spacing         https://raw.githubusercontent.com/Sv443/BetterYTM/develop/assets/style/fixSpacing.css?b=75ea0ef
-// @resource          doc-changelog           https://raw.githubusercontent.com/Sv443/BetterYTM/develop/changelog.md?b=75ea0ef
-// @resource          icon-advanced_mode      https://raw.githubusercontent.com/Sv443/BetterYTM/develop/assets/icons/plus_circle_small.svg?b=75ea0ef
-// @resource          icon-arrow_down         https://raw.githubusercontent.com/Sv443/BetterYTM/develop/assets/icons/arrow_down.svg?b=75ea0ef
-// @resource          icon-delete             https://raw.githubusercontent.com/Sv443/BetterYTM/develop/assets/icons/delete.svg?b=75ea0ef
-// @resource          icon-error              https://raw.githubusercontent.com/Sv443/BetterYTM/develop/assets/icons/error.svg?b=75ea0ef
-// @resource          icon-experimental       https://raw.githubusercontent.com/Sv443/BetterYTM/develop/assets/icons/beaker_small.svg?b=75ea0ef
-// @resource          icon-globe              https://raw.githubusercontent.com/Sv443/BetterYTM/develop/assets/icons/globe.svg?b=75ea0ef
-// @resource          icon-help               https://raw.githubusercontent.com/Sv443/BetterYTM/develop/assets/icons/help.svg?b=75ea0ef
-// @resource          icon-image              https://raw.githubusercontent.com/Sv443/BetterYTM/develop/assets/icons/image.svg?b=75ea0ef
-// @resource          icon-image_filled       https://raw.githubusercontent.com/Sv443/BetterYTM/develop/assets/icons/image_filled.svg?b=75ea0ef
-// @resource          icon-link               https://raw.githubusercontent.com/Sv443/BetterYTM/develop/assets/icons/link.svg?b=75ea0ef
-// @resource          icon-lyrics             https://raw.githubusercontent.com/Sv443/BetterYTM/develop/assets/icons/lyrics.svg?b=75ea0ef
-// @resource          icon-skip_to            https://raw.githubusercontent.com/Sv443/BetterYTM/develop/assets/icons/skip_to.svg?b=75ea0ef
-// @resource          icon-spinner            https://raw.githubusercontent.com/Sv443/BetterYTM/develop/assets/icons/spinner.svg?b=75ea0ef
-// @resource          img-logo                https://raw.githubusercontent.com/Sv443/BetterYTM/develop/assets/images/logo/logo_48.png?b=75ea0ef
-// @resource          img-close               https://raw.githubusercontent.com/Sv443/BetterYTM/develop/assets/images/close.png?b=75ea0ef
-// @resource          img-discord             https://raw.githubusercontent.com/Sv443/BetterYTM/develop/assets/images/external/discord.png?b=75ea0ef
-// @resource          img-github              https://raw.githubusercontent.com/Sv443/BetterYTM/develop/assets/images/external/github.png?b=75ea0ef
-// @resource          img-greasyfork          https://raw.githubusercontent.com/Sv443/BetterYTM/develop/assets/images/external/greasyfork.png?b=75ea0ef
-// @resource          img-openuserjs          https://raw.githubusercontent.com/Sv443/BetterYTM/develop/assets/images/external/openuserjs.png?b=75ea0ef
-// @resource          trans-de_DE             https://raw.githubusercontent.com/Sv443/BetterYTM/develop/assets/translations/de_DE.json?b=75ea0ef
-// @resource          trans-en_US             https://raw.githubusercontent.com/Sv443/BetterYTM/develop/assets/translations/en_US.json?b=75ea0ef
-// @resource          trans-en_UK             https://raw.githubusercontent.com/Sv443/BetterYTM/develop/assets/translations/en_UK.json?b=75ea0ef
-// @resource          trans-es_ES             https://raw.githubusercontent.com/Sv443/BetterYTM/develop/assets/translations/es_ES.json?b=75ea0ef
-// @resource          trans-fr_FR             https://raw.githubusercontent.com/Sv443/BetterYTM/develop/assets/translations/fr_FR.json?b=75ea0ef
-// @resource          trans-hi_IN             https://raw.githubusercontent.com/Sv443/BetterYTM/develop/assets/translations/hi_IN.json?b=75ea0ef
-// @resource          trans-ja_JA             https://raw.githubusercontent.com/Sv443/BetterYTM/develop/assets/translations/ja_JA.json?b=75ea0ef
-// @resource          trans-pt_BR             https://raw.githubusercontent.com/Sv443/BetterYTM/develop/assets/translations/pt_BR.json?b=75ea0ef
-// @resource          trans-zh_CN             https://raw.githubusercontent.com/Sv443/BetterYTM/develop/assets/translations/zh_CN.json?b=75ea0ef
+// @resource          css-anchor_improvements https://raw.githubusercontent.com/Sv443/BetterYTM/develop/assets/style/anchorImprovements.css?b=7ed0cd6
+// @resource          css-fix_spacing         https://raw.githubusercontent.com/Sv443/BetterYTM/develop/assets/style/fixSpacing.css?b=7ed0cd6
+// @resource          doc-changelog           https://raw.githubusercontent.com/Sv443/BetterYTM/develop/changelog.md?b=7ed0cd6
+// @resource          icon-advanced_mode      https://raw.githubusercontent.com/Sv443/BetterYTM/develop/assets/icons/plus_circle_small.svg?b=7ed0cd6
+// @resource          icon-arrow_down         https://raw.githubusercontent.com/Sv443/BetterYTM/develop/assets/icons/arrow_down.svg?b=7ed0cd6
+// @resource          icon-delete             https://raw.githubusercontent.com/Sv443/BetterYTM/develop/assets/icons/delete.svg?b=7ed0cd6
+// @resource          icon-error              https://raw.githubusercontent.com/Sv443/BetterYTM/develop/assets/icons/error.svg?b=7ed0cd6
+// @resource          icon-experimental       https://raw.githubusercontent.com/Sv443/BetterYTM/develop/assets/icons/beaker_small.svg?b=7ed0cd6
+// @resource          icon-globe              https://raw.githubusercontent.com/Sv443/BetterYTM/develop/assets/icons/globe.svg?b=7ed0cd6
+// @resource          icon-help               https://raw.githubusercontent.com/Sv443/BetterYTM/develop/assets/icons/help.svg?b=7ed0cd6
+// @resource          icon-image              https://raw.githubusercontent.com/Sv443/BetterYTM/develop/assets/icons/image.svg?b=7ed0cd6
+// @resource          icon-image_filled       https://raw.githubusercontent.com/Sv443/BetterYTM/develop/assets/icons/image_filled.svg?b=7ed0cd6
+// @resource          icon-link               https://raw.githubusercontent.com/Sv443/BetterYTM/develop/assets/icons/link.svg?b=7ed0cd6
+// @resource          icon-lyrics             https://raw.githubusercontent.com/Sv443/BetterYTM/develop/assets/icons/lyrics.svg?b=7ed0cd6
+// @resource          icon-skip_to            https://raw.githubusercontent.com/Sv443/BetterYTM/develop/assets/icons/skip_to.svg?b=7ed0cd6
+// @resource          icon-spinner            https://raw.githubusercontent.com/Sv443/BetterYTM/develop/assets/icons/spinner.svg?b=7ed0cd6
+// @resource          img-logo                https://raw.githubusercontent.com/Sv443/BetterYTM/develop/assets/images/logo/logo_48.png?b=7ed0cd6
+// @resource          img-close               https://raw.githubusercontent.com/Sv443/BetterYTM/develop/assets/images/close.png?b=7ed0cd6
+// @resource          img-discord             https://raw.githubusercontent.com/Sv443/BetterYTM/develop/assets/images/external/discord.png?b=7ed0cd6
+// @resource          img-github              https://raw.githubusercontent.com/Sv443/BetterYTM/develop/assets/images/external/github.png?b=7ed0cd6
+// @resource          img-greasyfork          https://raw.githubusercontent.com/Sv443/BetterYTM/develop/assets/images/external/greasyfork.png?b=7ed0cd6
+// @resource          img-openuserjs          https://raw.githubusercontent.com/Sv443/BetterYTM/develop/assets/images/external/openuserjs.png?b=7ed0cd6
+// @resource          trans-de_DE             https://raw.githubusercontent.com/Sv443/BetterYTM/develop/assets/translations/de_DE.json?b=7ed0cd6
+// @resource          trans-en_US             https://raw.githubusercontent.com/Sv443/BetterYTM/develop/assets/translations/en_US.json?b=7ed0cd6
+// @resource          trans-en_UK             https://raw.githubusercontent.com/Sv443/BetterYTM/develop/assets/translations/en_UK.json?b=7ed0cd6
+// @resource          trans-es_ES             https://raw.githubusercontent.com/Sv443/BetterYTM/develop/assets/translations/es_ES.json?b=7ed0cd6
+// @resource          trans-fr_FR             https://raw.githubusercontent.com/Sv443/BetterYTM/develop/assets/translations/fr_FR.json?b=7ed0cd6
+// @resource          trans-hi_IN             https://raw.githubusercontent.com/Sv443/BetterYTM/develop/assets/translations/hi_IN.json?b=7ed0cd6
+// @resource          trans-ja_JA             https://raw.githubusercontent.com/Sv443/BetterYTM/develop/assets/translations/ja_JA.json?b=7ed0cd6
+// @resource          trans-pt_BR             https://raw.githubusercontent.com/Sv443/BetterYTM/develop/assets/translations/pt_BR.json?b=7ed0cd6
+// @resource          trans-zh_CN             https://raw.githubusercontent.com/Sv443/BetterYTM/develop/assets/translations/zh_CN.json?b=7ed0cd6
 // @require           https://cdn.jsdelivr.net/npm/@sv443-network/[email protected]/dist/index.global.js
 // @require           https://cdn.jsdelivr.net/npm/[email protected]/dist/fuse.basic.js
 // @require           https://cdn.jsdelivr.net/npm/[email protected]/lib/marked.umd.js
@@ -193,7 +193,7 @@ var PluginIntent;
 })(PluginIntent || (PluginIntent = {}));const modeRaw = "development";
 const branchRaw = "develop";
 const hostRaw = "github";
-const buildNumberRaw = "75ea0ef";
+const buildNumberRaw = "7ed0cd6";
 /** The mode in which the script was built (production or development) */
 const mode = (modeRaw.match(/^#{{.+}}$/) ? "production" : modeRaw);
 /** The branch to use in various URLs that point to the GitHub repo */
@@ -206,10 +206,11 @@ const host = (hostRaw.match(/^#{{.+}}$/) ? "github" : hostRaw);
 const buildNumber = (buildNumberRaw.match(/^#{{.+}}$/) ? "BUILD_ERROR!" : buildNumberRaw); // asserted as generic string instead of literal
 /** Default compression format used throughout BYTM */
 const compressionFormat = "deflate-raw";
-typeof sessionStorage !== "undefined"
+/** Whether sessionStorage is available and working */
+typeof (sessionStorage === null || sessionStorage === void 0 ? void 0 : sessionStorage.setItem) !== "undefined"
     && (() => {
         try {
-            const key = `_bytm_${UserUtils.randomId(4)}`;
+            const key = `_bytm_test_${UserUtils.randomId(4)}`;
             sessionStorage.setItem(key, "test");
             sessionStorage.removeItem(key);
             return true;
@@ -4730,25 +4731,18 @@ const migrations = {
     // 5 -> 6 (v1.3)
     // 6: (oldData: FeatureConfig) => 
 };
-/**
- * Uses the passed {@linkcode baseData} as the base if given, and sets all passed feature {@linkcode keys} to their default value
- * @returns Returns a copy of the object
- */
-function useDefaultConfig(keys, baseData) {
-    const newData = Object.assign({}, (baseData !== null && baseData !== void 0 ? baseData : {}));
-    for (const key of keys)
-        newData[key] = getFeatureDefault(key);
-    return newData;
-}
-/** Returns the default value for the given feature key */
-function getFeatureDefault(key) {
-    return featInfo[key].default;
-}
 const defaultData = Object.keys(featInfo)
     .reduce((acc, key) => {
     acc[key] = featInfo[key].default;
     return acc;
 }, {});
+/** Uses the default config as the base, then overwrites all values with the passed {@linkcode baseData}, then sets all passed {@linkcode resetKeys} to their default values */
+function useDefaultConfig(resetKeys, baseData) {
+    const newData = Object.assign(Object.assign({}, defaultData), (baseData !== null && baseData !== void 0 ? baseData : {}));
+    for (const key of resetKeys)
+        newData[key] = featInfo[key].default; // typescript funny moments
+    return newData;
+}
 let canCompress = true;
 const bytmCfgStore = new UserUtils.DataStore({
     id: "bytm-config",
@@ -5280,7 +5274,7 @@ function getSessionId() {
     }
 }
 let isCompressionSupported;
-/** Tests whether compression via the predefined {@linkcode compressionFormat} is supported */
+/** Tests whether compression via the predefined {@linkcode compressionFormat} is supported (only on the first call, then returns the cached result) */
 function compressionSupported() {
     return __awaiter(this, void 0, void 0, function* () {
         if (typeof isCompressionSupported === "boolean")

+ 8 - 16
src/config.ts

@@ -63,28 +63,20 @@ export const migrations: DataMigrationsDict = {
   // 6: (oldData: FeatureConfig) => 
 } as const satisfies DataMigrationsDict;
 
-/**
- * Uses the passed {@linkcode baseData} as the base if given, and sets all passed feature {@linkcode keys} to their default value  
- * @returns Returns a copy of the object
- */
-function useDefaultConfig(keys: (keyof typeof featInfo)[], baseData?: FeatureConfig): Partial<FeatureConfig> {
-  const newData = { ...(baseData ?? {}) };
-  for(const key of keys)
-    newData[key as keyof typeof featInfo] = getFeatureDefault(key as keyof typeof featInfo) as unknown as never;
-  return newData;
-}
-
-/** Returns the default value for the given feature key */
-function getFeatureDefault<TKey extends keyof typeof featInfo>(key: TKey): typeof featInfo[TKey]["default"] {
-  return featInfo[key].default;
-}
-
 export const defaultData = (Object.keys(featInfo) as (keyof typeof featInfo)[])
   .reduce<Partial<FeatureConfig>>((acc, key) => {
     acc[key] = featInfo[key].default as unknown as undefined;
     return acc;
   }, {}) as FeatureConfig;
 
+/** Uses the default config as the base, then overwrites all values with the passed {@linkcode baseData}, then sets all passed {@linkcode resetKeys} to their default values */
+function useDefaultConfig(resetKeys: (keyof typeof featInfo)[], baseData?: FeatureConfig): Partial<FeatureConfig> {
+  const newData = { ...defaultData, ...(baseData ?? {}) };
+  for(const key of resetKeys)
+    newData[key] = featInfo[key].default as never; // typescript funny moments
+  return newData;
+}
+
 let canCompress = true;
 
 const bytmCfgStore = new DataStore({

+ 3 - 2
src/constants.ts

@@ -27,11 +27,12 @@ export const platformNames: Record<typeof host, string> = {
 /** Default compression format used throughout BYTM */
 export const compressionFormat: CompressionFormat = "deflate-raw";
 
+/** Whether sessionStorage is available and working */
 export const sessionStorageAllowed =
-  typeof sessionStorage !== "undefined"
+  typeof sessionStorage?.setItem !== "undefined"
   && (() => {
     try {
-      const key = `_bytm_${randomId(4)}`;
+      const key = `_bytm_test_${randomId(4)}`;
       sessionStorage.setItem(key, "test");
       sessionStorage.removeItem(key);
       return true;

+ 3 - 3
src/tools/post-build.ts

@@ -1,4 +1,4 @@
-import { access, readFile, writeFile, constants as fsconstants } from "node:fs/promises";
+import { access, readFile, writeFile, constants as fsconst } from "node:fs/promises";
 import { dirname, join, relative } from "node:path";
 import { fileURLToPath } from "node:url";
 import { randomUUID } from "node:crypto";
@@ -208,7 +208,7 @@ I welcome every contribution on GitHub!
 /** Replaces tokens in the format `#{{key}}` or `/⋆#{{key}}⋆/` of the `replacements` param with their respective value */
 function insertValues(userscript: string, replacements: Record<string, Stringifiable>) {
   for(const key in replacements)
-    userscript = userscript.replace(new RegExp(`(\\/\\*)?#{{${key}}}(\\*\\/)?`, "gm"), String(replacements[key]));
+    userscript = userscript.replace(new RegExp(`(\\/\\*\\s*)?#{{${key}}}(\\s*\\*\\/)?`, "gm"), String(replacements[key]));
   return userscript;
 }
 
@@ -236,7 +236,7 @@ function getLastCommitSha() {
 
 async function exists(path: string) {
   try {
-    await access(path, fsconstants.R_OK | fsconstants.W_OK);
+    await access(path, fsconst.R_OK | fsconst.W_OK);
     return true;
   }
   catch(err) {

+ 3 - 4
src/tools/run-invisible.mjs

@@ -1,10 +1,11 @@
 import { exec } from "node:child_process";
 
-function runDetached() {
+(() => {
   const command = process.argv.slice(2).join(" ");
   const child = exec(command, (error, _stdout, _stderr) => {
     if(error)
       console.error("\x1b[31m[run-silent error]\x1b[0m", error);
+    // #DEBUG
     // console.log("[run-silent debug] out:", _stdout ?? "undefined", "err:", _stderr ?? "undefined");
   });
   child.on("exit", (code, signal) => {
@@ -13,6 +14,4 @@ function runDetached() {
     if(signal !== null)
       setImmediate(() => process.kill(process.pid, signal));
   });
-}
-
-runDetached();
+})();

+ 1 - 1
src/tools/serve.ts

@@ -1,8 +1,8 @@
 import { resolve } from "node:path";
 import { fileURLToPath } from "node:url";
 import express, { NextFunction, Request, Response } from "express";
-import { outputDir } from "../../rollup.config.mjs";
 import "dotenv/config";
+import { outputDir } from "../../rollup.config.mjs";
 
 const envPort = Number(process.env.DEV_SERVER_PORT);
 

+ 2 - 2
src/tools/tr-progress.ts

@@ -1,9 +1,9 @@
+import { autoPlural, mapRange } from "@sv443-network/userutils";
 import { readFile, writeFile } from "node:fs/promises";
 import { join, relative, resolve } from "node:path";
 import { fileURLToPath } from "node:url";
-import { autoPlural, mapRange } from "@sv443-network/userutils";
-import locales from "../../assets/locales.json" assert { type: "json" };
 import type { TrLocale } from "../utils";
+import locales from "../../assets/locales.json" assert { type: "json" };
 
 const { exit } = process;
 

+ 1 - 1
src/utils/misc.ts

@@ -38,7 +38,7 @@ export function getSessionId(): string | null {
 
 let isCompressionSupported: boolean | undefined;
 
-/** Tests whether compression via the predefined {@linkcode compressionFormat} is supported */
+/** Tests whether compression via the predefined {@linkcode compressionFormat} is supported (only on the first call, then returns the cached result) */
 export async function compressionSupported() {
   if(typeof isCompressionSupported === "boolean")
     return isCompressionSupported;