Parcourir la source

feat: preload assets on startup

Sv443 il y a 21 heures
Parent
commit
51c1ad4fd8
3 fichiers modifiés avec 30 ajouts et 3 suppressions
  1. 7 1
      .vscode/schemas/resources.schema.json
  2. 1 0
      assets/resources.json
  3. 22 2
      src/index.ts

+ 7 - 1
.vscode/schemas/resources.schema.json

@@ -1,7 +1,7 @@
 {
   "$schema": "http://json-schema.org/draft-07/schema#",
   "type": "object",
-  "required": ["resources"],
+  "required": ["externalAssetPattern", "preloadAssetPattern", "resources"],
   "description": "Contains all resources (images, fonts, stylesheets, etc.) used by BYTM.",
   "properties": {
     "externalAssetPattern": {
@@ -10,6 +10,12 @@
       "format": "regex",
       "pattern": "^.+$"
     },
+    "preloadAssetPattern": {
+      "type": "string",
+      "description": "Pattern passed to `new RegExp()` that identifies an asset that should be preloaded.",
+      "format": "regex",
+      "pattern": "^.+$"
+    },
     "resources": {
       "type": "object",
       "description": "Mapping of resource file identifiers and paths.",

+ 1 - 0
assets/resources.json

@@ -1,5 +1,6 @@
 {
   "externalAssetPattern": "^(css|doc|font|icon|img|trans)-",
+  "preloadAssetPattern": "^(css|icon|img)-",
   "resources": {
     "css-above_queue_btns": "style/aboveQueueBtns.css",
     "css-above_queue_btns_sticky": "style/aboveQueueBtnsSticky.css",

+ 22 - 2
src/index.ts

@@ -1,4 +1,4 @@
-import { compress, decompress, fetchAdvanced, getUnsafeWindow, isDomLoaded, pauseFor, setInnerHtmlUnsafe, type Stringifiable } from "@sv443-network/userutils";
+import { compress, decompress, fetchAdvanced, getUnsafeWindow, isDomLoaded, pauseFor, preloadImages, setInnerHtmlUnsafe, type Stringifiable } from "@sv443-network/userutils";
 import { addStyle, addStyleFromResource, getResourceUrl, reloadTab, setGlobalCssVars, warn } from "./utils/index.js";
 import { clearConfig, getFeatures, initConfig } from "./config.js";
 import { buildNumber, compressionFormat, defaultLogLevel, mode, scriptInfo } from "./constants.js";
@@ -40,7 +40,8 @@ import {
   // menu:
   addConfigMenuOptionYT, addConfigMenuOptionYTM,
 } from "./features/index.js";
-// import { getAllDataExImDialog } from "./dialogs/allDataExIm.js";
+import resourcesJson from "../assets/resources.json" with { type: "json" };
+import type { ResourceKey } from "./types.js";
 
 //#region cns. watermark
 
@@ -318,6 +319,9 @@ async function onDomLoad() {
     // ensure site adjusts itself to new CSS files
     getUnsafeWindow().dispatchEvent(new Event("resize", { bubbles: true, cancelable: true }));
 
+    // preload icons
+    preloadResources();
+
     emitInterface("bytm:ready");
     info(`Done initializing ${ftInit.length} features after ${Math.floor(Date.now() - initStartTs)}ms`);
 
@@ -341,6 +345,22 @@ async function onDomLoad() {
   }
 }
 
+//#region preload icons
+
+/** Preloads all resources that should be preloaded */
+async function preloadResources() {
+  const preloadAssetRegex = new RegExp(resourcesJson.preloadAssetPattern);
+  const urlPromises = Object.keys(resourcesJson.resources)
+    .filter(k => preloadAssetRegex.test(k))
+    .map(k => getResourceUrl(k as ResourceKey));
+  const urls = await Promise.all(urlPromises);
+  if(urls.length > 0)
+    info("Preloading", urls.length, "resources:", urls);
+  else
+    info("No resources to preload");
+  await preloadImages(urls);
+}
+
 //#region css
 
 /** Inserts the bundled CSS files imported throughout the script into a <style> element in the <head> */