| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073 |
- document.addEventListener("alpine:init", () => {
- //TODO
- // - preserve playlist order (sort)
- // - "play/resume playlist" button (pick up where you left off)
- // X track timestamp to truly resume where you left off
- // X periodically remove watched videos
- // X or move to an "old" category, and add a screen to see the list
- // - consider separating context menu items (rather than having a sub-menu)
- // X view playback history (?)
- // X include currently open tabs in export
- // - support playlist management (add, remove, rename playlists)
- // - support input text box for adding to playlist (how to handle title?)
- // - or raw json editing
- // X option (probably a button) to add current page (url, title) to playlist
- // X align with addLinkToPlaylist in background.js (no repeated videos)
- // - button to add channel to youtube page (copy gql mutation to clipboard) (like the automa version)
- // - long-term: replace youtube page? rss feeds? need server?
- // X add personal rating feature ("enjoyed", "this was important", etc)
- // - option to move video (including status) to another playlist
- Alpine.data("playlistManager", () => ({
- playlists: {},
- currentIndices: {},
- history: {},
- sortedHistory: [],
- openMenus: new Set(), // Track which menus are open
- currentView: "playlists", // Track current view: 'playlists', 'history', 'playlist', or 'saveChannel'
- currentPlaylistName: "", // Track which playlist is being viewed
- playlistsForDisplay: [], // Computed array for display
- currentPlaylistVideos: [], // Videos for current playlist view
- currentTab: null, // Current active tab info
- isCurrentTabYoutube: false, // Whether current tab is YouTube
- isCurrentTabChannelPage: false, // Whether current tab is YouTube videos page
- addCurrentPageButtonText: "Add Current Page", // Button text
- // Save channel properties
- selectedCategory: "FOR BOTH", // Currently selected category
- availableCategories: ["FOR BOTH", "CHANNELS", "CREATIVE"], // Available categories
- curlCommand: "", // Generated curl command
- checkInterval: 14, // Check interval in days
- init() {
- this.loadPlaylists();
- this.loadHistory();
- this.getCurrentTab();
- // Add document click handler to close menus
- document.addEventListener("click", (e) => {
- // If click is not on a more button or menu, close all menus
- if (!e.target.closest(".more-menu-container")) {
- this.closeAllMenus();
- }
- });
- },
- async loadPlaylists() {
- try {
- const result = await browser.storage.local.get("playlists");
- console.log("LOAD RESULT", result.playlists);
- this.playlists = result.playlists || {};
- if (result.playlists) {
- this.currentIndices = Object.keys(result.playlists).reduce(
- (acc, pln) => {
- const ind = result.playlists[pln].findIndex(
- (v) => v.status !== "done",
- );
- if (ind === -1) {
- acc[pln] = result.playlists[pln].length;
- } else {
- acc[pln] = ind;
- }
- return acc;
- },
- {},
- );
- } else {
- this.currentIndices = {};
- }
- } catch (error) {
- console.error("Error loading playlists:", error);
- }
- this.updatePlaylistsForDisplay();
- },
- updatePlaylistsForDisplay() {
- this.playlistsForDisplay = Object.entries(this.playlists).map(
- ([playlistName, videos]) => {
- const currentIndex = this.currentIndices[playlistName] || 0;
- const shouldShowTruncation = currentIndex > 0;
- const truncationText = shouldShowTruncation
- ? `(${currentIndex} previous video${currentIndex === 1 ? "" : "s"})`
- : "";
- // Get visible videos from current index onwards with original indices
- const visibleVideos = videos
- .slice(currentIndex)
- .map((video, index) => ({
- ...video,
- originalIndex: currentIndex + index,
- doneButtonText: video.status === "done" ? "Remove Done Status" : "Mark as Done",
- isNonContiguousDone: this.isNonContiguousDone(playlistName, currentIndex + index),
- }));
- return {
- name: playlistName,
- videos: videos,
- visibleVideos: visibleVideos,
- shouldShowTruncation: shouldShowTruncation,
- truncationText: truncationText,
- };
- },
- );
- },
- updateCurrentPlaylistVideos() {
- if (
- !this.currentPlaylistName ||
- !this.playlists[this.currentPlaylistName]
- ) {
- this.currentPlaylistVideos = [];
- } else {
- this.currentPlaylistVideos = this.playlists[this.currentPlaylistName].map((video, index) => ({
- ...video,
- doneButtonText: video.status === "done" ? "Remove Done Status" : "Mark as Done",
- isNonContiguousDone: this.isNonContiguousDone(this.currentPlaylistName, index),
- }));
- }
- },
- async loadHistory() {
- try {
- const result = await browser.storage.local.get("history");
- console.log("LOAD HISTORY RESULT", result.history);
- this.history = result.history || {};
- this.sortHistoryByRecentInteraction();
- } catch (error) {
- console.error("Error loading history:", error);
- }
- },
- async getCurrentTab() {
- try {
- const tabs = await browser.tabs.query({
- active: true,
- currentWindow: true,
- });
- if (tabs.length > 0) {
- this.currentTab = tabs[0];
- const url = new URL(this.currentTab.url);
- this.isCurrentTabYoutube = url.hostname === "www.youtube.com";
- this.isCurrentTabChannelPage = this.isCurrentTabYoutube && url.pathname.includes("/videos");
- this.updateAddCurrentPageButtonText();
- this.updateCurlCommand();
- }
- } catch (error) {
- console.error("Error getting current tab:", error);
- this.currentTab = null;
- this.isCurrentTabYoutube = false;
- this.isCurrentTabChannelPage = false;
- this.updateAddCurrentPageButtonText();
- this.updateCurlCommand();
- }
- },
- updateAddCurrentPageButtonText() {
- if (!this.currentTab) {
- this.addCurrentPageButtonText = "Unable to get current page";
- } else if (!this.isCurrentTabYoutube) {
- this.addCurrentPageButtonText = "Add Current Page (YouTube only)";
- } else {
- this.addCurrentPageButtonText = "Add Current Page to Playlist";
- }
- },
- updateCurlCommand() {
- if (!this.currentTab || !this.isCurrentTabChannelPage) {
- this.curlCommand = "This feature is only available on YouTube channel pages (/videos).";
- return;
- }
- const title = this.currentTab.title.replace(" - YouTube", "");
- const url = this.currentTab.url;
- const category = this.selectedCategory;
- const interval = this.checkInterval;
- this.curlCommand = `curl -X POST -H 'content-type: application/json' -d '{"query": "mutation add{ addChannel(details: {category: \\"${category}\\", checkInterval: ${interval}, name: \\"${title}\\", url: \\"${url}\\"}){name} } "}' localhost:8543/data | jq '.'`;
- },
- async copyCurlCommand() {
- try {
- await navigator.clipboard.writeText(this.curlCommand);
- console.log("Curl command copied to clipboard");
- // Could add visual feedback here
- } catch (error) {
- console.error("Failed to copy to clipboard:", error);
- // Fallback for older browsers
- try {
- const textArea = document.createElement("textarea");
- textArea.value = this.curlCommand;
- document.body.appendChild(textArea);
- textArea.focus();
- textArea.select();
- document.execCommand("copy");
- document.body.removeChild(textArea);
- console.log("Curl command copied to clipboard (fallback)");
- } catch (fallbackError) {
- console.error("Fallback copy failed:", fallbackError);
- }
- }
- },
- selectCategory(category) {
- this.selectedCategory = category;
- this.updateCurlCommand();
- },
- updateCheckInterval(interval) {
- // Ensure it's a positive integer, default to 14 if invalid
- const parsedInterval = parseInt(interval);
- this.checkInterval = parsedInterval > 0 ? parsedInterval : 14;
- this.updateCurlCommand();
- },
- showSaveChannel() {
- this.currentView = "saveChannel";
- this.updateCurlCommand();
- },
- sortHistoryByRecentInteraction() {
- // Convert history object to array with video ID and sort by most recent interaction
- this.sortedHistory = Object.entries(this.history)
- .map(([videoId, videoData]) => {
- const lastInteraction =
- videoData.history.length > 0
- ? Math.max(...videoData.history.map((event) => event.timestamp))
- : 0;
- // Pre-process events with formatted data for CSP compliance
- const processedEvents = videoData.history
- .slice()
- .reverse()
- .map((event, index) => ({
- ...event,
- formattedAction: this.formatActionName(event.action),
- formattedPosition: `at ${this.formatVideoPosition(event.position)}`,
- formattedTimestamp: this.formatTimestamp(event.timestamp),
- uniqueKey: `${videoId}-${event.timestamp}-${index}`,
- }));
- return {
- videoId,
- formattedVideoId: `(${videoId})`,
- ...videoData,
- lastInteraction,
- processedEvents,
- tags: videoData.tags || [], // Ensure tags array exists
- };
- })
- .sort((a, b) => b.lastInteraction - a.lastInteraction);
- },
- formatTimestamp(timestamp) {
- const date = new Date(timestamp);
- const now = new Date();
- const diffMs = now - date;
- const diffMins = Math.floor(diffMs / (1000 * 60));
- const diffHours = Math.floor(diffMs / (1000 * 60 * 60));
- const diffDays = Math.floor(diffMs / (1000 * 60 * 60 * 24));
- if (diffMins < 1) return "Just now";
- if (diffMins < 60)
- return `${diffMins} minute${diffMins > 1 ? "s" : ""} ago`;
- if (diffHours < 24)
- return `${diffHours} hour${diffHours > 1 ? "s" : ""} ago`;
- if (diffDays < 7) return `${diffDays} day${diffDays > 1 ? "s" : ""} ago`;
- return (
- date.toLocaleDateString() +
- " " +
- date.toLocaleTimeString([], { hour: "2-digit", minute: "2-digit" })
- );
- },
- formatVideoPosition(seconds) {
- const minutes = Math.floor(seconds / 60);
- const remainingSeconds = Math.floor(seconds % 60);
- return `${minutes}:${remainingSeconds.toString().padStart(2, "0")}`;
- },
- async toggleTag(videoId, tag) {
- // Create a deep copy of history to avoid proxy issues
- const history = JSON.parse(JSON.stringify(this.history));
- if (!history[videoId]) {
- console.error(`Video ${videoId} not found in history`);
- return;
- }
- const videoData = history[videoId];
- if (!videoData.tags) {
- videoData.tags = [];
- }
- const tagIndex = videoData.tags.indexOf(tag);
- if (tagIndex === -1) {
- // Add tag
- videoData.tags.push(tag);
- } else {
- // Remove tag
- videoData.tags.splice(tagIndex, 1);
- }
- // Save to storage and update local state
- try {
- await browser.storage.local.set({ history: history });
- this.history = history;
- this.sortHistoryByRecentInteraction(); // Refresh display
- } catch (error) {
- console.error("Error saving tag changes:", error);
- }
- },
- isTagActive(videoId, tag) {
- const videoData = this.history[videoId];
- return videoData && videoData.tags && videoData.tags.includes(tag);
- },
- formatActionName(action) {
- const actionMap = {
- play: "Started",
- playing: "Playing",
- pause: "Paused",
- ended: "Finished",
- };
- return actionMap[action] || action;
- },
- formatPlaylistName(name) {
- // Convert "listening-1" to "Listening - 1"
- return name
- .split("-")
- .map((part) => part.charAt(0).toUpperCase() + part.slice(1))
- .join(" - ");
- },
- openVideo(url) {
- browser.tabs.create({ url });
- },
- async removeVideo(playlistName, index) {
- const playlists = JSON.parse(JSON.stringify(this.playlists));
- // Make a copy of the current playlist
- const playlist = [...playlists[playlistName]];
- // Remove the video at the specified index
- playlist.splice(index, 1);
- // Create an updated playlists object with the remaining playlists unchanged
- const updatedPlaylists = {
- ...playlists,
- [playlistName]: playlist,
- };
- // Update the playlists in storage
- try {
- await browser.storage.local.set({ playlists: updatedPlaylists });
- this.playlists = updatedPlaylists;
- this.updatePlaylistsForDisplay();
- this.updateCurrentPlaylistVideos();
- } catch (error) {
- console.error("Error removing video:", error);
- }
- },
- async moveVideoUp(playlistName, index) {
- // Can't move the first item up
- if (index <= 0) return;
- const playlists = JSON.parse(JSON.stringify(this.playlists));
- // Make a copy of the current playlist
- const playlist = [...playlists[playlistName]];
- // Swap the video with the one above it
- [playlist[index], playlist[index - 1]] = [
- playlist[index - 1],
- playlist[index],
- ];
- // Create an updated playlists object
- const updatedPlaylists = {
- ...playlists,
- [playlistName]: playlist,
- };
- // Update the playlists in storage
- try {
- await browser.storage.local.set({ playlists: updatedPlaylists });
- this.playlists = updatedPlaylists;
- this.updatePlaylistsForDisplay();
- this.updateCurrentPlaylistVideos();
- } catch (error) {
- console.error("Error moving video up:", error);
- }
- },
- async moveVideoDown(playlistName, index) {
- const playlists = JSON.parse(JSON.stringify(this.playlists));
- const playlist = [...playlists[playlistName]];
- // Can't move the last item down
- if (index >= playlist.length - 1) return;
- // Swap the video with the one below it
- [playlist[index], playlist[index + 1]] = [
- playlist[index + 1],
- playlist[index],
- ];
- // Create an updated playlists object
- const updatedPlaylists = {
- ...playlists,
- [playlistName]: playlist,
- };
- // Update the playlists in storage
- try {
- await browser.storage.local.set({ playlists: updatedPlaylists });
- this.playlists = updatedPlaylists;
- this.updatePlaylistsForDisplay();
- this.updateCurrentPlaylistVideos();
- } catch (error) {
- console.error("Error moving video down:", error);
- }
- },
- async toggleVideoDoneStatus(playlistName, index) {
- const playlists = JSON.parse(JSON.stringify(this.playlists));
- const playlist = [...playlists[playlistName]];
- const video = {...playlist[index]};
- // Toggle the done status
- if (video.status === "done") {
- // Remove status property (undefined status means not done)
- delete video.status;
- } else {
- // Set status to done
- video.status = "done";
- }
- // Update the video in the playlist
- playlist[index] = video;
- // Create an updated playlists object
- const updatedPlaylists = {
- ...playlists,
- [playlistName]: playlist,
- };
- // Update the playlists in storage
- try {
- await browser.storage.local.set({ playlists: updatedPlaylists });
- this.playlists = updatedPlaylists;
- this.updatePlaylistsForDisplay();
- this.updateCurrentPlaylistVideos();
- } catch (error) {
- console.error("Error toggling video done status:", error);
- }
- },
- async addCurrentPageToPlaylist() {
- if (
- !this.currentTab ||
- !this.isCurrentTabYoutube ||
- !this.currentPlaylistName
- ) {
- return;
- }
- // Create video object using current tab info
- const video = {
- url: this.currentTab.url,
- title: this.currentTab.title,
- };
- // Use shared utility function to add video (handles duplicate checking)
- const wasAdded = await PlaylistUtils.addVideoToPlaylist(
- this.currentPlaylistName,
- video,
- );
- if (wasAdded) {
- // Refresh displays only if video was actually added
- this.loadPlaylists(); // This will update both display arrays
- } else {
- // Could show user feedback that video already exists
- console.log("Video already exists in a playlist");
- }
- },
- async exportPlaylists() {
- try {
- // Get current playlists
- const playlistResult = await browser.storage.local.get("playlists");
- const playlists = playlistResult.playlists || {};
- // Get playback history
- const historyResult = await browser.storage.local.get("history");
- const playbackHistory = historyResult.history || {};
- // Get open tabs
- let openTabs = [];
- try {
- const tabsResult = await browser.tabs.query({});
- openTabs = tabsResult
- .filter(tab => tab.url &&
- !tab.url.startsWith('moz-extension://') &&
- !tab.url.startsWith('about:') &&
- !tab.url.startsWith('chrome://'))
- .map(tab => ({
- url: tab.url,
- title: tab.title || 'Untitled'
- }));
- } catch (error) {
- console.warn("Could not retrieve open tabs:", error);
- openTabs = [];
- }
- // Create export data object
- const exportData = {
- playlists,
- playbackHistory,
- openTabs,
- exportDate: new Date().toISOString(),
- };
- // Convert to JSON
- const jsonString = JSON.stringify(exportData, null, 2);
- // Create download
- const blob = new Blob([jsonString], { type: "application/json" });
- const url = URL.createObjectURL(blob);
- // Trigger download
- const a = document.createElement("a");
- a.href = url;
- a.download = `playlists-export-${new Date().toISOString().split("T")[0]}.json`;
- document.body.appendChild(a);
- a.click();
- // Clean up
- setTimeout(() => {
- document.body.removeChild(a);
- URL.revokeObjectURL(url);
- }, 100);
- } catch (error) {
- console.error("Error exporting playlists:", error);
- }
- },
- videotitle: {
- ["@click"]() {
- console.log("TITLE CLICK", this.$el);
- },
- },
- videoPlayLink: {
- ["@click.prevent"]() {
- browser.tabs.update({ url: this.$el.href });
- },
- },
- isNonContiguousDone(playlistName, videoIndex) {
- const playlist = this.playlists[playlistName];
- if (!playlist || !playlist[videoIndex] || playlist[videoIndex].status !== "done") {
- return false;
- }
- // Check if there's any non-done video before this done video
- for (let i = 0; i < videoIndex; i++) {
- if (playlist[i].status !== "done") {
- return true;
- }
- }
- return false;
- },
- isCurrentVideo(playlistName, index) {
- const currentIndex = this.currentIndices[playlistName];
- return currentIndex === index;
- },
- isDoneVideo(playlistName, index) {
- const currentIndex = this.currentIndices[playlistName];
- return index < currentIndex;
- },
- isVideoDone(playlistName, index) {
- const video = this.playlists[playlistName] && this.playlists[playlistName][index];
- return video && video.status === "done";
- },
- videoItemClass: {
- [":class"]() {
- const playlistName = this.$el.dataset.playlistName;
- const index = parseInt(this.$el.dataset.playlistIndex);
- const video = this.playlists[playlistName][index];
- return {
- "current-video": this.isCurrentVideo(playlistName, index),
- "done-video":
- this.isDoneVideo(playlistName, index) && video.status === "done",
- "non-contiguous-done-video": this.isNonContiguousDone(playlistName, index),
- };
- },
- },
- removeVideoButton: {
- ["@click"]() {
- this.removeVideo(
- this.$el.dataset.playlistName,
- this.$el.dataset.playlistIndex,
- );
- this.closeAllMenus();
- },
- },
- toggleVideoDoneButton: {
- ["@click"]() {
- this.toggleVideoDoneStatus(
- this.$el.dataset.playlistName,
- parseInt(this.$el.dataset.playlistIndex),
- );
- this.closeAllMenus();
- },
- },
- moveUpButton: {
- ["@click"]() {
- this.moveVideoUp(
- this.$el.dataset.playlistName,
- parseInt(this.$el.dataset.playlistIndex),
- );
- },
- [":disabled"]() {
- return parseInt(this.$el.dataset.playlistIndex) === 0;
- },
- },
- moveDownButton: {
- ["@click"]() {
- this.moveVideoDown(
- this.$el.dataset.playlistName,
- parseInt(this.$el.dataset.playlistIndex),
- );
- },
- [":disabled"]() {
- return (
- parseInt(this.$el.dataset.playlistIndex) ===
- this.playlists[this.$el.dataset.playlistName].length - 1
- );
- },
- },
- exportButton: {
- ["@click"]() {
- this.exportPlaylists();
- },
- },
- importButton: {
- ["@click"]() {
- document.getElementById("import-file-input").click();
- },
- },
- importFileInput: {
- ["@change"]() {
- this.importFile(this.$event);
- },
- },
- importFile(event) {
- const file = event.target.files[0];
- if (!file) return;
- const reader = new FileReader();
- reader.onload = (e) => {
- try {
- const importedData = JSON.parse(e.target.result);
- this.validateAndImportPlaylists(importedData);
- } catch (error) {
- console.error("Error parsing JSON file:", error);
- alert(
- "Invalid JSON file. Please select a valid playlist export file.",
- );
- } finally {
- // Reset the file input so the same file can be selected again
- event.target.value = "";
- }
- };
- reader.readAsText(file);
- },
- validateAndImportPlaylists(data) {
- // Validate the playlists structure
- if (!data.playlists || typeof data.playlists !== "object") {
- alert("Invalid file format: Missing or invalid 'playlists' property");
- return;
- }
- // Validate each playlist
- const validPlaylists = {};
- let hasErrors = false;
- for (const [playlistName, videos] of Object.entries(data.playlists)) {
- // Check if videos is an array
- if (!Array.isArray(videos)) {
- console.error(
- `Playlist '${playlistName}' does not contain a valid array of videos`,
- );
- hasErrors = true;
- continue;
- }
- // Validate each video in the playlist
- const validVideos = videos.filter((video) => {
- if (!video || typeof video !== "object") {
- console.error(`Invalid video object in '${playlistName}'`);
- return false;
- }
- if (
- !video.url ||
- typeof video.url !== "string" ||
- !video.title ||
- typeof video.title !== "string"
- ) {
- console.error(
- `Video in '${playlistName}' missing required properties (url, title)`,
- );
- return false;
- }
- return true;
- });
- // Add the validated playlist if it has valid videos
- if (validVideos.length > 0) {
- validPlaylists[playlistName] = validVideos;
- }
- }
- if (Object.keys(validPlaylists).length === 0) {
- alert("No valid playlists found in the import file");
- return;
- }
- if (hasErrors) {
- const confirmImport = confirm(
- "Some playlists or videos were invalid and will be skipped. Do you want to continue with the import?",
- );
- if (!confirmImport) return;
- }
- // Update storage and state
- this.updatePlaylists(validPlaylists);
- },
- async updatePlaylists(playlists) {
- try {
- await browser.storage.local.set({ playlists });
- this.playlists = playlists;
- this.updatePlaylistsForDisplay();
- this.updateCurrentPlaylistVideos();
- alert("Playlists imported successfully!");
- } catch (error) {
- console.error("Error updating playlists:", error);
- alert("Error importing playlists: " + error.message);
- }
- },
- getMenuId(playlistName, index) {
- return `${playlistName}-${index}`;
- },
- isMenuOpen(playlistName, index) {
- return this.openMenus.has(this.getMenuId(playlistName, index));
- },
- toggleMenu(playlistName, index) {
- const menuId = this.getMenuId(playlistName, index);
- if (this.openMenus.has(menuId)) {
- this.openMenus.delete(menuId);
- } else {
- // Close all other menus first
- this.openMenus.clear();
- this.openMenus.add(menuId);
- }
- },
- closeAllMenus() {
- this.openMenus.clear();
- },
- moreMenuButton: {
- ["@click.stop"]() {
- this.toggleMenu(
- this.$el.dataset.playlistName,
- parseInt(this.$el.dataset.playlistIndex),
- );
- },
- },
- moreMenu: {
- ["x-show"]() {
- return this.isMenuOpen(
- this.$el.dataset.playlistName,
- parseInt(this.$el.dataset.playlistIndex),
- );
- },
- },
- // View navigation methods
- showHistory() {
- this.currentView = "history";
- this.loadHistory(); // Refresh history data when switching to history view
- },
- showPlaylists() {
- this.currentView = "playlists";
- },
- showPlaylist(playlistName) {
- this.currentView = "playlist";
- this.currentPlaylistName = playlistName;
- this.updateCurrentPlaylistVideos();
- },
- // Methods for truncated display
- shouldShowTruncation(playlistName) {
- const currentIndex = this.currentIndices[playlistName] || 0;
- return currentIndex > 0;
- },
- getTruncationText(playlistName) {
- const currentIndex = this.currentIndices[playlistName] || 0;
- const count = currentIndex;
- return `(${count} previous video${count === 1 ? "" : "s"})`;
- },
- getVisibleVideos(playlistName, videos) {
- const currentIndex = this.currentIndices[playlistName] || 0;
- // Show from current video onwards, but keep original indices
- return videos.slice(currentIndex).map((video, index) => ({
- ...video,
- originalIndex: currentIndex + index,
- }));
- },
- getCurrentPlaylistVideos() {
- if (
- !this.currentPlaylistName ||
- !this.playlists[this.currentPlaylistName]
- ) {
- return [];
- }
- return this.playlists[this.currentPlaylistName];
- },
- // Button bindings for navigation
- historyButton: {
- ["@click"]() {
- this.showHistory();
- },
- },
- saveChannelButton: {
- ["@click"]() {
- this.showSaveChannel();
- },
- },
- backButton: {
- ["@click"]() {
- this.showPlaylists();
- },
- },
- // Event handlers for playlist navigation
- playlistNameClick: {
- ["@click"]() {
- const playlistName = this.$el.dataset.playlistName;
- this.showPlaylist(playlistName);
- },
- },
- truncatedVideosClick: {
- ["@click"]() {
- const playlistName = this.$el.dataset.playlistName;
- this.showPlaylist(playlistName);
- },
- },
- truncatedVideosClick: {
- ["@click"]() {
- const playlistName = this.$el.dataset.playlistName;
- this.showPlaylist(playlistName);
- },
- },
- truncatedVideosDisplay: {
- ["x-show"]() {
- const playlistName = this.$el.dataset.playlistName;
- const playlistData = this.playlistsForDisplay.find(
- (p) => p.name === playlistName,
- );
- return playlistData ? playlistData.shouldShowTruncation : false;
- },
- ["@click"]() {
- const playlistName = this.$el.dataset.playlistName;
- this.showPlaylist(playlistName);
- },
- },
- // CSP-compliant view bindings
- playlistsHeader: {
- ["x-show"]() {
- return this.currentView === "playlists";
- },
- },
- historyHeader: {
- ["x-show"]() {
- return this.currentView === "history";
- },
- },
- playlistViewHeader: {
- ["x-show"]() {
- return this.currentView === "playlist";
- },
- },
- saveChannelHeader: {
- ["x-show"]() {
- return this.currentView === "saveChannel";
- },
- },
- playlistsContainer: {
- ["x-show"]() {
- return this.currentView === "playlists";
- },
- },
- historyContainer: {
- ["x-show"]() {
- return this.currentView === "history";
- },
- },
- playlistViewContainer: {
- ["x-show"]() {
- return this.currentView === "playlist";
- },
- },
- saveChannelContainer: {
- ["x-show"]() {
- return this.currentView === "saveChannel";
- },
- },
- exportContainer: {
- ["x-show"]() {
- return this.currentView === "playlists";
- },
- },
- // History-specific bindings for CSP compliance
- historyEmptyState: {
- ["x-show"]() {
- return this.sortedHistory.length === 0;
- },
- },
- // Playlist view-specific bindings for CSP compliance
- playlistViewEmptyState: {
- ["x-show"]() {
- return this.currentPlaylistVideos.length === 0;
- },
- },
- // Tag chip bindings for CSP compliance
- tagChip: {
- ["@click"]() {
- const videoId = this.$el.dataset.videoId;
- const tag = this.$el.dataset.tag;
- this.toggleTag(videoId, tag);
- },
- [":class"]() {
- const videoId = this.$el.dataset.videoId;
- const tag = this.$el.dataset.tag;
- return {
- active: this.isTagActive(videoId, tag),
- };
- },
- },
- // Save Channel specific bindings
- copyCurlButton: {
- ["@click"]() {
- this.copyCurlCommand();
- },
- [":disabled"]() {
- return !this.isCurrentTabChannelPage;
- },
- },
- categoryButton: {
- ["@click"]() {
- const category = this.$el.dataset.category;
- this.selectCategory(category);
- },
- [":class"]() {
- const category = this.$el.dataset.category;
- return {
- active: this.selectedCategory === category,
- };
- },
- },
- saveChannelNotice: {
- ["x-show"]() {
- return !this.isCurrentTabChannelPage;
- },
- },
- intervalInput: {
- [":value"]() {
- return this.checkInterval;
- },
- ["@input"]() {
- this.updateCheckInterval(this.$el.value);
- },
- },
- // Add current page button binding
- addCurrentPageButton: {
- ["@click"]() {
- this.addCurrentPageToPlaylist();
- },
- [":disabled"]() {
- return (
- !this.currentTab ||
- !this.isCurrentTabYoutube ||
- !this.currentPlaylistName
- );
- },
- [":class"]() {
- return {
- disabled:
- !this.currentTab ||
- !this.isCurrentTabYoutube ||
- !this.currentPlaylistName,
- };
- },
- },
- }));
- });
|