Ver Fonte

ref: rename stuff

Sv443 há 8 meses atrás
pai
commit
5ee4bb1c71
3 ficheiros alterados com 24 adições e 19 exclusões
  1. 1 1
      README-summary.md
  2. 1 1
      README.md
  3. 22 17
      src/features/behavior.ts

+ 1 - 1
README-summary.md

@@ -31,7 +31,7 @@ All of these can be toggled and configured in the configuration menu.
   - Press a hotkey on a video/song to switch between YouTube and YouTube Music, while keeping the video time
   - Prevent the "unsaved data" popup that sometimes appears before leaving the site
   
-... and these are just the notable features, there are many more smaller improvements and bugfixes!
+... and these are just the notable features, there are many more smaller features, improvements and fixes!
 
 <br>
 

+ 1 - 1
README.md

@@ -33,7 +33,7 @@ All of these can be toggled and configured in the configuration menu.
   - Press a hotkey on a video/song to switch between YouTube and YouTube Music, while keeping the video time
   - Prevent the "unsaved data" popup that sometimes appears before leaving the site
   
-... and these are just the notable features, there are many more smaller improvements and bugfixes!
+... and these are just the notable features, there are many more smaller features, improvements and fixes!
 
 <br><br>
 

+ 22 - 17
src/features/behavior.ts

@@ -105,16 +105,21 @@ export async function initRememberSongTime() {
 
   log(`Initialized video time restoring with ${remVidsCache.length} initial entr${remVidsCache.length === 1 ? "y" : "ies"}`);
 
-  await restVidRestoreTime();
+  await remTimeRestoreTime();
 
-  if(!domLoaded)
-    document.addEventListener("DOMContentLoaded", restVidStartUpdateLoop);
-  else
-    restVidStartUpdateLoop();
+  try {
+    if(!domLoaded)
+      document.addEventListener("DOMContentLoaded", remTimeStartUpdateLoop);
+    else
+      remTimeStartUpdateLoop();
+  }
+  catch(err) {
+    error("Error in video time remembering update loop:", err);
+  }
 }
 
 /** Tries to restore the time of the currently playing video */
-async function restVidRestoreTime() {
+async function remTimeRestoreTime() {
   if(location.pathname.startsWith("/watch")) {
     const watchID = new URL(location.href).searchParams.get("v");
     if(!watchID)
@@ -126,7 +131,7 @@ async function restVidRestoreTime() {
     const entry = remVidsCache.find(entry => entry.watchID === watchID);
     if(entry) {
       if(Date.now() - entry.updateTimestamp > getFeature("rememberSongTimeDuration") * 1000) {
-        await restVidDeleteEntry(entry.watchID);
+        await remTimeDeleteEntry(entry.watchID);
         return;
       }
       else if(isNaN(Number(entry.songTime)))
@@ -139,7 +144,7 @@ async function restVidRestoreTime() {
             vidElem = await waitVideoElementReady();
           const vidRestoreTime = entry.songTime - (getFeature("rememberSongTimeReduction") ?? 0);
           vidElem.currentTime = clamp(Math.max(vidRestoreTime, 0), 0, vidElem.duration);
-          await restVidDeleteEntry(entry.watchID);
+          await remTimeDeleteEntry(entry.watchID);
           info(`Restored ${currentMediaType()} time to ${Math.floor(vidRestoreTime / 60)}m, ${(vidRestoreTime % 60).toFixed(1)}s`, LogLevel.Info);
         };
 
@@ -156,7 +161,7 @@ let lastSongTime = -1;
 let remVidCheckTimeout: ReturnType<typeof setTimeout> | undefined;
 
 /** Only call once as this calls itself after a timeout! - Updates the currently playing video's entry in GM storage */
-async function restVidStartUpdateLoop() {
+async function remTimeStartUpdateLoop() {
   if(location.pathname.startsWith("/watch")) {
     const songTime = await getVideoTime() ?? 0;
 
@@ -180,28 +185,28 @@ async function restVidStartUpdateLoop() {
         songTime,
         updateTimestamp: Date.now(),
       };
-      await restVidSetEntry(entry);
+      await remTimeUpsertEntry(entry);
     }
     // if the song is rewound to the beginning, update the entry accordingly
     else if(!paused) {
       const entry = remVidsCache.find(entry => entry.watchID === watchID);
       if(entry && songTime <= entry.songTime)
-        await restVidSetEntry({ ...entry, songTime, updateTimestamp: Date.now() });
+        await remTimeUpsertEntry({ ...entry, songTime, updateTimestamp: Date.now() });
     }
   }
 
   const expiredEntries = remVidsCache.filter(entry => Date.now() - entry.updateTimestamp > getFeature("rememberSongTimeDuration") * 1000);
   for(const entry of expiredEntries)
-    await restVidDeleteEntry(entry.watchID);
+    await remTimeDeleteEntry(entry.watchID);
 
   // for no overlapping calls and better error handling:
   if(remVidCheckTimeout)
     clearTimeout(remVidCheckTimeout);
-  remVidCheckTimeout = setTimeout(restVidStartUpdateLoop, 1000);
+  remVidCheckTimeout = setTimeout(remTimeStartUpdateLoop, 1000);
 }
 
-/** Updates an existing or creates a new entry */
-async function restVidSetEntry(data: RemVidObj) {
+/** Updates an existing or inserts a new entry to be remembered */
+async function remTimeUpsertEntry(data: RemVidObj) {
   const foundIdx = remVidsCache.findIndex(entry => entry.watchID === data.watchID);
   if(foundIdx >= 0)
     remVidsCache[foundIdx] = data;
@@ -211,8 +216,8 @@ async function restVidSetEntry(data: RemVidObj) {
   await GM.setValue("bytm-rem-songs", JSON.stringify(remVidsCache));
 }
 
-/** Deletes an entry */
-async function restVidDeleteEntry(watchID: string) {
+/** Deletes an entry in the "remember cache" */
+async function remTimeDeleteEntry(watchID: string) {
   dbg("Deleting entry with watchID", watchID);
   remVidsCache = [...remVidsCache.filter(entry => entry.watchID !== watchID)];
   await GM.setValue("bytm-rem-songs", JSON.stringify(remVidsCache));