Kaynağa Gözat

refactored video search function for reuse

Brandon Wong 1 yıl önce
ebeveyn
işleme
f377389140
1 değiştirilmiş dosya ile 26 ekleme ve 46 silme
  1. 26 46
      background.js

+ 26 - 46
background.js

@@ -83,14 +83,14 @@ browser.runtime.onInstalled.addListener(() => {
   });
 });
 
-function findPlaylist(current, url) {
+function findVideoInPlaylists(playlists, url) {
   // Extract the video id from URL query params
   let v;
   try {
     const urlObj = new URL(url);
     v = urlObj.searchParams.get("v");
   } catch (e) {
-    return false;
+    return false; // Invalid URL
   }
 
   // If no video id found, return false
@@ -99,18 +99,19 @@ function findPlaylist(current, url) {
   }
 
   // Check each playlist in the current set
-  for (const pl in current) {
-    const videos = current[pl];
+  for (const playlistName in playlists) {
+    const videos = playlists[playlistName];
 
     // Check each video in the array
-    for (const video of videos) {
+    for (let i = 0; i < videos.length; i++) {
       try {
-        const itemUrl = new URL(video.url);
+        const itemUrl = new URL(videos[i].url);
         const itemVParam = itemUrl.searchParams.get("v");
 
-        // If the "v" parameters match, return this pl
+        // If the "v" parameters match, return playlist info
         if (itemVParam === v) {
-          return pl;
+          const isLastVideo = i === videos.length - 1;
+          return [playlistName, i, isLastVideo];
         }
       } catch (e) {
         // Skip malformed URLs
@@ -123,6 +124,12 @@ function findPlaylist(current, url) {
   return false;
 }
 
+function findPlaylist(current, url) {
+  const result = findVideoInPlaylists(current, url);
+  // If found, return just the playlist name
+  return result ? result[0] : false;
+}
+
 async function addLinkToPlaylist(plName, item) {
   const { playlists: currentPlaylists } =
     await browser.storage.local.get("playlists");
@@ -212,49 +219,22 @@ async function updateHistory(message) {
 }
 
 function findNext(playlists, url) {
-  // Extract the "v" query parameter from input URL
-  let v;
-  try {
-    const urlObj = new URL(url);
-    v = urlObj.searchParams.get("v");
-  } catch (e) {
-    return false;
-  }
+  const result = findVideoInPlaylists(playlists, url);
 
-  // If no "v" parameter found, return false
-  if (!v) {
-    return false;
+  if (!result) {
+    return false; // Not found in any playlist
   }
 
-  // Check each video in the playlists
-  for (const pl in playlists) {
-    const videos = playlists[pl];
+  const [playlistName, videoIndex, isLastVideo] = result;
 
-    // Check each video in the array
-    for (let i = 0; i < videos.length; i++) {
-      try {
-        const itemUrl = new URL(videos[i].url);
-        const itemVParam = itemUrl.searchParams.get("v");
-
-        // If the "v" parameters match
-        if (itemVParam === v) {
-          console.log("FOUND VIDEO", videos[i]);
-          // Check if this is the last video in the array
-          if (i === videos.length - 1) {
-            return false; // Last video, no next video available
-          } else {
-            return videos[i + 1]; // Return the next video
-          }
-        }
-      } catch (e) {
-        // Skip malformed URLs
-        continue;
-      }
-    }
+  if (isLastVideo) {
+    return false; // Last video, no next video available
+  } else {
+    // Get next video in the playlist
+    const nextVideo = playlists[playlistName][videoIndex + 1];
+    console.log("FOUND VIDEO", playlists[playlistName][videoIndex]);
+    return nextVideo;
   }
-
-  // No match found
-  return false;
 }
 
 async function navigateAndWait(tabId, url) {