Spaces:
Running
Running
| /** | |
| * VideoCharm - Local Storage Management | |
| * Handles all local storage operations for history and favorites | |
| */ | |
| const StorageManager = (function() { | |
| // Storage keys | |
| const KEYS = { | |
| HISTORY: 'videocharm_history', | |
| FAVORITES: 'videocharm_favorites', | |
| LIKES: 'videocharm_likes', | |
| VIEWS: 'videocharm_views' | |
| }; | |
| // Maximum storage age (7 days in milliseconds) | |
| const MAX_HISTORY_AGE = 7 * 24 * 60 * 60 * 1000; | |
| /** | |
| * Initialize storage with default values if not exists | |
| */ | |
| function initStorage() { | |
| if (!localStorage.getItem(KEYS.HISTORY)) { | |
| localStorage.setItem(KEYS.HISTORY, JSON.stringify([])); | |
| } | |
| if (!localStorage.getItem(KEYS.FAVORITES)) { | |
| localStorage.setItem(KEYS.FAVORITES, JSON.stringify([])); | |
| } | |
| if (!localStorage.getItem(KEYS.LIKES)) { | |
| localStorage.setItem(KEYS.LIKES, JSON.stringify([])); | |
| } | |
| if (!localStorage.getItem(KEYS.VIEWS)) { | |
| localStorage.setItem(KEYS.VIEWS, JSON.stringify(0)); | |
| } | |
| // Clean up old history entries | |
| cleanupHistory(); | |
| } | |
| /** | |
| * Remove history items older than MAX_HISTORY_AGE | |
| */ | |
| function cleanupHistory() { | |
| const history = getHistory(); | |
| const now = new Date().getTime(); | |
| const filteredHistory = history.filter(item => { | |
| return (now - item.timestamp) < MAX_HISTORY_AGE; | |
| }); | |
| if (filteredHistory.length !== history.length) { | |
| localStorage.setItem(KEYS.HISTORY, JSON.stringify(filteredHistory)); | |
| } | |
| } | |
| /** | |
| * Get all history items | |
| * @returns {Array} History items | |
| */ | |
| function getHistory() { | |
| try { | |
| return JSON.parse(localStorage.getItem(KEYS.HISTORY)) || []; | |
| } catch (e) { | |
| console.error('Error parsing history from localStorage:', e); | |
| return []; | |
| } | |
| } | |
| /** | |
| * Get all favorites | |
| * @returns {Array} Favorite items | |
| */ | |
| function getFavorites() { | |
| try { | |
| return JSON.parse(localStorage.getItem(KEYS.FAVORITES)) || []; | |
| } catch (e) { | |
| console.error('Error parsing favorites from localStorage:', e); | |
| return []; | |
| } | |
| } | |
| /** | |
| * Get all liked video IDs | |
| * @returns {Array} Liked video IDs | |
| */ | |
| function getLikes() { | |
| try { | |
| return JSON.parse(localStorage.getItem(KEYS.LIKES)) || []; | |
| } catch (e) { | |
| console.error('Error parsing likes from localStorage:', e); | |
| return []; | |
| } | |
| } | |
| /** | |
| * Get view count | |
| * @returns {Number} Total view count | |
| */ | |
| function getViews() { | |
| try { | |
| return parseInt(JSON.parse(localStorage.getItem(KEYS.VIEWS))) || 0; | |
| } catch (e) { | |
| console.error('Error parsing views from localStorage:', e); | |
| return 0; | |
| } | |
| } | |
| /** | |
| * Add a video to history | |
| * @param {Object} video Video object to add | |
| */ | |
| function addToHistory(video) { | |
| if (!video || !video.id || !video.url) return; | |
| const history = getHistory(); | |
| // Check if video already exists in history | |
| const existingIndex = history.findIndex(item => item.id === video.id); | |
| if (existingIndex !== -1) { | |
| // Update timestamp of existing item | |
| history[existingIndex].timestamp = new Date().getTime(); | |
| // Update thumbnail if available | |
| if (video.thumbnail) { | |
| history[existingIndex].thumbnail = video.thumbnail; | |
| } | |
| } else { | |
| // Create simplified record for storage | |
| const historyItem = { | |
| id: video.id, | |
| url: video.url, | |
| title: video.title || '精彩视频', | |
| timestamp: new Date().getTime(), | |
| duration: video.duration || 0, | |
| thumbnail: video.thumbnail || null | |
| }; | |
| // Add to beginning of array | |
| history.unshift(historyItem); | |
| } | |
| // Save updated history | |
| localStorage.setItem(KEYS.HISTORY, JSON.stringify(history)); | |
| // Increment view count | |
| incrementViews(); | |
| } | |
| /** | |
| * Toggle favorite status for a video | |
| * @param {Object} video Video object | |
| * @returns {Boolean} New favorite status | |
| */ | |
| function toggleFavorite(video) { | |
| if (!video || !video.id) return false; | |
| const favorites = getFavorites(); | |
| const existingIndex = favorites.findIndex(item => item.id === video.id); | |
| if (existingIndex !== -1) { | |
| // Remove from favorites | |
| favorites.splice(existingIndex, 1); | |
| localStorage.setItem(KEYS.FAVORITES, JSON.stringify(favorites)); | |
| return false; | |
| } else { | |
| // Create simplified record for storage | |
| const favoriteItem = { | |
| id: video.id, | |
| url: video.url, | |
| title: video.title || '精彩视频', | |
| timestamp: new Date().getTime(), | |
| duration: video.duration || 0, | |
| thumbnail: video.thumbnail || null | |
| }; | |
| // Add to favorites | |
| favorites.unshift(favoriteItem); | |
| localStorage.setItem(KEYS.FAVORITES, JSON.stringify(favorites)); | |
| return true; | |
| } | |
| } | |
| /** | |
| * Check if a video is favorited | |
| * @param {String} videoId Video ID to check | |
| * @returns {Boolean} Whether video is favorited | |
| */ | |
| function isFavorite(videoId) { | |
| if (!videoId) return false; | |
| const favorites = getFavorites(); | |
| return favorites.some(item => item.id === videoId); | |
| } | |
| /** | |
| * Toggle like status for a video | |
| * @param {String} videoId Video ID | |
| * @returns {Boolean} New like status | |
| */ | |
| function toggleLike(videoId) { | |
| if (!videoId) return false; | |
| const likes = getLikes(); | |
| const index = likes.indexOf(videoId); | |
| if (index !== -1) { | |
| // Remove like | |
| likes.splice(index, 1); | |
| localStorage.setItem(KEYS.LIKES, JSON.stringify(likes)); | |
| return false; | |
| } else { | |
| // Add like | |
| likes.push(videoId); | |
| localStorage.setItem(KEYS.LIKES, JSON.stringify(likes)); | |
| return true; | |
| } | |
| } | |
| /** | |
| * Check if a video is liked | |
| * @param {String} videoId Video ID to check | |
| * @returns {Boolean} Whether video is liked | |
| */ | |
| function isLiked(videoId) { | |
| if (!videoId) return false; | |
| const likes = getLikes(); | |
| return likes.includes(videoId); | |
| } | |
| /** | |
| * Increment view count | |
| */ | |
| function incrementViews() { | |
| const views = getViews(); | |
| localStorage.setItem(KEYS.VIEWS, JSON.stringify(views + 1)); | |
| } | |
| /** | |
| * Clear all history | |
| */ | |
| function clearHistory() { | |
| localStorage.setItem(KEYS.HISTORY, JSON.stringify([])); | |
| } | |
| /** | |
| * Clear all favorites | |
| */ | |
| function clearFavorites() { | |
| localStorage.setItem(KEYS.FAVORITES, JSON.stringify([])); | |
| } | |
| // Public API | |
| return { | |
| init: initStorage, | |
| getHistory: getHistory, | |
| getFavorites: getFavorites, | |
| getLikes: getLikes, | |
| getViews: getViews, | |
| addToHistory: addToHistory, | |
| toggleFavorite: toggleFavorite, | |
| isFavorite: isFavorite, | |
| toggleLike: toggleLike, | |
| isLiked: isLiked, | |
| clearHistory: clearHistory, | |
| clearFavorites: clearFavorites | |
| }; | |
| })(); |