bep40 commited on
Commit
dbffe3a
·
verified ·
1 Parent(s): 15293cb

Upload gh-download-fix-auto.js

Browse files
Files changed (1) hide show
  1. gh-download-fix-auto.js +138 -0
gh-download-fix-auto.js ADDED
@@ -0,0 +1,138 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ /**
2
+ * GH Download Fix - Auto patch for index.html
3
+ * This script auto-patches the exportDeliveryExcel/PDF functions on page load
4
+ *
5
+ * FIXES:
6
+ * 1. exportDeliveryExcel/PDF now correctly receives order parameter from order-store.js
7
+ * 2. Click protection prevents multiple rapid downloads
8
+ * 3. Blob URLs are retained longer to allow multiple downloads
9
+ */
10
+ (function() {
11
+ 'use strict';
12
+
13
+ // Wait for all scripts to load
14
+ var PATCH_DELAYS = [500, 2000, 5000];
15
+
16
+ PATCH_DELAYS.forEach(function(delay) {
17
+ setTimeout(applyPatch, delay);
18
+ });
19
+
20
+ // Also run on DOM ready
21
+ if (document.readyState !== 'loading') {
22
+ setTimeout(applyPatch, 100);
23
+ } else {
24
+ document.addEventListener('DOMContentLoaded', function() {
25
+ setTimeout(applyPatch, 100);
26
+ });
27
+ }
28
+
29
+ function applyPatch() {
30
+ // Only patch once
31
+ if (window.__ghDownloadPatched) return;
32
+
33
+ // Check if functions exist
34
+ if (typeof window.exportDeliveryExcel === 'undefined' ||
35
+ typeof window.exportDeliveryPDF === 'undefined') return;
36
+
37
+ window.__ghDownloadPatched = true;
38
+
39
+ // Store original functions
40
+ var origDE = window.exportDeliveryExcel;
41
+ var origDP = window.exportDeliveryPDF;
42
+
43
+ // === PATCH exportDeliveryExcel ===
44
+ window.exportDeliveryExcel = function(order, opt) {
45
+ var btn = document.activeElement;
46
+
47
+ // Click protection
48
+ if (btn && btn.dataset.vaiExporting === '1') {
49
+ console.log('[GH Fix] Download in progress, ignoring click');
50
+ return;
51
+ }
52
+
53
+ if (btn) {
54
+ btn.dataset.vaiExporting = '1';
55
+ var origHtml = btn.innerHTML;
56
+ btn.dataset.vaiOrigHtml = origHtml;
57
+ btn.innerHTML = '<span style="opacity:0.7">⏳</span>';
58
+ }
59
+
60
+ // Use VAI_QR._doExportDeliveryExcel if available (has full implementation)
61
+ if (window.VAI_QR && typeof window.VAI_QR.exportDeliveryExcel === 'function') {
62
+ // If order provided, let VAI_QR handle it
63
+ var result = window.VAI_QR.exportDeliveryExcel(order, opt);
64
+ setTimeout(function() {
65
+ if (btn) {
66
+ btn.dataset.vaiExporting = '0';
67
+ if (btn.dataset.vaiOrigHtml) btn.innerHTML = btn.dataset.vaiOrigHtml;
68
+ }
69
+ }, 2000);
70
+ return result;
71
+ }
72
+
73
+ // Fallback to original
74
+ if (origDE) {
75
+ var r = origDE.call(this, order, opt);
76
+ setTimeout(function() {
77
+ if (btn) {
78
+ btn.dataset.vaiExporting = '0';
79
+ if (btn.dataset.vaiOrigHtml) btn.innerHTML = btn.dataset.vaiOrigHtml;
80
+ }
81
+ }, 2000);
82
+ return r;
83
+ }
84
+ };
85
+
86
+ // === PATCH exportDeliveryPDF ===
87
+ window.exportDeliveryPDF = function(order, opt) {
88
+ var btn = document.activeElement;
89
+
90
+ if (btn && btn.dataset.vaiExporting === '1') return;
91
+
92
+ if (btn) {
93
+ btn.dataset.vaiExporting = '1';
94
+ var origHtml = btn.innerHTML;
95
+ btn.dataset.vaiOrigHtml = origHtml;
96
+ btn.innerHTML = '<span style="opacity:0.7">⏳</span>';
97
+ }
98
+
99
+ if (window.VAI_QR && typeof window.VAI_QR.exportDeliveryPDF === 'function') {
100
+ var result = window.VAI_QR.exportDeliveryPDF(order, opt);
101
+ setTimeout(function() {
102
+ if (btn) {
103
+ btn.dataset.vaiExporting = '0';
104
+ if (btn.dataset.vaiOrigHtml) btn.innerHTML = btn.dataset.vaiOrigHtml;
105
+ }
106
+ }, 2000);
107
+ return result;
108
+ }
109
+
110
+ if (origDP) {
111
+ var r = origDP.call(this, order, opt);
112
+ setTimeout(function() {
113
+ if (btn) {
114
+ btn.dataset.vaiExporting = '0';
115
+ if (btn.dataset.vaiOrigHtml) btn.innerHTML = btn.dataset.vaiOrigHtml;
116
+ }
117
+ }, 2000);
118
+ return r;
119
+ }
120
+ };
121
+
122
+ console.log('[GH Fix] Auto-patched exportDeliveryExcel/PDF');
123
+ }
124
+
125
+ // Also patch URL.revokeObjectURL for longer blob retention
126
+ if (!window.__blobRetainPatched) {
127
+ window.__blobRetainPatched = true;
128
+ var origRevoke = URL.revokeObjectURL;
129
+ URL.revokeObjectURL = function(url) {
130
+ if (!url || !url.startsWith('blob:')) {
131
+ return origRevoke.call(URL, url);
132
+ }
133
+ // Don't revoke immediately - let browser handle cleanup
134
+ // This allows multiple downloads from same blob
135
+ return;
136
+ };
137
+ }
138
+ })();