/* vq.js — anonymous-session harness for venice.ai/chat Sections: A capture | B queue | C keepalive | D guard | E helpers Commands (in console): vq.add("p1","p2") vq.start() vq.stop() vq.status() vq.dump() vq.curl(0) vq.clear() vq.off() */ (() => { const LS_Q = 'vq:queue', LS_L = 'vq:log', LS_C = 'vq:cap'; const get = (k, d) => { try { return JSON.parse(localStorage.getItem(k)) ?? d; } catch { return d; } }; const put = (k, v) => localStorage.setItem(k, JSON.stringify(v)); const sleep = ms => new Promise(r => setTimeout(r, ms)); const log = (...a) => console.log('%c[vq]', 'color:#8ab4f8', ...a); let running = false, off = false, timer = null; let caps = get(LS_C, []); /* ---------- A. capture session traffic ---------- */ const rawFetch = window.fetch; window.fetch = async function (input, init = {}) { const url = typeof input === 'string' ? input : input?.url || ''; const h = {}; try { new Headers(init.headers || input?.headers || {}).forEach((v, k) => { h[k] = v; }); } catch {} const body = typeof init.body === 'string' ? init.body : null; if (url.includes('/api/') && (body || h.authorization || h['x-venice'])) { caps.push({ t: Date.now(), url, headers: h, body }); if (caps.length > 40) caps = caps.slice(-40); put(LS_C, caps); log('captured', url); } return rawFetch.apply(this, arguments); }; /* ---------- B. queue engine ---------- */ const box = () => document.querySelector('textarea, [contenteditable="true"]'); const sendBtn = () => document.querySelector('button[type="submit"]:not([disabled])') || [...document.querySelectorAll('button')].find(b => !b.disabled && /send|submit/i.test(b.getAttribute('aria-label') || b.textContent || '')); const stopBtn = () => [...document.querySelectorAll('button')].find(b => /stop/i.test(b.getAttribute('aria-label') || b.textContent || '')); const limitHit = () => { const t = document.body.innerText.toLowerCase(); return /(daily|message|prompt).{0,20}limit|limit.{0,20}reached|out of (messages|prompts)|upgrade to (pro|continue)/i.test(t); }; async function submit(text) { const b = box(); if (!b) return false; b.focus(); if (b.tagName === 'TEXTAREA') { const setter = Object.getOwnPropertyDescriptor(HTMLTextAreaElement.prototype, 'value').set; setter.call(b, text); b.dispatchEvent(new Event('input', { bubbles: true })); } else { b.textContent = text; b.dispatchEvent(new InputEvent('input', { bubbles: true, data: text })); } await sleep(200); const s = sendBtn(); if (s) { s.click(); return true; } b.dispatchEvent(new KeyboardEvent('keydown', { key: 'Enter', bubbles: true })); return true; } async function waitForAnswer() { let last = document.body.innerText.length, stable = 0; while (stable < 4) { await sleep(900); if (limitHit() && !stopBtn()) return 'limit'; const n = document.body.innerText.length; stable = n === last ? stable + 1 : 0; last = n; } return 'ok'; } function harvest() { const nodes = [...document.querySelectorAll('[data-message-role="assistant"], .prose')]; const text = nodes.length ? nodes[nodes.length - 1].innerText.trim() : ''; if (!text) return; const L = get(LS_L, []); L.push({ t: Date.now(), text }); put(LS_L, L); } async function step() { const q = get(LS_Q, []); if (!q.length || off) { running = false; return log('queue empty'); } const [next, ...rest] = q; put(LS_Q, rest); log('sending:', next.slice(0, 60)); if (!(await submit(next))) { running = false; return log('no composer found'); } const res = await waitForAnswer(); harvest(); if (res === 'limit') { off = true; log('LIMIT REACHED — queue parked, restart with vq.start() after reset'); running = false; return; } // auto-continue if the reply looks cut off if (/\b(and|the|with|of|to|because|,\s*)$/i.test((get(LS_L, []).at(-1)?.text || '').slice(-40))) { put(LS_Q, ['continue exactly where you stopped, no preamble', ...get(LS_Q, [])]); } await sleep(2500); // breathing room if (running) step(); } /* ---------- C. keepalive ---------- */ timer = setInterval(() => { if (document.visibilityState === 'visible' && !running) { fetch(location.origin + '/', { cache: 'no-store', credentials: 'include' }).catch(() => {}); } }, 240000); /* ---------- D/E. public api ---------- */ window.vq = { add: (...p) => { put(LS_Q, [...get(LS_Q, []), ...p]); log('queued', p.length, '— total', get(LS_Q, []).length); }, start: () => { if (running) return; off = false; running = true; step(); }, stop: () => { running = false; log('stopped'); }, status: () => console.table({ running, limitHit: limitHit(), queued: get(LS_Q, []).length, captured: caps.length, logged: get(LS_L, []).length }), dump: () => { const L = get(LS_L, []); L.forEach((e, i) => console.log(`--- ${i} ---\n${e.text}`)); return L; }, curl: (i = caps.length - 1) => { const c = caps[i]; if (!c) return log('no capture', i); const hdrs = Object.entries(c.headers).filter(([k]) => /auth|venice|x-|content-type/i.test(k)) .map(([k, v]) => `-H '${k}: ${v}'`).join(' \\\n '); const cmd = `curl '${c.url}' \\\n ${hdrs} \\\n --data-raw '${(c.body || '').replace(/'/g, "'\\''")}'`; console.log(cmd); return cmd; }, clear: () => { put(LS_Q, []); put(LS_L, []); log('cleared'); }, off: () => { clearInterval(timer); window.fetch = rawFetch; log('harness removed'); }, }; log('ready — vq.add("…"), vq.start(). Type vq.status() for state.'); })();