Files

34 lines
2.3 KiB
JavaScript

import { newPage, listPages, wait } from '/home/admin9webs/shein建品/sh_lib.mjs';
import { CDP, SAN as S } from '/tmp/cdp3.mjs';
let t = (await listPages()).find(x=>/91miaoshou/i.test(x.url));
let c;
if (t) { c = await CDP.connect(t.webSocketDebuggerUrl); await c.send('Page.enable'); await c.send('Runtime.enable'); }
else {
console.log('无妙手标签,新开…');
c = await newPage('https://erp.91miaoshou.com/common_collect_box/items');
await wait(14000);
}
await c.send('Page.navigate',{url:'https://erp.91miaoshou.com/common_collect_box/items'});
await wait(13000);
const st = await c.evalJS(`JSON.stringify({href:location.href, login:!!document.querySelector('input[type=password]'), title:document.title})`);
console.log('页面:', S(String(st)).slice(0,200));
const pos = JSON.parse(String(await c.evalJS(`(()=>{
const bs=[...document.querySelectorAll('button')].filter(e=>e.offsetParent!==null&&(e.innerText||'').trim()==='导入产品');
if(!bs.length) return '[]'; const b=bs[0]; b.scrollIntoView({block:'center'});
const r=b.getBoundingClientRect(); return JSON.stringify([Math.round(r.x+r.width/2),Math.round(r.y+r.height/2)]);})()`)));
console.log('导入产品坐标:', JSON.stringify(pos));
if(pos.length){
await c.send('Input.dispatchMouseEvent',{type:'mouseMoved',x:pos[0],y:pos[1],buttons:0}); await wait(300);
for(const tp of ['mousePressed','mouseReleased']) await c.send('Input.dispatchMouseEvent',{type:tp,x:pos[0],y:pos[1],button:'left',clickCount:1,buttons:1});
await wait(6000);
}
const o = await c.evalJS(`(()=>{
const dlgs=[...document.querySelectorAll('.jx-dialog,.jx-drawer,[class*=dialog]')].filter(e=>e.offsetParent!==null)
.map(e=>({txt:(e.innerText||'').replace(/\\s+/g,' ').trim().slice(0,260), btns:[...e.querySelectorAll('button')].map(b=>(b.innerText||'').trim()).filter(Boolean).slice(0,6), files:[...e.querySelectorAll('input[type=file]')].length}));
const rows=[...document.querySelectorAll('tr')].filter(e=>e.offsetParent!==null).map(e=>(e.innerText||'').replace(/\\s+/g,' ').trim()).filter(Boolean).slice(0,4);
const body=(document.body.innerText||'').replace(/\\s+/g,' ');
return JSON.stringify({dlgs:dlgs.slice(0,2), rows, empty:/暂无数据/.test(body)});})()`);
console.log('结果:', S(String(o)).slice(0,1200));
try{ await c.shot('/tmp/ms_importprod.jpg'); console.log('shot ok'); }catch(e){}
process.exit(0);