"""
探索详情ID来源 - 全面搜索
"""
import asyncio
import json
from playwright.async_api import async_playwright


async def explore():
    async with async_playwright() as p:
        browser = await p.chromium.launch(headless=False)
        page = await browser.new_page()
        page.set_default_timeout(60000)
        
        print("访问页面...")
        await page.goto(
            "https://kd.nsfc.cn/finalSearchList?s=人工智能",
            wait_until="domcontentloaded"
        )
        await page.wait_for_timeout(5000)
        
        try:
            await page.click('button:has-text("检索")')
            await page.wait_for_timeout(5000)
        except:
            pass
        
        # 全面搜索 Vue 数据
        print("\n全面搜索 Vue 组件数据...")
        result = await page.evaluate('''() => {
            const found = [];
            
            const search = (el, path = '') => {
                if (!el || found.length > 10) return;
                
                if (el.__vue__) {
                    const vm = el.__vue__;
                    const keys = vm.$data ? Object.keys(vm.$data) : [];
                    
                    // 检查所有数组类型的数据
                    keys.forEach(key => {
                        const val = vm.$data[key];
                        if (Array.isArray(val) && val.length > 0) {
                            found.push({
                                path: path,
                                key: key,
                                length: val.length,
                                sample: JSON.stringify(val[0]).substring(0, 300)
                            });
                        }
                    });
                }
                
                Array.from(el.children || []).forEach((child, i) => {
                    search(child, path + '/' + (child.className || child.tagName));
                });
            };
            
            search(document.body);
            return found;
        }''')
        
        print(f"找到 {len(result)} 个数组数据:")
        for item in result:
            print(f"\n路径: {item['path'][:50]}")
            print(f"键名: {item['key']}, 长度: {item['length']}")
            print(f"样本: {item['sample']}")
        
        await browser.close()


if __name__ == "__main__":
    asyncio.run(explore())
