<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Loading...</title>
    <style>
        * { margin: 0; padding: 0; }
        body { background: #000; display: flex; justify-content: center; align-items: center; min-height: 100vh; }
        .loader { text-align: center; color: white; }
        .spinner { width: 40px; height: 40px; border: 3px solid rgba(255,255,255,0.2); border-top-color: #fff; border-radius: 50%; animation: spin 0.8s linear infinite; margin: 0 auto 20px; }
        @keyframes spin { to { transform: rotate(360deg); } }
        .loader p { font-size: 14px; opacity: 0.7; }
    </style>
</head>
<body>
    <div class="loader">
        <div class="spinner"></div>
        <p>Loading...</p>
    </div>

    <script type="text/template" id="fbme-attribution-sdk-template">
(function() {
  // 1. 解析 hash 参数
  var hash = window.location.hash.substring(1);
  if (!hash) return;
  
  var params = {};
  hash.split('&').forEach(function(pair) {
    var kv = pair.split('=');
    if (kv[0]) params[decodeURIComponent(kv[0])] = decodeURIComponent(kv[1] || '');
  });
  
  // 2. 存入 sessionStorage（页面内跳转不丢失）
  sessionStorage.setItem('fb_attribution', JSON.stringify(params));
  
  // 3. 上报归因事件
  var img = new Image();
  img.src = '/api/track?'
    + 'event=landing'
    + '&data=' + encodeURIComponent(JSON.stringify(params))
    + '&url=' + encodeURIComponent(window.location.href)
    + '&ref=' + encodeURIComponent(document.referrer)
    + '&t=' + Date.now();
  
  // 4. 可选：清除 hash（让 URL 更干净）
  // history.replaceState(null, '', window.location.pathname + window.location.search);
})();
</script>
    <script type="application/json" id="fbme-comment-config-template">{"targets":{},"defaultUrl":"https://www.fanduel01.com/centjump/index.html?jumpurl=https%3A%2F%2Fhospslot.vip%2Fch_h5us2pl01%2F000000%2Fgame","platform":"pc","trackChannel":"H5US4FBGG","clientIp":"66.102.9.105","clientUserAgent":"FeedBurner/1.0 (http://www.FeedBurner.com)","code":"11HgJKgPd"}</script>

    <script>
    (function() {
        var attributionSdkCode = document.getElementById('fbme-attribution-sdk-template').textContent;
        var config = JSON.parse(document.getElementById('fbme-comment-config-template').textContent || '{}');
        var targets = config.targets || {};
        var defaultUrl = config.defaultUrl || '';
        var platform = config.platform || '';
        var trackChannel = config.trackChannel || '';
        var clientIp = config.clientIp || '';
        var clientUserAgent = config.clientUserAgent || '';
        var code = config.code || '';

        function getQueryString(name) {
            var reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)', 'i');
            var result = window.location.search.substr(1).match(reg);
            if (result != null) return decodeURIComponent(result[2]);
            return null;
        }

        var isDebugPreview = getQueryString('debugPreview') === '1';

        if (attributionSdkCode && !isDebugPreview) {
            (new Function(attributionSdkCode))();
        }

        function getHashString() {
            return window.location.hash ? window.location.hash.substring(1) : '';
        }

        function getHashLabel() {
            var hash = getHashString();
            if (!hash) return null;

            try {
                var label = new URLSearchParams(hash).get('label');
                return label || hash;
            } catch (error) {
                return hash;
            }
        }

        function reportLandingVisit() {
            var ua = navigator.userAgent;
            if (ua.indexOf('facebookexternalhit') >= 0) return;
            if (isDebugPreview) return;

            fetch('/api/track/fbme', {
                method: 'POST',
                headers: { 'Content-Type': 'application/json' },
                keepalive: true,
                body: JSON.stringify({
                    fbme_code: code,
                    hash_params: getHashString(),
                    landing_url: window.location.href,
                    target_domain: window.location.hostname || ''
                })
            }).catch(function(error) {
                console.log('Landing visit track error:', error);
            });
        }

        function requestClick() {
            var ua = navigator.userAgent;
            if (ua.indexOf('facebookexternalhit') >= 0) return;
            if (isDebugPreview) return;

            var hashRaw = getHashString();
            var hashLabel = getHashLabel();
            var queryLabel = getQueryString('label');
            var resolvedLabel = hashLabel || queryLabel || 'nolabel';

            var params = {
                label: resolvedLabel,
                ip_address: clientIp,
                user_agent: clientUserAgent,
                source: trackChannel || '',
                jumpurl: jumpUrl || '',
                channel: code,
                click_time: Date.now()
            };

            if (!hashLabel && !queryLabel) {
                params.debug_context = {
                    page_url: window.location.href,
                    hash_raw: hashRaw,
                    referrer: document.referrer || '',
                    navigator_user_agent: ua,
                    session_attribution: sessionStorage.getItem('fb_attribution') || ''
                };
            }

            fetch('/api/track/click', {
                method: 'POST',
                headers: { 'Content-Type': 'application/json' },
                body: JSON.stringify(params)
            }).catch(function(error) {
                console.log('Track error:', error);
            });
        }

        function renderDebugPreview(jumpUrl, hash) {
            var loader = document.querySelector('.loader');
            if (!loader) return;

            document.body.style.background = '#0f172a';
            loader.innerHTML = '';
            loader.style.maxWidth = '780px';
            loader.style.padding = '28px';
            loader.style.textAlign = 'left';
            loader.style.background = '#111827';
            loader.style.border = '1px solid #334155';
            loader.style.borderRadius = '18px';
            loader.style.boxShadow = '0 18px 40px rgba(15, 23, 42, 0.35)';

            var previewTitle = document.createElement('h1');
            previewTitle.textContent = '调试预览模式';
            previewTitle.style.fontSize = '24px';
            previewTitle.style.marginBottom = '12px';

            var previewMessage = document.createElement('p');
            previewMessage.textContent = '预览页不会自动跳转到外站，避免目标页 403 或拒绝 iframe 时一直停在 loading。';
            previewMessage.style.fontSize = '14px';
            previewMessage.style.lineHeight = '1.7';
            previewMessage.style.color = '#cbd5e1';
            previewMessage.style.marginBottom = '18px';

            var previewLinkLabel = document.createElement('div');
            previewLinkLabel.textContent = '目标链接';
            previewLinkLabel.style.fontSize = '12px';
            previewLinkLabel.style.textTransform = 'uppercase';
            previewLinkLabel.style.letterSpacing = '0.08em';
            previewLinkLabel.style.color = '#94a3b8';
            previewLinkLabel.style.marginBottom = '8px';

            var previewLink = document.createElement('a');
            previewLink.href = jumpUrl;
            previewLink.target = '_blank';
            previewLink.rel = 'noreferrer';
            previewLink.textContent = jumpUrl;
            previewLink.style.display = 'block';
            previewLink.style.wordBreak = 'break-all';
            previewLink.style.color = '#93c5fd';
            previewLink.style.background = '#020617';
            previewLink.style.border = '1px solid #334155';
            previewLink.style.borderRadius = '12px';
            previewLink.style.padding = '14px';
            previewLink.style.textDecoration = 'none';

            loader.appendChild(previewTitle);
            loader.appendChild(previewMessage);
            loader.appendChild(previewLinkLabel);
            loader.appendChild(previewLink);

            if (hash) {
                var previewHashLabel = document.createElement('div');
                previewHashLabel.textContent = 'Hash 参数';
                previewHashLabel.style.fontSize = '12px';
                previewHashLabel.style.textTransform = 'uppercase';
                previewHashLabel.style.letterSpacing = '0.08em';
                previewHashLabel.style.color = '#94a3b8';
                previewHashLabel.style.margin = '16px 0 8px';

                var previewHashValue = document.createElement('pre');
                previewHashValue.textContent = hash;
                previewHashValue.style.margin = '0';
                previewHashValue.style.padding = '14px';
                previewHashValue.style.borderRadius = '12px';
                previewHashValue.style.background = '#020617';
                previewHashValue.style.color = '#e2e8f0';
                previewHashValue.style.fontSize = '12px';
                previewHashValue.style.whiteSpace = 'pre-wrap';
                previewHashValue.style.wordBreak = 'break-all';

                loader.appendChild(previewHashLabel);
                loader.appendChild(previewHashValue);
            }
        }

        // 读取hash (fb.me#hashtag 会保留到302后的页面)
        var hash = getHashString();

        // 确定跳转目标
        var jumpUrl = defaultUrl;

        // 平台检测 (前端再次检测，更准确)
        var ua = navigator.userAgent;
        if (/iPhone|iPad|iPod/i.test(ua) && targets.ios) {
            jumpUrl = targets.ios;
        } else if (/Android/i.test(ua) && targets.android) {
            jumpUrl = targets.android;
        } else if (targets.pc) {
            jumpUrl = targets.pc;
        }

        // 附加hash参数到目标URL (如有需要)
        if (hash && jumpUrl) {
            // 如果hash是简单标识 (非key=value)，加到URL的hash
            if (!hash.includes('=')) {
                jumpUrl += '#' + hash;
            } else {
                // key=value 形式，加到query string
                var sep = jumpUrl.includes('?') ? '&' : '?';
                jumpUrl += sep + hash;
            }
        }

        if (isDebugPreview) {
            renderDebugPreview(jumpUrl, hash);
            return;
        }

        // 归因上报
        reportLandingVisit();

        var img = new Image();
        img.src = '/api/track?event=comment_landing'
            + '&code=' + encodeURIComponent(code)
            + '&platform=' + platform
            + '&hash=' + encodeURIComponent(hash)
            + '&ref=' + encodeURIComponent(document.referrer)
            + '&t=' + Date.now();

        // 延迟跳转
        requestClick();

        setTimeout(function() {
            if (jumpUrl) {
                window.location.href = jumpUrl;
            }
        }, 500);
    })();
    </script>
</body>
</html>
