import WebSocket from 'ws';
import fs from 'fs';
import path from 'path';
const WS_URL = 'ws://127.0.0.1:18800/devtools/page/4D8F9BA8FB6EFF0ED9A709D5FF07A8E2';
const OUT_DIR = '/Users/mariscal/.openclaw/workspace/style-library/screenshots-mobile';
const done = new Set(fs.readdirSync(OUT_DIR).map(f => f.replace('-mobile.jpg','')));
let msgId = 1;
function send(ws, method, params = {}) {
return new Promise((resolve, reject) => {
const id = msgId++;
const timer = setTimeout(() => { ws.off('message', handler); resolve(null); }, 30000);
function handler(data) {
const msg = JSON.parse(data.toString());
if (msg.id === id) {
clearTimeout(timer);
ws.off('message', handler);
if (msg.error) reject(new Error(JSON.stringify(msg.error)));
else resolve(msg.result);
}
}
ws.on('message', handler);
ws.send(JSON.stringify({ id, method, params }));
});
}
async function run() {
const ws = new WebSocket(WS_URL);
await new Promise((res, rej) => { ws.on('open', res); ws.on('error', rej); });
await send(ws, 'Page.enable');
for (let i = 1; i <= 100; i++) {
const code = String(i).padStart(2, '0');
if (done.has(`S${code}`)) { console.log(`S${code} skip`); continue; }
const url = `https://styleprompt.zeabur.app/styles/pro/S${code}`;
process.stdout.write(`S${code}...`);
await send(ws, 'Emulation.setDeviceMetricsOverride', {
width: 375, height: 812, deviceScaleFactor: 2, mobile: true
});
await send(ws, 'Page.navigate', { url });
await new Promise(r => setTimeout(r, 4000));
await send(ws, 'Page.stopLoading');
await new Promise(r => setTimeout(r, 1000));
const layout = await send(ws, 'Page.getLayoutMetrics');
if (!layout) { console.log(' ✗ timeout'); continue; }
// Cap height at 4000 to avoid massive screenshots that hang
const h = Math.min(4000, Math.max(812, Math.ceil(layout.contentSize.height)));
await send(ws, 'Emulation.setDeviceMetricsOverride', {
width: 375, height: h, deviceScaleFactor: 2, mobile: true
});
await new Promise(r => setTimeout(r, 500));
const screenshot = await send(ws, 'Page.captureScreenshot', {
format: 'jpeg', quality: 80,
clip: { x: 0, y: 0, width: 375, height: h, scale: 1 }
});
if (screenshot) {
fs.writeFileSync(path.join(OUT_DIR, `S${code}-mobile.jpg`), Buffer.from(screenshot.data, 'base64'));
console.log(` ✓ (h=${h})`);
} else {
console.log(` ✗`);
}
}
ws.close();
console.log('Done!');
}
run().catch(e => { console.error(e); process.exit(1); });