popup.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. document.addEventListener("alpine:init", () => {
  2. //TODO
  3. // - preserve playlist order (sort)
  4. // - "play/resume playlist" button (pick up where you left off)
  5. // - track timestamp to truly resume where you left off
  6. // - periodically remove watched videos
  7. // - or move to an "old" category, and add a screen to see the list
  8. // - consider separating context menu items (rather than having a sub-menu)
  9. // - view playback history (?)
  10. Alpine.data("playlistManager", () => ({
  11. playlists: {},
  12. currentIndices: {},
  13. init() {
  14. this.loadPlaylists();
  15. },
  16. async loadPlaylists() {
  17. try {
  18. const result = await browser.storage.local.get("playlists");
  19. console.log("LOAD RESULT", result.playlists);
  20. this.playlists = result.playlists || {};
  21. if (result.playlists) {
  22. this.currentIndices = Object.keys(result.playlists).reduce(
  23. (acc, pln) => {
  24. const ind = result.playlists[pln].findIndex(
  25. (v) => v.status !== "done",
  26. );
  27. if (ind === -1) {
  28. acc[pln] = result.playlists[pln].length;
  29. } else {
  30. acc[pln] = ind;
  31. }
  32. return acc;
  33. },
  34. {},
  35. );
  36. } else {
  37. this.currentIndices = {};
  38. }
  39. } catch (error) {
  40. console.error("Error loading playlists:", error);
  41. }
  42. },
  43. formatPlaylistName(name) {
  44. // Convert "listening-1" to "Listening - 1"
  45. return name
  46. .split("-")
  47. .map((part) => part.charAt(0).toUpperCase() + part.slice(1))
  48. .join(" - ");
  49. },
  50. openVideo(url) {
  51. browser.tabs.create({ url });
  52. },
  53. async removeVideo(playlistName, index) {
  54. const playlists = JSON.parse(JSON.stringify(this.playlists));
  55. // Make a copy of the current playlist
  56. const playlist = [...playlists[playlistName]];
  57. // Remove the video at the specified index
  58. playlist.splice(index, 1);
  59. // Create an updated playlists object with the remaining playlists unchanged
  60. const updatedPlaylists = {
  61. ...playlists,
  62. [playlistName]: playlist,
  63. };
  64. // Update the playlists in storage
  65. try {
  66. await browser.storage.local.set({ playlists: updatedPlaylists });
  67. this.playlists = updatedPlaylists;
  68. } catch (error) {
  69. console.error("Error removing video:", error);
  70. }
  71. },
  72. async moveVideoUp(playlistName, index) {
  73. // Can't move the first item up
  74. if (index <= 0) return;
  75. const playlists = JSON.parse(JSON.stringify(this.playlists));
  76. // Make a copy of the current playlist
  77. const playlist = [...playlists[playlistName]];
  78. // Swap the video with the one above it
  79. [playlist[index], playlist[index - 1]] = [
  80. playlist[index - 1],
  81. playlist[index],
  82. ];
  83. // Create an updated playlists object
  84. const updatedPlaylists = {
  85. ...playlists,
  86. [playlistName]: playlist,
  87. };
  88. // Update the playlists in storage
  89. try {
  90. await browser.storage.local.set({ playlists: updatedPlaylists });
  91. this.playlists = updatedPlaylists;
  92. } catch (error) {
  93. console.error("Error moving video up:", error);
  94. }
  95. },
  96. async moveVideoDown(playlistName, index) {
  97. const playlists = JSON.parse(JSON.stringify(this.playlists));
  98. const playlist = [...playlists[playlistName]];
  99. // Can't move the last item down
  100. if (index >= playlist.length - 1) return;
  101. // Swap the video with the one below it
  102. [playlist[index], playlist[index + 1]] = [
  103. playlist[index + 1],
  104. playlist[index],
  105. ];
  106. // Create an updated playlists object
  107. const updatedPlaylists = {
  108. ...playlists,
  109. [playlistName]: playlist,
  110. };
  111. // Update the playlists in storage
  112. try {
  113. await browser.storage.local.set({ playlists: updatedPlaylists });
  114. this.playlists = updatedPlaylists;
  115. } catch (error) {
  116. console.error("Error moving video down:", error);
  117. }
  118. },
  119. async exportPlaylists() {
  120. try {
  121. // Get current playlists
  122. const playlistResult = await browser.storage.local.get("playlists");
  123. const playlists = playlistResult.playlists || {};
  124. // Get playback history
  125. const historyResult = await browser.storage.local.get("history");
  126. const playbackHistory = historyResult.history || {};
  127. // Create export data object
  128. const exportData = {
  129. playlists,
  130. playbackHistory,
  131. exportDate: new Date().toISOString(),
  132. };
  133. // Convert to JSON
  134. const jsonString = JSON.stringify(exportData, null, 2);
  135. // Create download
  136. const blob = new Blob([jsonString], { type: "application/json" });
  137. const url = URL.createObjectURL(blob);
  138. // Trigger download
  139. const a = document.createElement("a");
  140. a.href = url;
  141. a.download = `playlists-export-${new Date().toISOString().split("T")[0]}.json`;
  142. document.body.appendChild(a);
  143. a.click();
  144. // Clean up
  145. setTimeout(() => {
  146. document.body.removeChild(a);
  147. URL.revokeObjectURL(url);
  148. }, 100);
  149. } catch (error) {
  150. console.error("Error exporting playlists:", error);
  151. }
  152. },
  153. videotitle: {
  154. ["@click"]() {
  155. console.log("TITLE CLICK", this.$el);
  156. },
  157. },
  158. videoPlayLink: {
  159. ["@click.prevent"]() {
  160. browser.tabs.update({ url: this.$el.href });
  161. },
  162. },
  163. isCurrentVideo(playlistName, index) {
  164. const currentIndex = this.currentIndices[playlistName];
  165. return currentIndex === index;
  166. },
  167. isDoneVideo(playlistName, index) {
  168. const currentIndex = this.currentIndices[playlistName];
  169. return index < currentIndex;
  170. },
  171. videoItemClass: {
  172. [":class"]() {
  173. const playlistName = this.$el.dataset.playlistName;
  174. const index = parseInt(this.$el.dataset.playlistIndex);
  175. const video = this.playlists[playlistName][index];
  176. return {
  177. "current-video": this.isCurrentVideo(playlistName, index),
  178. "done-video":
  179. this.isDoneVideo(playlistName, index) && video.status === "done",
  180. };
  181. },
  182. },
  183. removeVideoButton: {
  184. ["@click"]() {
  185. this.removeVideo(
  186. this.$el.dataset.playlistName,
  187. this.$el.dataset.playlistIndex,
  188. );
  189. },
  190. },
  191. moveUpButton: {
  192. ["@click"]() {
  193. this.moveVideoUp(
  194. this.$el.dataset.playlistName,
  195. parseInt(this.$el.dataset.playlistIndex),
  196. );
  197. },
  198. [":disabled"]() {
  199. return parseInt(this.$el.dataset.playlistIndex) === 0;
  200. },
  201. },
  202. moveDownButton: {
  203. ["@click"]() {
  204. this.moveVideoDown(
  205. this.$el.dataset.playlistName,
  206. parseInt(this.$el.dataset.playlistIndex),
  207. );
  208. },
  209. [":disabled"]() {
  210. return (
  211. parseInt(this.$el.dataset.playlistIndex) ===
  212. this.playlists[this.$el.dataset.playlistName].length - 1
  213. );
  214. },
  215. },
  216. exportButton: {
  217. ["@click"]() {
  218. this.exportPlaylists();
  219. },
  220. },
  221. importButton: {
  222. ["@click"]() {
  223. document.getElementById("import-file-input").click();
  224. },
  225. },
  226. importFile(event) {
  227. const file = event.target.files[0];
  228. if (!file) return;
  229. const reader = new FileReader();
  230. reader.onload = (e) => {
  231. try {
  232. const importedData = JSON.parse(e.target.result);
  233. this.validateAndImportPlaylists(importedData);
  234. } catch (error) {
  235. console.error("Error parsing JSON file:", error);
  236. alert(
  237. "Invalid JSON file. Please select a valid playlist export file.",
  238. );
  239. } finally {
  240. // Reset the file input so the same file can be selected again
  241. event.target.value = "";
  242. }
  243. };
  244. reader.readAsText(file);
  245. },
  246. validateAndImportPlaylists(data) {
  247. // Validate the playlists structure
  248. if (!data.playlists || typeof data.playlists !== "object") {
  249. alert("Invalid file format: Missing or invalid 'playlists' property");
  250. return;
  251. }
  252. // Validate each playlist
  253. const validPlaylists = {};
  254. let hasErrors = false;
  255. for (const [playlistName, videos] of Object.entries(data.playlists)) {
  256. // Check if videos is an array
  257. if (!Array.isArray(videos)) {
  258. console.error(
  259. `Playlist '${playlistName}' does not contain a valid array of videos`,
  260. );
  261. hasErrors = true;
  262. continue;
  263. }
  264. // Validate each video in the playlist
  265. const validVideos = videos.filter((video) => {
  266. if (!video || typeof video !== "object") {
  267. console.error(`Invalid video object in '${playlistName}'`);
  268. return false;
  269. }
  270. if (
  271. !video.url ||
  272. typeof video.url !== "string" ||
  273. !video.title ||
  274. typeof video.title !== "string"
  275. ) {
  276. console.error(
  277. `Video in '${playlistName}' missing required properties (url, title)`,
  278. );
  279. return false;
  280. }
  281. return true;
  282. });
  283. // Add the validated playlist if it has valid videos
  284. if (validVideos.length > 0) {
  285. validPlaylists[playlistName] = validVideos;
  286. }
  287. }
  288. if (Object.keys(validPlaylists).length === 0) {
  289. alert("No valid playlists found in the import file");
  290. return;
  291. }
  292. if (hasErrors) {
  293. const confirmImport = confirm(
  294. "Some playlists or videos were invalid and will be skipped. Do you want to continue with the import?",
  295. );
  296. if (!confirmImport) return;
  297. }
  298. // Update storage and state
  299. this.updatePlaylists(validPlaylists);
  300. },
  301. async updatePlaylists(playlists) {
  302. try {
  303. await browser.storage.local.set({ playlists });
  304. this.playlists = playlists;
  305. alert("Playlists imported successfully!");
  306. } catch (error) {
  307. console.error("Error updating playlists:", error);
  308. alert("Error importing playlists: " + error.message);
  309. }
  310. },
  311. }));
  312. });