'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]; console.log('Attaching to:',t&&t.url); 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('On promo page, waiting 8s...'); setTimeout(getHTML,8000); } } }); function getHTML(){ send('Runtime.evaluate',{expression:'document.documentElement.outerHTML',returnByValue:true},(res)=>{ const html = res&&res.result&&res.result.value; if(!html){console.log('no html, result:',JSON.stringify(res).slice(0,200));ws.close();return;} console.log('Got HTML, length:',html.length); fs.writeFileSync('/tmp/dia_promos.html',html,'utf8'); console.log('Saved to /tmp/dia_promos.html'); parsePromos(html); ws.close(); }); } function parsePromos(html){ // Extract visible text between promo markers // Find all "Ver Legales" button occurrences const legalRegex=/Ver Legales/g; let match; const positions=[]; while((match=legalRegex.exec(html))!==null) positions.push(match.index); console.log('Found',positions.length,'Ver Legales occurrences in HTML'); // For each, extract surrounding context (look backwards for promo content) positions.forEach((pos,i)=>{ const before=html.slice(Math.max(0,pos-2000),pos); // Extract text by stripping tags const text=before.replace(/<[^>]+>/g,' ').replace(/\s+/g,' ').trim().slice(-500); console.log('\n['+i+'] ...'+text+' | Ver Legales'); }); // Also look for any legal text in hidden/data attributes const dataRegex=/data-legal[^"]*="([^"]{20,})"/gi; while((match=dataRegex.exec(html))!==null){ console.log('data-legal attr:', match[1].slice(0,200)); } // JSON.parse embedded data const scriptRegex=/]*>([^<]*(?:legal|promo|banco|descuento)[^<]*)<\/script>/gi; let scriptCount=0; while((match=scriptRegex.exec(html))!==null&&scriptCount<5){ console.log('Script with legal/promo:', match[1].slice(0,400)); scriptCount++; } } ws.on('error',e=>console.error('err:',e.message)); ws.on('close',()=>process.exit(0)); setTimeout(()=>{ws.close();},60000);