download-integration-patch.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /**
  2. * @fileoverview Integration patch for enhanced download methods
  3. * @author GrabZilla Development Team
  4. * @version 2.1.0
  5. * @since 2024-01-01
  6. */
  7. /**
  8. * DOWNLOAD INTEGRATION PATCH
  9. *
  10. * Patches the main GrabZilla app with enhanced download functionality
  11. * Replaces placeholder methods with real yt-dlp integration
  12. *
  13. * Usage: Include this script after the main app.js to apply patches
  14. */
  15. // Wait for DOM and app to be ready
  16. document.addEventListener('DOMContentLoaded', function() {
  17. // Wait a bit more for the app to initialize
  18. setTimeout(function() {
  19. if (typeof window.app !== 'undefined' && window.app instanceof GrabZilla) {
  20. console.log('Applying enhanced download method patches...');
  21. applyDownloadPatches(window.app);
  22. } else {
  23. console.warn('GrabZilla app not found, retrying...');
  24. // Retry after a longer delay
  25. setTimeout(function() {
  26. if (typeof window.app !== 'undefined' && window.app instanceof GrabZilla) {
  27. console.log('Applying enhanced download method patches (retry)...');
  28. applyDownloadPatches(window.app);
  29. } else {
  30. console.error('Failed to find GrabZilla app instance for patching');
  31. console.log('Available on window:', Object.keys(window).filter(k => k.includes('app') || k.includes('grab')));
  32. }
  33. }, 2000);
  34. }
  35. }, 500);
  36. });
  37. /**
  38. * Apply enhanced download method patches to the app instance
  39. * @param {GrabZilla} app - The main application instance
  40. */
  41. function applyDownloadPatches(app) {
  42. try {
  43. // Load enhanced methods
  44. if (typeof window.EnhancedDownloadMethods === 'undefined') {
  45. console.error('Enhanced download methods not loaded');
  46. return;
  47. }
  48. const methods = window.EnhancedDownloadMethods;
  49. // Patch core download methods
  50. console.log('Patching handleDownloadVideos method...');
  51. app.handleDownloadVideos = methods.handleDownloadVideos.bind(app);
  52. console.log('Patching fetchVideoMetadata method...');
  53. app.fetchVideoMetadata = methods.fetchVideoMetadata.bind(app);
  54. console.log('Patching handleDownloadProgress method...');
  55. app.handleDownloadProgress = methods.handleDownloadProgress.bind(app);
  56. console.log('Patching checkBinaries method...');
  57. app.checkBinaries = methods.checkBinaries.bind(app);
  58. // Patch UI update methods
  59. console.log('Patching updateBinaryStatus method...');
  60. app.updateBinaryStatus = methods.updateBinaryStatus.bind(app);
  61. console.log('Patching file selection methods...');
  62. app.handleSelectSaveDirectory = methods.handleSelectSaveDirectory.bind(app);
  63. app.handleSelectCookieFile = methods.handleSelectCookieFile.bind(app);
  64. // Patch UI helper methods
  65. app.updateSavePathUI = methods.updateSavePathUI.bind(app);
  66. app.updateCookieFileUI = methods.updateCookieFileUI.bind(app);
  67. // Re-initialize binary checking with enhanced methods
  68. console.log('Re-initializing binary check with enhanced methods...');
  69. app.checkBinaries();
  70. // Update event listeners for file selection if they exist
  71. patchFileSelectionListeners(app);
  72. console.log('Enhanced download method patches applied successfully!');
  73. } catch (error) {
  74. console.error('Error applying download method patches:', error);
  75. }
  76. }
  77. /**
  78. * Patch file selection event listeners
  79. * @param {GrabZilla} app - The main application instance
  80. */
  81. function patchFileSelectionListeners(app) {
  82. try {
  83. // Patch save directory selection
  84. const savePathBtn = document.getElementById('savePathBtn');
  85. if (savePathBtn) {
  86. // Remove existing listeners by cloning the element
  87. const newSavePathBtn = savePathBtn.cloneNode(true);
  88. savePathBtn.parentNode.replaceChild(newSavePathBtn, savePathBtn);
  89. // Add enhanced listener
  90. newSavePathBtn.addEventListener('click', () => {
  91. app.handleSelectSaveDirectory();
  92. });
  93. console.log('Patched save directory selection listener');
  94. }
  95. // Patch cookie file selection
  96. const cookieFileBtn = document.getElementById('cookieFileBtn');
  97. if (cookieFileBtn) {
  98. // Remove existing listeners by cloning the element
  99. const newCookieFileBtn = cookieFileBtn.cloneNode(true);
  100. cookieFileBtn.parentNode.replaceChild(newCookieFileBtn, cookieFileBtn);
  101. // Add enhanced listener
  102. newCookieFileBtn.addEventListener('click', () => {
  103. app.handleSelectCookieFile();
  104. });
  105. console.log('Patched cookie file selection listener');
  106. }
  107. // Patch download button if it exists
  108. const downloadBtn = document.getElementById('downloadVideosBtn');
  109. if (downloadBtn) {
  110. // Remove existing listeners by cloning the element
  111. const newDownloadBtn = downloadBtn.cloneNode(true);
  112. downloadBtn.parentNode.replaceChild(newDownloadBtn, downloadBtn);
  113. // Add enhanced listener
  114. newDownloadBtn.addEventListener('click', () => {
  115. app.handleDownloadVideos();
  116. });
  117. console.log('Patched download button listener');
  118. }
  119. } catch (error) {
  120. console.error('Error patching file selection listeners:', error);
  121. }
  122. }
  123. // Export for manual application if needed
  124. if (typeof window !== 'undefined') {
  125. window.applyDownloadPatches = applyDownloadPatches;
  126. }