playlist-utils.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. // Shared playlist utility functions
  2. // Used by both popup.js and background.js
  3. const PlaylistUtils = {
  4. /**
  5. * Extract video ID from YouTube URL
  6. * @param {string} url - YouTube URL
  7. * @returns {string|null} - Video ID or null if not found
  8. */
  9. extractVideoId(url) {
  10. try {
  11. const urlObj = new URL(url);
  12. return urlObj.searchParams.get("v");
  13. } catch (e) {
  14. return null;
  15. }
  16. },
  17. /**
  18. * Find a video in all playlists by URL
  19. * @param {Object} playlists - All playlists object
  20. * @param {string} url - Video URL to search for
  21. * @returns {Array|false} - [playlistName, videoIndex, isLastVideo] or false if not found
  22. */
  23. findVideoInPlaylists(playlists, url) {
  24. const videoId = this.extractVideoId(url);
  25. if (!videoId) {
  26. return false;
  27. }
  28. // Check each playlist
  29. for (const playlistName in playlists) {
  30. const videos = playlists[playlistName];
  31. // Check each video in the playlist
  32. for (let i = 0; i < videos.length; i++) {
  33. const itemVideoId = this.extractVideoId(videos[i].url);
  34. // If the video IDs match, return playlist info
  35. if (itemVideoId === videoId) {
  36. const isLastVideo = i === videos.length - 1;
  37. return [playlistName, i, isLastVideo];
  38. }
  39. }
  40. }
  41. return false;
  42. },
  43. /**
  44. * Find which playlist contains a video by URL
  45. * @param {Object} playlists - All playlists object
  46. * @param {string} url - Video URL to search for
  47. * @returns {string|false} - Playlist name or false if not found
  48. */
  49. findPlaylist(playlists, url) {
  50. const result = this.findVideoInPlaylists(playlists, url);
  51. return result ? result[0] : false;
  52. },
  53. /**
  54. * Add a video to a playlist if it's not already present in any playlist
  55. * @param {string} playlistName - Name of playlist to add to
  56. * @param {Object} video - Video object with url and title properties
  57. * @returns {Promise<boolean>} - True if added, false if already exists
  58. */
  59. async addVideoToPlaylist(playlistName, video) {
  60. const { playlists: currentPlaylists } = await browser.storage.local.get("playlists");
  61. const alreadyExists = this.findPlaylist(currentPlaylists, video.url);
  62. if (alreadyExists) {
  63. console.log("Video already exists in playlist:", alreadyExists);
  64. return false;
  65. }
  66. // Add video to the specified playlist
  67. const updatedPlaylist = [...currentPlaylists[playlistName], video];
  68. const updatedPlaylists = {
  69. ...currentPlaylists,
  70. [playlistName]: updatedPlaylist
  71. };
  72. await browser.storage.local.set({ playlists: updatedPlaylists });
  73. console.log("Added video to playlist:", playlistName);
  74. return true;
  75. },
  76. /**
  77. * Legacy function for backward compatibility with context menu
  78. * @param {string} playlistName - Name of playlist to add to
  79. * @param {Object} item - Context menu item with linkUrl and linkText
  80. * @returns {Promise<boolean>} - True if added, false if already exists
  81. */
  82. async addLinkToPlaylist(playlistName, item) {
  83. const video = {
  84. url: item.linkUrl,
  85. title: item.linkText,
  86. status: "new"
  87. };
  88. return await this.addVideoToPlaylist(playlistName, video);
  89. }
  90. };