import sys
import time
from pathlib import Path
from invisible_playwright import InvisiblePlaywright

if sys.platform == "win32":
    sys.stdout.reconfigure(encoding="utf-8", line_buffering=True)
    sys.stderr.reconfigure(encoding="utf-8", line_buffering=True)

pdir = Path.home() / 'AntigravityProfiles' / 'phu2234_worker'

with InvisiblePlaywright(profile_dir=pdir, headless=True) as ctx:
    page = ctx.pages[0] if ctx.pages else ctx.new_page()
    page.goto('https://muse.ai', wait_until='networkidle', timeout=30000)
    time.sleep(3)
    
    # Lấy toàn bộ các phần tử có chứa text của subthreads
    # In screenshot we saw: "Tạo video cận cảnh mắt ho...", "Tạo video cận cảnh cụ ông", etc.
    items = page.evaluate('''() => {
        const results = [];
        const elements = document.querySelectorAll('button, div[role="button"], a');
        for (const el of elements) {
            const txt = el.innerText?.trim();
            if (txt && (txt.startsWith('Tạo video') || txt.startsWith('Video') || txt.includes('Kiều') || txt.includes('Kim Trọng'))) {
                results.push({
                    tag: el.tagName,
                    text: txt.slice(0, 50),
                    className: el.className
                });
            }
        }
        return results;
    }''')
    
    print(f"Tìm thấy {len(items)} phần tử tiềm năng:")
    for idx, it in enumerate(items[:20]):
        print(f"[{idx}] {it['tag']}: {it['text']}")
