popup.js 25 KB

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