| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
import { spawn } from 'node:child_process'; |
| 43 |
import { mkdtemp, mkdir, writeFile, rm } from 'node:fs/promises'; |
| 44 |
import { tmpdir } from 'node:os'; |
| 45 |
import path from 'node:path'; |
| 46 |
import process from 'node:process'; |
| 47 |
|
| 48 |
const BASE = (process.env.BASE ?? 'https://testnot.work').replace(/\/+$/, ''); |
| 49 |
const CHROME = process.env.CHROME ?? `${process.env.HOME}/.local/bin/chrome-for-testing`; |
| 50 |
const OUT_DIR = process.env.OUT_DIR ?? path.join(import.meta.dirname, '..', 'static', 'images', 'shots'); |
| 51 |
const SESSION_COOKIE = process.env.CAPTURE_SESSION_COOKIE ?? ''; |
| 52 |
const SESSION_COOKIE_NAME = process.env.CAPTURE_SESSION_COOKIE_NAME ?? 'id'; |
| 53 |
const BUYER_PASSWORD = process.env.CAPTURE_BUYER_PASSWORD ?? ''; |
| 54 |
|
| 55 |
const BUYER_LOGIN = process.env.CAPTURE_BUYER_LOGIN ?? 'demo_collector'; |
| 56 |
const CAN_AUTH = Boolean(SESSION_COOKIE || BUYER_PASSWORD); |
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
const VIEWPORT = { width: 1440, height: 1600 }; |
| 61 |
const SCALE = 2; |
| 62 |
const ASPECT = Number(process.env.CAPTURE_ASPECT ?? 3 / 2); |
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
const CROP_WIDTH = Number(process.env.CAPTURE_WIDTH ?? 1280); |
| 69 |
const SETTLE_MS = 1200; |
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
const FORMAT = process.env.CAPTURE_FORMAT ?? 'webp'; |
| 77 |
const QUALITY = Number(process.env.CAPTURE_QUALITY ?? 90); |
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
const DEFAULT_SELECTOR = '.container'; |
| 93 |
|
| 94 |
|
| 95 |
* The three frames, in carousel order. Paths are resolved against BASE. |
| 96 |
* `alt` mirrors what landing.rs claims the frame shows; keep them in step. |
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
const STOREFRONT = process.env.CAPTURE_STOREFRONT ?? '/p/deskriver-suite'; |
| 107 |
|
| 108 |
const FRAMES = [ |
| 109 |
{ |
| 110 |
name: 'storefront', |
| 111 |
path: STOREFRONT, |
| 112 |
alt: "A creator's storefront on Makenotwork, titled and described, above a row of their work in cover art", |
| 113 |
needsAuth: false, |
| 114 |
}, |
| 115 |
{ |
| 116 |
name: 'item', |
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
resolvePath: async (send) => { |
| 128 |
if (process.env.CAPTURE_ITEM_PATH) return process.env.CAPTURE_ITEM_PATH; |
| 129 |
const { result } = await send('Runtime.evaluate', { |
| 130 |
expression: `(() => { |
| 131 |
// Prefer an item the storefront prices, so the frame shows a sale. |
| 132 |
const priced = [...document.querySelectorAll('a[href^="/i/"], a[href^="/purchase/"]')] |
| 133 |
.find((a) => /\\$\\s*\\d/.test(a.closest('article, li, .item-card, div')?.textContent ?? '')); |
| 134 |
const a = priced ?? document.querySelector('a[href^="/i/"], a[href^="/purchase/"]'); |
| 135 |
if (!a) return ''; |
| 136 |
// /purchase/<id> and /i/<id> share the id; the item page is the frame. |
| 137 |
return new URL(a.href).pathname.replace(/^\\/purchase\\//, '/i/'); |
| 138 |
})()`, |
| 139 |
returnByValue: true, |
| 140 |
}); |
| 141 |
return result.value; |
| 142 |
}, |
| 143 |
from: STOREFRONT, |
| 144 |
alt: 'An item page showing its cover art, an $8 price, and a buy button, beside what the purchase includes', |
| 145 |
needsAuth: false, |
| 146 |
}, |
| 147 |
{ |
| 148 |
name: 'library', |
| 149 |
path: '/library', |
| 150 |
alt: 'A buyer library listing what they have bought, each row with its creator, type, purchase date and a button to open it', |
| 151 |
needsAuth: true, |
| 152 |
}, |
| 153 |
]; |
| 154 |
|
| 155 |
|
| 156 |
const sleep = (ms) => new Promise((r) => setTimeout(r, ms)); |
| 157 |
|
| 158 |
|
| 159 |
async function launchChrome(userDataDir) { |
| 160 |
const child = spawn( |
| 161 |
CHROME, |
| 162 |
[ |
| 163 |
'--headless', |
| 164 |
'--disable-gpu', |
| 165 |
'--hide-scrollbars', |
| 166 |
'--no-sandbox', |
| 167 |
'--no-first-run', |
| 168 |
'--remote-debugging-port=0', |
| 169 |
`--user-data-dir=${userDataDir}`, |
| 170 |
'--force-color-profile=srgb', |
| 171 |
'about:blank', |
| 172 |
], |
| 173 |
{ stdio: ['ignore', 'ignore', 'pipe'] }, |
| 174 |
); |
| 175 |
|
| 176 |
const wsUrl = await new Promise((resolve, reject) => { |
| 177 |
let buffered = ''; |
| 178 |
const timer = setTimeout( |
| 179 |
() => reject(new Error(`Chrome did not report a DevTools endpoint.\n${buffered}`)), |
| 180 |
20_000, |
| 181 |
); |
| 182 |
child.stderr.on('data', (chunk) => { |
| 183 |
buffered += chunk; |
| 184 |
const match = buffered.match(/DevTools listening on (ws:\/\/\S+)/); |
| 185 |
if (match) { |
| 186 |
clearTimeout(timer); |
| 187 |
resolve(match[1]); |
| 188 |
} |
| 189 |
}); |
| 190 |
child.on('exit', (code) => { |
| 191 |
clearTimeout(timer); |
| 192 |
reject(new Error(`Chrome exited (${code}) before listening.\n${buffered}`)); |
| 193 |
}); |
| 194 |
}); |
| 195 |
|
| 196 |
return { child, wsUrl }; |
| 197 |
} |
| 198 |
|
| 199 |
|
| 200 |
* A minimal CDP client: id-tagged requests over one socket, flat-mode sessions. |
| 201 |
|
| 202 |
async function connect(wsUrl) { |
| 203 |
const socket = new WebSocket(wsUrl); |
| 204 |
await new Promise((resolve, reject) => { |
| 205 |
socket.addEventListener('open', resolve, { once: true }); |
| 206 |
socket.addEventListener('error', () => reject(new Error(`cannot connect to ${wsUrl}`)), { |
| 207 |
once: true, |
| 208 |
}); |
| 209 |
}); |
| 210 |
|
| 211 |
let nextId = 1; |
| 212 |
const pending = new Map(); |
| 213 |
const listeners = new Set(); |
| 214 |
|
| 215 |
socket.addEventListener('message', (event) => { |
| 216 |
const message = JSON.parse(event.data); |
| 217 |
if (message.id && pending.has(message.id)) { |
| 218 |
const { resolve, reject } = pending.get(message.id); |
| 219 |
pending.delete(message.id); |
| 220 |
if (message.error) reject(new Error(`${message.error.message} (${message.error.code})`)); |
| 221 |
else resolve(message.result); |
| 222 |
return; |
| 223 |
} |
| 224 |
for (const listener of listeners) listener(message); |
| 225 |
}); |
| 226 |
|
| 227 |
const send = (method, params = {}, sessionId) => |
| 228 |
new Promise((resolve, reject) => { |
| 229 |
const id = nextId++; |
| 230 |
pending.set(id, { resolve, reject }); |
| 231 |
socket.send(JSON.stringify({ id, method, params, ...(sessionId ? { sessionId } : {}) })); |
| 232 |
}); |
| 233 |
|
| 234 |
const once = (method, sessionId, timeoutMs = 30_000) => |
| 235 |
new Promise((resolve, reject) => { |
| 236 |
const timer = setTimeout(() => { |
| 237 |
listeners.delete(listener); |
| 238 |
reject(new Error(`timed out waiting for ${method}`)); |
| 239 |
}, timeoutMs); |
| 240 |
const listener = (message) => { |
| 241 |
if (message.method === method && (!sessionId || message.sessionId === sessionId)) { |
| 242 |
clearTimeout(timer); |
| 243 |
listeners.delete(listener); |
| 244 |
resolve(message.params); |
| 245 |
} |
| 246 |
}; |
| 247 |
listeners.add(listener); |
| 248 |
}); |
| 249 |
|
| 250 |
return { socket, send, once }; |
| 251 |
} |
| 252 |
|
| 253 |
|
| 254 |
* Sign in as the demo buyer through the real login form. |
| 255 |
* |
| 256 |
* Submitting the page's own form would go through htmx, which swaps the |
| 257 |
* response into an error div instead of navigating. So this reads the CSRF |
| 258 |
* token and field names off that form and posts a plain detached copy, which |
| 259 |
* the handler treats as a full-page POST and answers with a redirect. Reading |
| 260 |
* the token rather than minting one is the point: the session that results is |
| 261 |
* the same session a person would get. |
| 262 |
* |
| 263 |
* Returns null on success, or a string saying what went wrong. |
| 264 |
|
| 265 |
async function logIn(call, awaitLoad) { |
| 266 |
await call('Page.navigate', { url: `${BASE}/login` }); |
| 267 |
await awaitLoad(); |
| 268 |
|
| 269 |
const submitted = await call('Runtime.evaluate', { |
| 270 |
expression: `(() => { |
| 271 |
const src = document.querySelector('form.login-form'); |
| 272 |
if (!src) return 'no login form at /login'; |
| 273 |
const form = document.createElement('form'); |
| 274 |
form.method = 'post'; |
| 275 |
form.action = '/login'; |
| 276 |
const put = (name, value) => { |
| 277 |
const input = document.createElement('input'); |
| 278 |
input.type = 'hidden'; |
| 279 |
input.name = name; |
| 280 |
input.value = value; |
| 281 |
form.appendChild(input); |
| 282 |
}; |
| 283 |
// Carry every hidden field the real form has (the CSRF token above all), |
| 284 |
// then set the credentials. |
| 285 |
for (const [name, value] of new FormData(src).entries()) { |
| 286 |
if (name !== 'login' && name !== 'password') put(name, value); |
| 287 |
} |
| 288 |
put('login', ${JSON.stringify(BUYER_LOGIN)}); |
| 289 |
put('password', ${JSON.stringify(BUYER_PASSWORD)}); |
| 290 |
document.body.appendChild(form); |
| 291 |
form.submit(); |
| 292 |
return ''; |
| 293 |
})()`, |
| 294 |
returnByValue: true, |
| 295 |
}); |
| 296 |
if (submitted.result.value) return submitted.result.value; |
| 297 |
await awaitLoad(); |
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
const check = await call('Runtime.evaluate', { |
| 302 |
expression: `document.querySelector('form.login-form') ? 'rejected' : ''`, |
| 303 |
returnByValue: true, |
| 304 |
}); |
| 305 |
return check.result.value ? 'credentials rejected' : null; |
| 306 |
} |
| 307 |
|
| 308 |
|
| 309 |
* Clip rectangle for a frame: CROP_WIDTH x (CROP_WIDTH / ASPECT), centred |
| 310 |
* horizontally on the element and anchored at its top, because the top of these |
| 311 |
* pages is the part worth showing. Every frame gets the same rectangle, which is |
| 312 |
* what lets the carousel cross-fade without anything appearing to resize. |
| 313 |
|
| 314 |
function clipFor(box) { |
| 315 |
const width = CROP_WIDTH; |
| 316 |
const height = width / ASPECT; |
| 317 |
return { x: Math.max(0, box.x + (box.width - width) / 2), y: box.y, width, height }; |
| 318 |
} |
| 319 |
|
| 320 |
async function main() { |
| 321 |
const userDataDir = await mkdtemp(path.join(tmpdir(), 'mnw-capture-')); |
| 322 |
await mkdir(OUT_DIR, { recursive: true }); |
| 323 |
const { child, wsUrl } = await launchChrome(userDataDir); |
| 324 |
const { socket, send, once } = await connect(wsUrl); |
| 325 |
|
| 326 |
const missing = []; |
| 327 |
try { |
| 328 |
const { targetId } = await send('Target.createTarget', { url: 'about:blank' }); |
| 329 |
const { sessionId } = await send('Target.attachToTarget', { targetId, flatten: true }); |
| 330 |
const call = (method, params) => send(method, params, sessionId); |
| 331 |
|
| 332 |
await call('Page.enable'); |
| 333 |
await call('Network.enable'); |
| 334 |
await call('Emulation.setDeviceMetricsOverride', { |
| 335 |
width: VIEWPORT.width, |
| 336 |
height: VIEWPORT.height, |
| 337 |
deviceScaleFactor: SCALE, |
| 338 |
mobile: false, |
| 339 |
}); |
| 340 |
|
| 341 |
if (SESSION_COOKIE) { |
| 342 |
const { host } = new URL(BASE); |
| 343 |
await call('Network.setCookie', { |
| 344 |
name: SESSION_COOKIE_NAME, |
| 345 |
value: SESSION_COOKIE, |
| 346 |
domain: host, |
| 347 |
path: '/', |
| 348 |
httpOnly: true, |
| 349 |
secure: BASE.startsWith('https'), |
| 350 |
}); |
| 351 |
} |
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
let signedIn = Boolean(SESSION_COOKIE); |
| 363 |
let loginFailure = null; |
| 364 |
const ensureSignedIn = async () => { |
| 365 |
if (signedIn || loginFailure || !BUYER_PASSWORD) return; |
| 366 |
loginFailure = await logIn(call, () => once('Page.loadEventFired', sessionId)); |
| 367 |
if (loginFailure) { |
| 368 |
console.error(` login: ${loginFailure}`); |
| 369 |
} else { |
| 370 |
signedIn = true; |
| 371 |
console.error(` login: signed in as ${BUYER_LOGIN}`); |
| 372 |
} |
| 373 |
}; |
| 374 |
|
| 375 |
for (const frame of FRAMES) { |
| 376 |
if (frame.needsAuth) { |
| 377 |
if (!CAN_AUTH) { |
| 378 |
console.error( |
| 379 |
` ${frame.name}: skipped, needs CAPTURE_BUYER_PASSWORD (or CAPTURE_SESSION_COOKIE)`, |
| 380 |
); |
| 381 |
missing.push(frame.name); |
| 382 |
continue; |
| 383 |
} |
| 384 |
await ensureSignedIn(); |
| 385 |
if (!signedIn) { |
| 386 |
console.error(` ${frame.name}: skipped, not signed in`); |
| 387 |
missing.push(frame.name); |
| 388 |
continue; |
| 389 |
} |
| 390 |
} |
| 391 |
|
| 392 |
|
| 393 |
|
| 394 |
let target = frame.path; |
| 395 |
if (frame.resolvePath) { |
| 396 |
await call('Page.navigate', { url: BASE + frame.from }); |
| 397 |
await once('Page.loadEventFired', sessionId); |
| 398 |
target = await frame.resolvePath(call); |
| 399 |
if (!target) { |
| 400 |
console.error(` ${frame.name}: skipped, no item link on ${frame.from}`); |
| 401 |
missing.push(frame.name); |
| 402 |
continue; |
| 403 |
} |
| 404 |
} |
| 405 |
|
| 406 |
const url = BASE + target; |
| 407 |
await call('Page.navigate', { url }); |
| 408 |
await once('Page.loadEventFired', sessionId); |
| 409 |
await call('Runtime.evaluate', { |
| 410 |
expression: 'document.fonts.ready.then(() => true)', |
| 411 |
awaitPromise: true, |
| 412 |
}); |
| 413 |
|
| 414 |
|
| 415 |
|
| 416 |
|
| 417 |
|
| 418 |
|
| 419 |
|
| 420 |
|
| 421 |
|
| 422 |
await call('Runtime.evaluate', { |
| 423 |
expression: `(async () => { |
| 424 |
// Everything here races a deadline. An HTMX indicator spins forever, |
| 425 |
// so its animation's finished promise never settles, and a lazy image |
| 426 |
// that 404s can leave a pending decode: without the cap the run hangs |
| 427 |
// instead of shooting a slightly early frame. |
| 428 |
const capped = (p, ms) => Promise.race([p, new Promise((r) => setTimeout(r, ms))]); |
| 429 |
window.scrollTo(0, document.body.scrollHeight); |
| 430 |
await new Promise((r) => setTimeout(r, 250)); |
| 431 |
window.scrollTo(0, 0); |
| 432 |
await capped(Promise.all([...document.images].map((i) => i.complete |
| 433 |
? Promise.resolve() |
| 434 |
: new Promise((r) => { i.addEventListener('load', r, {once: true}); |
| 435 |
i.addEventListener('error', r, {once: true}); }))), 8000); |
| 436 |
await capped(Promise.all( |
| 437 |
document.getAnimations().map((a) => a.finished.catch(() => {}))), 2000); |
| 438 |
return true; |
| 439 |
})()`, |
| 440 |
awaitPromise: true, |
| 441 |
}); |
| 442 |
await sleep(SETTLE_MS); |
| 443 |
|
| 444 |
const selector = frame.selector ?? DEFAULT_SELECTOR; |
| 445 |
const { result } = await call('Runtime.evaluate', { |
| 446 |
expression: `(() => { |
| 447 |
const el = document.querySelector(${JSON.stringify(selector)}); |
| 448 |
if (!el) return null; |
| 449 |
const r = el.getBoundingClientRect(); |
| 450 |
return { x: r.x + scrollX, y: r.y + scrollY, width: r.width, height: r.height }; |
| 451 |
})()`, |
| 452 |
returnByValue: true, |
| 453 |
}); |
| 454 |
if (!result.value) { |
| 455 |
|
| 456 |
|
| 457 |
|
| 458 |
const { result: landed } = await call('Runtime.evaluate', { |
| 459 |
expression: `({ title: document.title, href: location.href })`, |
| 460 |
returnByValue: true, |
| 461 |
}); |
| 462 |
const where = landed.value ?? {}; |
| 463 |
console.error( |
| 464 |
` ${frame.name}: skipped, ${selector} not present at ${url}` + |
| 465 |
` (landed on ${where.href ?? '?'}, titled ${JSON.stringify(where.title ?? '?')})`, |
| 466 |
); |
| 467 |
missing.push(frame.name); |
| 468 |
continue; |
| 469 |
} |
| 470 |
|
| 471 |
const clip = { ...clipFor(result.value), scale: SCALE }; |
| 472 |
const shot = await call('Page.captureScreenshot', { |
| 473 |
format: FORMAT, |
| 474 |
|
| 475 |
...(FORMAT === 'png' ? {} : { quality: QUALITY }), |
| 476 |
clip, |
| 477 |
captureBeyondViewport: true, |
| 478 |
fromSurface: true, |
| 479 |
}); |
| 480 |
const file = path.join(OUT_DIR, `${frame.name}.${FORMAT}`); |
| 481 |
const bytes = Buffer.from(shot.data, 'base64'); |
| 482 |
await writeFile(file, bytes); |
| 483 |
console.error( |
| 484 |
` ${frame.name}: ${Math.round(clip.width * SCALE)}x${Math.round(clip.height * SCALE)}, ` + |
| 485 |
`${Math.round(bytes.length / 1024)}KB -> ${path.relative(process.cwd(), file)}`, |
| 486 |
); |
| 487 |
} |
| 488 |
} finally { |
| 489 |
socket.close(); |
| 490 |
|
| 491 |
|
| 492 |
|
| 493 |
|
| 494 |
const exited = new Promise((resolve) => child.once('exit', resolve)); |
| 495 |
child.kill(); |
| 496 |
await Promise.race([exited, sleep(5_000)]); |
| 497 |
await rm(userDataDir, { recursive: true, force: true }).catch(() => {}); |
| 498 |
} |
| 499 |
|
| 500 |
if (missing.length > 0) { |
| 501 |
console.error( |
| 502 |
`\n${missing.length} of ${FRAMES.length} frames missing (${missing.join(', ')}). ` + |
| 503 |
'The carousel needs all three; nothing has been swapped into landing.rs.', |
| 504 |
); |
| 505 |
process.exit(1); |
| 506 |
} |
| 507 |
|
| 508 |
console.error(`\nAll ${FRAMES.length} frames captured from ${BASE}.`); |
| 509 |
console.error(`Next: review them (GoingsOn 7f3da540), then point landing.rs at the .${FORMAT} files`); |
| 510 |
console.error('and delete static/images/shots/placeholder-*.svg.'); |
| 511 |
} |
| 512 |
|
| 513 |
await main(); |
| 514 |
|