Browse Source

fix: restore video time edge cases

Sven 10 months ago
parent
commit
52e8f88d6e
4 changed files with 11 additions and 12 deletions
  1. 0 1
      README-summary.md
  2. 0 1
      README.md
  3. 5 4
      src/constants.ts
  4. 6 6
      src/features/behavior.ts

+ 0 - 1
README-summary.md

@@ -25,7 +25,6 @@ All of these can be toggled and configured in the configuration menu.
   - Quickly scroll to the currently active song in the queue by clicking a button
   - Quickly scroll to the currently active song in the queue by clicking a button
   - Remove the tracking parameter from URLs in the share menu
   - Remove the tracking parameter from URLs in the share menu
   - Automatically close permanent notifications
   - Automatically close permanent notifications
-  - Remove the premium tab in the sidebar
 - Input / Interaction:
 - Input / Interaction:
   - Use arrow keys to skip forward or backward by a configurable amount of time
   - Use arrow keys to skip forward or backward by a configurable amount of time
   - Press number keys to skip to a percentage of the currently playing song
   - Press number keys to skip to a percentage of the currently playing song

+ 0 - 1
README.md

@@ -27,7 +27,6 @@ All of these can be toggled and configured in the configuration menu.
   - Quickly scroll to the currently active song in the queue by clicking a button
   - Quickly scroll to the currently active song in the queue by clicking a button
   - Remove the tracking parameter from URLs in the share menu
   - Remove the tracking parameter from URLs in the share menu
   - Automatically close permanent notifications
   - Automatically close permanent notifications
-  - Remove the premium tab in the sidebar
 - Input / Interaction:
 - Input / Interaction:
   - Use arrow keys to skip forward or backward by a configurable amount of time
   - Use arrow keys to skip forward or backward by a configurable amount of time
   - Press number keys to skip to a percentage of the currently playing song
   - Press number keys to skip to a percentage of the currently playing song

+ 5 - 4
src/constants.ts

@@ -1,6 +1,7 @@
 import { randomId } from "@sv443-network/userutils";
 import { randomId } from "@sv443-network/userutils";
 import { LogLevel } from "./types.js";
 import { LogLevel } from "./types.js";
 
 
+// these variables will have their values replaced by the build script:
 const modeRaw = "#{{MODE}}";
 const modeRaw = "#{{MODE}}";
 const branchRaw = "#{{BRANCH}}";
 const branchRaw = "#{{BRANCH}}";
 const hostRaw = "#{{HOST}}";
 const hostRaw = "#{{HOST}}";
@@ -17,7 +18,7 @@ export const host = (hostRaw.match(/^#{{.+}}$/) ? "github" : hostRaw) as "github
 /** The build number of the userscript */
 /** The build number of the userscript */
 export const buildNumber = (buildNumberRaw.match(/^#{{.+}}$/) ? "BUILD_ERROR!" : buildNumberRaw) as string; // asserted as generic string instead of literal
 export const buildNumber = (buildNumberRaw.match(/^#{{.+}}$/) ? "BUILD_ERROR!" : buildNumberRaw) as string; // asserted as generic string instead of literal
 
 
-/** URL search parameters at the earliest possible time */
+/** The URL search parameters at the earliest possible time */
 export const initialParams = new URL(location.href).searchParams;
 export const initialParams = new URL(location.href).searchParams;
 
 
 /** Names of platforms by value of {@linkcode host} */
 /** Names of platforms by value of {@linkcode host} */
@@ -32,10 +33,10 @@ export const compressionFormat: CompressionFormat = "deflate-raw";
 
 
 /** Whether sessionStorage is available and working */
 /** Whether sessionStorage is available and working */
 export const sessionStorageAllowed =
 export const sessionStorageAllowed =
-  typeof sessionStorage?.setItem !== "undefined"
+  typeof sessionStorage?.setItem === "function"
   && (() => {
   && (() => {
     try {
     try {
-      const key = `_bytm_test_${randomId(4)}`;
+      const key = `_bytm_test_${randomId(4, 36)}`;
       sessionStorage.setItem(key, "test");
       sessionStorage.setItem(key, "test");
       sessionStorage.removeItem(key);
       sessionStorage.removeItem(key);
       return true;
       return true;
@@ -46,7 +47,7 @@ export const sessionStorageAllowed =
   })();
   })();
 
 
 /**
 /**
- * How much info should be logged to the devtools console  
+ * Fallback and initial value of how much info should be logged to the devtools console  
  * 0 = Debug (show everything) or 1 = Info (show only important stuff)
  * 0 = Debug (show everything) or 1 = Info (show only important stuff)
  */
  */
 export const defaultLogLevel: LogLevel = mode === "production" ? LogLevel.Info : LogLevel.Debug;
 export const defaultLogLevel: LogLevel = mode === "production" ? LogLevel.Info : LogLevel.Debug;

+ 6 - 6
src/features/behavior.ts

@@ -97,15 +97,15 @@ export async function initRememberSongTime() {
   try {
   try {
     remVidsCache = JSON.parse(String(storedDataRaw ?? "[]")) as RemVidObj[];
     remVidsCache = JSON.parse(String(storedDataRaw ?? "[]")) as RemVidObj[];
   }
   }
-  catch {
+  catch(err) {
+    error("Error parsing stored video time data, defaulting to empty cache:", err);
     await GM.setValue("bytm-rem-songs", "[]");
     await GM.setValue("bytm-rem-songs", "[]");
     remVidsCache = [];
     remVidsCache = [];
   }
   }
 
 
   log(`Initialized video time restoring with ${remVidsCache.length} initial entr${remVidsCache.length === 1 ? "y" : "ies"}`);
   log(`Initialized video time restoring with ${remVidsCache.length} initial entr${remVidsCache.length === 1 ? "y" : "ies"}`);
 
 
-  if(location.pathname.startsWith("/watch"))
-    await restVidRestoreTime();
+  await restVidRestoreTime();
 
 
   if(!domLoaded)
   if(!domLoaded)
     document.addEventListener("DOMContentLoaded", restVidStartUpdateLoop);
     document.addEventListener("DOMContentLoaded", restVidStartUpdateLoop);
@@ -179,11 +179,11 @@ async function restVidStartUpdateLoop() {
       };
       };
       await restVidSetEntry(entry);
       await restVidSetEntry(entry);
     }
     }
-    // if the song is rewound to the beginning, delete the entry
+    // if the song is rewound to the beginning, update the entry accordingly
     else {
     else {
       const entry = remVidsCache.find(entry => entry.watchID === watchID);
       const entry = remVidsCache.find(entry => entry.watchID === watchID);
-      if(entry && songTime <= getFeature("rememberSongTimeMinPlayTime"))
-        await restVidDeleteEntry(entry.watchID);
+      if(entry && songTime <= entry.songTime)
+        await restVidSetEntry({ ...entry, songTime, updateTimestamp: Date.now() });
     }
     }
   }
   }