popup.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476
  1. document.addEventListener("alpine:init", () => {
  2. //TODO
  3. // - preserve playlist order (sort)
  4. // - "play/resume playlist" button (pick up where you left off)
  5. // X 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. // - include currently open tabs in export
  11. // - support playlist management (add, remove, rename playlists)
  12. // - support input text box for adding to playlist (how to handle title?)
  13. // - option (probably a button) to add current page (url, title) to playlist
  14. // - button to add channel to youtube page (copy gql mutation to clipboard) (like the automa version)
  15. // - long-term: replace youtube page? rss feeds? need server?
  16. // - add personal rating feature ("enjoyed", "this was important", etc)
  17. Alpine.data("playlistManager", () => ({
  18. playlists: {},
  19. currentIndices: {},
  20. openMenus: new Set(), // Track which menus are open
  21. currentView: 'playlists', // Track current view: 'playlists' or 'history'
  22. init() {
  23. this.loadPlaylists();
  24. // Add document click handler to close menus
  25. document.addEventListener('click', (e) => {
  26. // If click is not on a more button or menu, close all menus
  27. if (!e.target.closest('.more-menu-container')) {
  28. this.closeAllMenus();
  29. }
  30. });
  31. },
  32. async loadPlaylists() {
  33. try {
  34. const result = await browser.storage.local.get("playlists");
  35. console.log("LOAD RESULT", result.playlists);
  36. this.playlists = result.playlists || {};
  37. if (result.playlists) {
  38. this.currentIndices = Object.keys(result.playlists).reduce(
  39. (acc, pln) => {
  40. const ind = result.playlists[pln].findIndex(
  41. (v) => v.status !== "done",
  42. );
  43. if (ind === -1) {
  44. acc[pln] = result.playlists[pln].length;
  45. } else {
  46. acc[pln] = ind;
  47. }
  48. return acc;
  49. },
  50. {},
  51. );
  52. } else {
  53. this.currentIndices = {};
  54. }
  55. } catch (error) {
  56. console.error("Error loading playlists:", error);
  57. }
  58. },
  59. formatPlaylistName(name) {
  60. // Convert "listening-1" to "Listening - 1"
  61. return name
  62. .split("-")
  63. .map((part) => part.charAt(0).toUpperCase() + part.slice(1))
  64. .join(" - ");
  65. },
  66. openVideo(url) {
  67. browser.tabs.create({ url });
  68. },
  69. async removeVideo(playlistName, index) {
  70. const playlists = JSON.parse(JSON.stringify(this.playlists));
  71. // Make a copy of the current playlist
  72. const playlist = [...playlists[playlistName]];
  73. // Remove the video at the specified index
  74. playlist.splice(index, 1);
  75. // Create an updated playlists object with the remaining playlists unchanged
  76. const updatedPlaylists = {
  77. ...playlists,
  78. [playlistName]: playlist,
  79. };
  80. // Update the playlists in storage
  81. try {
  82. await browser.storage.local.set({ playlists: updatedPlaylists });
  83. this.playlists = updatedPlaylists;
  84. } catch (error) {
  85. console.error("Error removing video:", error);
  86. }
  87. },
  88. async moveVideoUp(playlistName, index) {
  89. // Can't move the first item up
  90. if (index <= 0) return;
  91. const playlists = JSON.parse(JSON.stringify(this.playlists));
  92. // Make a copy of the current playlist
  93. const playlist = [...playlists[playlistName]];
  94. // Swap the video with the one above it
  95. [playlist[index], playlist[index - 1]] = [
  96. playlist[index - 1],
  97. playlist[index],
  98. ];
  99. // Create an updated playlists object
  100. const updatedPlaylists = {
  101. ...playlists,
  102. [playlistName]: playlist,
  103. };
  104. // Update the playlists in storage
  105. try {
  106. await browser.storage.local.set({ playlists: updatedPlaylists });
  107. this.playlists = updatedPlaylists;
  108. } catch (error) {
  109. console.error("Error moving video up:", error);
  110. }
  111. },
  112. async moveVideoDown(playlistName, index) {
  113. const playlists = JSON.parse(JSON.stringify(this.playlists));
  114. const playlist = [...playlists[playlistName]];
  115. // Can't move the last item down
  116. if (index >= playlist.length - 1) return;
  117. // Swap the video with the one below it
  118. [playlist[index], playlist[index + 1]] = [
  119. playlist[index + 1],
  120. playlist[index],
  121. ];
  122. // Create an updated playlists object
  123. const updatedPlaylists = {
  124. ...playlists,
  125. [playlistName]: playlist,
  126. };
  127. // Update the playlists in storage
  128. try {
  129. await browser.storage.local.set({ playlists: updatedPlaylists });
  130. this.playlists = updatedPlaylists;
  131. } catch (error) {
  132. console.error("Error moving video down:", error);
  133. }
  134. },
  135. async exportPlaylists() {
  136. try {
  137. // Get current playlists
  138. const playlistResult = await browser.storage.local.get("playlists");
  139. const playlists = playlistResult.playlists || {};
  140. // Get playback history
  141. const historyResult = await browser.storage.local.get("history");
  142. const playbackHistory = historyResult.history || {};
  143. // Create export data object
  144. const exportData = {
  145. playlists,
  146. playbackHistory,
  147. exportDate: new Date().toISOString(),
  148. };
  149. // Convert to JSON
  150. const jsonString = JSON.stringify(exportData, null, 2);
  151. // Create download
  152. const blob = new Blob([jsonString], { type: "application/json" });
  153. const url = URL.createObjectURL(blob);
  154. // Trigger download
  155. const a = document.createElement("a");
  156. a.href = url;
  157. a.download = `playlists-export-${new Date().toISOString().split("T")[0]}.json`;
  158. document.body.appendChild(a);
  159. a.click();
  160. // Clean up
  161. setTimeout(() => {
  162. document.body.removeChild(a);
  163. URL.revokeObjectURL(url);
  164. }, 100);
  165. } catch (error) {
  166. console.error("Error exporting playlists:", error);
  167. }
  168. },
  169. videotitle: {
  170. ["@click"]() {
  171. console.log("TITLE CLICK", this.$el);
  172. },
  173. },
  174. videoPlayLink: {
  175. ["@click.prevent"]() {
  176. browser.tabs.update({ url: this.$el.href });
  177. },
  178. },
  179. isCurrentVideo(playlistName, index) {
  180. const currentIndex = this.currentIndices[playlistName];
  181. return currentIndex === index;
  182. },
  183. isDoneVideo(playlistName, index) {
  184. const currentIndex = this.currentIndices[playlistName];
  185. return index < currentIndex;
  186. },
  187. videoItemClass: {
  188. [":class"]() {
  189. const playlistName = this.$el.dataset.playlistName;
  190. const index = parseInt(this.$el.dataset.playlistIndex);
  191. const video = this.playlists[playlistName][index];
  192. return {
  193. "current-video": this.isCurrentVideo(playlistName, index),
  194. "done-video":
  195. this.isDoneVideo(playlistName, index) && video.status === "done",
  196. };
  197. },
  198. },
  199. removeVideoButton: {
  200. ["@click"]() {
  201. this.removeVideo(
  202. this.$el.dataset.playlistName,
  203. this.$el.dataset.playlistIndex,
  204. );
  205. this.closeAllMenus();
  206. },
  207. },
  208. moveUpButton: {
  209. ["@click"]() {
  210. this.moveVideoUp(
  211. this.$el.dataset.playlistName,
  212. parseInt(this.$el.dataset.playlistIndex),
  213. );
  214. },
  215. [":disabled"]() {
  216. return parseInt(this.$el.dataset.playlistIndex) === 0;
  217. },
  218. },
  219. moveDownButton: {
  220. ["@click"]() {
  221. this.moveVideoDown(
  222. this.$el.dataset.playlistName,
  223. parseInt(this.$el.dataset.playlistIndex),
  224. );
  225. },
  226. [":disabled"]() {
  227. return (
  228. parseInt(this.$el.dataset.playlistIndex) ===
  229. this.playlists[this.$el.dataset.playlistName].length - 1
  230. );
  231. },
  232. },
  233. exportButton: {
  234. ["@click"]() {
  235. this.exportPlaylists();
  236. },
  237. },
  238. importButton: {
  239. ["@click"]() {
  240. document.getElementById("import-file-input").click();
  241. },
  242. },
  243. importFile(event) {
  244. const file = event.target.files[0];
  245. if (!file) return;
  246. const reader = new FileReader();
  247. reader.onload = (e) => {
  248. try {
  249. const importedData = JSON.parse(e.target.result);
  250. this.validateAndImportPlaylists(importedData);
  251. } catch (error) {
  252. console.error("Error parsing JSON file:", error);
  253. alert(
  254. "Invalid JSON file. Please select a valid playlist export file.",
  255. );
  256. } finally {
  257. // Reset the file input so the same file can be selected again
  258. event.target.value = "";
  259. }
  260. };
  261. reader.readAsText(file);
  262. },
  263. validateAndImportPlaylists(data) {
  264. // Validate the playlists structure
  265. if (!data.playlists || typeof data.playlists !== "object") {
  266. alert("Invalid file format: Missing or invalid 'playlists' property");
  267. return;
  268. }
  269. // Validate each playlist
  270. const validPlaylists = {};
  271. let hasErrors = false;
  272. for (const [playlistName, videos] of Object.entries(data.playlists)) {
  273. // Check if videos is an array
  274. if (!Array.isArray(videos)) {
  275. console.error(
  276. `Playlist '${playlistName}' does not contain a valid array of videos`,
  277. );
  278. hasErrors = true;
  279. continue;
  280. }
  281. // Validate each video in the playlist
  282. const validVideos = videos.filter((video) => {
  283. if (!video || typeof video !== "object") {
  284. console.error(`Invalid video object in '${playlistName}'`);
  285. return false;
  286. }
  287. if (
  288. !video.url ||
  289. typeof video.url !== "string" ||
  290. !video.title ||
  291. typeof video.title !== "string"
  292. ) {
  293. console.error(
  294. `Video in '${playlistName}' missing required properties (url, title)`,
  295. );
  296. return false;
  297. }
  298. return true;
  299. });
  300. // Add the validated playlist if it has valid videos
  301. if (validVideos.length > 0) {
  302. validPlaylists[playlistName] = validVideos;
  303. }
  304. }
  305. if (Object.keys(validPlaylists).length === 0) {
  306. alert("No valid playlists found in the import file");
  307. return;
  308. }
  309. if (hasErrors) {
  310. const confirmImport = confirm(
  311. "Some playlists or videos were invalid and will be skipped. Do you want to continue with the import?",
  312. );
  313. if (!confirmImport) return;
  314. }
  315. // Update storage and state
  316. this.updatePlaylists(validPlaylists);
  317. },
  318. async updatePlaylists(playlists) {
  319. try {
  320. await browser.storage.local.set({ playlists });
  321. this.playlists = playlists;
  322. alert("Playlists imported successfully!");
  323. } catch (error) {
  324. console.error("Error updating playlists:", error);
  325. alert("Error importing playlists: " + error.message);
  326. }
  327. },
  328. getMenuId(playlistName, index) {
  329. return `${playlistName}-${index}`;
  330. },
  331. isMenuOpen(playlistName, index) {
  332. return this.openMenus.has(this.getMenuId(playlistName, index));
  333. },
  334. toggleMenu(playlistName, index) {
  335. const menuId = this.getMenuId(playlistName, index);
  336. if (this.openMenus.has(menuId)) {
  337. this.openMenus.delete(menuId);
  338. } else {
  339. // Close all other menus first
  340. this.openMenus.clear();
  341. this.openMenus.add(menuId);
  342. }
  343. },
  344. closeAllMenus() {
  345. this.openMenus.clear();
  346. },
  347. moreMenuButton: {
  348. ["@click.stop"]() {
  349. this.toggleMenu(
  350. this.$el.dataset.playlistName,
  351. parseInt(this.$el.dataset.playlistIndex)
  352. );
  353. },
  354. },
  355. moreMenu: {
  356. ["x-show"]() {
  357. return this.isMenuOpen(
  358. this.$el.dataset.playlistName,
  359. parseInt(this.$el.dataset.playlistIndex)
  360. );
  361. },
  362. },
  363. // View navigation methods
  364. showHistory() {
  365. this.currentView = 'history';
  366. },
  367. showPlaylists() {
  368. this.currentView = 'playlists';
  369. },
  370. // Button bindings for navigation
  371. historyButton: {
  372. ["@click"]() {
  373. this.showHistory();
  374. },
  375. },
  376. backButton: {
  377. ["@click"]() {
  378. this.showPlaylists();
  379. },
  380. },
  381. // CSP-compliant view bindings
  382. playlistsHeader: {
  383. ["x-show"]() {
  384. return this.currentView === 'playlists';
  385. },
  386. },
  387. historyHeader: {
  388. ["x-show"]() {
  389. return this.currentView === 'history';
  390. },
  391. },
  392. playlistsContainer: {
  393. ["x-show"]() {
  394. return this.currentView === 'playlists';
  395. },
  396. },
  397. historyContainer: {
  398. ["x-show"]() {
  399. return this.currentView === 'history';
  400. },
  401. },
  402. exportContainer: {
  403. ["x-show"]() {
  404. return this.currentView === 'playlists';
  405. },
  406. },
  407. }));
  408. });