import sys
import time
from playwright.sync_api import sync_playwright

if hasattr(sys.stdout, 'reconfigure'):
    sys.stdout.reconfigure(encoding='utf-8')

with sync_playwright() as p:
    browser = p.chromium.connect_over_cdp("http://127.0.0.1:9224")
    page = browser.contexts[0].pages[0]

    # Quét tất cả các khối request
    # Mỗi khối thường là một thẻ a hoặc div chứa tên, thông tin mutual friend, nút Confirm
    rows = page.locator("div[role='listitem']").all()
    print(f"Scanning {len(rows)} items...")

    for i, row in enumerate(rows):
        text = row.inner_text()
        first_line = text.split('\n')[0] if text else ""
        
        # Kiểm tra ảnh avatar trong row
        img = row.locator("img, svg, image").first
        img_src = ""
        if img.count() > 0:
            img_src = img.get_attribute("src") or img.get_attribute("xlink:href") or ""

        is_blank = "rsrc.php" in img_src or "blank" in img_src or "svg" in img.evaluate("e => e.tagName.toLowerCase()")
        has_mutual = "mutual friend" in text.lower() or "bạn chung" in text.lower()

        print(f"[{i}] Name: '{first_line}' | Has Mutual: {has_mutual} | ImgSrc snippet: '{img_src[:60]}' | IsBlank: {is_blank}")
