popup.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. document.addEventListener("alpine:init", () => {
  2. //TODO
  3. // - reorder videos in playlist
  4. // - remove videos from playlist
  5. // - preserve playlist order (sort)
  6. // - "play/resume playlist" button (pick up where you left off)
  7. // - track timestamp to truly resume where you left off
  8. // - periodically remove watched videos
  9. // - consider separating context menu items (rather than having a sub-menu)
  10. Alpine.data("playlistManager", () => ({
  11. playlists: {},
  12. init() {
  13. this.loadPlaylists();
  14. },
  15. async loadPlaylists() {
  16. try {
  17. const result = await browser.storage.local.get("playlists");
  18. console.log("LOAD RESULT", result.playlists);
  19. this.playlists = result.playlists || {};
  20. } catch (error) {
  21. console.error("Error loading playlists:", error);
  22. }
  23. },
  24. formatPlaylistName(name) {
  25. // Convert "listening-1" to "Listening - 1"
  26. return name
  27. .split("-")
  28. .map((part) => part.charAt(0).toUpperCase() + part.slice(1))
  29. .join(" - ");
  30. },
  31. openVideo(url) {
  32. browser.tabs.create({ url });
  33. },
  34. async removeVideo(playlistName, index) {
  35. const playlists = JSON.parse(JSON.stringify(this.playlists));
  36. // Make a copy of the current playlist
  37. const playlist = [...playlists[playlistName]];
  38. // Remove the video at the specified index
  39. playlist.splice(index, 1);
  40. // Create an updated playlists object with the remaining playlists unchanged
  41. const updatedPlaylists = {
  42. ...playlists,
  43. [playlistName]: playlist,
  44. };
  45. // Update the playlists in storage
  46. try {
  47. await browser.storage.local.set({ playlists: updatedPlaylists });
  48. this.playlists = updatedPlaylists;
  49. } catch (error) {
  50. console.error("Error removing video:", error);
  51. }
  52. },
  53. async exportPlaylists() {
  54. try {
  55. // Get current playlists
  56. const playlistResult = await browser.storage.local.get("playlists");
  57. const playlists = playlistResult.playlists || {};
  58. // Get playback history
  59. const historyResult = await browser.storage.local.get("history");
  60. const playbackHistory = historyResult.history || {};
  61. // Create export data object
  62. const exportData = {
  63. playlists,
  64. playbackHistory,
  65. exportDate: new Date().toISOString(),
  66. };
  67. // Convert to JSON
  68. const jsonString = JSON.stringify(exportData, null, 2);
  69. // Create download
  70. const blob = new Blob([jsonString], { type: "application/json" });
  71. const url = URL.createObjectURL(blob);
  72. // Trigger download
  73. const a = document.createElement("a");
  74. a.href = url;
  75. a.download = `playlists-export-${new Date().toISOString().split("T")[0]}.json`;
  76. document.body.appendChild(a);
  77. a.click();
  78. // Clean up
  79. setTimeout(() => {
  80. document.body.removeChild(a);
  81. URL.revokeObjectURL(url);
  82. }, 100);
  83. } catch (error) {
  84. console.error("Error exporting playlists:", error);
  85. }
  86. },
  87. videotitle: {
  88. ["@click"]() {
  89. console.log("TITLE CLICK", this.$el);
  90. },
  91. },
  92. videoPlayLink: {
  93. ["@click.prevent"]() {
  94. browser.tabs.update({ url: this.$el.href });
  95. },
  96. },
  97. removeVideoButton: {
  98. ["@click"]() {
  99. this.removeVideo(
  100. this.$el.dataset.playlistName,
  101. this.$el.dataset.playlistIndex,
  102. );
  103. },
  104. },
  105. exportButton: {
  106. ["@click"]() {
  107. this.exportPlaylists();
  108. },
  109. },
  110. importButton: {
  111. ["@click"]() {
  112. document.getElementById("import-file-input").click();
  113. },
  114. },
  115. importFile(event) {
  116. const file = event.target.files[0];
  117. if (!file) return;
  118. const reader = new FileReader();
  119. reader.onload = (e) => {
  120. try {
  121. const importedData = JSON.parse(e.target.result);
  122. this.validateAndImportPlaylists(importedData);
  123. } catch (error) {
  124. console.error("Error parsing JSON file:", error);
  125. alert(
  126. "Invalid JSON file. Please select a valid playlist export file.",
  127. );
  128. } finally {
  129. // Reset the file input so the same file can be selected again
  130. event.target.value = "";
  131. }
  132. };
  133. reader.readAsText(file);
  134. },
  135. validateAndImportPlaylists(data) {
  136. // Validate the playlists structure
  137. if (!data.playlists || typeof data.playlists !== "object") {
  138. alert("Invalid file format: Missing or invalid 'playlists' property");
  139. return;
  140. }
  141. // Validate each playlist
  142. const validPlaylists = {};
  143. let hasErrors = false;
  144. for (const [playlistName, videos] of Object.entries(data.playlists)) {
  145. // Check if videos is an array
  146. if (!Array.isArray(videos)) {
  147. console.error(
  148. `Playlist '${playlistName}' does not contain a valid array of videos`,
  149. );
  150. hasErrors = true;
  151. continue;
  152. }
  153. // Validate each video in the playlist
  154. const validVideos = videos.filter((video) => {
  155. if (!video || typeof video !== "object") {
  156. console.error(`Invalid video object in '${playlistName}'`);
  157. return false;
  158. }
  159. if (
  160. !video.url ||
  161. typeof video.url !== "string" ||
  162. !video.title ||
  163. typeof video.title !== "string"
  164. ) {
  165. console.error(
  166. `Video in '${playlistName}' missing required properties (url, title)`,
  167. );
  168. return false;
  169. }
  170. return true;
  171. });
  172. // Add the validated playlist if it has valid videos
  173. if (validVideos.length > 0) {
  174. validPlaylists[playlistName] = validVideos;
  175. }
  176. }
  177. if (Object.keys(validPlaylists).length === 0) {
  178. alert("No valid playlists found in the import file");
  179. return;
  180. }
  181. if (hasErrors) {
  182. const confirmImport = confirm(
  183. "Some playlists or videos were invalid and will be skipped. Do you want to continue with the import?",
  184. );
  185. if (!confirmImport) return;
  186. }
  187. // Update storage and state
  188. this.updatePlaylists(validPlaylists);
  189. },
  190. async updatePlaylists(playlists) {
  191. try {
  192. await browser.storage.local.set({ playlists });
  193. this.playlists = playlists;
  194. alert("Playlists imported successfully!");
  195. } catch (error) {
  196. console.error("Error updating playlists:", error);
  197. alert("Error importing playlists: " + error.message);
  198. }
  199. },
  200. }));
  201. });