'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 ws = new WS(WS_URL, { headers: { 'x-openclaw-relay-token': TOKEN } }); let _id = 1, sessionId = null; const callbacks = new Map(); const capturedLegals = []; function send(method, params, cb) { const id = _id++; const msg = { id, method: method, params: params || {} }; if (sessionId) msg.sessionId = sessionId; if (cb) callbacks.set(id, cb); ws.send(JSON.stringify(msg)); } function eval_(expr, cb) { send('Runtime.evaluate', { expression: expr, returnByValue: true, awaitPromise: false }, res => { cb(res && res.result && res.result.value); }); } ws.on('open', () => { send('Target.getTargets', {}, res => { const target = (res.targetInfos || []).filter(t => t.type === 'page')[0]; console.log('Tab:', target && target.url); send('Target.attachToTarget', { targetId: target.targetId, flatten: true }, r2 => { sessionId = r2.sessionId; send('Network.enable'); send('Page.enable'); send('Page.navigate', { url: TARGET_URL }); }); }); }); ws.on('message', raw => { const msg = JSON.parse(raw); if (msg.id && callbacks.has(msg.id)) { const cb = callbacks.get(msg.id); callbacks.delete(msg.id); cb(msg.result || {}); return; } if (msg.method === 'Page.loadEventFired') { console.log('Page loaded — waiting 5s...'); setTimeout(start, 5000); } }); function start() { // Extract all promo cards from DOM as JSON string eval_(`(function(){ const legalBtns = Array.from(document.querySelectorAll('button,a,span')).filter(function(el){ return /ver legal/i.test(el.textContent); }); var cards = legalBtns.map(function(btn, i) { var container = btn; for (var j = 0; j < 12; j++) { var p = container.parentElement; if (!p || p.tagName === 'MAIN' || p.tagName === 'BODY' || p.tagName === 'SECTION') break; var t = p.innerText || ''; if (t.length > 30 && t.length < 2000 && t.indexOf('Ver Legales') !== -1) { container = p; } else break; } // Extract img info var imgs = Array.from(container.querySelectorAll('img')).map(function(img){ return (img.alt || '') + ':' + img.src.replace(/.*\//,''); }); // Get all text tokens var text = (container.innerText || '').replace(/\\s+/g,' ').trim(); return i + '|||' + text + '|||' + imgs.join(';'); }); return JSON.stringify({ count: legalBtns.length, cards: cards }); })()`, val => { if (!val) { console.log('eval failed'); ws.close(); return; } let parsed; try { parsed = JSON.parse(val); } catch(e) { console.log('parse err:', val.slice(0,200)); ws.close(); return; } console.log('\n=== DIA PROMO CARDS:', parsed.count, '===\n'); (parsed.cards || []).forEach(c => { const parts = c.split('|||'); console.log('[' + parts[0] + ']', parts[1]); if (parts[2]) console.log(' imgs:', parts[2]); console.log(); }); // Now click each Ver Legales and get the modal text clickNext(0, parsed.count); }); } function clickNext(idx, total) { if (idx >= Math.min(total, 20)) { console.log('\n=== CAPTURED LEGALS ==='); capturedLegals.forEach((l,i) => console.log('[' + i + '] ' + l)); ws.close(); return; } eval_(`(function(){ var btns = Array.from(document.querySelectorAll('button,a,span')).filter(function(el){ return /ver legal/i.test(el.textContent); }); if (!btns[${idx}]) return 'no btn'; btns[${idx}].click(); return 'ok'; })()`, () => { // Wait for modal, then extract setTimeout(() => { eval_(`(function(){ // Try every possible modal selector var selectors = [ '[role="dialog"]','[class*="modal"]','[class*="Modal"]','[class*="legal"]', '[class*="Legal"]','[class*="drawer"]','[class*="Drawer"]','[class*="overlay"]', '[class*="popup"]','[class*="Popup"]','[class*="terms"]','[class*="Terms"]', '[class*="lightbox"]','[class*="bottom-sheet"]' ]; for (var i = 0; i < selectors.length; i++) { var el = document.querySelector(selectors[i]); if (el && el.offsetParent !== null) { var text = (el.innerText || '').replace(/\\s+/g,' ').trim(); if (text.length > 30) { // Close it var closeBtn = el.querySelector('[aria-label],[class*="close"],[class*="Close"]') || el.querySelector('button'); if (closeBtn) closeBtn.click(); return 'MODAL:' + selectors[i] + '::' + text.slice(0,600); } } } // Fallback: look for any element that appeared with legal-ish content var allVis = Array.from(document.querySelectorAll('p,div,article')) .filter(function(el){ if (!el.offsetParent) return false; var t = el.innerText || ''; return t.length > 100 && t.length < 3000 && /promovin|vigente|exclu|cuota|reintegro|tarjeta|banco/i.test(t); }); if (allVis.length) { var t = (allVis[0].innerText || '').replace(/\\s+/g,' ').trim(); return 'DOM:' + t.slice(0,400); } return 'none'; })()`, val => { console.log('[' + idx + '] modal:', val ? val.slice(0, 200) : 'null'); if (val && val !== 'none' && val.length > 20) { capturedLegals.push('[' + idx + '] ' + val); } // close any open modal via Escape key eval_('document.dispatchEvent(new KeyboardEvent("keydown",{key:"Escape",bubbles:true})); "esc"', () => { setTimeout(() => clickNext(idx + 1, total), 600); }); }); }, 1800); }); } ws.on('error', e => console.error('WS error:', e.message)); ws.on('close', () => process.exit(0)); setTimeout(() => { console.log('Timeout'); ws.close(); }, 120000);