97 lines
6.8 KiB
JavaScript
97 lines
6.8 KiB
JavaScript
import { newPage, closePage, wait, installHook, readHook } from '/home/admin9webs/shein建品/sh_lib.mjs';
|
|
import { fillRequiredSelects } from '/home/admin9webs/shein建品/sh_build.mjs';
|
|
import { SAN as S } from '/tmp/cdp3.mjs';
|
|
import { readFileSync } from 'fs';
|
|
const P = `document.querySelector('#app').__vue_app__.config.globalProperties.$pinia`;
|
|
const pid = process.argv[2] || '676236596821';
|
|
const it = JSON.parse(readFileSync('/home/admin9webs/tiktok建品/tk_input.json','utf8'))[pid];
|
|
const IMGS = (it.imgs||[]).slice(0,6);
|
|
|
|
const c = await newPage('https://www.dianxiaomi.com/web/joomProduct/add');
|
|
await wait(12000);
|
|
await installHook(c);
|
|
// 店
|
|
console.log('店:', String(await c.evalJS(`(()=>{const it=[...document.querySelectorAll('.ant-form-item')].filter(e=>e.offsetParent!==null).find(e=>/店铺名称/.test((e.querySelector('label')||{}).innerText||''));
|
|
const w=[...it.querySelectorAll('.ant-checkbox-wrapper')].find(x=>{const t=(x.innerText||'').trim(); return t&&!/^全部/.test(t);});
|
|
for(const ev of ['mousedown','mouseup','click']) w.dispatchEvent(new MouseEvent(ev,{bubbles:true,cancelable:true,view:window}));
|
|
const i=w.querySelector('input'); if(i&&!i.checked) i.click(); return (w.innerText||'').trim();})()`)));
|
|
await wait(6000);
|
|
// 类目
|
|
const pos = JSON.parse(String(await c.evalJS(`(()=>{const f=[...document.querySelectorAll('.ant-form-item')].filter(e=>e.offsetParent!==null).find(e=>/产品分类/.test((e.querySelector('label')||{}).innerText||''));
|
|
const s=f.querySelector('.ant-select-selector'); s.scrollIntoView({block:'center'}); const r=s.getBoundingClientRect();
|
|
return JSON.stringify([Math.round(r.x+r.width/2),Math.round(r.y+r.height/2)]);})()`)));
|
|
await c.send('Input.dispatchMouseEvent',{type:'mouseMoved',x:pos[0],y:pos[1],buttons:0}); await wait(250);
|
|
for(const t of ['mousePressed','mouseReleased']) await c.send('Input.dispatchMouseEvent',{type:t,x:pos[0],y:pos[1],button:'left',clickCount:1,buttons:1});
|
|
await wait(1500);
|
|
await c.send('Input.insertText',{text: it.cat_kw || '发夹'}).catch(()=>{});
|
|
await wait(2500);
|
|
console.log('类目:', String(await c.evalJS(`(()=>{const dd=[...document.querySelectorAll('.ant-select-dropdown')].filter(e=>e.offsetParent!==null).pop();
|
|
if(!dd) return 'no-dd'; const op=[...dd.querySelectorAll('.ant-select-item-option')][0]; if(!op) return 'no-op';
|
|
const r=op.getBoundingClientRect();
|
|
for(const ev of ['mousedown','mouseup','click']) op.dispatchEvent(new MouseEvent(ev,{bubbles:true,cancelable:true,view:window,clientX:r.x+r.width/2,clientY:r.y+r.height/2}));
|
|
return (op.innerText||'').trim().slice(0,30);})()`)));
|
|
await wait(7000);
|
|
// store 填
|
|
const fill = await c.evalJS(`(()=>{const p=${P}; const imgs=${JSON.stringify(IMGS)};
|
|
const mk=(u,i)=>({url:u,imgUrl:u,name:'img'+(i+1)+'.jpg',width:800,height:800});
|
|
const b=p._s.get('smtChoiceAddBasicStore').$state.formState;
|
|
b.name=${JSON.stringify(it.title)}; b.parentSku='JM-'+${JSON.stringify(pid)}; b.description=(${JSON.stringify((it.bullets||[]).join('. '))}||b.name); b.tags=(${JSON.stringify(it.keywords||'')}.split(', ').slice(0,10));
|
|
const pr=p._s.get('joomAddPriceStore').$state.formState;
|
|
pr.price=String(${JSON.stringify(String(it.price_cny||'19.90'))}); pr.inventory='999'; pr.msrp=''; pr.shipping='0'; pr.declaredValue=String(${JSON.stringify(String(it.cost||'10'))});
|
|
const im=p._s.get('joomAddImageStore').$state.formState;
|
|
im.imageList=imgs.map(mk);
|
|
if(Array.isArray(im.detailImageList)&&im.detailImageList[0]) im.detailImageList[0].images=imgs.map(mk);
|
|
return JSON.stringify({name:b.name.slice(0,20), price:pr.price, imgs:im.imageList.length, cat:b.categoryId});})()`);
|
|
console.log('填充:', S(String(fill)).slice(0,400));
|
|
await wait(4000);
|
|
// 列出所有带 select 的表单项,找「详情图语言」
|
|
const pairs = await c.evalJS(`(()=>{ try {
|
|
const out = [];
|
|
for (const e of document.querySelectorAll('.ant-form-item')) {
|
|
if (e.offsetParent === null) continue;
|
|
const sel = e.querySelector('.ant-select');
|
|
if (!sel) continue;
|
|
const l = e.querySelector('label');
|
|
out.push(((l ? l.innerText : '') || '?').replace(/\\s+/g, ' ').trim().slice(0, 26) + ' => ' + (sel.innerText || '').replace(/\\s+/g, ' ').trim().slice(0, 24));
|
|
}
|
|
return JSON.stringify(out.slice(0, 20));
|
|
} catch (e) { return 'ERR ' + e.message; } })()`);
|
|
console.log('带下拉的表项:', S(String(pairs)).slice(0, 1200));
|
|
|
|
// 点「详情图语言」那个 select,选第一项
|
|
const lang = JSON.parse(String(await c.evalJS(`(()=>{ try {
|
|
let target = null;
|
|
for (const e of document.querySelectorAll('.ant-form-item')) {
|
|
if (e.offsetParent === null) continue;
|
|
const l = e.querySelector('label'); const t = l ? (l.innerText || '') : '';
|
|
if (t.indexOf('详情图') >= 0 && e.querySelector('.ant-select')) { target = e; break; }
|
|
}
|
|
if (!target) return '[]';
|
|
const s = target.querySelector('.ant-select-selector'); s.scrollIntoView({ block: 'center' });
|
|
const r = s.getBoundingClientRect();
|
|
return JSON.stringify([Math.round(r.x + r.width / 2), Math.round(r.y + r.height / 2)]);
|
|
} catch (e) { return '[]'; } })()`)));
|
|
console.log('语言控件坐标:', JSON.stringify(lang));
|
|
if (lang.length) {
|
|
await c.send('Input.dispatchMouseEvent', { type: 'mouseMoved', x: lang[0], y: lang[1], buttons: 0 }); await wait(300);
|
|
for (const t of ['mousePressed', 'mouseReleased']) await c.send('Input.dispatchMouseEvent', { type: t, x: lang[0], y: lang[1], button: 'left', clickCount: 1, buttons: 1 });
|
|
await wait(2000);
|
|
console.log('选了:', String(await c.evalJS(`(()=>{const dd=[...document.querySelectorAll('.ant-select-dropdown')].filter(e=>e.offsetParent!==null).pop();
|
|
if(!dd) return 'no-dd'; const op=[...dd.querySelectorAll('.ant-select-item-option')][0]; if(!op) return 'no-op';
|
|
const r=op.getBoundingClientRect();
|
|
for(const ev of ['mousedown','mouseup','click']) op.dispatchEvent(new MouseEvent(ev,{bubbles:true,cancelable:true,view:window,clientX:r.x+r.width/2,clientY:r.y+r.height/2}));
|
|
return (op.innerText||'').trim().slice(0,20);})()`)));
|
|
await wait(3000);
|
|
}
|
|
// 保存
|
|
await c.evalJS(`window.scrollTo(0,0)`); await wait(600);
|
|
await c.evalJS(`(()=>{const xs=[...document.querySelectorAll('button')].filter(e=>e.offsetParent!==null);
|
|
const b=xs.find(e=>/^\\s*保存\\s*$/.test((e.innerText||'').trim()))||xs.find(e=>/保存/.test(e.innerText||''));
|
|
if(b) for(const ev of ['mousedown','mouseup','click']) b.dispatchEvent(new MouseEvent(ev,{bubbles:true,cancelable:true,view:window})); return 1;})()`);
|
|
await wait(13000);
|
|
console.log('hook:', String(await readHook(c)).slice(-260));
|
|
const res = await c.evalJS(`JSON.stringify({mods:[...document.querySelectorAll('.ant-modal')].filter(e=>e.offsetParent!==null).map(e=>(e.innerText||'').replace(/\\s+/g,' ').trim().slice(0,120))})`);
|
|
console.log('结果:', S(String(res)).slice(0,400));
|
|
try { await c.shot('/tmp/joom_lang.jpg'); console.log('shot ok'); } catch (e) {}
|
|
await closePage(c); process.exit(0);
|