Ver código fonte

feat: auto-close permanent toasts

Sven 1 ano atrás
pai
commit
2c6d199087
4 arquivos alterados com 57 adições e 3 exclusões
  1. 11 1
      src/features/index.ts
  2. 39 0
      src/features/layout.ts
  3. 5 2
      src/index.ts
  4. 2 0
      src/types.ts

+ 11 - 1
src/features/index.ts

@@ -92,6 +92,16 @@ export const featInfo = {
     category: "layout",
     default: true,
   },
+  closeToastsTimeout: {
+    desc: "After how many seconds to close permanent toasts - 0 to only close them manually (default behavior)",
+    type: "number",
+    category: "layout",
+    min: 0,
+    max: 30,
+    step: 0.5,
+    default: 0,
+    unit: "s",
+  },
 
   //#SECTION lyrics
   geniusLyrics: {
@@ -100,4 +110,4 @@ export const featInfo = {
     category: "lyrics",
     default: true,
   },
-};
+} as const;

+ 39 - 0
src/features/layout.ts

@@ -613,3 +613,42 @@ function improveCarouselAnchors(itemsElement: HTMLElement) {
 
   return improvedElems;
 }
+
+//#MARKER auto close toasts
+
+/** Closes toasts after a set amount of time */
+export function initAutoCloseToasts() {
+  try {
+    const animTimeout = 300;
+    const closeTimeout = Math.max(features.closeToastsTimeout * 1000 + animTimeout, animTimeout);
+
+    onSelector("tp-yt-paper-toast#toast", {
+      all: true,
+      continuous: true,
+      listener: (toastElems) => {
+        for(const toastElem of toastElems) {
+          if(!toastElem.hasAttribute("allow-click-through"))
+            continue;
+
+          if(toastElem.classList.contains("bytm-closing"))
+            continue;
+          toastElem.classList.add("bytm-closing");
+
+          setTimeout(() => {
+            toastElem.classList.remove("paper-toast-open");
+            // wait for the transition to finish
+            setTimeout(() => {
+              toastElem.style.display = "none";
+              log(`Automatically closed toast '${toastElem.querySelector<HTMLDivElement>("#text-container yt-formatted-string")?.innerText}' after ${closeTimeout + animTimeout}ms`);
+            }, animTimeout);
+          }, closeTimeout);
+        }
+      },
+    });
+
+    log("Initialized automatic toast closing");
+  }
+  catch(err) {
+    error("Error in automatic toast closing:", err);
+  }
+}

+ 5 - 2
src/index.ts

@@ -7,7 +7,7 @@ import {
   // layout
   initQueueButtons, addWatermark,
   preInitLayout, removeUpgradeTab,
-  initVolumeFeatures,
+  initVolumeFeatures, initAutoCloseToasts,
   // lyrics
   addMediaCtrlLyricsBtn, geniUrlBase,
   // input
@@ -111,6 +111,9 @@ async function onDomLoad() {
       if(features.anchorImprovements)
         addAnchorImprovements();
 
+      if(features.closeToastsTimeout > 0)
+        initAutoCloseToasts();
+
       initVolumeFeatures();
     }
 
@@ -120,7 +123,7 @@ async function onDomLoad() {
     }
   }
   catch(err) {
-    error("General error while executing feature:", err);
+    error("Feature error:", err);
   }
 }
 

+ 2 - 0
src/types.ts

@@ -42,6 +42,8 @@ export interface FeatureConfig {
   watermarkEnabled: boolean;
   /** Add buttons while hovering over a song in a queue to quickly remove it or open its lyrics */
   queueButtons: boolean;
+  /** After how many seconds to close toasts */
+  closeToastsTimeout: number;
 
   //#SECTION lyrics
   /** Add a button to the media controls to open the current song's lyrics on genius.com in a new tab */