app.js 109 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753
  1. // GrabZilla 2.1 - Application Entry Point
  2. // Modular architecture with clear separation of concerns
  3. // Wrap entire file in try-catch to ensure initializeGrabZilla always gets defined
  4. (function() {
  5. // Use logger from window (loaded by scripts/utils/logger.js)
  6. const logger = window.logger || console;
  7. class GrabZillaApp {
  8. constructor() {
  9. this.state = null;
  10. this.eventBus = null;
  11. this.initialized = false;
  12. this.modules = new Map();
  13. }
  14. // Initialize the application
  15. async init() {
  16. try {
  17. logger.debug('🚀 Initializing GrabZilla 2.1...');
  18. // Initialize event bus
  19. this.eventBus = window.eventBus;
  20. if (!this.eventBus) {
  21. throw new Error('EventBus not available');
  22. }
  23. // Initialize application state
  24. this.state = new window.AppState();
  25. if (!this.state) {
  26. throw new Error('AppState not available');
  27. }
  28. // Expose state globally for Video model to access current defaults
  29. window.appState = this.state;
  30. // Set up error handling
  31. this.setupErrorHandling();
  32. // Initialize UI components
  33. await this.initializeUI();
  34. // Set up event listeners
  35. this.setupEventListeners();
  36. // Load saved state if available
  37. await this.loadState();
  38. // Ensure save directory exists
  39. await this.ensureSaveDirectoryExists();
  40. // Check binary status and validate
  41. await this.checkAndValidateBinaries();
  42. // Initialize keyboard navigation
  43. this.initializeKeyboardNavigation();
  44. this.initialized = true;
  45. logger.debug('✅ GrabZilla 2.1 initialized successfully');
  46. // Notify that the app is ready
  47. this.eventBus.emit('app:ready', { app: this });
  48. } catch (error) {
  49. logger.error('❌ Failed to initialize GrabZilla:', error.message);
  50. this.handleInitializationError(error);
  51. }
  52. }
  53. // Set up global error handling
  54. setupErrorHandling() {
  55. // Handle unhandled errors
  56. window.addEventListener('error', (event) => {
  57. logger.error('Global error:', event.error);
  58. this.eventBus.emit('app:error', {
  59. type: 'global',
  60. error: event.error,
  61. filename: event.filename,
  62. lineno: event.lineno
  63. });
  64. });
  65. // Handle unhandled promise rejections
  66. window.addEventListener('unhandledrejection', (event) => {
  67. logger.error('Unhandled promise rejection:', event.reason);
  68. this.eventBus.emit('app:error', {
  69. type: 'promise',
  70. error: event.reason
  71. });
  72. });
  73. // Listen for application errors
  74. this.eventBus.on('app:error', (errorData) => {
  75. // Handle errors appropriately
  76. this.displayError(errorData);
  77. });
  78. }
  79. // Initialize UI components
  80. async initializeUI() {
  81. // Update save path display
  82. this.updateSavePathDisplay();
  83. // Initialize dropdown values
  84. this.initializeDropdowns();
  85. // Set up video list
  86. this.initializeVideoList();
  87. // Set up status display
  88. this.updateStatusMessage('Ready to download videos');
  89. }
  90. // Set up main event listeners
  91. setupEventListeners() {
  92. // State change listeners
  93. this.state.on('videoAdded', (data) => this.onVideoAdded(data));
  94. this.state.on('videoRemoved', (data) => this.onVideoRemoved(data));
  95. this.state.on('videoUpdated', (data) => this.onVideoUpdated(data));
  96. this.state.on('videosReordered', (data) => this.onVideosReordered(data));
  97. this.state.on('videosCleared', (data) => this.onVideosCleared(data));
  98. this.state.on('configUpdated', (data) => this.onConfigUpdated(data));
  99. this.state.on('videoSelectionChanged', (data) => this.onVideoSelectionChanged(data));
  100. // UI event listeners
  101. this.setupButtonEventListeners();
  102. this.setupInputEventListeners();
  103. this.setupVideoListEventListeners();
  104. }
  105. // Set up button event listeners
  106. setupButtonEventListeners() {
  107. // Add Video button
  108. const addVideoBtn = document.getElementById('addVideoBtn');
  109. if (addVideoBtn) {
  110. addVideoBtn.addEventListener('click', () => this.handleAddVideo());
  111. }
  112. // Import URLs button
  113. const importUrlsBtn = document.getElementById('importUrlsBtn');
  114. if (importUrlsBtn) {
  115. importUrlsBtn.addEventListener('click', () => this.handleImportUrls());
  116. }
  117. // Save Path button
  118. const savePathBtn = document.getElementById('savePathBtn');
  119. if (savePathBtn) {
  120. savePathBtn.addEventListener('click', () => this.handleSelectSavePath());
  121. }
  122. // Note: Cookie file and save path buttons removed from main panel,
  123. // now only accessible via Settings modal
  124. // Clipboard monitoring toggle
  125. const clipboardToggle = document.getElementById('clipboardMonitorToggle');
  126. if (clipboardToggle) {
  127. clipboardToggle.addEventListener('change', (e) => this.handleClipboardToggle(e.target.checked));
  128. }
  129. // Control panel buttons
  130. const clearListBtn = document.getElementById('clearListBtn');
  131. if (clearListBtn) {
  132. clearListBtn.addEventListener('click', () => this.handleClearList());
  133. }
  134. const downloadVideosBtn = document.getElementById('downloadVideosBtn');
  135. if (downloadVideosBtn) {
  136. downloadVideosBtn.addEventListener('click', () => this.handleDownloadVideos());
  137. }
  138. const cancelDownloadsBtn = document.getElementById('cancelDownloadsBtn');
  139. if (cancelDownloadsBtn) {
  140. cancelDownloadsBtn.addEventListener('click', () => this.handleCancelDownloads());
  141. }
  142. const updateDepsBtn = document.getElementById('updateDepsBtn');
  143. if (updateDepsBtn) {
  144. updateDepsBtn.addEventListener('click', () => this.handleUpdateDependencies());
  145. }
  146. const exportListBtn = document.getElementById('exportListBtn');
  147. if (exportListBtn) {
  148. exportListBtn.addEventListener('click', () => this.handleExportList());
  149. }
  150. const importListBtn = document.getElementById('importListBtn');
  151. if (importListBtn) {
  152. importListBtn.addEventListener('click', () => this.handleImportList());
  153. }
  154. const settingsBtn = document.getElementById('settingsBtn');
  155. if (settingsBtn) {
  156. settingsBtn.addEventListener('click', () => this.showSettingsModal());
  157. }
  158. const settingsBtn2 = document.getElementById('settingsBtn2');
  159. if (settingsBtn2) {
  160. settingsBtn2.addEventListener('click', () => this.showSettingsModal());
  161. }
  162. const showHistoryBtn = document.getElementById('showHistoryBtn');
  163. if (showHistoryBtn) {
  164. showHistoryBtn.addEventListener('click', () => this.showHistoryModal());
  165. }
  166. }
  167. // Set up input event listeners
  168. setupInputEventListeners() {
  169. // URL input - no paste handler needed, user clicks "Add Video" button
  170. const urlInput = document.getElementById('urlInput');
  171. if (urlInput) {
  172. // Optional: could add real-time validation feedback here
  173. }
  174. // Configuration inputs
  175. const defaultQuality = document.getElementById('defaultQuality');
  176. if (defaultQuality) {
  177. defaultQuality.addEventListener('change', (e) => {
  178. const newValue = e.target.value;
  179. this.state.updateConfig({ defaultQuality: newValue });
  180. // Ask if user wants to update existing videos
  181. this.promptUpdateExistingVideos('quality', newValue);
  182. });
  183. }
  184. const defaultFormat = document.getElementById('defaultFormat');
  185. if (defaultFormat) {
  186. defaultFormat.addEventListener('change', (e) => {
  187. const newValue = e.target.value;
  188. this.state.updateConfig({ defaultFormat: newValue });
  189. // Ask if user wants to update existing videos
  190. this.promptUpdateExistingVideos('format', newValue);
  191. });
  192. }
  193. }
  194. // Set up video list event listeners
  195. setupVideoListEventListeners() {
  196. const videoList = document.getElementById('videoList');
  197. if (videoList) {
  198. videoList.addEventListener('click', (e) => this.handleVideoListClick(e));
  199. videoList.addEventListener('change', (e) => this.handleVideoListChange(e));
  200. this.setupDragAndDrop(videoList);
  201. }
  202. }
  203. // Set up drag-and-drop reordering
  204. setupDragAndDrop(videoList) {
  205. let draggedElement = null;
  206. let draggedVideoId = null;
  207. videoList.addEventListener('dragstart', (e) => {
  208. const videoItem = e.target.closest('.video-item');
  209. if (!videoItem) return;
  210. draggedElement = videoItem;
  211. draggedVideoId = videoItem.dataset.videoId;
  212. videoItem.classList.add('opacity-50');
  213. e.dataTransfer.effectAllowed = 'move';
  214. e.dataTransfer.setData('text/html', videoItem.innerHTML);
  215. });
  216. videoList.addEventListener('dragover', (e) => {
  217. e.preventDefault();
  218. const videoItem = e.target.closest('.video-item');
  219. if (!videoItem || videoItem === draggedElement) return;
  220. e.dataTransfer.dropEffect = 'move';
  221. // Visual feedback - show where it will drop
  222. const rect = videoItem.getBoundingClientRect();
  223. const midpoint = rect.top + rect.height / 2;
  224. if (e.clientY < midpoint) {
  225. videoItem.classList.add('border-t-2', 'border-[#155dfc]');
  226. videoItem.classList.remove('border-b-2');
  227. } else {
  228. videoItem.classList.add('border-b-2', 'border-[#155dfc]');
  229. videoItem.classList.remove('border-t-2');
  230. }
  231. });
  232. videoList.addEventListener('dragleave', (e) => {
  233. const videoItem = e.target.closest('.video-item');
  234. if (videoItem) {
  235. videoItem.classList.remove('border-t-2', 'border-b-2', 'border-[#155dfc]');
  236. }
  237. });
  238. videoList.addEventListener('drop', (e) => {
  239. e.preventDefault();
  240. const targetItem = e.target.closest('.video-item');
  241. if (!targetItem || !draggedVideoId) return;
  242. const targetVideoId = targetItem.dataset.videoId;
  243. // Calculate drop position
  244. const rect = targetItem.getBoundingClientRect();
  245. const midpoint = rect.top + rect.height / 2;
  246. const dropBefore = e.clientY < midpoint;
  247. // Reorder in state
  248. this.handleVideoReorder(draggedVideoId, targetVideoId, dropBefore);
  249. // Clean up visual feedback
  250. targetItem.classList.remove('border-t-2', 'border-b-2', 'border-[#155dfc]');
  251. });
  252. videoList.addEventListener('dragend', (e) => {
  253. const videoItem = e.target.closest('.video-item');
  254. if (videoItem) {
  255. videoItem.classList.remove('opacity-50');
  256. }
  257. // Clean up all visual feedback
  258. document.querySelectorAll('.video-item').forEach(item => {
  259. item.classList.remove('border-t-2', 'border-b-2', 'border-[#155dfc]');
  260. });
  261. draggedElement = null;
  262. draggedVideoId = null;
  263. });
  264. }
  265. handleVideoReorder(draggedId, targetId, insertBefore) {
  266. const videos = this.state.getVideos();
  267. const draggedIndex = videos.findIndex(v => v.id === draggedId);
  268. const targetIndex = videos.findIndex(v => v.id === targetId);
  269. if (draggedIndex === -1 || targetIndex === -1) return;
  270. let newIndex = targetIndex;
  271. if (draggedIndex < targetIndex && !insertBefore) {
  272. newIndex = targetIndex;
  273. } else if (draggedIndex > targetIndex && insertBefore) {
  274. newIndex = targetIndex;
  275. } else if (insertBefore) {
  276. newIndex = targetIndex;
  277. } else {
  278. newIndex = targetIndex + 1;
  279. }
  280. this.state.reorderVideos(draggedIndex, newIndex);
  281. }
  282. // Handle clicks in video list (checkboxes, delete buttons)
  283. handleVideoListClick(event) {
  284. const target = event.target;
  285. const videoItem = target.closest('.video-item');
  286. if (!videoItem) return;
  287. const videoId = videoItem.dataset.videoId;
  288. if (!videoId) return;
  289. // Handle checkbox click
  290. if (target.closest('.video-checkbox')) {
  291. event.preventDefault();
  292. this.toggleVideoSelection(videoId);
  293. return;
  294. }
  295. // Handle thumbnail click (preview)
  296. if (target.closest('.video-thumbnail-container')) {
  297. event.preventDefault();
  298. const previewUrl = target.closest('.video-thumbnail-container').dataset.previewUrl;
  299. if (previewUrl) {
  300. this.showVideoPreview(videoId, previewUrl);
  301. }
  302. return;
  303. }
  304. // Handle delete button click
  305. if (target.closest('.delete-video-btn')) {
  306. event.preventDefault();
  307. this.handleRemoveVideo(videoId);
  308. return;
  309. }
  310. // Handle pause/resume button click
  311. if (target.closest('.pause-resume-btn')) {
  312. event.preventDefault();
  313. const btn = target.closest('.pause-resume-btn');
  314. const action = btn.dataset.action;
  315. if (action === 'pause') {
  316. this.handlePauseDownload(videoId);
  317. } else if (action === 'resume') {
  318. this.handleResumeDownload(videoId);
  319. }
  320. return;
  321. }
  322. }
  323. // Handle dropdown changes in video list (quality, format)
  324. handleVideoListChange(event) {
  325. const target = event.target;
  326. const videoItem = target.closest('.video-item');
  327. if (!videoItem) return;
  328. const videoId = videoItem.dataset.videoId;
  329. if (!videoId) return;
  330. // Handle quality dropdown change
  331. if (target.classList.contains('quality-select')) {
  332. const quality = target.value;
  333. this.state.updateVideo(videoId, { quality });
  334. logger.debug(`Updated video ${videoId} quality to ${quality}`);
  335. return;
  336. }
  337. // Handle format dropdown change
  338. if (target.classList.contains('format-select')) {
  339. const format = target.value;
  340. this.state.updateVideo(videoId, { format });
  341. logger.debug(`Updated video ${videoId} format to ${format}`);
  342. return;
  343. }
  344. }
  345. // Toggle video selection
  346. toggleVideoSelection(videoId) {
  347. this.state.toggleVideoSelection(videoId);
  348. this.updateVideoCheckbox(videoId);
  349. }
  350. // Update checkbox visual state
  351. updateVideoCheckbox(videoId) {
  352. const videoItem = document.querySelector(`[data-video-id="${videoId}"]`);
  353. if (!videoItem) return;
  354. const checkbox = videoItem.querySelector('.video-checkbox');
  355. if (!checkbox) return;
  356. const isSelected = this.state.ui.selectedVideos.includes(videoId);
  357. checkbox.setAttribute('aria-checked', isSelected ? 'true' : 'false');
  358. // Update checkbox SVG
  359. const svg = checkbox.querySelector('svg');
  360. if (svg) {
  361. if (isSelected) {
  362. svg.innerHTML = `<rect x="3" y="3" width="10" height="10" stroke="currentColor" stroke-width="1.5" fill="currentColor" rx="2" />
  363. <path d="M5 8L7 10L11 6" stroke="white" stroke-width="1.5" fill="none" stroke-linecap="round" stroke-linejoin="round"/>`;
  364. } else {
  365. svg.innerHTML = `<rect x="3" y="3" width="10" height="10" stroke="currentColor" stroke-width="1.5" fill="none" rx="2" />`;
  366. }
  367. }
  368. }
  369. // Remove video from list
  370. handleRemoveVideo(videoId) {
  371. try {
  372. const video = this.state.getVideo(videoId);
  373. if (video && confirm(`Remove "${video.getDisplayName()}"?`)) {
  374. this.state.removeVideo(videoId);
  375. this.updateStatusMessage('Video removed');
  376. }
  377. } catch (error) {
  378. logger.error('Error removing video:', error.message);
  379. this.showError(`Failed to remove video: ${error.message}`);
  380. }
  381. }
  382. /**
  383. * Check for duplicate URLs in the list
  384. * @param {string[]} urls - URLs to check
  385. * @returns {Object} Object with unique and duplicate URLs
  386. */
  387. checkForDuplicates(urls) {
  388. const unique = [];
  389. const duplicates = [];
  390. for (const url of urls) {
  391. const normalizedUrl = window.URLValidator ? window.URLValidator.normalizeUrl(url) : url;
  392. const existingVideo = this.state.videos.find(v => v.getNormalizedUrl() === normalizedUrl);
  393. if (existingVideo) {
  394. duplicates.push({
  395. url,
  396. existingVideo
  397. });
  398. } else {
  399. unique.push(url);
  400. }
  401. }
  402. return { unique, duplicates };
  403. }
  404. /**
  405. * Show dialog for handling duplicate URLs
  406. * @param {Object} duplicateInfo - Info about duplicates
  407. * @returns {Promise<string>} Action: 'skip', 'replace', 'keep-both', or null (cancel)
  408. */
  409. async handleDuplicateUrls(duplicateInfo) {
  410. const duplicateCount = duplicateInfo.duplicates.length;
  411. const uniqueCount = duplicateInfo.unique.length;
  412. // Show titles of duplicate videos
  413. const duplicateTitles = duplicateInfo.duplicates
  414. .map(dup => `• ${dup.existingVideo.title}`)
  415. .slice(0, 5) // Show max 5
  416. .join('\n');
  417. const moreText = duplicateCount > 5 ? `\n... and ${duplicateCount - 5} more` : '';
  418. const message =
  419. `Found ${duplicateCount} duplicate URL(s):\n\n` +
  420. duplicateTitles + moreText + '\n\n' +
  421. `Choose an action:\n\n` +
  422. `1. SKIP duplicates (add ${uniqueCount} new video(s) only)\n` +
  423. `2. REPLACE existing videos with new ones\n` +
  424. `3. KEEP BOTH (add duplicates again)\n\n` +
  425. `Enter 1, 2, or 3:`;
  426. const choice = prompt(message);
  427. if (choice === '1') {
  428. return 'skip';
  429. } else if (choice === '2') {
  430. return 'replace';
  431. } else if (choice === '3') {
  432. return 'keep-both';
  433. } else {
  434. return null; // Cancel
  435. }
  436. }
  437. /**
  438. * Handle playlist URL - show modal with all videos
  439. * @param {string} playlistUrl - YouTube playlist URL
  440. */
  441. async handlePlaylistUrl(playlistUrl) {
  442. try {
  443. this.updateStatusMessage('Extracting playlist...');
  444. const result = await window.electronAPI.extractPlaylistVideos(playlistUrl);
  445. if (!result.success) {
  446. this.showError('Failed to extract playlist');
  447. return;
  448. }
  449. this.showPlaylistModal(result);
  450. } catch (error) {
  451. logger.error('Error handling playlist:', error.message);
  452. this.showError(`Playlist extraction failed: ${error.message}`);
  453. }
  454. }
  455. /**
  456. * Show playlist modal with video list
  457. * @param {Object} playlistData - Playlist data from extraction
  458. */
  459. showPlaylistModal(playlistData) {
  460. const modal = document.getElementById('playlistModal');
  461. const title = document.getElementById('playlistTitle');
  462. const info = document.getElementById('playlistInfo');
  463. const videoList = document.getElementById('playlistVideoList');
  464. if (!modal || !title || !info || !videoList) return;
  465. // Update modal content
  466. title.textContent = `Playlist (${playlistData.videoCount} videos)`;
  467. info.textContent = `${playlistData.videoCount} video(s) found in this playlist`;
  468. // Clear previous video list
  469. videoList.innerHTML = '';
  470. // Store playlist videos for later use
  471. this.currentPlaylistVideos = playlistData.videos;
  472. // Create checkbox for each video
  473. playlistData.videos.forEach((video, index) => {
  474. const videoItem = document.createElement('label');
  475. videoItem.className = 'flex items-center gap-3 p-2 hover:bg-[#45556c]/30 rounded cursor-pointer';
  476. // Check if video is private/unavailable (deselect by default)
  477. const isPrivate = video.title.includes('[Private') ||
  478. video.title.includes('Private video') ||
  479. video.title.includes('[Deleted') ||
  480. video.title.includes('Unavailable');
  481. const checkedAttr = isPrivate ? '' : 'checked';
  482. videoItem.innerHTML = `
  483. <input type="checkbox" class="playlist-video-checkbox w-4 h-4" data-index="${index}" ${checkedAttr}>
  484. <div class="flex-1 min-w-0">
  485. <p class="text-sm text-white truncate">${video.title}</p>
  486. <p class="text-xs text-[#90a1b9]">${video.duration ? this.formatDuration(video.duration) : 'Unknown duration'}</p>
  487. </div>
  488. `;
  489. videoList.appendChild(videoItem);
  490. });
  491. // Setup modal event listeners
  492. this.setupPlaylistModalListeners();
  493. // Show modal
  494. modal.classList.remove('hidden');
  495. modal.classList.add('flex');
  496. }
  497. /**
  498. * Setup event listeners for playlist modal
  499. */
  500. setupPlaylistModalListeners() {
  501. const modal = document.getElementById('playlistModal');
  502. const closeBtn = document.getElementById('closePlaylistModal');
  503. const cancelBtn = document.getElementById('cancelPlaylistBtn');
  504. const downloadBtn = document.getElementById('downloadSelectedPlaylistBtn');
  505. const selectAllCheckbox = document.getElementById('selectAllPlaylistVideos');
  506. // Close modal handlers
  507. const closeModal = () => {
  508. modal.classList.remove('flex');
  509. modal.classList.add('hidden');
  510. this.currentPlaylistVideos = null;
  511. };
  512. closeBtn?.addEventListener('click', closeModal);
  513. cancelBtn?.addEventListener('click', closeModal);
  514. // Select all handler
  515. selectAllCheckbox?.addEventListener('change', (e) => {
  516. const checkboxes = document.querySelectorAll('.playlist-video-checkbox');
  517. checkboxes.forEach(cb => cb.checked = e.target.checked);
  518. });
  519. // Download selected handler
  520. downloadBtn?.addEventListener('click', async () => {
  521. const checkboxes = document.querySelectorAll('.playlist-video-checkbox:checked');
  522. const selectedIndices = Array.from(checkboxes).map(cb => parseInt(cb.dataset.index));
  523. if (selectedIndices.length === 0) {
  524. this.showError('Please select at least one video');
  525. return;
  526. }
  527. const selectedUrls = selectedIndices.map(i => this.currentPlaylistVideos[i].url);
  528. // Add selected videos to queue
  529. const results = await this.state.addVideosFromUrls(selectedUrls);
  530. this.showToast(`Added ${results.successful.length} video(s) from playlist`, 'success');
  531. closeModal();
  532. });
  533. }
  534. /**
  535. * Format duration in seconds to MM:SS
  536. * @param {number} seconds - Duration in seconds
  537. * @returns {string} Formatted duration
  538. */
  539. formatDuration(seconds) {
  540. if (!seconds || isNaN(seconds)) return 'Unknown';
  541. const mins = Math.floor(seconds / 60);
  542. const secs = Math.floor(seconds % 60);
  543. return `${mins}:${secs.toString().padStart(2, '0')}`;
  544. }
  545. /**
  546. * Show video preview modal
  547. * @param {string} videoId - Video ID
  548. * @param {string} url - Video URL
  549. */
  550. async showVideoPreview(videoId, url) {
  551. const modal = document.getElementById('previewModal');
  552. const player = document.getElementById('previewPlayer');
  553. const title = document.getElementById('previewTitle');
  554. const duration = document.getElementById('previewDuration');
  555. const views = document.getElementById('previewViews');
  556. const likes = document.getElementById('previewLikes');
  557. const description = document.getElementById('previewDescription');
  558. const downloadBtn = document.getElementById('downloadFromPreviewBtn');
  559. if (!modal || !player) return;
  560. const video = this.state.getVideo(videoId);
  561. if (!video) return;
  562. // Store current video for download button
  563. this.currentPreviewVideoId = videoId;
  564. // Set title
  565. title.textContent = video.title || video.url;
  566. // Extract video ID and create embed URL
  567. let embedUrl = '';
  568. if (url.includes('youtube.com') || url.includes('youtu.be')) {
  569. const youtubeIdMatch = url.match(/(?:youtube\.com\/(?:[^\/\n\s]+\/\S+\/|(?:v|e(?:mbed)?)\/|\S*?[?&]v=)|youtu\.be\/)([a-zA-Z0-9_-]{11})/);
  570. if (youtubeIdMatch) {
  571. embedUrl = `https://www.youtube.com/embed/${youtubeIdMatch[1]}`;
  572. }
  573. } else if (url.includes('vimeo.com')) {
  574. const vimeoIdMatch = url.match(/vimeo\.com\/(?:video\/)?(\d+)/);
  575. if (vimeoIdMatch) {
  576. embedUrl = `https://player.vimeo.com/video/${vimeoIdMatch[1]}`;
  577. }
  578. }
  579. if (!embedUrl) {
  580. this.showError('Preview not available for this video');
  581. return;
  582. }
  583. // Set iframe src
  584. player.src = embedUrl;
  585. // Set duration
  586. if (video.duration) {
  587. duration.querySelector('span').textContent = video.duration;
  588. } else {
  589. duration.querySelector('span').textContent = '--:--';
  590. }
  591. // Show loading state for other info
  592. views.querySelector('span').textContent = 'Loading...';
  593. likes.querySelector('span').textContent = 'Loading...';
  594. description.textContent = 'Loading video information...';
  595. // Fetch full metadata (views, likes, description)
  596. try {
  597. const metadata = await window.electronAPI.getVideoMetadata(url);
  598. if (metadata.views) {
  599. views.querySelector('span').textContent = this.formatNumber(metadata.views);
  600. }
  601. if (metadata.likes) {
  602. likes.querySelector('span').textContent = this.formatNumber(metadata.likes);
  603. }
  604. if (metadata.description) {
  605. description.textContent = metadata.description.slice(0, 500) + (metadata.description.length > 500 ? '...' : '');
  606. }
  607. } catch (error) {
  608. logger.error('Error fetching preview metadata:', error.message);
  609. views.querySelector('span').textContent = 'N/A';
  610. likes.querySelector('span').textContent = 'N/A';
  611. description.textContent = 'Unable to load video information.';
  612. }
  613. // Setup modal event listeners
  614. this.setupPreviewModalListeners();
  615. // Show modal
  616. modal.classList.remove('hidden');
  617. modal.classList.add('flex');
  618. }
  619. /**
  620. * Setup event listeners for preview modal
  621. */
  622. setupPreviewModalListeners() {
  623. const modal = document.getElementById('previewModal');
  624. const closeBtn = document.getElementById('closePreviewModal');
  625. const closeBtn2 = document.getElementById('closePreviewBtn');
  626. const downloadBtn = document.getElementById('downloadFromPreviewBtn');
  627. const player = document.getElementById('previewPlayer');
  628. const closeModal = () => {
  629. modal.classList.remove('flex');
  630. modal.classList.add('hidden');
  631. player.src = ''; // Stop video playback
  632. this.currentPreviewVideoId = null;
  633. };
  634. closeBtn?.addEventListener('click', closeModal);
  635. closeBtn2?.addEventListener('click', closeModal);
  636. downloadBtn?.addEventListener('click', async () => {
  637. if (this.currentPreviewVideoId) {
  638. // Mark video as selected and trigger download
  639. const video = this.state.getVideo(this.currentPreviewVideoId);
  640. if (video && video.status === 'ready') {
  641. // Select this video only
  642. this.state.clearVideoSelection();
  643. this.state.toggleVideoSelection(this.currentPreviewVideoId);
  644. // Trigger download
  645. await this.handleDownloadVideos();
  646. }
  647. }
  648. closeModal();
  649. });
  650. }
  651. /**
  652. * Format number with K/M suffix
  653. * @param {number} num - Number to format
  654. * @returns {string} Formatted number
  655. */
  656. formatNumber(num) {
  657. if (!num || isNaN(num)) return 'N/A';
  658. if (num >= 1000000) {
  659. return (num / 1000000).toFixed(1) + 'M';
  660. } else if (num >= 1000) {
  661. return (num / 1000).toFixed(1) + 'K';
  662. }
  663. return num.toString();
  664. }
  665. /**
  666. * Show toast notification
  667. * @param {string} message - Message to display
  668. * @param {string} type - Type of toast: 'success', 'error', 'warning', 'info'
  669. * @param {number} duration - Duration in milliseconds (default: 4000)
  670. */
  671. showToast(message, type = 'info', duration = 4000) {
  672. const container = document.getElementById('toastContainer');
  673. if (!container) return;
  674. // Create toast element
  675. const toast = document.createElement('div');
  676. toast.className = 'toast bg-[#314158] rounded-lg shadow-lg p-4 flex items-start gap-3 border border-[#45556c]';
  677. // Icon based on type
  678. let icon = '';
  679. let iconColor = '';
  680. switch (type) {
  681. case 'success':
  682. iconColor = '#00a63e';
  683. icon = `<svg width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="${iconColor}" stroke-width="2">
  684. <path d="M22 11.08V12a10 10 0 1 1-5.93-9.14"/>
  685. <polyline points="22 4 12 14.01 9 11.01"/>
  686. </svg>`;
  687. break;
  688. case 'error':
  689. iconColor = '#e7000b';
  690. icon = `<svg width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="${iconColor}" stroke-width="2">
  691. <circle cx="12" cy="12" r="10"/>
  692. <line x1="15" y1="9" x2="9" y2="15"/>
  693. <line x1="9" y1="9" x2="15" y2="15"/>
  694. </svg>`;
  695. break;
  696. case 'warning':
  697. iconColor = '#ffa500';
  698. icon = `<svg width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="${iconColor}" stroke-width="2">
  699. <path d="M10.29 3.86L1.82 18a2 2 0 0 0 1.71 3h16.94a2 2 0 0 0 1.71-3L13.71 3.86a2 2 0 0 0-3.42 0z"/>
  700. <line x1="12" y1="9" x2="12" y2="13"/>
  701. <line x1="12" y1="17" x2="12.01" y2="17"/>
  702. </svg>`;
  703. break;
  704. default: // info
  705. iconColor = '#155dfc';
  706. icon = `<svg width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="${iconColor}" stroke-width="2">
  707. <circle cx="12" cy="12" r="10"/>
  708. <line x1="12" y1="16" x2="12" y2="12"/>
  709. <line x1="12" y1="8" x2="12.01" y2="8"/>
  710. </svg>`;
  711. }
  712. toast.innerHTML = `
  713. <div class="flex-shrink-0">${icon}</div>
  714. <div class="flex-1 text-sm text-[#cad5e2]">${message}</div>
  715. <button class="toast-close flex-shrink-0 text-[#90a1b9] hover:text-white transition-colors">
  716. <svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
  717. <line x1="18" y1="6" x2="6" y2="18"/>
  718. <line x1="6" y1="6" x2="18" y2="18"/>
  719. </svg>
  720. </button>
  721. `;
  722. // Add to container
  723. container.appendChild(toast);
  724. // Close button handler
  725. const closeBtn = toast.querySelector('.toast-close');
  726. closeBtn.addEventListener('click', () => {
  727. this.removeToast(toast);
  728. });
  729. // Auto-remove after duration
  730. setTimeout(() => {
  731. this.removeToast(toast);
  732. }, duration);
  733. }
  734. /**
  735. * Remove toast notification
  736. * @param {HTMLElement} toast - Toast element to remove
  737. */
  738. removeToast(toast) {
  739. if (!toast || !toast.parentElement) return;
  740. toast.classList.add('removing');
  741. setTimeout(() => {
  742. if (toast.parentElement) {
  743. toast.parentElement.removeChild(toast);
  744. }
  745. }, 300);
  746. }
  747. /**
  748. * Show settings modal
  749. */
  750. showSettingsModal() {
  751. const modal = document.getElementById('settingsModal');
  752. if (!modal) return;
  753. // Load current settings into form
  754. this.loadSettingsIntoModal();
  755. // Set up tab switching
  756. this.setupSettingsTabs();
  757. // Set up concurrent downloads slider
  758. const concurrentSlider = document.getElementById('settings-concurrent-downloads');
  759. const concurrentValue = document.getElementById('concurrent-value');
  760. if (concurrentSlider && concurrentValue) {
  761. concurrentSlider.addEventListener('input', (e) => {
  762. concurrentValue.textContent = e.target.value;
  763. });
  764. }
  765. // Setup event listeners
  766. this.setupSettingsModalListeners();
  767. // Show modal
  768. modal.classList.remove('hidden');
  769. modal.classList.add('flex');
  770. }
  771. /**
  772. * Load current settings into modal form
  773. */
  774. loadSettingsIntoModal() {
  775. // General tab
  776. const savePathInput = document.getElementById('settings-save-path');
  777. if (savePathInput) {
  778. savePathInput.value = this.state.config.savePath || '';
  779. }
  780. // Downloads tab
  781. const concurrentSlider = document.getElementById('settings-concurrent-downloads');
  782. const concurrentValue = document.getElementById('concurrent-value');
  783. const concurrentDownloads = this.state.config.concurrentDownloads || 3;
  784. if (concurrentSlider) concurrentSlider.value = concurrentDownloads;
  785. if (concurrentValue) concurrentValue.textContent = concurrentDownloads;
  786. // Advanced tab
  787. const cookieFileInput = document.getElementById('settings-cookie-file');
  788. if (cookieFileInput) {
  789. cookieFileInput.value = this.state.config.cookieFile || '';
  790. }
  791. }
  792. /**
  793. * Setup tab switching for settings modal
  794. */
  795. setupSettingsTabs() {
  796. const tabs = document.querySelectorAll('.settings-tab');
  797. const contents = document.querySelectorAll('.settings-content');
  798. tabs.forEach(tab => {
  799. tab.addEventListener('click', () => {
  800. // Remove active class from all tabs
  801. tabs.forEach(t => t.classList.remove('active'));
  802. // Add active class to clicked tab
  803. tab.classList.add('active');
  804. // Hide all content
  805. contents.forEach(c => c.classList.add('hidden'));
  806. // Show selected content
  807. const tabName = tab.dataset.tab;
  808. const content = document.getElementById(`tab-${tabName}`);
  809. if (content) content.classList.remove('hidden');
  810. });
  811. });
  812. }
  813. /**
  814. * Setup event listeners for settings modal
  815. */
  816. setupSettingsModalListeners() {
  817. const modal = document.getElementById('settingsModal');
  818. const closeBtn = document.getElementById('closeSettingsModal');
  819. const cancelBtn = document.getElementById('cancelSettingsBtn');
  820. const saveBtn = document.getElementById('saveSettingsBtn');
  821. const changePathBtn = document.getElementById('settings-change-path');
  822. const selectCookieBtn = document.getElementById('settings-select-cookie');
  823. const clearCookieBtn = document.getElementById('settings-clear-cookie');
  824. const closeModal = () => {
  825. modal.classList.remove('flex');
  826. modal.classList.add('hidden');
  827. };
  828. closeBtn?.addEventListener('click', closeModal);
  829. cancelBtn?.addEventListener('click', closeModal);
  830. // Save settings
  831. saveBtn?.addEventListener('click', async () => {
  832. await this.saveSettings();
  833. closeModal();
  834. });
  835. // Change save path
  836. changePathBtn?.addEventListener('click', async () => {
  837. const result = await window.electronAPI.selectSaveDirectory();
  838. if (result.success && result.path) {
  839. document.getElementById('settings-save-path').value = result.path;
  840. }
  841. });
  842. // Select cookie file
  843. selectCookieBtn?.addEventListener('click', async () => {
  844. const result = await window.electronAPI.selectCookieFile();
  845. if (result.success && result.path) {
  846. document.getElementById('settings-cookie-file').value = result.path;
  847. }
  848. });
  849. // Clear cookie file
  850. clearCookieBtn?.addEventListener('click', () => {
  851. document.getElementById('settings-cookie-file').value = '';
  852. });
  853. // Export/Import/Update buttons in Data tab
  854. const exportListBtnSettings = document.getElementById('exportListBtnSettings');
  855. const importListBtnSettings = document.getElementById('importListBtnSettings');
  856. const updateDepsBtnSettings = document.getElementById('updateDepsBtnSettings');
  857. exportListBtnSettings?.addEventListener('click', () => {
  858. this.handleExportList();
  859. });
  860. importListBtnSettings?.addEventListener('click', () => {
  861. this.handleImportList();
  862. });
  863. updateDepsBtnSettings?.addEventListener('click', () => {
  864. this.handleUpdateDependencies();
  865. });
  866. // Close on Escape key
  867. const escHandler = (e) => {
  868. if (e.key === 'Escape') {
  869. closeModal();
  870. document.removeEventListener('keydown', escHandler);
  871. }
  872. };
  873. document.addEventListener('keydown', escHandler);
  874. // Close on click outside
  875. modal.addEventListener('click', (e) => {
  876. if (e.target === modal) {
  877. closeModal();
  878. }
  879. });
  880. }
  881. /**
  882. * Save settings from modal to state
  883. */
  884. async saveSettings() {
  885. const newSettings = {
  886. savePath: document.getElementById('settings-save-path')?.value || this.state.config.savePath,
  887. concurrentDownloads: parseInt(document.getElementById('settings-concurrent-downloads')?.value) || 3,
  888. autoOrganize: document.getElementById('settings-auto-organize')?.checked || false,
  889. filenameTemplate: document.getElementById('settings-filename-template')?.value || '%(title)s',
  890. autoDownloadSubtitles: document.getElementById('settings-auto-download-subtitles')?.checked || false,
  891. subtitleLanguage: document.getElementById('settings-subtitle-language')?.value || 'en',
  892. desktopNotifications: document.getElementById('settings-desktop-notifications')?.checked || true,
  893. maxRetries: parseInt(document.getElementById('settings-max-retries')?.value) || 3,
  894. timeout: parseInt(document.getElementById('settings-timeout')?.value) || 30,
  895. cookieFile: document.getElementById('settings-cookie-file')?.value || null
  896. };
  897. // Update state
  898. this.state.updateConfig(newSettings);
  899. // Update main UI if save path changed
  900. if (newSettings.savePath !== this.state.config.savePath) {
  901. const savePathDisplay = document.getElementById('savePath');
  902. if (savePathDisplay) {
  903. savePathDisplay.textContent = newSettings.savePath;
  904. }
  905. }
  906. this.showToast('Settings saved successfully', 'success');
  907. }
  908. // Show history modal
  909. showHistoryModal() {
  910. const modal = document.getElementById('historyModal');
  911. if (!modal) return;
  912. this.renderHistoryList();
  913. this.setupHistoryModalListeners();
  914. modal.classList.remove('hidden');
  915. modal.classList.add('flex');
  916. }
  917. // Render history list
  918. renderHistoryList() {
  919. const historyList = document.getElementById('historyList');
  920. const emptyState = document.getElementById('historyEmptyState');
  921. const history = this.state.getHistory();
  922. if (!historyList) return;
  923. if (history.length === 0) {
  924. historyList.innerHTML = '';
  925. if (emptyState) {
  926. emptyState.classList.remove('hidden');
  927. emptyState.classList.add('flex');
  928. }
  929. return;
  930. }
  931. if (emptyState) {
  932. emptyState.classList.add('hidden');
  933. emptyState.classList.remove('flex');
  934. }
  935. historyList.innerHTML = history.map(entry => {
  936. const downloadDate = new Date(entry.downloadedAt);
  937. const dateStr = downloadDate.toLocaleDateString();
  938. const timeStr = downloadDate.toLocaleTimeString([], { hour: '2-digit', minute: '2-digit' });
  939. return `
  940. <div class="bg-[#1d293d] rounded-lg p-3 flex items-center gap-3 hover:bg-[#243447] transition-colors">
  941. <img src="${entry.thumbnail || 'assets/icons/placeholder.svg'}"
  942. alt="${entry.title}"
  943. class="w-16 h-12 object-cover rounded flex-shrink-0">
  944. <div class="flex-1 min-w-0">
  945. <h3 class="text-sm text-white font-medium truncate">${entry.title}</h3>
  946. <div class="flex items-center gap-3 text-xs text-[#90a1b9] mt-1">
  947. <span>${entry.quality} • ${entry.format !== 'None' ? entry.format : 'MP4'}</span>
  948. <span>•</span>
  949. <span>${dateStr} ${timeStr}</span>
  950. </div>
  951. </div>
  952. <div class="flex items-center gap-2 flex-shrink-0">
  953. <button class="redownload-history-btn text-[#155dfc] hover:text-white px-3 py-1 rounded border border-[#155dfc] hover:bg-[#155dfc] transition-colors text-xs"
  954. data-entry-id="${entry.id}"
  955. data-url="${entry.url}"
  956. title="Re-download this video">
  957. Re-download
  958. </button>
  959. <button class="delete-history-btn text-[#90a1b9] hover:text-[#e7000b] transition-colors p-1"
  960. data-entry-id="${entry.id}"
  961. title="Remove from history">
  962. <svg width="16" height="16" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1.5">
  963. <path d="M3 4h10M5 4V3a1 1 0 011-1h4a1 1 0 011 1v1M6 7v4M10 7v4M4 4l1 9a1 1 0 001 1h4a1 1 0 001-1l1-9"
  964. stroke-linecap="round" stroke-linejoin="round"/>
  965. </svg>
  966. </button>
  967. </div>
  968. </div>
  969. `;
  970. }).join('');
  971. }
  972. // Setup history modal listeners
  973. setupHistoryModalListeners() {
  974. const closeBtn = document.getElementById('closeHistoryModal');
  975. const clearBtn = document.getElementById('clearHistoryBtn');
  976. const historyList = document.getElementById('historyList');
  977. const modal = document.getElementById('historyModal');
  978. // Remove existing listeners if any
  979. if (closeBtn) {
  980. closeBtn.replaceWith(closeBtn.cloneNode(true));
  981. const newCloseBtn = document.getElementById('closeHistoryModal');
  982. newCloseBtn.addEventListener('click', () => {
  983. modal.classList.remove('flex');
  984. modal.classList.add('hidden');
  985. });
  986. }
  987. if (clearBtn) {
  988. clearBtn.replaceWith(clearBtn.cloneNode(true));
  989. const newClearBtn = document.getElementById('clearHistoryBtn');
  990. newClearBtn.addEventListener('click', () => {
  991. if (confirm('Are you sure you want to clear all download history? This cannot be undone.')) {
  992. this.state.clearHistory();
  993. this.renderHistoryList();
  994. this.showToast('Download history cleared', 'info');
  995. }
  996. });
  997. }
  998. // Handle delete and redownload buttons
  999. if (historyList) {
  1000. historyList.addEventListener('click', (e) => {
  1001. const deleteBtn = e.target.closest('.delete-history-btn');
  1002. const redownloadBtn = e.target.closest('.redownload-history-btn');
  1003. if (deleteBtn) {
  1004. const entryId = deleteBtn.dataset.entryId;
  1005. this.state.removeHistoryEntry(entryId);
  1006. this.renderHistoryList();
  1007. this.showToast('Removed from history', 'info');
  1008. }
  1009. if (redownloadBtn) {
  1010. const url = redownloadBtn.dataset.url;
  1011. // Close history modal
  1012. modal.classList.remove('flex');
  1013. modal.classList.add('hidden');
  1014. // Add video to queue
  1015. const urlInput = document.getElementById('urlInput');
  1016. if (urlInput) {
  1017. urlInput.value = url;
  1018. this.handleAddVideo();
  1019. }
  1020. }
  1021. });
  1022. }
  1023. // Close on outside click
  1024. if (modal) {
  1025. modal.addEventListener('click', (e) => {
  1026. if (e.target === modal) {
  1027. modal.classList.remove('flex');
  1028. modal.classList.add('hidden');
  1029. }
  1030. });
  1031. }
  1032. }
  1033. // Event handlers
  1034. async handleAddVideo() {
  1035. const urlInput = document.getElementById('urlInput');
  1036. const inputText = urlInput?.value.trim();
  1037. if (!inputText) {
  1038. this.showError('Please enter a URL');
  1039. return;
  1040. }
  1041. try {
  1042. this.updateStatusMessage('Adding videos...');
  1043. // Check if it's a playlist URL
  1044. const playlistPattern = /[?&]list=([\w\-]+)/;
  1045. if (playlistPattern.test(inputText)) {
  1046. await this.handlePlaylistUrl(inputText);
  1047. if (urlInput) {
  1048. urlInput.value = '';
  1049. }
  1050. return;
  1051. }
  1052. // Validate URLs
  1053. const validation = window.URLValidator.validateMultipleUrls(inputText);
  1054. if (validation.invalid.length > 0) {
  1055. this.showError(`Invalid URLs found: ${validation.invalid.join(', ')}`);
  1056. return;
  1057. }
  1058. if (validation.valid.length === 0) {
  1059. this.showError('No valid URLs found');
  1060. return;
  1061. }
  1062. // Check for duplicates first
  1063. const duplicateInfo = this.checkForDuplicates(validation.valid);
  1064. let urlsToAdd = validation.valid;
  1065. let addOptions = {};
  1066. // If duplicates found, ask user what to do
  1067. if (duplicateInfo.duplicates.length > 0) {
  1068. const action = await this.handleDuplicateUrls(duplicateInfo);
  1069. if (action === 'skip') {
  1070. // Only add non-duplicate URLs
  1071. urlsToAdd = duplicateInfo.unique;
  1072. } else if (action === 'replace') {
  1073. // Remove existing duplicates, then add all URLs
  1074. duplicateInfo.duplicates.forEach(dup => {
  1075. this.state.removeVideo(dup.existingVideo.id);
  1076. });
  1077. urlsToAdd = validation.valid;
  1078. } else if (action === 'keep-both') {
  1079. // Add all URLs (duplicates will be added again)
  1080. urlsToAdd = validation.valid;
  1081. addOptions = { allowDuplicates: true };
  1082. } else {
  1083. // User cancelled
  1084. return;
  1085. }
  1086. }
  1087. // Add videos to state
  1088. const results = await this.state.addVideosFromUrls(urlsToAdd, addOptions);
  1089. // Clear input on success
  1090. if (urlInput) {
  1091. urlInput.value = '';
  1092. }
  1093. // Show results with toast
  1094. const successCount = results.successful.length;
  1095. const failedCount = results.failed.length;
  1096. if (successCount > 0) {
  1097. const message = `Added ${successCount} video(s)`;
  1098. this.showToast(message, 'success');
  1099. }
  1100. if (failedCount > 0) {
  1101. this.showToast(`${failedCount} video(s) failed to add`, 'error');
  1102. }
  1103. } catch (error) {
  1104. logger.error('Error adding videos:', error.message);
  1105. this.showError(`Failed to add videos: ${error.message}`);
  1106. }
  1107. }
  1108. async handleImportUrls() {
  1109. if (!window.electronAPI) {
  1110. this.showError('File import requires Electron environment');
  1111. return;
  1112. }
  1113. try {
  1114. // Implementation would use Electron file dialog
  1115. this.updateStatusMessage('Import URLs functionality coming soon');
  1116. } catch (error) {
  1117. this.showError(`Failed to import URLs: ${error.message}`);
  1118. }
  1119. }
  1120. async handleSelectSavePath() {
  1121. if (!window.IPCManager || !window.IPCManager.isAvailable()) {
  1122. this.showError('Path selection requires Electron environment');
  1123. return;
  1124. }
  1125. try {
  1126. this.updateStatusMessage('Select download directory...');
  1127. const result = await window.IPCManager.selectSaveDirectory();
  1128. if (result && result.success && result.path) {
  1129. this.state.updateConfig({ savePath: result.path });
  1130. await this.ensureSaveDirectoryExists(); // Auto-create directory
  1131. this.updateSavePathDisplay();
  1132. this.updateStatusMessage(`Save path set to: ${result.path}`);
  1133. } else if (result && result.error) {
  1134. this.showError(result.error);
  1135. } else {
  1136. this.updateStatusMessage('No directory selected');
  1137. }
  1138. } catch (error) {
  1139. logger.error('Error selecting save path:', error.message);
  1140. this.showError(`Failed to select save path: ${error.message}`);
  1141. }
  1142. }
  1143. async handleSelectCookieFile() {
  1144. if (!window.IPCManager || !window.IPCManager.isAvailable()) {
  1145. this.showError('File selection requires Electron environment');
  1146. return;
  1147. }
  1148. try {
  1149. this.updateStatusMessage('Select cookie file...');
  1150. const result = await window.IPCManager.selectCookieFile();
  1151. if (result && result.success && result.path) {
  1152. this.state.updateConfig({ cookieFile: result.path });
  1153. this.updateStatusMessage(`Cookie file set: ${result.path}`);
  1154. // Update UI to show selected cookie file
  1155. const cookieFilePathElement = document.getElementById('cookieFilePath');
  1156. if (cookieFilePathElement) {
  1157. const fileName = result.path.split('/').pop() || result.path.split('\\').pop();
  1158. cookieFilePathElement.textContent = fileName;
  1159. cookieFilePathElement.title = result.path;
  1160. }
  1161. } else if (result && result.error) {
  1162. this.showError(result.error);
  1163. } else {
  1164. this.updateStatusMessage('No file selected');
  1165. }
  1166. } catch (error) {
  1167. logger.error('Error selecting cookie file:', error.message);
  1168. this.showError(`Failed to select cookie file: ${error.message}`);
  1169. }
  1170. }
  1171. async handleOpenFolder() {
  1172. // Check if IPC is available
  1173. if (!window.electronAPI || !window.electronAPI.openDownloadsFolder) {
  1174. this.showError('Open folder functionality requires Electron environment');
  1175. return;
  1176. }
  1177. // Get save path from state
  1178. const savePath = this.state.config.savePath;
  1179. if (!savePath) {
  1180. this.showError('No download folder configured. Please set a save path first.');
  1181. return;
  1182. }
  1183. try {
  1184. const result = await window.electronAPI.openDownloadsFolder(savePath);
  1185. if (!result.success) {
  1186. this.showError(result.error || 'Failed to open folder');
  1187. }
  1188. // On success, no message needed - folder opens in file explorer
  1189. } catch (error) {
  1190. logger.error('Error opening folder:', error.message);
  1191. this.showError(`Failed to open folder: ${error.message}`);
  1192. }
  1193. }
  1194. async handleClipboardToggle(enabled) {
  1195. if (!window.electronAPI) {
  1196. this.showError('Clipboard monitoring requires Electron environment');
  1197. return;
  1198. }
  1199. try {
  1200. if (enabled) {
  1201. // Check if consent has been given
  1202. if (!this.state.hasClipboardConsentAnswer()) {
  1203. // Show consent dialog for first time
  1204. const consentDialog = new window.ClipboardConsentDialog();
  1205. const { allowed, rememberChoice } = await consentDialog.show();
  1206. if (rememberChoice) {
  1207. // Save consent preference
  1208. this.state.setClipboardConsent(allowed);
  1209. }
  1210. if (!allowed) {
  1211. // User denied consent
  1212. document.getElementById('clipboardMonitorToggle').checked = false;
  1213. this.updateStatusMessage('Clipboard monitoring requires your permission');
  1214. return;
  1215. }
  1216. } else if (!this.state.hasClipboardConsent()) {
  1217. // User previously denied consent
  1218. document.getElementById('clipboardMonitorToggle').checked = false;
  1219. this.showError('You have previously denied clipboard monitoring permission');
  1220. return;
  1221. }
  1222. // User has consented - start monitoring
  1223. const result = await window.electronAPI.startClipboardMonitor(true);
  1224. if (result.success) {
  1225. // Set up listener for detected URLs
  1226. window.electronAPI.onClipboardUrlDetected((event, url) => {
  1227. this.showClipboardNotification(url);
  1228. });
  1229. this.updateStatusMessage('Clipboard monitoring enabled');
  1230. } else {
  1231. this.showError(result.error || 'Failed to start clipboard monitoring');
  1232. document.getElementById('clipboardMonitorToggle').checked = false;
  1233. }
  1234. } else {
  1235. await window.electronAPI.stopClipboardMonitor();
  1236. this.updateStatusMessage('Clipboard monitoring disabled');
  1237. }
  1238. } catch (error) {
  1239. logger.error('Error toggling clipboard monitor:', error.message);
  1240. this.showError(`Clipboard monitoring error: ${error.message}`);
  1241. document.getElementById('clipboardMonitorToggle').checked = false;
  1242. }
  1243. }
  1244. async showClipboardNotification(url) {
  1245. if (!window.electronAPI) return;
  1246. try {
  1247. await window.electronAPI.showNotification({
  1248. title: 'Video URL Detected',
  1249. message: `Click to add: ${url.substring(0, 50)}...`,
  1250. sound: true
  1251. });
  1252. // Auto-add the URL
  1253. const urlInput = document.getElementById('urlInput');
  1254. if (urlInput) {
  1255. urlInput.value = url;
  1256. // Trigger add action
  1257. await this.handleAddVideo();
  1258. }
  1259. } catch (error) {
  1260. logger.error('Error showing clipboard notification:', error.message);
  1261. }
  1262. }
  1263. /**
  1264. * Export current video list to JSON file
  1265. */
  1266. async handleExportList() {
  1267. const videos = this.state.videos;
  1268. if (videos.length === 0) {
  1269. this.showError('No videos to export');
  1270. return;
  1271. }
  1272. try {
  1273. const result = await window.electronAPI.exportVideoList(videos);
  1274. if (result.cancelled) {
  1275. return; // User cancelled dialog
  1276. }
  1277. if (result.success) {
  1278. this.showToast(`Exported ${videos.length} video(s)`, 'success');
  1279. } else {
  1280. this.showToast(`Export failed: ${result.error}`, 'error');
  1281. }
  1282. } catch (error) {
  1283. logger.error('Error exporting video list:', error.message);
  1284. this.showError('Failed to export video list');
  1285. }
  1286. }
  1287. /**
  1288. * Import video list from JSON file
  1289. */
  1290. async handleImportList() {
  1291. try {
  1292. const result = await window.electronAPI.importVideoList();
  1293. if (result.cancelled) {
  1294. return; // User cancelled dialog
  1295. }
  1296. if (!result.success) {
  1297. this.showError(`Import failed: ${result.error}`);
  1298. return;
  1299. }
  1300. // Ask user if they want to replace or merge
  1301. const action = confirm(
  1302. `Import ${result.videos.length} video(s)?\n\n` +
  1303. `Click OK to REPLACE current list\n` +
  1304. `Click Cancel to MERGE with current list`
  1305. );
  1306. if (action) {
  1307. // Replace: clear current list first
  1308. this.state.clearVideos();
  1309. }
  1310. // Add imported videos
  1311. let addedCount = 0;
  1312. let skippedCount = 0;
  1313. for (const videoData of result.videos) {
  1314. // Check for duplicates (only if merging)
  1315. if (!action) {
  1316. const existingVideo = this.state.videos.find(v => v.url === videoData.url);
  1317. if (existingVideo) {
  1318. skippedCount++;
  1319. continue;
  1320. }
  1321. }
  1322. // Create new video with imported data - Video constructor takes (url, options)
  1323. const video = new Video(videoData.url, {
  1324. title: videoData.title || 'Imported Video',
  1325. thumbnail: videoData.thumbnail || '',
  1326. duration: videoData.duration || '',
  1327. quality: videoData.quality || this.state.config.defaultQuality,
  1328. format: videoData.format || this.state.config.defaultFormat,
  1329. status: 'ready' // Always reset to ready on import
  1330. });
  1331. this.state.addVideo(video);
  1332. addedCount++;
  1333. }
  1334. const message = action
  1335. ? `Imported ${addedCount} video(s)`
  1336. : `Imported ${addedCount} video(s)${skippedCount > 0 ? `, skipped ${skippedCount} duplicate(s)` : ''}`;
  1337. this.showToast(message, 'success');
  1338. this.renderVideoList();
  1339. } catch (error) {
  1340. logger.error('Error importing video list:', error.message);
  1341. this.showError('Failed to import video list');
  1342. }
  1343. }
  1344. handleClearList() {
  1345. const selectedVideos = this.state.getSelectedVideos();
  1346. const hasSelection = selectedVideos.length > 0;
  1347. if (hasSelection) {
  1348. // Clear only selected videos
  1349. selectedVideos.forEach(video => {
  1350. this.state.removeVideo(video.id);
  1351. });
  1352. this.updateStatusMessage(`Cleared ${selectedVideos.length} selected video(s)`);
  1353. } else {
  1354. // Clear all videos
  1355. if (this.state.getVideos().length === 0) {
  1356. this.updateStatusMessage('No videos to clear');
  1357. return;
  1358. }
  1359. const removedVideos = this.state.clearVideos();
  1360. this.updateStatusMessage(`Cleared ${removedVideos.length} video(s)`);
  1361. }
  1362. }
  1363. async handleDownloadVideos() {
  1364. // Check if IPC is available
  1365. if (!window.IPCManager || !window.IPCManager.isAvailable()) {
  1366. this.showError('Download functionality requires Electron environment');
  1367. return;
  1368. }
  1369. // Check completed videos for missing files and reset them to ready
  1370. const completedVideos = this.state.getVideosByStatus('completed');
  1371. for (const video of completedVideos) {
  1372. if (video.filename) {
  1373. const filePath = `${this.state.config.savePath}/${video.filename}`;
  1374. try {
  1375. const result = await window.electronAPI.checkFileExists(filePath);
  1376. if (!result.exists) {
  1377. logger.debug(`File missing for ${video.title}, resetting to ready`);
  1378. this.state.updateVideo(video.id, {
  1379. status: 'ready',
  1380. progress: 0,
  1381. filename: '',
  1382. error: null
  1383. });
  1384. }
  1385. } catch (error) {
  1386. logger.error(`Error checking file existence for ${video.title}:`, error.message);
  1387. }
  1388. }
  1389. }
  1390. // Get downloadable videos (either selected or all ready videos)
  1391. const selectedVideos = this.state.getSelectedVideos().filter(v => v.isDownloadable());
  1392. const videos = selectedVideos.length > 0
  1393. ? selectedVideos
  1394. : this.state.getVideos().filter(v => v.isDownloadable());
  1395. if (videos.length === 0) {
  1396. this.showError('No videos ready for download');
  1397. return;
  1398. }
  1399. // Validate save path
  1400. if (!this.state.config.savePath) {
  1401. this.showError('Please select a save directory first');
  1402. return;
  1403. }
  1404. this.state.updateUI({ isDownloading: true });
  1405. this.updateStatusMessage(`Starting parallel download of ${videos.length} video(s)...`);
  1406. // Set up download progress listener
  1407. window.IPCManager.onDownloadProgress('app', (progressData) => {
  1408. this.handleDownloadProgress(progressData);
  1409. });
  1410. // PARALLEL DOWNLOADS: Start all downloads simultaneously
  1411. // The DownloadManager will handle concurrency limits automatically
  1412. logger.debug(`Starting ${videos.length} downloads in parallel...`);
  1413. const downloadPromises = videos.map(async (video) => {
  1414. try {
  1415. // Update video status to downloading
  1416. this.state.updateVideo(video.id, { status: 'downloading', progress: 0 });
  1417. const result = await window.IPCManager.downloadVideo({
  1418. videoId: video.id,
  1419. url: video.url,
  1420. quality: video.quality,
  1421. format: video.format,
  1422. savePath: this.state.config.savePath,
  1423. cookieFile: this.state.config.cookieFile
  1424. });
  1425. if (result.success) {
  1426. this.state.updateVideo(video.id, {
  1427. status: 'completed',
  1428. progress: 100,
  1429. filename: result.filename
  1430. });
  1431. // Add to download history
  1432. const completedVideo = this.state.getVideo(video.id);
  1433. if (completedVideo) {
  1434. this.state.addToHistory(completedVideo);
  1435. }
  1436. // Show notification for successful download
  1437. this.showDownloadNotification(video, 'success');
  1438. return { success: true, video };
  1439. } else {
  1440. this.state.updateVideo(video.id, {
  1441. status: 'error',
  1442. error: result.error || 'Download failed'
  1443. });
  1444. // Show notification for failed download
  1445. this.showDownloadNotification(video, 'error', result.error);
  1446. return { success: false, video, error: result.error };
  1447. }
  1448. } catch (error) {
  1449. logger.error(`Error downloading video ${video.id}:`, error.message);
  1450. this.state.updateVideo(video.id, {
  1451. status: 'error',
  1452. error: error.message
  1453. });
  1454. return { success: false, video, error: error.message };
  1455. }
  1456. });
  1457. // Wait for all downloads to complete
  1458. const results = await Promise.all(downloadPromises);
  1459. // Count successes and failures
  1460. const successCount = results.filter(r => r.success).length;
  1461. const failedCount = results.filter(r => !r.success).length;
  1462. // Clean up progress listener
  1463. window.IPCManager.removeDownloadProgressListener('app');
  1464. this.state.updateUI({ isDownloading: false });
  1465. // Show final status
  1466. let message = `Download complete: ${successCount} succeeded`;
  1467. if (failedCount > 0) {
  1468. message += `, ${failedCount} failed`;
  1469. }
  1470. this.updateStatusMessage(message);
  1471. }
  1472. // Handle pause download
  1473. async handlePauseDownload(videoId) {
  1474. if (!window.electronAPI) {
  1475. this.showToast('Pause functionality requires Electron environment', 'error');
  1476. return;
  1477. }
  1478. try {
  1479. const result = await window.electronAPI.pauseDownload(videoId);
  1480. if (result.success) {
  1481. this.state.updateVideo(videoId, { status: 'paused' });
  1482. this.showToast('Download paused', 'info');
  1483. } else {
  1484. this.showToast(result.message || 'Failed to pause download', 'error');
  1485. }
  1486. } catch (error) {
  1487. logger.error('Error pausing download:', error.message);
  1488. this.showToast('Failed to pause download', 'error');
  1489. }
  1490. }
  1491. // Handle resume download
  1492. async handleResumeDownload(videoId) {
  1493. if (!window.electronAPI) {
  1494. this.showToast('Resume functionality requires Electron environment', 'error');
  1495. return;
  1496. }
  1497. try {
  1498. const result = await window.electronAPI.resumeDownload(videoId);
  1499. if (result.success) {
  1500. this.state.updateVideo(videoId, { status: 'downloading' });
  1501. this.showToast('Download resumed', 'success');
  1502. } else {
  1503. this.showToast(result.message || 'Failed to resume download', 'error');
  1504. }
  1505. } catch (error) {
  1506. logger.error('Error resuming download:', error.message);
  1507. this.showToast('Failed to resume download', 'error');
  1508. }
  1509. }
  1510. // Handle download progress updates from IPC
  1511. handleDownloadProgress(progressData) {
  1512. const { url, progress, status, stage, message, speed, eta } = progressData;
  1513. // Find video by URL
  1514. const video = this.state.getVideos().find(v => v.url === url);
  1515. if (!video) return;
  1516. // Update video progress
  1517. this.state.updateVideo(video.id, {
  1518. progress: Math.round(progress),
  1519. status: status || 'downloading',
  1520. downloadSpeed: speed,
  1521. eta: eta
  1522. });
  1523. }
  1524. // Show download notification
  1525. async showDownloadNotification(video, type, errorMessage = null) {
  1526. if (!window.electronAPI) return;
  1527. try {
  1528. const notificationOptions = {
  1529. title: type === 'success' ? 'Download Complete' : 'Download Failed',
  1530. message: type === 'success'
  1531. ? `${video.getDisplayName()}`
  1532. : `${video.getDisplayName()}: ${errorMessage || 'Unknown error'}`,
  1533. sound: true
  1534. };
  1535. await window.electronAPI.showNotification(notificationOptions);
  1536. } catch (error) {
  1537. logger.warn('Failed to show notification:', error);
  1538. }
  1539. }
  1540. async handleCancelDownloads() {
  1541. const selectedVideos = this.state.getSelectedVideos();
  1542. const hasSelection = selectedVideos.length > 0;
  1543. if (!window.IPCManager || !window.IPCManager.isAvailable()) {
  1544. this.showError('Cancel functionality requires Electron environment');
  1545. return;
  1546. }
  1547. try {
  1548. let videosToCancel = [];
  1549. if (hasSelection) {
  1550. // Cancel only selected videos that are downloading or converting
  1551. videosToCancel = selectedVideos.filter(v =>
  1552. v.status === 'downloading' || v.status === 'converting'
  1553. );
  1554. if (videosToCancel.length === 0) {
  1555. this.updateStatusMessage('No active downloads in selection');
  1556. return;
  1557. }
  1558. this.updateStatusMessage(`Cancelling ${videosToCancel.length} selected download(s)...`);
  1559. // Cancel each selected video individually
  1560. for (const video of videosToCancel) {
  1561. try {
  1562. await window.electronAPI.cancelDownload(video.id);
  1563. } catch (error) {
  1564. logger.error(`Error cancelling download for ${video.id}:`, error.message);
  1565. }
  1566. }
  1567. } else {
  1568. // Cancel all active downloads
  1569. const downloadingVideos = this.state.getVideosByStatus('downloading');
  1570. const convertingVideos = this.state.getVideosByStatus('converting');
  1571. videosToCancel = [...downloadingVideos, ...convertingVideos];
  1572. if (videosToCancel.length === 0) {
  1573. this.updateStatusMessage('No active downloads to cancel');
  1574. return;
  1575. }
  1576. this.updateStatusMessage(`Cancelling ${videosToCancel.length} active download(s)...`);
  1577. // Cancel all downloads via IPC
  1578. await window.electronAPI.cancelAllDownloads();
  1579. // Cancel all conversions via IPC
  1580. await window.electronAPI.cancelAllConversions();
  1581. }
  1582. // Update video statuses to ready
  1583. videosToCancel.forEach(video => {
  1584. this.state.updateVideo(video.id, {
  1585. status: 'ready',
  1586. progress: 0,
  1587. error: 'Cancelled by user'
  1588. });
  1589. });
  1590. this.state.updateUI({ isDownloading: false });
  1591. this.updateStatusMessage(hasSelection ? 'Selected downloads cancelled' : 'Downloads cancelled');
  1592. } catch (error) {
  1593. logger.error('Error cancelling downloads:', error.message);
  1594. this.showError(`Failed to cancel downloads: ${error.message}`);
  1595. }
  1596. }
  1597. async handleUpdateDependencies() {
  1598. if (!window.IPCManager || !window.IPCManager.isAvailable()) {
  1599. this.showError('Update functionality requires Electron environment');
  1600. return;
  1601. }
  1602. const btn = document.getElementById('updateDepsBtn');
  1603. const originalBtnHTML = btn ? btn.innerHTML : '';
  1604. try {
  1605. // Show loading state
  1606. this.updateStatusMessage('Checking binary versions...');
  1607. if (btn) {
  1608. btn.disabled = true;
  1609. btn.innerHTML = '<img src="assets/icons/refresh.svg" alt="" width="16" height="16" loading="lazy" class="animate-spin">Checking...';
  1610. }
  1611. const versions = await window.IPCManager.checkBinaryVersions();
  1612. // Handle both ytDlp (from main.js) and ytdlp (legacy) formats
  1613. const ytdlp = versions.ytDlp || versions.ytdlp;
  1614. const ffmpeg = versions.ffmpeg;
  1615. if (versions && (ytdlp || ffmpeg)) {
  1616. // Update both button status and version display
  1617. const ytdlpMissing = !ytdlp || !ytdlp.available;
  1618. const ffmpegMissing = !ffmpeg || !ffmpeg.available;
  1619. if (ytdlpMissing || ffmpegMissing) {
  1620. this.updateDependenciesButtonStatus('missing');
  1621. this.updateBinaryVersionDisplay(null);
  1622. } else {
  1623. this.updateDependenciesButtonStatus('ok');
  1624. // Normalize the format for display
  1625. const normalizedVersions = {
  1626. ytdlp: ytdlp,
  1627. ffmpeg: ffmpeg
  1628. };
  1629. this.updateBinaryVersionDisplay(normalizedVersions);
  1630. // Show dialog if updates are available
  1631. if (ytdlp.updateAvailable) {
  1632. this.showInfo({
  1633. title: 'Update Available',
  1634. message: `A newer version of yt-dlp is available:\nInstalled: ${ytdlp.version}\nLatest: ${ytdlp.latestVersion || 'newer version'}\n\nPlease run 'npm run setup' to update.`
  1635. });
  1636. }
  1637. }
  1638. } else {
  1639. this.showError('Could not check binary versions');
  1640. }
  1641. } catch (error) {
  1642. logger.error('Error checking dependencies:', error.message);
  1643. this.showError(`Failed to check dependencies: ${error.message}`);
  1644. } finally {
  1645. // Restore button state
  1646. if (btn) {
  1647. btn.disabled = false;
  1648. btn.innerHTML = originalBtnHTML || '<img src="assets/icons/refresh.svg" alt="" width="16" height="16" loading="lazy">Check for Updates';
  1649. }
  1650. }
  1651. }
  1652. // State change handlers
  1653. onVideoAdded(data) {
  1654. this.renderVideoList();
  1655. this.updateStatsDisplay();
  1656. }
  1657. onVideoRemoved(data) {
  1658. this.renderVideoList();
  1659. this.updateStatsDisplay();
  1660. }
  1661. onVideoUpdated(data) {
  1662. this.updateVideoElement(data.video);
  1663. this.updateStatsDisplay();
  1664. }
  1665. onVideosReordered(data) {
  1666. // Re-render entire list to reflect new order
  1667. this.renderVideoList();
  1668. logger.debug('Video order updated:', data);
  1669. }
  1670. onVideosCleared(data) {
  1671. this.renderVideoList();
  1672. this.updateStatsDisplay();
  1673. }
  1674. onConfigUpdated(data) {
  1675. this.updateConfigUI(data.config);
  1676. }
  1677. onVideoSelectionChanged(data) {
  1678. const selectedVideos = data.selectedVideos || [];
  1679. const hasSelection = selectedVideos.length > 0;
  1680. // Update Clear List button
  1681. const clearListBtn = document.getElementById('clearListBtn');
  1682. if (clearListBtn) {
  1683. clearListBtn.textContent = hasSelection ? 'Clear Selected' : 'Clear List';
  1684. clearListBtn.setAttribute('aria-label', hasSelection ? 'Clear selected videos' : 'Clear all videos');
  1685. }
  1686. // Update Cancel Downloads button
  1687. const cancelDownloadsBtn = document.getElementById('cancelDownloadsBtn');
  1688. if (cancelDownloadsBtn) {
  1689. cancelDownloadsBtn.textContent = hasSelection ? 'Cancel Selected' : 'Cancel Downloads';
  1690. cancelDownloadsBtn.setAttribute('aria-label', hasSelection ? 'Cancel selected downloads' : 'Cancel all downloads');
  1691. }
  1692. // Update Download Videos button
  1693. const downloadVideosBtn = document.getElementById('downloadVideosBtn');
  1694. if (downloadVideosBtn) {
  1695. downloadVideosBtn.textContent = hasSelection ? 'Download Selected' : 'Download Videos';
  1696. downloadVideosBtn.setAttribute('aria-label', hasSelection ? 'Download selected videos' : 'Download all videos');
  1697. }
  1698. }
  1699. // UI update methods
  1700. updateSavePathDisplay() {
  1701. const savePathElement = document.getElementById('savePath');
  1702. if (savePathElement) {
  1703. savePathElement.textContent = this.state.config.savePath;
  1704. }
  1705. }
  1706. initializeDropdowns() {
  1707. // Set dropdown values from config
  1708. const defaultQuality = document.getElementById('defaultQuality');
  1709. if (defaultQuality) {
  1710. defaultQuality.value = this.state.config.defaultQuality;
  1711. }
  1712. const defaultFormat = document.getElementById('defaultFormat');
  1713. if (defaultFormat) {
  1714. defaultFormat.value = this.state.config.defaultFormat;
  1715. }
  1716. }
  1717. initializeVideoList() {
  1718. this.renderVideoList();
  1719. }
  1720. renderVideoList() {
  1721. const videoList = document.getElementById('videoList');
  1722. if (!videoList) return;
  1723. const videos = this.state.getVideos();
  1724. // Clear all existing videos (including mockups)
  1725. videoList.innerHTML = '';
  1726. // If no videos, show empty state
  1727. if (videos.length === 0) {
  1728. videoList.innerHTML = `
  1729. <div class="text-center py-12 text-[#90a1b9]">
  1730. <p class="text-lg mb-2">No videos yet</p>
  1731. <p class="text-sm">Paste YouTube or Vimeo URLs above to get started</p>
  1732. </div>
  1733. `;
  1734. return;
  1735. }
  1736. // Render each video
  1737. videos.forEach(video => {
  1738. const videoElement = this.createVideoElement(video);
  1739. videoList.appendChild(videoElement);
  1740. });
  1741. }
  1742. createVideoElement(video) {
  1743. const div = document.createElement('div');
  1744. div.className = 'video-item grid grid-cols-[40px_40px_1fr_120px_100px_120px_100px_40px] gap-4 items-center p-2 rounded bg-[#314158] hover:bg-[#3a4a68] transition-colors duration-200';
  1745. div.dataset.videoId = video.id;
  1746. div.setAttribute('draggable', 'true'); // Make video item draggable
  1747. div.innerHTML = `
  1748. <!-- Checkbox -->
  1749. <div class="flex items-center justify-center">
  1750. <button class="video-checkbox w-6 h-6 rounded flex items-center justify-center hover:bg-[#45556c] transition-colors"
  1751. role="checkbox" aria-checked="false" aria-label="Select ${video.getDisplayName()}">
  1752. <svg width="16" height="16" viewBox="0 0 16 16" fill="none" class="text-white">
  1753. <rect x="3" y="3" width="10" height="10" stroke="currentColor" stroke-width="1.5" fill="none" rx="2" />
  1754. </svg>
  1755. </button>
  1756. </div>
  1757. <!-- Drag Handle -->
  1758. <div class="flex items-center justify-center text-[#90a1b9] hover:text-white cursor-grab transition-colors">
  1759. <svg width="16" height="16" viewBox="0 0 16 16" fill="currentColor">
  1760. <circle cx="4" cy="4" r="1" />
  1761. <circle cx="4" cy="8" r="1" />
  1762. <circle cx="4" cy="12" r="1" />
  1763. <circle cx="8" cy="4" r="1" />
  1764. <circle cx="8" cy="8" r="1" />
  1765. <circle cx="8" cy="12" r="1" />
  1766. <circle cx="12" cy="4" r="1" />
  1767. <circle cx="12" cy="8" r="1" />
  1768. <circle cx="12" cy="12" r="1" />
  1769. </svg>
  1770. </div>
  1771. <!-- Video Info -->
  1772. <div class="flex items-center gap-3 min-w-0">
  1773. <div class="video-thumbnail-container w-16 h-12 bg-[#45556c] rounded overflow-hidden flex-shrink-0 relative group cursor-pointer" data-preview-url="${video.url}">
  1774. ${video.isFetchingMetadata ?
  1775. `<div class="w-full h-full bg-gradient-to-br from-[#4a5568] to-[#2d3748] flex items-center justify-center">
  1776. <svg class="animate-spin h-5 w-5 text-[#155dfc]" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24">
  1777. <circle class="opacity-25" cx="12" cy="12" r="10" stroke="currentColor" stroke-width="4"></circle>
  1778. <path class="opacity-75" fill="currentColor" d="M4 12a8 8 0 018-8V0C5.373 0 0 5.373 0 12h4zm2 5.291A7.962 7.962 0 014 12H0c0 3.042 1.135 5.824 3 7.938l3-2.647z"></path>
  1779. </svg>
  1780. </div>` :
  1781. video.thumbnail ?
  1782. `<img src="${video.thumbnail}" alt="${video.getDisplayName()}" class="w-full h-full object-cover">` :
  1783. `<div class="w-full h-full bg-gradient-to-br from-[#4a5568] to-[#2d3748] flex items-center justify-center">
  1784. <svg width="24" height="24" viewBox="0 0 24 24" fill="none" class="text-[#90a1b9]">
  1785. <path d="M8 5V19L19 12L8 5Z" stroke="currentColor" stroke-width="2" stroke-linejoin="round" />
  1786. </svg>
  1787. </div>`
  1788. }
  1789. <!-- Preview Overlay -->
  1790. <div class="absolute inset-0 bg-black/70 opacity-0 group-hover:opacity-100 transition-opacity flex items-center justify-center">
  1791. <svg width="20" height="20" viewBox="0 0 24 24" fill="white">
  1792. <path d="M15 12a3 3 0 11-6 0 3 3 0 016 0z"/>
  1793. <path fill-rule="evenodd" d="M1.323 11.447C2.811 6.976 7.028 3.75 12.001 3.75c4.97 0 9.185 3.223 10.675 7.69.12.362.12.752 0 1.113-1.487 4.471-5.705 7.697-10.677 7.697-4.97 0-9.186-3.223-10.675-7.69a1.762 1.762 0 010-1.113zM17.25 12a5.25 5.25 0 11-10.5 0 5.25 5.25 0 0110.5 0z" clip-rule="evenodd"/>
  1794. </svg>
  1795. </div>
  1796. </div>
  1797. <div class="min-w-0 flex-1">
  1798. <div class="flex items-center gap-2">
  1799. <div class="text-sm text-white truncate font-medium flex-1">${video.getDisplayName()}</div>
  1800. ${video.requiresAuth ? `
  1801. <div class="flex-shrink-0 group relative">
  1802. <svg width="14" height="14" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" class="text-[#f59e0b]" stroke-linecap="round" stroke-linejoin="round">
  1803. <rect x="3" y="11" width="18" height="11" rx="2" ry="2"/>
  1804. <path d="M7 11V7a5 5 0 0 1 10 0v4"/>
  1805. </svg>
  1806. <div class="absolute bottom-full right-0 mb-2 hidden group-hover:block z-10 w-48">
  1807. <div class="bg-[#1d293d] border border-[#45556c] rounded-lg p-2 text-xs text-[#cad5e2] shadow-lg">
  1808. <div class="flex items-start gap-1">
  1809. <svg width="12" height="12" class="mt-0.5 flex-shrink-0 text-[#f59e0b]" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
  1810. <circle cx="12" cy="12" r="10"/>
  1811. <line x1="12" y1="8" x2="12" y2="12"/>
  1812. <line x1="12" y1="16" x2="12.01" y2="16"/>
  1813. </svg>
  1814. <span>Requires cookie file. Set in Settings → Advanced.</span>
  1815. </div>
  1816. </div>
  1817. </div>
  1818. </div>
  1819. ` : ''}
  1820. </div>
  1821. ${video.isFetchingMetadata ?
  1822. `<div class="text-xs text-[#155dfc] animate-pulse">Fetching info...</div>` :
  1823. video.requiresAuth && !window.appState?.config?.cookieFile ?
  1824. `<div class="text-xs text-[#f59e0b]">⚠️ Cookie file needed</div>` :
  1825. ''
  1826. }
  1827. </div>
  1828. </div>
  1829. <!-- Duration -->
  1830. <div class="text-sm text-[#cad5e2] text-center">${video.duration || '--:--'}</div>
  1831. <!-- Quality Dropdown -->
  1832. <div class="flex justify-center">
  1833. <select class="quality-select bg-[#314158] border border-[#45556c] text-[#cad5e2] px-2 py-1 rounded text-xs font-medium min-w-0 w-full text-center"
  1834. aria-label="Quality for ${video.getDisplayName()}">
  1835. <option value="Best" ${video.quality === 'Best' ? 'selected' : ''}>Best</option>
  1836. <option value="4K" ${video.quality === '4K' ? 'selected' : ''}>4K</option>
  1837. <option value="1080p" ${video.quality === '1080p' ? 'selected' : ''}>1080p</option>
  1838. <option value="720p" ${video.quality === '720p' ? 'selected' : ''}>720p</option>
  1839. </select>
  1840. </div>
  1841. <!-- Format Dropdown -->
  1842. <div class="flex justify-center">
  1843. <select class="format-select bg-[#314158] border border-[#45556c] text-[#cad5e2] px-2 py-1 rounded text-xs font-medium min-w-0 w-full text-center"
  1844. aria-label="Format for ${video.getDisplayName()}">
  1845. <option value="None" ${video.format === 'None' ? 'selected' : ''}>None</option>
  1846. <option value="H264" ${video.format === 'H264' ? 'selected' : ''}>H264</option>
  1847. <option value="ProRes" ${video.format === 'ProRes' ? 'selected' : ''}>ProRes</option>
  1848. <option value="DNxHR" ${video.format === 'DNxHR' ? 'selected' : ''}>DNxHR</option>
  1849. <option value="Audio only" ${video.format === 'Audio only' ? 'selected' : ''}>Audio only</option>
  1850. </select>
  1851. </div>
  1852. <!-- Status Badge with Pause/Resume -->
  1853. <div class="flex items-center justify-center gap-2 status-column">
  1854. <span class="status-badge ${video.status}" role="status" aria-live="polite">
  1855. ${this.getStatusText(video)}
  1856. </span>
  1857. ${video.status === 'downloading' || video.status === 'paused' ? `
  1858. <button class="pause-resume-btn w-6 h-6 rounded flex items-center justify-center hover:bg-[#45556c] text-[#90a1b9] hover:text-white transition-colors duration-200"
  1859. data-video-id="${video.id}"
  1860. data-action="${video.status === 'paused' ? 'resume' : 'pause'}"
  1861. aria-label="${video.status === 'paused' ? 'Resume' : 'Pause'} download"
  1862. title="${video.status === 'paused' ? 'Resume download (Space)' : 'Pause download (Space)'}">
  1863. ${video.status === 'paused' ? `
  1864. <svg width="14" height="14" viewBox="0 0 16 16" fill="currentColor">
  1865. <path d="M3 2l10 6-10 6V2z"/>
  1866. </svg>
  1867. ` : `
  1868. <svg width="14" height="14" viewBox="0 0 16 16" fill="currentColor">
  1869. <path d="M5 3h2v10H5V3zm4 0h2v10H9V3z"/>
  1870. </svg>
  1871. `}
  1872. </button>
  1873. ` : ''}
  1874. </div>
  1875. <!-- Delete Button -->
  1876. <div class="flex items-center justify-center">
  1877. <button class="delete-video-btn w-6 h-6 rounded flex items-center justify-center hover:bg-red-600 hover:text-white text-[#90a1b9] transition-colors duration-200"
  1878. aria-label="Delete ${video.getDisplayName()}" title="Remove from queue">
  1879. <svg width="16" height="16" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1.5">
  1880. <path d="M3 4h10M5 4V3a1 1 0 011-1h4a1 1 0 011 1v1M6 7v4M10 7v4M4 4l1 9a1 1 0 001 1h4a1 1 0 001-1l1-9"
  1881. stroke-linecap="round" stroke-linejoin="round"/>
  1882. </svg>
  1883. </button>
  1884. </div>
  1885. `;
  1886. return div;
  1887. }
  1888. getStatusText(video) {
  1889. switch (video.status) {
  1890. case 'downloading':
  1891. let downloadText = `Downloading ${video.progress || 0}%`;
  1892. if (video.downloadSpeed) {
  1893. downloadText += ` (${video.downloadSpeed})`;
  1894. }
  1895. if (video.eta) {
  1896. downloadText += ` ETA ${video.eta}`;
  1897. }
  1898. return downloadText;
  1899. case 'paused':
  1900. return `Paused ${video.progress || 0}%`;
  1901. case 'converting':
  1902. return `Converting ${video.progress || 0}%`;
  1903. case 'completed':
  1904. return 'Completed';
  1905. case 'error':
  1906. const retryText = video.retryCount > 0 ? ` (Retry ${video.retryCount}/${video.maxRetries})` : '';
  1907. return `Error${retryText}`;
  1908. case 'ready':
  1909. default:
  1910. return 'Ready';
  1911. }
  1912. }
  1913. updateVideoElement(video) {
  1914. const videoElement = document.querySelector(`[data-video-id="${video.id}"]`);
  1915. if (!videoElement) return;
  1916. // Update thumbnail - show loading spinner if fetching metadata
  1917. const thumbnailContainer = videoElement.querySelector('.w-16.h-12');
  1918. if (thumbnailContainer) {
  1919. if (video.isFetchingMetadata) {
  1920. thumbnailContainer.innerHTML = `
  1921. <div class="w-full h-full bg-gradient-to-br from-[#4a5568] to-[#2d3748] flex items-center justify-center">
  1922. <svg class="animate-spin h-5 w-5 text-[#155dfc]" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24">
  1923. <circle class="opacity-25" cx="12" cy="12" r="10" stroke="currentColor" stroke-width="4"></circle>
  1924. <path class="opacity-75" fill="currentColor" d="M4 12a8 8 0 018-8V0C5.373 0 0 5.373 0 12h4zm2 5.291A7.962 7.962 0 014 12H0c0 3.042 1.135 5.824 3 7.938l3-2.647z"></path>
  1925. </svg>
  1926. </div>`;
  1927. } else if (video.thumbnail) {
  1928. thumbnailContainer.innerHTML = `<img src="${video.thumbnail}" alt="${video.getDisplayName()}" class="w-full h-full object-cover">`;
  1929. } else {
  1930. thumbnailContainer.innerHTML = `
  1931. <div class="w-full h-full bg-gradient-to-br from-[#4a5568] to-[#2d3748] flex items-center justify-center">
  1932. <svg width="24" height="24" viewBox="0 0 24 24" fill="none" class="text-[#90a1b9]">
  1933. <path d="M8 5V19L19 12L8 5Z" stroke="currentColor" stroke-width="2" stroke-linejoin="round" />
  1934. </svg>
  1935. </div>`;
  1936. }
  1937. }
  1938. // Update title and loading message
  1939. const titleContainer = videoElement.querySelector('.min-w-0.flex-1');
  1940. if (titleContainer) {
  1941. const titleElement = titleContainer.querySelector('.text-sm.text-white.truncate');
  1942. if (titleElement) {
  1943. titleElement.textContent = video.getDisplayName();
  1944. }
  1945. // Update or remove "Fetching info..." message
  1946. const existingLoadingMsg = titleContainer.querySelector('.text-xs.text-\\[\\#155dfc\\]');
  1947. if (video.isFetchingMetadata && !existingLoadingMsg) {
  1948. const loadingMsg = document.createElement('div');
  1949. loadingMsg.className = 'text-xs text-[#155dfc] animate-pulse';
  1950. loadingMsg.textContent = 'Fetching info...';
  1951. titleContainer.appendChild(loadingMsg);
  1952. } else if (!video.isFetchingMetadata && existingLoadingMsg) {
  1953. existingLoadingMsg.remove();
  1954. }
  1955. }
  1956. // Update duration
  1957. const durationElement = videoElement.querySelector('.text-sm.text-\\[\\#cad5e2\\].text-center');
  1958. if (durationElement) {
  1959. durationElement.textContent = video.duration || '--:--';
  1960. }
  1961. // Update quality dropdown
  1962. const qualitySelect = videoElement.querySelector('.quality-select');
  1963. if (qualitySelect) {
  1964. qualitySelect.value = video.quality;
  1965. }
  1966. // Update format dropdown
  1967. const formatSelect = videoElement.querySelector('.format-select');
  1968. if (formatSelect) {
  1969. formatSelect.value = video.format;
  1970. }
  1971. // Update status badge with progress
  1972. const statusBadge = videoElement.querySelector('.status-badge');
  1973. if (statusBadge) {
  1974. statusBadge.className = `status-badge ${video.status}`;
  1975. statusBadge.textContent = this.getStatusText(video);
  1976. // Add progress bar for downloading/converting states
  1977. if (video.status === 'downloading' || video.status === 'converting') {
  1978. const progress = video.progress || 0;
  1979. statusBadge.style.background = `linear-gradient(to right, #155dfc ${progress}%, #314158 ${progress}%)`;
  1980. } else {
  1981. statusBadge.style.background = '';
  1982. }
  1983. }
  1984. }
  1985. updateStatsDisplay() {
  1986. const stats = this.state.getStats();
  1987. // Update UI with current statistics
  1988. }
  1989. updateConfigUI(config) {
  1990. this.updateSavePathDisplay();
  1991. this.initializeDropdowns();
  1992. }
  1993. updateStatusMessage(message) {
  1994. const statusElement = document.getElementById('statusMessage');
  1995. if (statusElement) {
  1996. statusElement.textContent = message;
  1997. }
  1998. // Auto-clear success messages
  1999. if (!message.toLowerCase().includes('error') && !message.toLowerCase().includes('failed')) {
  2000. setTimeout(() => {
  2001. if (statusElement && statusElement.textContent === message) {
  2002. statusElement.textContent = 'Ready to download videos';
  2003. }
  2004. }, 5000);
  2005. }
  2006. }
  2007. showError(message) {
  2008. this.updateStatusMessage(`Error: ${message}`);
  2009. logger.error('App Error:', message);
  2010. this.eventBus.emit('app:error', { type: 'user', message });
  2011. }
  2012. displayError(errorData) {
  2013. const message = errorData.error?.message || errorData.message || 'An error occurred';
  2014. this.updateStatusMessage(`Error: ${message}`);
  2015. }
  2016. /**
  2017. * Prompt user to update existing videos with new default settings
  2018. * @param {string} property - 'quality' or 'format'
  2019. * @param {string} newValue - New default value
  2020. */
  2021. promptUpdateExistingVideos(property, newValue) {
  2022. const allVideos = this.state.getVideos();
  2023. const selectedVideos = this.state.getSelectedVideos();
  2024. // Determine which videos to potentially update
  2025. // If videos are selected, only update those; otherwise, update all downloadable videos
  2026. const videosToCheck = selectedVideos.length > 0
  2027. ? selectedVideos.filter(v => v.status === 'ready' || v.status === 'error')
  2028. : allVideos.filter(v => v.status === 'ready' || v.status === 'error');
  2029. // Only prompt if there are videos that could be updated
  2030. if (videosToCheck.length === 0) {
  2031. return;
  2032. }
  2033. const propertyName = property === 'quality' ? 'quality' : 'format';
  2034. const scope = selectedVideos.length > 0 ? 'selected' : 'all';
  2035. const message = `Update ${scope} ${videosToCheck.length} video(s) in the list to use ${propertyName}: ${newValue}?`;
  2036. if (confirm(message)) {
  2037. let updatedCount = 0;
  2038. videosToCheck.forEach(video => {
  2039. this.state.updateVideo(video.id, { [property]: newValue });
  2040. updatedCount++;
  2041. });
  2042. this.updateStatusMessage(`Updated ${updatedCount} ${scope} video(s) with new ${propertyName}: ${newValue}`);
  2043. this.renderVideoList();
  2044. }
  2045. }
  2046. // Keyboard navigation
  2047. initializeKeyboardNavigation() {
  2048. // Enhanced keyboard navigation setup
  2049. document.addEventListener('keydown', (e) => {
  2050. // Ignore if user is typing in an input
  2051. if (e.target.tagName === 'INPUT' || e.target.tagName === 'TEXTAREA' || e.target.tagName === 'SELECT') {
  2052. return;
  2053. }
  2054. // Ctrl/Cmd shortcuts
  2055. if (e.ctrlKey || e.metaKey) {
  2056. switch (e.key) {
  2057. case 'a':
  2058. e.preventDefault();
  2059. this.state.selectAllVideos();
  2060. this.updateStatusMessage('All videos selected');
  2061. break;
  2062. case 'd':
  2063. e.preventDefault();
  2064. this.handleDownloadVideos();
  2065. break;
  2066. case ',':
  2067. e.preventDefault();
  2068. this.showSettingsModal();
  2069. break;
  2070. case '/':
  2071. e.preventDefault();
  2072. // Show shortcuts tab in settings
  2073. this.showSettingsModal();
  2074. // Switch to shortcuts tab
  2075. setTimeout(() => {
  2076. const shortcutsTab = document.querySelector('.settings-tab[data-tab="shortcuts"]');
  2077. if (shortcutsTab) shortcutsTab.click();
  2078. }, 100);
  2079. break;
  2080. }
  2081. }
  2082. // Non-modifier shortcuts
  2083. else {
  2084. switch (e.key) {
  2085. case 'Delete':
  2086. case 'Backspace':
  2087. e.preventDefault();
  2088. const selectedVideos = this.state.getSelectedVideos();
  2089. if (selectedVideos.length > 0) {
  2090. selectedVideos.forEach(video => {
  2091. this.state.removeVideo(video.id);
  2092. });
  2093. this.updateStatusMessage(`Removed ${selectedVideos.length} video(s)`);
  2094. }
  2095. break;
  2096. case ' ':
  2097. // Space to toggle selection of focused video
  2098. e.preventDefault();
  2099. const focusedItem = document.querySelector('.video-item:focus-within');
  2100. if (focusedItem) {
  2101. const videoId = focusedItem.dataset.videoId;
  2102. if (videoId) {
  2103. this.state.toggleVideoSelection(videoId);
  2104. }
  2105. }
  2106. break;
  2107. case 'Escape':
  2108. // Close any open modals
  2109. const modals = ['settingsModal', 'playlistModal', 'previewModal', 'historyModal'];
  2110. modals.forEach(modalId => {
  2111. const modal = document.getElementById(modalId);
  2112. if (modal && modal.classList.contains('flex')) {
  2113. modal.classList.remove('flex');
  2114. modal.classList.add('hidden');
  2115. }
  2116. });
  2117. break;
  2118. case 'p':
  2119. case 'P':
  2120. // Pause/Resume selected downloading videos
  2121. e.preventDefault();
  2122. const selectedVids = this.state.getSelectedVideos();
  2123. if (selectedVids.length > 0) {
  2124. selectedVids.forEach(video => {
  2125. if (video.status === 'downloading') {
  2126. this.handlePauseDownload(video.id);
  2127. } else if (video.status === 'paused') {
  2128. this.handleResumeDownload(video.id);
  2129. }
  2130. });
  2131. }
  2132. break;
  2133. }
  2134. }
  2135. });
  2136. }
  2137. // Ensure save directory exists
  2138. async ensureSaveDirectoryExists() {
  2139. const savePath = this.state.config.savePath;
  2140. if (!savePath || !window.electronAPI) return;
  2141. try {
  2142. const result = await window.electronAPI.createDirectory(savePath);
  2143. if (!result.success) {
  2144. logger.warn('Failed to create save directory:', result.error);
  2145. } else {
  2146. logger.debug('Save directory ready:', result.path);
  2147. }
  2148. } catch (error) {
  2149. logger.error('Error creating directory:', error.message);
  2150. }
  2151. }
  2152. // Check binary status and validate with blocking dialog if missing
  2153. async checkAndValidateBinaries() {
  2154. if (!window.IPCManager || !window.IPCManager.isAvailable()) return;
  2155. try {
  2156. const versions = await window.IPCManager.checkBinaryVersions();
  2157. // Handle both ytDlp (from main.js) and ytdlp (legacy) formats
  2158. const ytdlp = versions.ytDlp || versions.ytdlp;
  2159. const ffmpeg = versions.ffmpeg;
  2160. if (!versions || !ytdlp || !ytdlp.available || !ffmpeg || !ffmpeg.available) {
  2161. this.updateDependenciesButtonStatus('missing');
  2162. this.updateBinaryVersionDisplay(null);
  2163. // Show blocking dialog to warn user
  2164. await this.showMissingBinariesDialog(ytdlp, ffmpeg);
  2165. } else {
  2166. this.updateDependenciesButtonStatus('ok');
  2167. // Normalize the format for display
  2168. const normalizedVersions = {
  2169. ytdlp: ytdlp,
  2170. ffmpeg: ffmpeg
  2171. };
  2172. this.updateBinaryVersionDisplay(normalizedVersions);
  2173. }
  2174. } catch (error) {
  2175. logger.error('Error checking binary status:', error.message);
  2176. // Set missing status on error
  2177. this.updateDependenciesButtonStatus('missing');
  2178. this.updateBinaryVersionDisplay(null);
  2179. // Show dialog on error too
  2180. await this.showMissingBinariesDialog(null, null);
  2181. }
  2182. }
  2183. // Show blocking dialog when binaries are missing
  2184. async showMissingBinariesDialog(ytdlp, ffmpeg) {
  2185. // Determine which binaries are missing
  2186. const missingBinaries = [];
  2187. if (!ytdlp || !ytdlp.available) missingBinaries.push('yt-dlp');
  2188. if (!ffmpeg || !ffmpeg.available) missingBinaries.push('ffmpeg');
  2189. const missingList = missingBinaries.length > 0
  2190. ? missingBinaries.join(', ')
  2191. : 'yt-dlp and ffmpeg';
  2192. if (window.electronAPI && window.electronAPI.showErrorDialog) {
  2193. // Use native Electron dialog
  2194. await window.electronAPI.showErrorDialog({
  2195. title: 'Required Binaries Missing',
  2196. message: `The following required binaries are missing: ${missingList}`,
  2197. detail: 'Please run "npm run setup" in the terminal to download the required binaries.\n\n' +
  2198. 'Without these binaries, GrabZilla cannot download or convert videos.\n\n' +
  2199. 'After running "npm run setup", restart the application.'
  2200. });
  2201. } else {
  2202. // Fallback to browser alert
  2203. alert(
  2204. `⚠️ Required Binaries Missing\n\n` +
  2205. `Missing: ${missingList}\n\n` +
  2206. `Please run "npm run setup" to download the required binaries.\n\n` +
  2207. `Without these binaries, GrabZilla cannot download or convert videos.`
  2208. );
  2209. }
  2210. }
  2211. // Check binary status and update UI (non-blocking version for updates)
  2212. async checkBinaryStatus() {
  2213. if (!window.IPCManager || !window.IPCManager.isAvailable()) return;
  2214. try {
  2215. const versions = await window.IPCManager.checkBinaryVersions();
  2216. // Handle both ytDlp (from main.js) and ytdlp (legacy) formats
  2217. const ytdlp = versions.ytDlp || versions.ytdlp;
  2218. const ffmpeg = versions.ffmpeg;
  2219. if (!versions || !ytdlp || !ytdlp.available || !ffmpeg || !ffmpeg.available) {
  2220. this.updateDependenciesButtonStatus('missing');
  2221. this.updateBinaryVersionDisplay(null);
  2222. } else {
  2223. this.updateDependenciesButtonStatus('ok');
  2224. // Normalize the format for display
  2225. const normalizedVersions = {
  2226. ytdlp: ytdlp,
  2227. ffmpeg: ffmpeg
  2228. };
  2229. this.updateBinaryVersionDisplay(normalizedVersions);
  2230. }
  2231. } catch (error) {
  2232. logger.error('Error checking binary status:', error.message);
  2233. // Set missing status on error
  2234. this.updateDependenciesButtonStatus('missing');
  2235. this.updateBinaryVersionDisplay(null);
  2236. }
  2237. }
  2238. updateBinaryVersionDisplay(versions) {
  2239. const statusMessage = document.getElementById('statusMessage');
  2240. const ytdlpVersionNumber = document.getElementById('ytdlpVersionNumber');
  2241. const ytdlpUpdateBadge = document.getElementById('ytdlpUpdateBadge');
  2242. const ffmpegVersionNumber = document.getElementById('ffmpegVersionNumber');
  2243. const lastUpdateCheck = document.getElementById('lastUpdateCheck');
  2244. if (!versions) {
  2245. // Binaries missing
  2246. if (statusMessage) statusMessage.textContent = 'Ready to download videos - Binaries required';
  2247. if (ytdlpVersionNumber) ytdlpVersionNumber.textContent = 'missing';
  2248. if (ffmpegVersionNumber) ffmpegVersionNumber.textContent = 'missing';
  2249. if (ytdlpUpdateBadge) ytdlpUpdateBadge.classList.add('hidden');
  2250. if (lastUpdateCheck) lastUpdateCheck.textContent = '--';
  2251. return;
  2252. }
  2253. // Update yt-dlp version
  2254. if (ytdlpVersionNumber) {
  2255. const ytdlpVersion = versions.ytdlp?.version || 'unknown';
  2256. ytdlpVersionNumber.textContent = ytdlpVersion;
  2257. }
  2258. // Show/hide update badge for yt-dlp
  2259. if (ytdlpUpdateBadge) {
  2260. if (versions.ytdlp?.updateAvailable) {
  2261. ytdlpUpdateBadge.classList.remove('hidden');
  2262. ytdlpUpdateBadge.title = `Update available: ${versions.ytdlp.latestVersion || 'newer version'}`;
  2263. } else {
  2264. ytdlpUpdateBadge.classList.add('hidden');
  2265. }
  2266. }
  2267. // Update ffmpeg version
  2268. if (ffmpegVersionNumber) {
  2269. const ffmpegVersion = versions.ffmpeg?.version || 'unknown';
  2270. ffmpegVersionNumber.textContent = ffmpegVersion;
  2271. }
  2272. // Update last check timestamp
  2273. if (lastUpdateCheck) {
  2274. const now = new Date();
  2275. const timeString = now.toLocaleTimeString([], { hour: '2-digit', minute: '2-digit' });
  2276. lastUpdateCheck.textContent = `checked ${timeString}`;
  2277. lastUpdateCheck.title = `Last update check: ${now.toLocaleString()}`;
  2278. }
  2279. // Update status message
  2280. if (statusMessage) {
  2281. const hasUpdates = versions.ytdlp?.updateAvailable;
  2282. statusMessage.textContent = hasUpdates ?
  2283. 'Update available for yt-dlp' :
  2284. 'Ready to download videos';
  2285. }
  2286. }
  2287. updateDependenciesButtonStatus(status) {
  2288. const btn = document.getElementById('updateDepsBtn');
  2289. if (!btn) return;
  2290. if (status === 'missing') {
  2291. btn.classList.add('bg-red-600', 'animate-pulse');
  2292. btn.classList.remove('bg-[#314158]');
  2293. btn.innerHTML = '<img src="assets/icons/refresh.svg" alt="" width="16" height="16" loading="lazy">⚠️ Required';
  2294. } else {
  2295. btn.classList.remove('bg-red-600', 'animate-pulse');
  2296. btn.classList.add('bg-[#314158]');
  2297. btn.innerHTML = '<img src="assets/icons/refresh.svg" alt="" width="16" height="16" loading="lazy">Check for Updates';
  2298. }
  2299. }
  2300. // State persistence
  2301. async loadState() {
  2302. try {
  2303. const savedState = localStorage.getItem('grabzilla-state');
  2304. if (savedState) {
  2305. const data = JSON.parse(savedState);
  2306. this.state.fromJSON(data);
  2307. logger.debug('✅ Loaded saved state');
  2308. // Re-render video list to show restored videos
  2309. this.renderVideoList();
  2310. this.updateSavePathDisplay();
  2311. this.updateStatsDisplay();
  2312. }
  2313. } catch (error) {
  2314. logger.warn('Failed to load saved state:', error);
  2315. }
  2316. }
  2317. async saveState() {
  2318. try {
  2319. const stateData = this.state.toJSON();
  2320. localStorage.setItem('grabzilla-state', JSON.stringify(stateData));
  2321. } catch (error) {
  2322. logger.warn('Failed to save state:', error);
  2323. }
  2324. }
  2325. // Lifecycle methods
  2326. handleInitializationError(error) {
  2327. // Show fallback UI or error message
  2328. const statusElement = document.getElementById('statusMessage');
  2329. if (statusElement) {
  2330. statusElement.textContent = 'Failed to initialize application';
  2331. }
  2332. }
  2333. destroy() {
  2334. // Clean up resources
  2335. if (this.state) {
  2336. this.saveState();
  2337. }
  2338. // Remove event listeners
  2339. this.eventBus?.removeAllListeners();
  2340. this.initialized = false;
  2341. logger.debug('🧹 GrabZilla app destroyed');
  2342. }
  2343. }
  2344. // Initialize function to be called after all scripts are loaded
  2345. window.initializeGrabZilla = function() {
  2346. window.app = new GrabZillaApp();
  2347. window.app.init();
  2348. };
  2349. // Auto-save state on page unload
  2350. window.addEventListener('beforeunload', () => {
  2351. if (window.app?.initialized) {
  2352. window.app.saveState();
  2353. }
  2354. });
  2355. // Export the app class
  2356. if (typeof module !== 'undefined' && module.exports) {
  2357. module.exports = GrabZillaApp;
  2358. } else {
  2359. window.GrabZillaApp = GrabZillaApp;
  2360. }
  2361. })(); // End of wrapper function