// --- 6. engine.js --- ajaxHooker.protect(); ajaxHooker.filter([{ url: '.m3u8', method: 'GET' }]); ajaxHooker.hook(async request => { if (request.url.includes('.m3u8') && (request.url.includes('?') || request.url.includes('.cloudfront.net'))) { const videoKey = request.url.split('?')[0]; const isTrialFormat = request.url.includes('_0001.m3u8') || request.url.includes('preview'); if (isTrialFormat) { if (currentVideoKey === videoKey) { if (window.real_m3u8_url) request.url = window.real_m3u8_url; return; } const { data: { session } } = await supabaseClient.auth.getSession(); const token = session ? session.access_token : null; const deviceId = getDeviceId(); try { const response = await fetch(`${CONFIG.SB_URL}/functions/v1/get-vip-video`, { method: 'POST', headers: { 'Content-Type': 'application/json', 'Authorization': token ? `Bearer ${token}` : 'Bearer null' }, body: JSON.stringify({ trialUrl: request.url, deviceId: deviceId }) }); const data = await response.json(); if (response.ok && data.fullUrl) { currentVideoKey = videoKey; request.url = data.fullUrl; window.real_m3u8_url = data.fullUrl; if (data.isFreeTrial) showToast(`🎁 Đã dùng ${data.viewsTaken}/5 lượt xem miễn phí thiết bị`); } else { showToast("❌ Hết 5 lượt xem thử trên thiết bị này! Vui lòng nâng cấp VIP."); document.getElementById('auth-panel').style.display = 'block'; document.getElementById('vip-floating-btn').style.display = 'none'; } } catch (e) { console.error("Lỗi kết nối máy chủ VIP", e); } } else { if (currentVideoKey !== videoKey) { currentVideoKey = videoKey; window.real_m3u8_url = request.url; } } } }); function mainLoop() { document.querySelectorAll("div.homeAdPop, div.vue-nice-modal-root, .exo-native-widget, div.preview-ui").forEach(s => s.remove()); if (window.real_m3u8_url && window.real_m3u8_url !== (window.his_m3u8_url || '')) { window.his_m3u8_url = window.real_m3u8_url; startHls(window.real_m3u8_url); } } function startHls(url) { const container = document.getElementById('mini-player-container'), videoElement = document.getElementById('mini-video'); container.style.display = 'block'; if (Hls.isSupported()) { const hls = new Hls({ maxBufferLength: 30 }); hls.loadSource(url); hls.attachMedia(videoElement); hls.on(Hls.Events.MANIFEST_PARSED, () => videoElement.play().catch(() => {})); } else if (videoElement.canPlayType('application/vnd.apple.mpegurl')) { videoElement.src = url; videoElement.play(); } }