// 检查导航结构并测试点击导航项 const wsUrl = process.argv[2] || "ws://127.0.0.1:9222/devtools/page/3"; const ws = new WebSocket(wsUrl); let id = 0; const pending = new Map(); function send(method, params = {}) { return new Promise((resolve, reject) => { const msgId = ++id; pending.set(msgId, { resolve, reject }); ws.send(JSON.stringify({ id: msgId, method, params })); }); } ws.onmessage = (event) => { const msg = JSON.parse(event.data); if (msg.id && pending.has(msg.id)) { const { resolve, reject } = pending.get(msg.id); pending.delete(msg.id); if (msg.error) reject(new Error(JSON.stringify(msg.error))); else resolve(msg.result); } }; ws.onerror = (e) => { console.error("WS error:", e.message || e); process.exit(1); }; ws.onopen = async () => { try { // 1. 检查导航结构 const navExpr = `JSON.stringify({ navEls: Array.from(document.querySelectorAll('a, [role=tab], button')).filter(e => e.offsetParent !== null).slice(0, 15).map(e => ({ tag: e.tagName, text: (e.innerText||'').trim().slice(0,20), href: e.getAttribute('href') || '' })), scrollableNav: (() => { const el = document.querySelector('[class*=nav], [class*=Navbar], header'); return el ? { cls: el.className, scrollW: el.scrollWidth, clientW: el.clientWidth } : null; })() })`; const navRes = await send("Runtime.evaluate", { expression: navExpr, returnByValue: true }); console.log("NAV_STRUCTURE:", navRes.result.value); // 2. 点击"实用工具"导航项 const clickExpr = `(() => { const els = Array.from(document.querySelectorAll('a, [role=tab], button, div, span')); const target = els.find(e => (e.innerText||'').trim() === '实用工具' && e.offsetParent !== null); if (target) { target.click(); return 'clicked: ' + target.tagName; } return 'not found'; })()`; const clickRes = await send("Runtime.evaluate", { expression: clickExpr, returnByValue: true }); console.log("CLICK:", clickRes.result.value); // 3. 等待路由变化 await new Promise(r => setTimeout(r, 2500)); // 4. 检查新页面 const afterExpr = `JSON.stringify({ url: location.href, title: document.title, bodyText: document.body.innerText.slice(0, 400), overflowX: document.documentElement.scrollWidth > document.documentElement.clientWidth })`; const afterRes = await send("Runtime.evaluate", { expression: afterExpr, returnByValue: true }); console.log("AFTER_NAV:", afterRes.result.value); } catch (e) { console.error("Error:", e.message); } finally { ws.close(); process.exit(0); } };