import { newTarget, CDP } from '/tmp/cdp3.mjs'; import fs from 'fs'; const URLX = process.argv[2]; const TAG = process.argv[3] || 'x'; const wait = ms => new Promise(z => setTimeout(z, ms)); const tl = await (await fetch('http://127.0.0.1:9223/json/list')).json(); for (const t of tl.filter(x => x.type==='page' && x.url.includes(TAG))) await fetch(`http://127.0.0.1:9223/json/close/${t.id}`).catch(()=>{}); await wait(1000); const t = await newTarget('about:blank'); const c = await CDP.connect(t.webSocketDebuggerUrl); await c.send('Page.enable'); await c.send('Runtime.enable'); await c.send('Page.navigate',{url:URLX}); await wait(22000); console.log('URL:', await c.evalJS('location.href')); const loc = await c.evalJS(`(()=>{ const items=[...document.querySelectorAll('.ant-form-item')].filter(e=>e.offsetParent!==null); for(let i=0;ie.offsetParent!==null)[0]; if(!s) return 'no-select'; const r=s.getBoundingClientRect(); return JSON.stringify({i:-1, label:'(第一个)', x:Math.round(r.x+r.width/2), y:Math.round(r.y+r.height/2), cur:''});})()`); console.log('店铺选择器:', loc); const L = JSON.parse(String(loc)); if (L.x) { for (const type of ['mousePressed','mouseReleased']) await c.send('Input.dispatchMouseEvent',{type,x:L.x,y:L.y,button:'left',clickCount:1}); await wait(2500); const all = new Set(); for (let i=0;i<20;i++){ const got = await c.evalJS(`(()=>{const dd=[...document.querySelectorAll('.ant-select-dropdown')].filter(e=>e.offsetParent!==null); if(!dd.length)return'[]'; const d=dd[dd.length-1]; return JSON.stringify([...d.querySelectorAll('.ant-select-item-option')].map(e=>(e.innerText||'').trim()).filter(Boolean));})()`); const arr=JSON.parse(String(got)); const before=all.size; arr.forEach(x=>all.add(x)); await c.evalJS(`(()=>{const dd=[...document.querySelectorAll('.ant-select-dropdown')].filter(e=>e.offsetParent!==null); if(!dd.length)return 0; const d=dd[dd.length-1]; const rc=d.querySelector('.rc-virtual-list-holder')||d; rc.scrollTop=rc.scrollTop+800; return rc.scrollTop;})()`); await wait(700); if (all.size===before && i>2) break; } const list=[...all]; fs.writeFileSync('/tmp/shops_'+TAG+'.json', JSON.stringify(list,null,1)); console.log('店铺数:', list.length); list.sort((a,b)=>a.localeCompare(b)); list.forEach((n,i)=>console.log(' '+String(i+1).padStart(2)+'. '+n)); } process.exit(0);