- TikTok/Shopee/速卖通/Walmart 各 46-47 条已在待发布 - Ozon 47 条已建、改价 40/47 - SHEIN 配方跑通第一条;Temu 差「预览图」一步 - 核心产出:MYJ.message.error 钩子诊断法(解开了三个平台的保存静默拦截) - 含下一轮提示词 NEXT-ROUND-PROMPT.md
173 lines
12 KiB
JavaScript
173 lines
12 KiB
JavaScript
import { newTarget, CDP, SAN } from '/tmp/cdp3.mjs';
|
|
import fs from 'fs';
|
|
|
|
/* ── Shopee (Ljbgkj, shopId 6142973) 单件建品配方 ──
|
|
node spbatch.mjs <oid> [catKeyword]
|
|
选店铺 → 分类弹窗 → 单件 → 标题/描述 → 价格(CNY)/库存 → 重量/尺寸
|
|
→ 图片 → 来源URL → 保存 → 回读 */
|
|
|
|
const OID = process.argv[2];
|
|
const CAT_KW = process.argv[3] || '';
|
|
if (!OID) { console.log('用法: node spbatch.mjs <oid> [catKeyword]'); process.exit(1); }
|
|
const IN = JSON.parse(fs.readFileSync('/tmp/tk_input.json', 'utf8'))[OID];
|
|
if (!IN) { console.log('无输入数据', OID); process.exit(1); }
|
|
|
|
const SHOP = 'Ljbgkj';
|
|
const IS_KID = /baby|kid|infant|toddler|children|newborn/i.test(IN.title);
|
|
const wait = ms => new Promise(z => setTimeout(z, ms));
|
|
const log = (...a) => console.log(' ', ...a);
|
|
const DIMS = (() => { const W = Math.max(100, Math.round(IN.weight * 1.3));
|
|
return { L: Math.max(60, Math.min(400, W)), Wi: Math.max(40, Math.min(300, Math.round(W * 0.6))), H: Math.max(10, Math.min(200, Math.round(W * 0.2))) }; })();
|
|
|
|
const tl = await (await fetch('http://127.0.0.1:9223/json/list')).json();
|
|
let k = 0;
|
|
for (const t of tl.filter(x => x.type === 'page' && /shopee/i.test(x.url))) { await fetch(`http://127.0.0.1:9223/json/close/${t.id}`).catch(() => {}); k++; }
|
|
if (k) { log(`清理 shopee 页 ${k} 个`); await wait(1200); }
|
|
|
|
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: 'https://www.dianxiaomi.com/web/shopeeGlobal/add' });
|
|
await wait(22000);
|
|
|
|
// 1. 店铺
|
|
async function pickShop() {
|
|
for (let a = 0; a < 3; a++) {
|
|
const pos = JSON.parse(String(await c.evalJS(`(()=>{const it=[...document.querySelectorAll('.ant-form-item')].filter(e=>e.offsetParent!==null)
|
|
.find(x=>{const l=x.querySelector('label'); return l&&/店铺|商家账号/.test(l.innerText||'');});
|
|
const s=it?it.querySelector('.ant-select-selector'):[...document.querySelectorAll('.ant-select-selector')].filter(e=>e.offsetParent!==null)[0];
|
|
if(!s)return'[]'; 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)]);})()`)));
|
|
if (!pos.length) return 'no-select';
|
|
for (const type of ['mousePressed', 'mouseReleased']) await c.send('Input.dispatchMouseEvent', { type, x: pos[0], y: pos[1], button: 'left', clickCount: 1 });
|
|
await wait(2200);
|
|
const r = await c.evalJS(`(()=>{const dd=[...document.querySelectorAll('.ant-select-dropdown')].filter(e=>e.offsetParent!==null).pop();
|
|
if(!dd)return'no-dd'; const hit=[...dd.querySelectorAll('.ant-select-item-option')].find(o=>new RegExp('^\\\\s*${SHOP}\\\\s*$','i').test(o.innerText||''));
|
|
if(!hit)return'no-hit'; for(const ev of ['mousedown','mouseup','click']) hit.dispatchEvent(new MouseEvent(ev,{bubbles:true,cancelable:true,view:window})); return 'picked';})()`);
|
|
await wait(7000);
|
|
const cur = String(await c.evalJS(`(()=>{const it=[...document.querySelectorAll('.ant-form-item')].filter(e=>e.offsetParent!==null)
|
|
.find(x=>{const l=x.querySelector('label'); return l&&/店铺|商家账号/.test(l.innerText||'');});
|
|
const s=it&&it.querySelector('.ant-select-selection-item'); return s?s.innerText.trim():'';})()`));
|
|
if (new RegExp(SHOP, 'i').test(cur)) return 'ok:' + cur;
|
|
log(' 店铺重试', a + 1, r, JSON.stringify(cur));
|
|
}
|
|
return 'fail';
|
|
}
|
|
|
|
// 2. 分类(同 TikTok 结构)
|
|
async function pickCategory(kw) {
|
|
await c.evalJS(`(()=>{document.querySelectorAll('.ant-select-dropdown').forEach(d=>{if(d.offsetParent!==null)d.style.display='none';}); document.body.click(); return 1;})()`);
|
|
await wait(900);
|
|
const pos = JSON.parse(String(await c.evalJS(`(()=>{const it=[...document.querySelectorAll('.ant-form-item')].filter(e=>e.offsetParent!==null)
|
|
.find(x=>{const l=x.querySelector('label'); return l&&/产品分类/.test(l.innerText||'');});
|
|
if(!it)return'[]'; const b=[...it.querySelectorAll('button,a,span')].filter(e=>e.offsetParent!==null&&/^\\s*选择分类\\s*$/.test((e.innerText||'').trim()));
|
|
if(!b.length)return'[]'; const el=b[b.length-1]; el.scrollIntoView({block:'center'}); const r=el.getBoundingClientRect();
|
|
return JSON.stringify([Math.round(r.x+r.width/2),Math.round(r.y+r.height/2)]);})()`)));
|
|
if (!pos.length) return 'no-btn';
|
|
for (const type of ['mousePressed', 'mouseReleased']) await c.send('Input.dispatchMouseEvent', { type, x: pos[0], y: pos[1], button: 'left', clickCount: 1 });
|
|
await wait(3500);
|
|
const r = await c.evalJS(`(()=>{const m=[...document.querySelectorAll('.ant-modal')].filter(e=>e.offsetParent!==null).pop();
|
|
if(!m)return'no-modal'; const inp=m.querySelector('input[placeholder="搜索分类名称"]'); if(!inp)return'no-input';
|
|
inp.focus(); Object.getOwnPropertyDescriptor(window.HTMLInputElement.prototype,'value').set.call(inp,${JSON.stringify(kw)});
|
|
inp.dispatchEvent(new Event('input',{bubbles:true})); inp.dispatchEvent(new Event('change',{bubbles:true}));
|
|
const b=[...m.querySelectorAll('button')].find(x=>/^\\s*搜索\\s*$/.test(x.innerText||''));
|
|
if(b) for(const ev of ['mousedown','mouseup','click']) b.dispatchEvent(new MouseEvent(ev,{bubbles:true,cancelable:true,view:window}));
|
|
return 'searched';})()`);
|
|
if (r !== 'searched') return r;
|
|
await wait(4000);
|
|
const pick = await c.evalJS(`(()=>{const m=[...document.querySelectorAll('.ant-modal')].filter(e=>e.offsetParent!==null).pop();
|
|
if(!m)return'no-modal'; const its=[...m.querySelectorAll('.search-result-item')]; if(!its.length)return'no-result';
|
|
const BAD=/二手|Barang Terpakai|Second-?hand|Used|Pre-?owned|Refurb|Sexual|Sex Toy|情趣|Adult Toy|Dewasa/i;
|
|
const KID=/Bayi|Baby|Kids|Kid|Infant|Toddler|Children|Kanak/i;
|
|
const ISK=${IS_KID};
|
|
const KW=${JSON.stringify(kw)}.toLowerCase();
|
|
const texts=its.map(e=>(e.innerText||'').replace(/\s+/g,' ').trim());
|
|
const lastSeg=t=>{const a=t.split('/'); return (a[a.length-1]||'').replace(/\(.*?\)/g,'').trim().toLowerCase();};
|
|
let idx=texts.findIndex(t=>!BAD.test(t) && lastSeg(t).includes(KW));
|
|
if(idx<0) idx=texts.findIndex(t=>!BAD.test(t) && t.toLowerCase().includes(KW) && (ISK || !KID.test(t)));
|
|
if(idx<0) idx=texts.findIndex(t=>!BAD.test(t) && (ISK || !KID.test(t)));
|
|
if(idx<0) idx=texts.findIndex(t=>!BAD.test(t));
|
|
if(idx<0) idx=0;
|
|
const hit=its[idx];
|
|
for(const ev of ['mousedown','mouseup','click']) hit.dispatchEvent(new MouseEvent(ev,{bubbles:true,cancelable:true,view:window}));
|
|
return 'picked['+idx+'/'+texts.length+']:'+texts[idx];})()`);
|
|
await wait(4500);
|
|
return pick;
|
|
}
|
|
|
|
async function pickCategoryAny(kws) {
|
|
const tried = [];
|
|
for (const kw of kws) {
|
|
const r = String(await pickCategory(kw));
|
|
tried.push(kw + '\u2192' + r.slice(0, 46));
|
|
if (!/no-result|no-input|no-modal|no-btn/.test(r)) return { r, tried };
|
|
await c.evalJS(`(()=>{const m=[...document.querySelectorAll('.ant-modal')].filter(e=>e.offsetParent!==null).pop();
|
|
if(m){const x=m.querySelector('.ant-modal-close'); if(x&&x.offsetParent!==null)x.click();} return 1;})()`);
|
|
await wait(1500);
|
|
}
|
|
return { r: 'ALL-FAILED', tried };
|
|
}
|
|
|
|
log('=== Shopee 建品 ===', OID, IN.title.slice(0, 46));
|
|
log('1) 店铺:', await pickShop());
|
|
|
|
const kw = CAT_KW || IN.sp_kw || 'Accessories';
|
|
const catRes = await pickCategoryAny([kw].concat(IN.sp_fb || []));
|
|
log('2) 分类:', SAN(String(catRes.r)).slice(0, 120), '| 尝试:', catRes.tried.join(' / '));
|
|
if (/ALL-FAILED|no-result|no-input|no-btn/.test(String(catRes.r))) { log('\u274c 分类未选中,跳过保存'); process.exit(2); }
|
|
|
|
// 3. 单件模式 + 文案
|
|
const wr = await c.evalJS(`(()=>{const p=document.querySelector('#app').__vue_app__.config.globalProperties.$pinia;
|
|
const ps=p._s.get('shopeeGlobalProductStore').$state.formState;
|
|
ps.type='single';
|
|
ps.title=${JSON.stringify(IN.title)};
|
|
const txt=${JSON.stringify(IN.bullets.map(b => '• ' + b).join('\n') + '\n\nKeywords: ' + (IN.keywords || ''))};
|
|
ps.descriptionInfo={descriptionText:txt, descriptionHtml:'<p>'+${JSON.stringify(IN.bullets.join('<br>'))}+'</p>'};
|
|
return 'ok:'+ps.title.length;})()`);
|
|
log('3) 文案:', wr);
|
|
await wait(2000);
|
|
|
|
// 4. 价格 / 库存 / 重量 / 尺寸
|
|
const setItem = (rx, sel, val) => c.evalJS(`(()=>{
|
|
const it=[...document.querySelectorAll('.ant-form-item')].filter(e=>e.offsetParent!==null).find(x=>{const l=x.querySelector('label'); return l&&new RegExp(${JSON.stringify(rx)}).test(l.innerText||'');});
|
|
const el=it?it.querySelector(${JSON.stringify(sel)}):null; if(!el)return'nf';
|
|
const proto=el.tagName==='TEXTAREA'?window.HTMLTextAreaElement.prototype:window.HTMLInputElement.prototype;
|
|
Object.getOwnPropertyDescriptor(proto,'value').set.call(el, ${JSON.stringify(String(val))});
|
|
el.dispatchEvent(new Event('input',{bubbles:true})); el.dispatchEvent(new Event('change',{bubbles:true})); el.dispatchEvent(new Event('blur',{bubbles:true})); return 'ok';})()`);
|
|
log('4) 价格(CNY):', await setItem('价格', 'input:not([type=hidden])', IN.price_cny));
|
|
log(' 库存:', await setItem('库存', 'input:not([type=hidden])', '999'));
|
|
log(' 重量:', await setItem('重量', 'input:not([type=hidden])', IN.weight));
|
|
const dim = await c.evalJS(`(()=>{const it=[...document.querySelectorAll('.ant-form-item')].filter(e=>e.offsetParent!==null).find(x=>{const l=x.querySelector('label'); return l&&/尺寸规格/.test(l.innerText||'');});
|
|
if(!it)return'nf'; const ins=[...it.querySelectorAll('input')];
|
|
const setV=(el,v)=>{ Object.getOwnPropertyDescriptor(window.HTMLInputElement.prototype,'value').set.call(el,String(v));
|
|
el.dispatchEvent(new Event('input',{bubbles:true})); el.dispatchEvent(new Event('change',{bubbles:true})); el.dispatchEvent(new Event('blur',{bubbles:true})); };
|
|
const L=ins.find(i=>i.placeholder==='长')||ins[0], W2=ins.find(i=>i.placeholder==='宽')||ins[1], H=ins.find(i=>i.placeholder==='高')||ins[2];
|
|
if(L)setV(L,${DIMS.L}); if(W2)setV(W2,${DIMS.Wi}); if(H)setV(H,${DIMS.H}); return 'ok:'+ins.length;})()`);
|
|
log('5) 尺寸:', dim, JSON.stringify(DIMS));
|
|
|
|
// 5. 图片
|
|
const ir = await c.evalJS(`(()=>{const p=document.querySelector('#app').__vue_app__.config.globalProperties.$pinia;
|
|
const st=p._s.get('shopeeGlobalImageInfoStore').$state;
|
|
const urls=${JSON.stringify(IN.imgs)};
|
|
st.imageInfo.mainImages=urls.map((u,i)=>({url:u,imgUrl:u,name:'img'+(i+1)+'.jpg',width:800,height:800,checked:true}));
|
|
return 'imgs:'+st.imageInfo.mainImages.length;})()`);
|
|
log('6) 图片:', ir, '(共', IN.imgs.length, ')');
|
|
await wait(2500);
|
|
|
|
log('7) 来源URL:', await setItem('来源URL', 'input:not([type=hidden])', 'https://detail.1688.com/offer/' + OID + '.html'));
|
|
await wait(1200);
|
|
|
|
const st = await c.evalJS(`(()=>{const p=document.querySelector('#app').__vue_app__.config.globalProperties.$pinia;
|
|
const b=p._s.get('shopeeGlobalBasicStore').$state;
|
|
return JSON.stringify({shopId:b.formState.shopId, cur:b.dataState.currency});})()`);
|
|
log('8) 保存前:', st);
|
|
const sv = await c.evalJS(`(()=>{const b=[...document.querySelectorAll('button,a,span')].filter(e=>e.offsetParent!==null&&/^\\s*保存\\s*$/.test(e.innerText||''));
|
|
if(!b.length)return'nf'; for(const ev of ['mousedown','mouseup','click']) b[b.length-1].dispatchEvent(new MouseEvent(ev,{bubbles:true,cancelable:true,view:window})); return 'clicked';})()`);
|
|
log('9) 保存:', sv);
|
|
await wait(9000);
|
|
const msg = await c.evalJS(`(()=>{const ms=[...document.querySelectorAll('.ant-message-notice,.ant-notification-notice,.ant-modal')].filter(e=>e.offsetParent!==null)
|
|
.map(e=>(e.innerText||'').replace(/\\s+/g,' ').trim().slice(0,170)).filter(Boolean); return JSON.stringify({url:location.href, msgs:ms.slice(0,4)});})()`);
|
|
log('10) 结果:', SAN(String(msg)).slice(0, 400));
|
|
await c.shot('/tmp/sp_pilot.jpeg').catch(() => {});
|
|
process.exit(0);
|