popup.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  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 moveVideoUp(playlistName, index) {
  54. // Can't move the first item up
  55. if (index <= 0) return;
  56. const playlists = JSON.parse(JSON.stringify(this.playlists));
  57. // Make a copy of the current playlist
  58. const playlist = [...playlists[playlistName]];
  59. // Swap the video with the one above it
  60. [playlist[index], playlist[index - 1]] = [
  61. playlist[index - 1],
  62. playlist[index],
  63. ];
  64. // Create an updated playlists object
  65. const updatedPlaylists = {
  66. ...playlists,
  67. [playlistName]: playlist,
  68. };
  69. // Update the playlists in storage
  70. try {
  71. await browser.storage.local.set({ playlists: updatedPlaylists });
  72. this.playlists = updatedPlaylists;
  73. } catch (error) {
  74. console.error("Error moving video up:", error);
  75. }
  76. },
  77. async moveVideoDown(playlistName, index) {
  78. const playlists = JSON.parse(JSON.stringify(this.playlists));
  79. const playlist = [...playlists[playlistName]];
  80. // Can't move the last item down
  81. if (index >= playlist.length - 1) return;
  82. // Swap the video with the one below it
  83. [playlist[index], playlist[index + 1]] = [
  84. playlist[index + 1],
  85. playlist[index],
  86. ];
  87. // Create an updated playlists object
  88. const updatedPlaylists = {
  89. ...playlists,
  90. [playlistName]: playlist,
  91. };
  92. // Update the playlists in storage
  93. try {
  94. await browser.storage.local.set({ playlists: updatedPlaylists });
  95. this.playlists = updatedPlaylists;
  96. } catch (error) {
  97. console.error("Error moving video down:", error);
  98. }
  99. },
  100. async exportPlaylists() {
  101. try {
  102. // Get current playlists
  103. const playlistResult = await browser.storage.local.get("playlists");
  104. const playlists = playlistResult.playlists || {};
  105. // Get playback history
  106. const historyResult = await browser.storage.local.get("history");
  107. const playbackHistory = historyResult.history || {};
  108. // Create export data object
  109. const exportData = {
  110. playlists,
  111. playbackHistory,
  112. exportDate: new Date().toISOString(),
  113. };
  114. // Convert to JSON
  115. const jsonString = JSON.stringify(exportData, null, 2);
  116. // Create download
  117. const blob = new Blob([jsonString], { type: "application/json" });
  118. const url = URL.createObjectURL(blob);
  119. // Trigger download
  120. const a = document.createElement("a");
  121. a.href = url;
  122. a.download = `playlists-export-${new Date().toISOString().split("T")[0]}.json`;
  123. document.body.appendChild(a);
  124. a.click();
  125. // Clean up
  126. setTimeout(() => {
  127. document.body.removeChild(a);
  128. URL.revokeObjectURL(url);
  129. }, 100);
  130. } catch (error) {
  131. console.error("Error exporting playlists:", error);
  132. }
  133. },
  134. videotitle: {
  135. ["@click"]() {
  136. console.log("TITLE CLICK", this.$el);
  137. },
  138. },
  139. videoPlayLink: {
  140. ["@click.prevent"]() {
  141. browser.tabs.update({ url: this.$el.href });
  142. },
  143. },
  144. isCurrentVideo(playlistName, index) {
  145. const playlist = this.playlists[playlistName];
  146. if (!playlist || !Array.isArray(playlist)) return false;
  147. // Find the index of the first video that is not "done"
  148. const currentIndex = playlist.findIndex((video) => {
  149. // Consider a video as not done if:
  150. // 1. It has no status property, or
  151. // 2. The status is not "done" (could be a number for partially watched)
  152. return !video.status || video.status !== "done";
  153. });
  154. // If no "current" video found (all are done) or current index doesn't match, return false
  155. return currentIndex === index && currentIndex !== -1;
  156. },
  157. isConsecutivelyDone(playlistName, index) {
  158. const playlist = this.playlists[playlistName];
  159. if (!playlist || !Array.isArray(playlist)) return false;
  160. // Current video must be "done" first
  161. if (!playlist[index]?.status || playlist[index].status !== "done") {
  162. return false;
  163. }
  164. // Check all previous videos - they must ALL be done
  165. for (let i = 0; i < index; i++) {
  166. if (!playlist[i]?.status || playlist[i].status !== "done") {
  167. return false; // Found a non-done video before the current one
  168. }
  169. }
  170. return true; // Current video is done, and all videos before it are done
  171. },
  172. videoItemClass: {
  173. [":class"]() {
  174. return {
  175. "current-video": this.isCurrentVideo(
  176. this.$el.dataset.playlistName,
  177. parseInt(this.$el.dataset.playlistIndex),
  178. ),
  179. "done-video": this.isConsecutivelyDone(
  180. this.$el.dataset.playlistName,
  181. parseInt(this.$el.dataset.playlistIndex),
  182. ),
  183. };
  184. },
  185. },
  186. removeVideoButton: {
  187. ["@click"]() {
  188. this.removeVideo(
  189. this.$el.dataset.playlistName,
  190. this.$el.dataset.playlistIndex,
  191. );
  192. },
  193. },
  194. moveUpButton: {
  195. ["@click"]() {
  196. this.moveVideoUp(
  197. this.$el.dataset.playlistName,
  198. parseInt(this.$el.dataset.playlistIndex),
  199. );
  200. },
  201. [":disabled"]() {
  202. return parseInt(this.$el.dataset.playlistIndex) === 0;
  203. },
  204. },
  205. moveDownButton: {
  206. ["@click"]() {
  207. this.moveVideoDown(
  208. this.$el.dataset.playlistName,
  209. parseInt(this.$el.dataset.playlistIndex),
  210. );
  211. },
  212. [":disabled"]() {
  213. return (
  214. parseInt(this.$el.dataset.playlistIndex) ===
  215. this.playlists[this.$el.dataset.playlistName].length - 1
  216. );
  217. },
  218. },
  219. exportButton: {
  220. ["@click"]() {
  221. this.exportPlaylists();
  222. },
  223. },
  224. importButton: {
  225. ["@click"]() {
  226. document.getElementById("import-file-input").click();
  227. },
  228. },
  229. importFile(event) {
  230. const file = event.target.files[0];
  231. if (!file) return;
  232. const reader = new FileReader();
  233. reader.onload = (e) => {
  234. try {
  235. const importedData = JSON.parse(e.target.result);
  236. this.validateAndImportPlaylists(importedData);
  237. } catch (error) {
  238. console.error("Error parsing JSON file:", error);
  239. alert(
  240. "Invalid JSON file. Please select a valid playlist export file.",
  241. );
  242. } finally {
  243. // Reset the file input so the same file can be selected again
  244. event.target.value = "";
  245. }
  246. };
  247. reader.readAsText(file);
  248. },
  249. validateAndImportPlaylists(data) {
  250. // Validate the playlists structure
  251. if (!data.playlists || typeof data.playlists !== "object") {
  252. alert("Invalid file format: Missing or invalid 'playlists' property");
  253. return;
  254. }
  255. // Validate each playlist
  256. const validPlaylists = {};
  257. let hasErrors = false;
  258. for (const [playlistName, videos] of Object.entries(data.playlists)) {
  259. // Check if videos is an array
  260. if (!Array.isArray(videos)) {
  261. console.error(
  262. `Playlist '${playlistName}' does not contain a valid array of videos`,
  263. );
  264. hasErrors = true;
  265. continue;
  266. }
  267. // Validate each video in the playlist
  268. const validVideos = videos.filter((video) => {
  269. if (!video || typeof video !== "object") {
  270. console.error(`Invalid video object in '${playlistName}'`);
  271. return false;
  272. }
  273. if (
  274. !video.url ||
  275. typeof video.url !== "string" ||
  276. !video.title ||
  277. typeof video.title !== "string"
  278. ) {
  279. console.error(
  280. `Video in '${playlistName}' missing required properties (url, title)`,
  281. );
  282. return false;
  283. }
  284. return true;
  285. });
  286. // Add the validated playlist if it has valid videos
  287. if (validVideos.length > 0) {
  288. validPlaylists[playlistName] = validVideos;
  289. }
  290. }
  291. if (Object.keys(validPlaylists).length === 0) {
  292. alert("No valid playlists found in the import file");
  293. return;
  294. }
  295. if (hasErrors) {
  296. const confirmImport = confirm(
  297. "Some playlists or videos were invalid and will be skipped. Do you want to continue with the import?",
  298. );
  299. if (!confirmImport) return;
  300. }
  301. // Update storage and state
  302. this.updatePlaylists(validPlaylists);
  303. },
  304. async updatePlaylists(playlists) {
  305. try {
  306. await browser.storage.local.set({ playlists });
  307. this.playlists = playlists;
  308. alert("Playlists imported successfully!");
  309. } catch (error) {
  310. console.error("Error updating playlists:", error);
  311. alert("Error importing playlists: " + error.message);
  312. }
  313. },
  314. }));
  315. });