app.js 108 KB

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