File size: 3,200 Bytes
3b63ecd
 
 
1ac45dd
 
41709e3
3b63ecd
 
 
1ac45dd
 
 
41709e3
3b63ecd
 
1ac45dd
 
 
41709e3
 
3b63ecd
 
 
 
 
 
 
 
 
 
1ac45dd
3b63ecd
 
1ac45dd
 
 
 
 
 
 
3b63ecd
 
 
 
 
1ac45dd
3b63ecd
 
 
1ac45dd
 
 
 
 
 
 
 
 
 
 
3b63ecd
 
 
 
 
1ac45dd
3b63ecd
 
 
 
 
 
 
 
 
 
 
 
 
41709e3
 
3b63ecd
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/**
 * V.AI STUDIO - Auto Excel Fix Loader v2.0
 * Tự động tải fix script nếu chưa có, inject vào cuối body
 * 
 * CẬP NHẬT: Hỗ trợ cả fix v2.0 - multi-download hoàn hảo
 */
(function() {
    'use strict';
    
    // Kiểm tra xem file fix đã tồn tại chưa - ưu tiên v2
    var fixFile = 'vai-excel-fix-v2.js';
    var fixFileLegacy = 'vai-quote-excel-order-fix-v1050.js';
    
    // Kiểm tra script đã load chưa
    function checkFixLoaded() {
        return (typeof window.VAI_EXCEL_FIX !== 'undefined') ||
               (typeof window.VAI_FIX !== 'undefined') ||
               document.querySelector('script[src*="vai-excel-fix"]');
    }
    
    // Inject script tag
    function injectFix() {
        if (checkFixLoaded()) {
            console.log('[Auto Loader] Fix script already loaded');
            return;
        }
        
        var script = document.createElement('script');
        script.src = '/' + fixFile + '?v=' + Date.now();
        script.onload = function() {
            console.log('[Auto Loader] Excel fix v2 loaded successfully - multi-download enabled');
        };
        script.onerror = function() {
            console.warn('[Auto Loader] v2 failed, trying legacy fix');
            var legacy = document.createElement('script');
            legacy.src = '/' + fixFileLegacy + '?v=' + Date.now();
            legacy.onload = function() {
                console.log('[Auto Loader] Legacy fix loaded');
            };
            document.head.appendChild(legacy);
        };
        
        document.head.appendChild(script);
    }
    
    // Inline fix fallback (backup)
    function applyInlineFix() {
        var originalRevoke = URL.revokeObjectURL;
        URL.revokeObjectURL = function(url) {
            if (!url || typeof url !== 'string') return originalRevoke.apply(this, arguments);
            if (url.startsWith('blob:')) {
                // Register but don't revoke immediately - blob persists until unload
                window.__VAI_BLOB_REGISTRY = window.__VAI_BLOB_REGISTRY || { blobs: [], isUnloading: false };
                window.__VAI_BLOB_REGISTRY.blobs.push({ url: url, revoked: false });
                window.addEventListener('beforeunload', function() {
                    window.__VAI_BLOB_REGISTRY.isUnloading = true;
                    window.__VAI_BLOB_REGISTRY.blobs.forEach(function(b) {
                        if (!b.revoked) try { URL.revokeObjectURL(b.url); b.revoked = true; } catch(e) {}
                    });
                });
                return;
            }
            return originalRevoke.apply(this, arguments);
        };
        
        console.log('[Auto Loader] Inline fallback applied');
    }
    
    // Chờ DOM ready
    if (document.readyState === 'loading') {
        document.addEventListener('DOMContentLoaded', injectFix);
    } else {
        injectFix();
    }
    
    // Thử lại sau 2s nếu chưa load
    setTimeout(injectFix, 2000);
    setTimeout(injectFix, 5000);
    
})();

// Legacy support for older fix loaders
window.__VAI_BLOB_REGISTRY = window.__VAI_BLOB_REGISTRY || { blobs: [], registry: [], isUnloading: false };