popup.js 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753
  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. // X periodically remove watched videos
  7. // X 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. // X 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. history: {},
  21. sortedHistory: [],
  22. openMenus: new Set(), // Track which menus are open
  23. currentView: 'playlists', // Track current view: 'playlists', 'history', or 'playlist'
  24. currentPlaylistName: '', // Track which playlist is being viewed
  25. playlistsForDisplay: [], // Computed array for display
  26. currentPlaylistVideos: [], // Videos for current playlist view
  27. init() {
  28. this.loadPlaylists();
  29. this.loadHistory();
  30. // Add document click handler to close menus
  31. document.addEventListener('click', (e) => {
  32. // If click is not on a more button or menu, close all menus
  33. if (!e.target.closest('.more-menu-container')) {
  34. this.closeAllMenus();
  35. }
  36. });
  37. },
  38. async loadPlaylists() {
  39. try {
  40. const result = await browser.storage.local.get("playlists");
  41. console.log("LOAD RESULT", result.playlists);
  42. this.playlists = result.playlists || {};
  43. if (result.playlists) {
  44. this.currentIndices = Object.keys(result.playlists).reduce(
  45. (acc, pln) => {
  46. const ind = result.playlists[pln].findIndex(
  47. (v) => v.status !== "done",
  48. );
  49. if (ind === -1) {
  50. acc[pln] = result.playlists[pln].length;
  51. } else {
  52. acc[pln] = ind;
  53. }
  54. return acc;
  55. },
  56. {},
  57. );
  58. } else {
  59. this.currentIndices = {};
  60. }
  61. } catch (error) {
  62. console.error("Error loading playlists:", error);
  63. }
  64. this.updatePlaylistsForDisplay();
  65. },
  66. updatePlaylistsForDisplay() {
  67. this.playlistsForDisplay = Object.entries(this.playlists).map(([playlistName, videos]) => {
  68. const currentIndex = this.currentIndices[playlistName] || 0;
  69. const shouldShowTruncation = currentIndex > 0;
  70. const truncationText = shouldShowTruncation
  71. ? `(${currentIndex} previous video${currentIndex === 1 ? '' : 's'})`
  72. : '';
  73. // Get visible videos from current index onwards with original indices
  74. const visibleVideos = videos.slice(currentIndex).map((video, index) => ({
  75. ...video,
  76. originalIndex: currentIndex + index
  77. }));
  78. return {
  79. name: playlistName,
  80. videos: videos,
  81. visibleVideos: visibleVideos,
  82. shouldShowTruncation: shouldShowTruncation,
  83. truncationText: truncationText
  84. };
  85. });
  86. },
  87. updateCurrentPlaylistVideos() {
  88. if (!this.currentPlaylistName || !this.playlists[this.currentPlaylistName]) {
  89. this.currentPlaylistVideos = [];
  90. } else {
  91. this.currentPlaylistVideos = this.playlists[this.currentPlaylistName];
  92. }
  93. },
  94. async loadHistory() {
  95. try {
  96. const result = await browser.storage.local.get("history");
  97. console.log("LOAD HISTORY RESULT", result.history);
  98. this.history = result.history || {};
  99. this.sortHistoryByRecentInteraction();
  100. } catch (error) {
  101. console.error("Error loading history:", error);
  102. }
  103. },
  104. sortHistoryByRecentInteraction() {
  105. // Convert history object to array with video ID and sort by most recent interaction
  106. this.sortedHistory = Object.entries(this.history)
  107. .map(([videoId, videoData]) => {
  108. const lastInteraction = videoData.history.length > 0
  109. ? Math.max(...videoData.history.map(event => event.timestamp))
  110. : 0;
  111. // Pre-process events with formatted data for CSP compliance
  112. const processedEvents = videoData.history
  113. .slice()
  114. .reverse()
  115. .map((event, index) => ({
  116. ...event,
  117. formattedAction: this.formatActionName(event.action),
  118. formattedPosition: `at ${this.formatVideoPosition(event.position)}`,
  119. formattedTimestamp: this.formatTimestamp(event.timestamp),
  120. uniqueKey: `${videoId}-${event.timestamp}-${index}`
  121. }));
  122. return {
  123. videoId,
  124. formattedVideoId: `(${videoId})`,
  125. ...videoData,
  126. lastInteraction,
  127. processedEvents,
  128. tags: videoData.tags || [] // Ensure tags array exists
  129. };
  130. })
  131. .sort((a, b) => b.lastInteraction - a.lastInteraction);
  132. },
  133. formatTimestamp(timestamp) {
  134. const date = new Date(timestamp);
  135. const now = new Date();
  136. const diffMs = now - date;
  137. const diffMins = Math.floor(diffMs / (1000 * 60));
  138. const diffHours = Math.floor(diffMs / (1000 * 60 * 60));
  139. const diffDays = Math.floor(diffMs / (1000 * 60 * 60 * 24));
  140. if (diffMins < 1) return 'Just now';
  141. if (diffMins < 60) return `${diffMins} minute${diffMins > 1 ? 's' : ''} ago`;
  142. if (diffHours < 24) return `${diffHours} hour${diffHours > 1 ? 's' : ''} ago`;
  143. if (diffDays < 7) return `${diffDays} day${diffDays > 1 ? 's' : ''} ago`;
  144. return date.toLocaleDateString() + ' ' + date.toLocaleTimeString([], {hour: '2-digit', minute:'2-digit'});
  145. },
  146. formatVideoPosition(seconds) {
  147. const minutes = Math.floor(seconds / 60);
  148. const remainingSeconds = Math.floor(seconds % 60);
  149. return `${minutes}:${remainingSeconds.toString().padStart(2, '0')}`;
  150. },
  151. async toggleTag(videoId, tag) {
  152. // Create a deep copy of history to avoid proxy issues
  153. const history = JSON.parse(JSON.stringify(this.history));
  154. if (!history[videoId]) {
  155. console.error(`Video ${videoId} not found in history`);
  156. return;
  157. }
  158. const videoData = history[videoId];
  159. if (!videoData.tags) {
  160. videoData.tags = [];
  161. }
  162. const tagIndex = videoData.tags.indexOf(tag);
  163. if (tagIndex === -1) {
  164. // Add tag
  165. videoData.tags.push(tag);
  166. } else {
  167. // Remove tag
  168. videoData.tags.splice(tagIndex, 1);
  169. }
  170. // Save to storage and update local state
  171. try {
  172. await browser.storage.local.set({ history: history });
  173. this.history = history;
  174. this.sortHistoryByRecentInteraction(); // Refresh display
  175. } catch (error) {
  176. console.error("Error saving tag changes:", error);
  177. }
  178. },
  179. isTagActive(videoId, tag) {
  180. const videoData = this.history[videoId];
  181. return videoData && videoData.tags && videoData.tags.includes(tag);
  182. },
  183. formatActionName(action) {
  184. const actionMap = {
  185. 'play': 'Started',
  186. 'playing': 'Playing',
  187. 'pause': 'Paused',
  188. 'ended': 'Finished'
  189. };
  190. return actionMap[action] || action;
  191. },
  192. formatPlaylistName(name) {
  193. // Convert "listening-1" to "Listening - 1"
  194. return name
  195. .split("-")
  196. .map((part) => part.charAt(0).toUpperCase() + part.slice(1))
  197. .join(" - ");
  198. },
  199. openVideo(url) {
  200. browser.tabs.create({ url });
  201. },
  202. async removeVideo(playlistName, index) {
  203. const playlists = JSON.parse(JSON.stringify(this.playlists));
  204. // Make a copy of the current playlist
  205. const playlist = [...playlists[playlistName]];
  206. // Remove the video at the specified index
  207. playlist.splice(index, 1);
  208. // Create an updated playlists object with the remaining playlists unchanged
  209. const updatedPlaylists = {
  210. ...playlists,
  211. [playlistName]: playlist,
  212. };
  213. // Update the playlists in storage
  214. try {
  215. await browser.storage.local.set({ playlists: updatedPlaylists });
  216. this.playlists = updatedPlaylists;
  217. this.updatePlaylistsForDisplay();
  218. this.updateCurrentPlaylistVideos();
  219. } catch (error) {
  220. console.error("Error removing video:", error);
  221. }
  222. },
  223. async moveVideoUp(playlistName, index) {
  224. // Can't move the first item up
  225. if (index <= 0) return;
  226. const playlists = JSON.parse(JSON.stringify(this.playlists));
  227. // Make a copy of the current playlist
  228. const playlist = [...playlists[playlistName]];
  229. // Swap the video with the one above it
  230. [playlist[index], playlist[index - 1]] = [
  231. playlist[index - 1],
  232. playlist[index],
  233. ];
  234. // Create an updated playlists object
  235. const updatedPlaylists = {
  236. ...playlists,
  237. [playlistName]: playlist,
  238. };
  239. // Update the playlists in storage
  240. try {
  241. await browser.storage.local.set({ playlists: updatedPlaylists });
  242. this.playlists = updatedPlaylists;
  243. this.updatePlaylistsForDisplay();
  244. this.updateCurrentPlaylistVideos();
  245. } catch (error) {
  246. console.error("Error moving video up:", error);
  247. }
  248. },
  249. async moveVideoDown(playlistName, index) {
  250. const playlists = JSON.parse(JSON.stringify(this.playlists));
  251. const playlist = [...playlists[playlistName]];
  252. // Can't move the last item down
  253. if (index >= playlist.length - 1) return;
  254. // Swap the video with the one below it
  255. [playlist[index], playlist[index + 1]] = [
  256. playlist[index + 1],
  257. playlist[index],
  258. ];
  259. // Create an updated playlists object
  260. const updatedPlaylists = {
  261. ...playlists,
  262. [playlistName]: playlist,
  263. };
  264. // Update the playlists in storage
  265. try {
  266. await browser.storage.local.set({ playlists: updatedPlaylists });
  267. this.playlists = updatedPlaylists;
  268. this.updatePlaylistsForDisplay();
  269. this.updateCurrentPlaylistVideos();
  270. } catch (error) {
  271. console.error("Error moving video down:", error);
  272. }
  273. },
  274. async exportPlaylists() {
  275. try {
  276. // Get current playlists
  277. const playlistResult = await browser.storage.local.get("playlists");
  278. const playlists = playlistResult.playlists || {};
  279. // Get playback history
  280. const historyResult = await browser.storage.local.get("history");
  281. const playbackHistory = historyResult.history || {};
  282. // Create export data object
  283. const exportData = {
  284. playlists,
  285. playbackHistory,
  286. exportDate: new Date().toISOString(),
  287. };
  288. // Convert to JSON
  289. const jsonString = JSON.stringify(exportData, null, 2);
  290. // Create download
  291. const blob = new Blob([jsonString], { type: "application/json" });
  292. const url = URL.createObjectURL(blob);
  293. // Trigger download
  294. const a = document.createElement("a");
  295. a.href = url;
  296. a.download = `playlists-export-${new Date().toISOString().split("T")[0]}.json`;
  297. document.body.appendChild(a);
  298. a.click();
  299. // Clean up
  300. setTimeout(() => {
  301. document.body.removeChild(a);
  302. URL.revokeObjectURL(url);
  303. }, 100);
  304. } catch (error) {
  305. console.error("Error exporting playlists:", error);
  306. }
  307. },
  308. videotitle: {
  309. ["@click"]() {
  310. console.log("TITLE CLICK", this.$el);
  311. },
  312. },
  313. videoPlayLink: {
  314. ["@click.prevent"]() {
  315. browser.tabs.update({ url: this.$el.href });
  316. },
  317. },
  318. isCurrentVideo(playlistName, index) {
  319. const currentIndex = this.currentIndices[playlistName];
  320. return currentIndex === index;
  321. },
  322. isDoneVideo(playlistName, index) {
  323. const currentIndex = this.currentIndices[playlistName];
  324. return index < currentIndex;
  325. },
  326. videoItemClass: {
  327. [":class"]() {
  328. const playlistName = this.$el.dataset.playlistName;
  329. const index = parseInt(this.$el.dataset.playlistIndex);
  330. const video = this.playlists[playlistName][index];
  331. return {
  332. "current-video": this.isCurrentVideo(playlistName, index),
  333. "done-video":
  334. this.isDoneVideo(playlistName, index) && video.status === "done",
  335. };
  336. },
  337. },
  338. removeVideoButton: {
  339. ["@click"]() {
  340. this.removeVideo(
  341. this.$el.dataset.playlistName,
  342. this.$el.dataset.playlistIndex,
  343. );
  344. this.closeAllMenus();
  345. },
  346. },
  347. moveUpButton: {
  348. ["@click"]() {
  349. this.moveVideoUp(
  350. this.$el.dataset.playlistName,
  351. parseInt(this.$el.dataset.playlistIndex),
  352. );
  353. },
  354. [":disabled"]() {
  355. return parseInt(this.$el.dataset.playlistIndex) === 0;
  356. },
  357. },
  358. moveDownButton: {
  359. ["@click"]() {
  360. this.moveVideoDown(
  361. this.$el.dataset.playlistName,
  362. parseInt(this.$el.dataset.playlistIndex),
  363. );
  364. },
  365. [":disabled"]() {
  366. return (
  367. parseInt(this.$el.dataset.playlistIndex) ===
  368. this.playlists[this.$el.dataset.playlistName].length - 1
  369. );
  370. },
  371. },
  372. exportButton: {
  373. ["@click"]() {
  374. this.exportPlaylists();
  375. },
  376. },
  377. importButton: {
  378. ["@click"]() {
  379. document.getElementById("import-file-input").click();
  380. },
  381. },
  382. importFileInput: {
  383. ["@change"]() {
  384. this.importFile(this.$event);
  385. },
  386. },
  387. importFile(event) {
  388. const file = event.target.files[0];
  389. if (!file) return;
  390. const reader = new FileReader();
  391. reader.onload = (e) => {
  392. try {
  393. const importedData = JSON.parse(e.target.result);
  394. this.validateAndImportPlaylists(importedData);
  395. } catch (error) {
  396. console.error("Error parsing JSON file:", error);
  397. alert(
  398. "Invalid JSON file. Please select a valid playlist export file.",
  399. );
  400. } finally {
  401. // Reset the file input so the same file can be selected again
  402. event.target.value = "";
  403. }
  404. };
  405. reader.readAsText(file);
  406. },
  407. validateAndImportPlaylists(data) {
  408. // Validate the playlists structure
  409. if (!data.playlists || typeof data.playlists !== "object") {
  410. alert("Invalid file format: Missing or invalid 'playlists' property");
  411. return;
  412. }
  413. // Validate each playlist
  414. const validPlaylists = {};
  415. let hasErrors = false;
  416. for (const [playlistName, videos] of Object.entries(data.playlists)) {
  417. // Check if videos is an array
  418. if (!Array.isArray(videos)) {
  419. console.error(
  420. `Playlist '${playlistName}' does not contain a valid array of videos`,
  421. );
  422. hasErrors = true;
  423. continue;
  424. }
  425. // Validate each video in the playlist
  426. const validVideos = videos.filter((video) => {
  427. if (!video || typeof video !== "object") {
  428. console.error(`Invalid video object in '${playlistName}'`);
  429. return false;
  430. }
  431. if (
  432. !video.url ||
  433. typeof video.url !== "string" ||
  434. !video.title ||
  435. typeof video.title !== "string"
  436. ) {
  437. console.error(
  438. `Video in '${playlistName}' missing required properties (url, title)`,
  439. );
  440. return false;
  441. }
  442. return true;
  443. });
  444. // Add the validated playlist if it has valid videos
  445. if (validVideos.length > 0) {
  446. validPlaylists[playlistName] = validVideos;
  447. }
  448. }
  449. if (Object.keys(validPlaylists).length === 0) {
  450. alert("No valid playlists found in the import file");
  451. return;
  452. }
  453. if (hasErrors) {
  454. const confirmImport = confirm(
  455. "Some playlists or videos were invalid and will be skipped. Do you want to continue with the import?",
  456. );
  457. if (!confirmImport) return;
  458. }
  459. // Update storage and state
  460. this.updatePlaylists(validPlaylists);
  461. },
  462. async updatePlaylists(playlists) {
  463. try {
  464. await browser.storage.local.set({ playlists });
  465. this.playlists = playlists;
  466. this.updatePlaylistsForDisplay();
  467. this.updateCurrentPlaylistVideos();
  468. alert("Playlists imported successfully!");
  469. } catch (error) {
  470. console.error("Error updating playlists:", error);
  471. alert("Error importing playlists: " + error.message);
  472. }
  473. },
  474. getMenuId(playlistName, index) {
  475. return `${playlistName}-${index}`;
  476. },
  477. isMenuOpen(playlistName, index) {
  478. return this.openMenus.has(this.getMenuId(playlistName, index));
  479. },
  480. toggleMenu(playlistName, index) {
  481. const menuId = this.getMenuId(playlistName, index);
  482. if (this.openMenus.has(menuId)) {
  483. this.openMenus.delete(menuId);
  484. } else {
  485. // Close all other menus first
  486. this.openMenus.clear();
  487. this.openMenus.add(menuId);
  488. }
  489. },
  490. closeAllMenus() {
  491. this.openMenus.clear();
  492. },
  493. moreMenuButton: {
  494. ["@click.stop"]() {
  495. this.toggleMenu(
  496. this.$el.dataset.playlistName,
  497. parseInt(this.$el.dataset.playlistIndex)
  498. );
  499. },
  500. },
  501. moreMenu: {
  502. ["x-show"]() {
  503. return this.isMenuOpen(
  504. this.$el.dataset.playlistName,
  505. parseInt(this.$el.dataset.playlistIndex)
  506. );
  507. },
  508. },
  509. // View navigation methods
  510. showHistory() {
  511. this.currentView = 'history';
  512. this.loadHistory(); // Refresh history data when switching to history view
  513. },
  514. showPlaylists() {
  515. this.currentView = 'playlists';
  516. },
  517. showPlaylist(playlistName) {
  518. this.currentView = 'playlist';
  519. this.currentPlaylistName = playlistName;
  520. this.updateCurrentPlaylistVideos();
  521. },
  522. // Methods for truncated display
  523. shouldShowTruncation(playlistName) {
  524. const currentIndex = this.currentIndices[playlistName] || 0;
  525. return currentIndex > 0;
  526. },
  527. getTruncationText(playlistName) {
  528. const currentIndex = this.currentIndices[playlistName] || 0;
  529. const count = currentIndex;
  530. return `(${count} previous video${count === 1 ? '' : 's'})`;
  531. },
  532. getVisibleVideos(playlistName, videos) {
  533. const currentIndex = this.currentIndices[playlistName] || 0;
  534. // Show from current video onwards, but keep original indices
  535. return videos.slice(currentIndex).map((video, index) => ({
  536. ...video,
  537. originalIndex: currentIndex + index
  538. }));
  539. },
  540. getCurrentPlaylistVideos() {
  541. if (!this.currentPlaylistName || !this.playlists[this.currentPlaylistName]) {
  542. return [];
  543. }
  544. return this.playlists[this.currentPlaylistName];
  545. },
  546. // Button bindings for navigation
  547. historyButton: {
  548. ["@click"]() {
  549. this.showHistory();
  550. },
  551. },
  552. backButton: {
  553. ["@click"]() {
  554. this.showPlaylists();
  555. },
  556. },
  557. // Event handlers for playlist navigation
  558. playlistNameClick: {
  559. ["@click"]() {
  560. const playlistName = this.$el.dataset.playlistName;
  561. this.showPlaylist(playlistName);
  562. },
  563. },
  564. truncatedVideosClick: {
  565. ["@click"]() {
  566. const playlistName = this.$el.dataset.playlistName;
  567. this.showPlaylist(playlistName);
  568. },
  569. },
  570. truncatedVideosClick: {
  571. ["@click"]() {
  572. const playlistName = this.$el.dataset.playlistName;
  573. this.showPlaylist(playlistName);
  574. },
  575. },
  576. truncatedVideosDisplay: {
  577. ["x-show"]() {
  578. const playlistName = this.$el.dataset.playlistName;
  579. const playlistData = this.playlistsForDisplay.find(p => p.name === playlistName);
  580. return playlistData ? playlistData.shouldShowTruncation : false;
  581. },
  582. ["@click"]() {
  583. const playlistName = this.$el.dataset.playlistName;
  584. this.showPlaylist(playlistName);
  585. },
  586. },
  587. // CSP-compliant view bindings
  588. playlistsHeader: {
  589. ["x-show"]() {
  590. return this.currentView === 'playlists';
  591. },
  592. },
  593. historyHeader: {
  594. ["x-show"]() {
  595. return this.currentView === 'history';
  596. },
  597. },
  598. playlistViewHeader: {
  599. ["x-show"]() {
  600. return this.currentView === 'playlist';
  601. },
  602. },
  603. playlistsContainer: {
  604. ["x-show"]() {
  605. return this.currentView === 'playlists';
  606. },
  607. },
  608. historyContainer: {
  609. ["x-show"]() {
  610. return this.currentView === 'history';
  611. },
  612. },
  613. playlistViewContainer: {
  614. ["x-show"]() {
  615. return this.currentView === 'playlist';
  616. },
  617. },
  618. exportContainer: {
  619. ["x-show"]() {
  620. return this.currentView === 'playlists';
  621. },
  622. },
  623. // History-specific bindings for CSP compliance
  624. historyEmptyState: {
  625. ["x-show"]() {
  626. return this.sortedHistory.length === 0;
  627. },
  628. },
  629. // Playlist view-specific bindings for CSP compliance
  630. playlistViewEmptyState: {
  631. ["x-show"]() {
  632. return this.currentPlaylistVideos.length === 0;
  633. },
  634. },
  635. // Tag chip bindings for CSP compliance
  636. tagChip: {
  637. ["@click"]() {
  638. const videoId = this.$el.dataset.videoId;
  639. const tag = this.$el.dataset.tag;
  640. this.toggleTag(videoId, tag);
  641. },
  642. [":class"]() {
  643. const videoId = this.$el.dataset.videoId;
  644. const tag = this.$el.dataset.tag;
  645. return {
  646. 'active': this.isTagActive(videoId, tag)
  647. };
  648. },
  649. },
  650. }));
  651. });