'use strict'; const WS_URL = 'ws://127.0.0.1:18792/cdp'; const TOKEN = '0537d84a67f2e43b525964bb43d93f6dfae1ec1b50946455'; const TARGET_URL = 'https://diaonline.supermercadosdia.com.ar/medios-de-pago-y-promociones'; const WS = require('/usr/lib/node_modules/openclaw/node_modules/ws').WebSocket || require('/usr/lib/node_modules/openclaw/node_modules/ws'); const fs = require('fs'); const ws = new WS(WS_URL, { headers: { 'x-openclaw-relay-token': TOKEN } }); let _id=1, sessionId=null, started=false; const cbs=new Map(); function send(method,params,cb){const id=_id++;const m={id,method,params:params||{}};if(sessionId)m.sessionId=sessionId;if(cb)cbs.set(id,cb);ws.send(JSON.stringify(m));} ws.on('open',()=>{ send('Target.getTargets',{},(res)=>{ const t=(res.targetInfos||[]).filter(t=>t.type==='page')[0]; send('Target.attachToTarget',{targetId:t.targetId,flatten:true},(r2)=>{ sessionId=r2.sessionId; send('Page.enable'); send('Page.navigate',{url:TARGET_URL}); }); }); }); ws.on('message',raw=>{ const msg=JSON.parse(raw); if(msg.id&&cbs.has(msg.id)){const cb=cbs.get(msg.id);cbs.delete(msg.id);cb(msg.result||{});return;} if(!msg.method)return; if(msg.method==='Page.frameNavigated'){ const url=msg.params&&msg.params.frame&&msg.params.frame.url; if(url&&url.includes('medios-de-pago')&&!started){ started=true; console.log('Page navigated, waiting 8s for React...'); setTimeout(extractAll,8000); } } }); function extractAll(){ // 1. Click ALL Ver Legales buttons at once send('Runtime.evaluate',{expression:`(function(){ var btns=document.querySelectorAll('.diaio-custom-bank-promotions-0-x-bank-modal__button'); [].forEach.call(btns,function(b){try{b.click();}catch(e){}}); return btns.length+' buttons clicked'; })()`},(res)=>{ console.log('Clicked:', res&&res.result&&res.result.value); // 2. Wait 2s for React to render all modals, then extract everything setTimeout(()=>{ send('Runtime.evaluate',{ expression:`(function(){ var ITEM = 'diaio-custom-bank-promotions-0-x-list-by-days__item'; var items = document.querySelectorAll('.'+ITEM.split(' ')[0]); // Also try alternate class if(!items.length) items = document.querySelectorAll('[class*="list-by-days__item"]'); var promos = []; [].forEach.call(items, function(item){ var flags = [].map.call(item.querySelectorAll('[class*="flag"]'), function(el){return el.textContent.trim();}); var first = item.querySelector('[class*="first-text"]'); var second = item.querySelector('[class*="second-text"]'); var third = item.querySelector('[class*="third-text"]'); var img = item.querySelector('img'); var legal = item.querySelector('[class*="bank-modal__text"]'); var btnText = item.querySelector('[class*="bank-modal__button"]'); promos.push({ canal: flags.join('/'), descuento: first ? first.textContent.trim() : '', detalle: second ? second.textContent.trim() : '', vigencia: third ? third.textContent.trim() : '', imgSrc: img ? img.src : '', imgAlt: img ? img.alt : '', legal: legal ? legal.textContent.replace(/\\s+/g,' ').trim() : '' }); }); return JSON.stringify(promos); })()`,returnByValue:true},(res)=>{ const raw=res&&res.result&&res.result.value; if(!raw){console.log('No data returned, result:',JSON.stringify(res).slice(0,200));ws.close();return;} let promos; try{promos=JSON.parse(raw);}catch(e){console.log('parse err:',raw.slice(0,200));ws.close();return;} console.log('\n=== DIA PROMOS (',promos.length,') ===\n'); promos.forEach((p,i)=>{ console.log('['+i+'] '+p.canal+' | '+p.descuento+' | '+p.detalle.slice(0,60)+' | '+p.vigencia.slice(0,40)); if(p.imgAlt) console.log(' bank: '+p.imgAlt+' | img: '+p.imgSrc.split('/').pop()); if(p.legal) console.log(' LEGAL: '+p.legal.slice(0,300)); console.log(); }); // Save to file const out={generatedAt:new Date().toISOString(),promos}; fs.writeFileSync('/tmp/dia_promos_data.json',JSON.stringify(out,null,2)); console.log('Saved to /tmp/dia_promos_data.json'); ws.close(); }); },2500); }); } ws.on('error',e=>console.error('WS err:',e.message)); ws.on('close',()=>process.exit(0)); setTimeout(()=>{console.log('Timeout');ws.close();},60000);