@jonandev/longbeard 0.11.0 → 0.11.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +8 -0
- package/README.md +2 -2
- package/package.json +1 -1
- package/skills/design/SKILL.md +15 -5
- package/skills/design/scripts/capture.mjs +356 -36
- package/skills/design-check/scripts/capture.mjs +356 -36
- package/skills/design-direction/SKILL.md +44 -21
- package/skills/design-direction/references/sources.md +6 -4
- package/skills/design-direction/scripts/capture.mjs +356 -36
- package/skills/design-direction/scripts/moodboard.mjs +290 -53
- package/skills/idea/SKILL.md +11 -4
|
@@ -14,10 +14,33 @@
|
|
|
14
14
|
// shot is of the page asked for.
|
|
15
15
|
// node capture.mjs file:///abs/path/moodboard.html --out /tmp/moodboard.jpg
|
|
16
16
|
// node capture.mjs file:///abs/path/shot.jpg --out design-exploration/<slug>/refs/name.jpg --clip x,y,w,h
|
|
17
|
+
//
|
|
18
|
+
// Before the shot it waits out loaders: until no loader covers the screen and
|
|
19
|
+
// the screen is not one flat colour and the network is quiet, for at most --wait ms (15 s on the web,
|
|
20
|
+
// 2 s for files and localhost). If a full page comes back blank at the top
|
|
21
|
+
// where the screen was not (a canvas that redraws only what is on screen),
|
|
22
|
+
// it keeps the screen it saw as the shot, the full page beside it as
|
|
23
|
+
// <name>-full.jpg, and says so.
|
|
24
|
+
//
|
|
25
|
+
// Motion: --frames N writes N screen-sized frames, --every ms apart (500), to
|
|
26
|
+
// the folder --out names: 01-00000ms.jpg, 02-00500ms.jpg… Without --scroll they
|
|
27
|
+
// start as the page opens, so the loader and the intro are in them. With
|
|
28
|
+
// --scroll they start once it has loaded, and each frame scrolls half a
|
|
29
|
+
// screen by the wheel, so scroll-driven motion plays.
|
|
30
|
+
// node capture.mjs <url> --out design-exploration/<slug>/refs/name-intro --frames 12
|
|
31
|
+
// node capture.mjs <url> --out design-exploration/<slug>/refs/name-scroll --frames 12 --every 400 --scroll
|
|
32
|
+
//
|
|
33
|
+
// --click "css" clicks that element once the page has loaded, as a person
|
|
34
|
+
// would. Alone it keeps the screen after the click (an open menu). With
|
|
35
|
+
// --frames the first frame is before the click and the rest record what the
|
|
36
|
+
// click does (a menu opening, a page transition).
|
|
37
|
+
// node capture.mjs <url> --out design-exploration/<slug>/refs/name-menu.jpg --click "button[aria-label*=menu i]"
|
|
38
|
+
// node capture.mjs <url> --out design-exploration/<slug>/refs/name-menu-open --frames 10 --every 150 --click "header button"
|
|
17
39
|
import { spawn, execSync } from "node:child_process";
|
|
18
40
|
import { existsSync, mkdirSync, mkdtempSync, rmSync, writeFileSync } from "node:fs";
|
|
19
41
|
import { tmpdir } from "node:os";
|
|
20
42
|
import { dirname, isAbsolute, join, resolve } from "node:path";
|
|
43
|
+
import { inflateSync } from "node:zlib";
|
|
21
44
|
|
|
22
45
|
const args = process.argv.slice(2);
|
|
23
46
|
const flag = (name, def) => {
|
|
@@ -34,14 +57,23 @@ const clipArg = flag("clip", "") || "";
|
|
|
34
57
|
const clipSelector = flag("clip-selector", "") || "";
|
|
35
58
|
// Sent with every request to the page's own origin, as a logged-in browser would.
|
|
36
59
|
const cookieArg = flag("cookie", "") || "";
|
|
37
|
-
const
|
|
38
|
-
const
|
|
39
|
-
if (
|
|
60
|
+
const bool = (name) => {
|
|
61
|
+
const i = args.indexOf(`--${name}`);
|
|
62
|
+
if (i !== -1) args.splice(i, 1);
|
|
63
|
+
return i !== -1;
|
|
64
|
+
};
|
|
65
|
+
const strict = bool("strict");
|
|
66
|
+
const scrollFrames = bool("scroll");
|
|
67
|
+
const framesArg = flag("frames", "") || "";
|
|
68
|
+
// Clicks this element once the page has loaded: a menu button, a link, a tab.
|
|
69
|
+
const clickArg = flag("click", "") || "";
|
|
70
|
+
const everyArg = flag("every", "") || "";
|
|
71
|
+
const waitArg = flag("wait", "") || "";
|
|
40
72
|
// Below 768 the page gets a phone viewport: mobile layout, 2x pixels.
|
|
41
73
|
const width = Math.round(Number(flag("width", "1440")));
|
|
42
74
|
if (!url || !/^(https?|file):\/\//.test(url) || !outArg) {
|
|
43
75
|
console.error(
|
|
44
|
-
'usage: node capture.mjs <https://url | file:///path> --out design-exploration/<slug>/refs/name.jpg [--review /tmp/name-review.jpg] [--clip x,y,w,h | --clip-selector "css"] [--width 390] [--cookie "name=value; …"] [--strict]',
|
|
76
|
+
'usage: node capture.mjs <https://url | file:///path> --out design-exploration/<slug>/refs/name.jpg [--review /tmp/name-review.jpg] [--clip x,y,w,h | --clip-selector "css"] [--width 390] [--cookie "name=value; …"] [--strict] [--wait ms] [--click "css"] [--frames N [--every ms] [--scroll]]',
|
|
45
77
|
);
|
|
46
78
|
process.exit(2);
|
|
47
79
|
}
|
|
@@ -49,6 +81,37 @@ if (!Number.isFinite(width) || width < 200 || width > 3840) {
|
|
|
49
81
|
console.error("width must be a number from 200 to 3840");
|
|
50
82
|
process.exit(2);
|
|
51
83
|
}
|
|
84
|
+
const frames = framesArg ? Number(framesArg) : 0;
|
|
85
|
+
const every = everyArg ? Number(everyArg) : 500;
|
|
86
|
+
if (framesArg && (!Number.isInteger(frames) || frames < 2 || frames > 60)) {
|
|
87
|
+
console.error("frames must be a whole number from 2 to 60");
|
|
88
|
+
process.exit(2);
|
|
89
|
+
}
|
|
90
|
+
if (!Number.isInteger(every) || every < 100 || every > 10000) {
|
|
91
|
+
console.error("every must be a whole number of ms from 100 to 10000");
|
|
92
|
+
process.exit(2);
|
|
93
|
+
}
|
|
94
|
+
if ((everyArg || scrollFrames) && !frames) {
|
|
95
|
+
console.error("--every and --scroll go with --frames");
|
|
96
|
+
process.exit(2);
|
|
97
|
+
}
|
|
98
|
+
if (clickArg && scrollFrames) {
|
|
99
|
+
console.error("pass --click or --scroll, not both");
|
|
100
|
+
process.exit(2);
|
|
101
|
+
}
|
|
102
|
+
if ((frames || clickArg) && (reviewArg || clipArg || clipSelector)) {
|
|
103
|
+
console.error("--frames and --click take whole screens: drop --review, --clip and --clip-selector");
|
|
104
|
+
process.exit(2);
|
|
105
|
+
}
|
|
106
|
+
// A local page has no loader worth waiting for, so a sparse screen costs little.
|
|
107
|
+
// A full page stops at 14000 px; our own files (a picker, a moodboard) can run longer.
|
|
108
|
+
const tallest = /^file:/.test(url) ? 30000 : 14000;
|
|
109
|
+
const local = /^file:/.test(url) || /^https?:\/\/(localhost|127\.0\.0\.1|\[::1\])(:|\/|$)/.test(url);
|
|
110
|
+
const maxWait = waitArg ? Number(waitArg) : local ? 2000 : 15000;
|
|
111
|
+
if (!Number.isFinite(maxWait) || maxWait < 0 || maxWait > 120000) {
|
|
112
|
+
console.error("wait must be ms from 0 to 120000");
|
|
113
|
+
process.exit(2);
|
|
114
|
+
}
|
|
52
115
|
const mobile = width < 768;
|
|
53
116
|
const height = mobile ? 844 : 900;
|
|
54
117
|
if (clipArg && clipSelector) {
|
|
@@ -148,6 +211,8 @@ proc = spawn(
|
|
|
148
211
|
"--hide-scrollbars",
|
|
149
212
|
"--mute-audio",
|
|
150
213
|
"--force-color-profile=srgb",
|
|
214
|
+
// Without a GPU, Chrome no longer falls back to software WebGL on its own.
|
|
215
|
+
"--enable-unsafe-swiftshader",
|
|
151
216
|
`--window-size=${width},${height}`,
|
|
152
217
|
"about:blank",
|
|
153
218
|
],
|
|
@@ -178,8 +243,20 @@ await new Promise((res, rej) => {
|
|
|
178
243
|
});
|
|
179
244
|
let nextId = 1;
|
|
180
245
|
const pending = new Map();
|
|
246
|
+
// Requests in flight, so the wait can tell a loader still fetching its assets.
|
|
247
|
+
// A request open for over 10 s is a stream or a poll, not a load.
|
|
248
|
+
const inflight = new Map();
|
|
249
|
+
let lastNetwork = Date.now();
|
|
181
250
|
ws.onmessage = (ev) => {
|
|
182
251
|
const msg = JSON.parse(typeof ev.data === "string" ? ev.data : Buffer.from(ev.data).toString());
|
|
252
|
+
// A hot-reload stream or a socket stays open by design, so it is not a load.
|
|
253
|
+
if (msg.method === "Network.requestWillBeSent" && !/^(EventSource|WebSocket)$/.test(msg.params.type || "")) {
|
|
254
|
+
inflight.set(msg.params.requestId, Date.now());
|
|
255
|
+
lastNetwork = Date.now();
|
|
256
|
+
} else if (msg.method === "Network.loadingFinished" || msg.method === "Network.loadingFailed") {
|
|
257
|
+
inflight.delete(msg.params.requestId);
|
|
258
|
+
lastNetwork = Date.now();
|
|
259
|
+
}
|
|
183
260
|
if (msg.id && pending.has(msg.id)) {
|
|
184
261
|
const { resolve: ok, reject: bad } = pending.get(msg.id);
|
|
185
262
|
pending.delete(msg.id);
|
|
@@ -214,6 +291,7 @@ const page = (method, params, timeoutMs) => send(method, params, sessionId, time
|
|
|
214
291
|
|
|
215
292
|
await page("Page.enable");
|
|
216
293
|
await page("Runtime.enable");
|
|
294
|
+
await page("Network.enable");
|
|
217
295
|
await page("Emulation.setDeviceMetricsOverride", {
|
|
218
296
|
width,
|
|
219
297
|
height,
|
|
@@ -243,44 +321,250 @@ if (cookieArg && /^https?:/.test(url)) {
|
|
|
243
321
|
}
|
|
244
322
|
await page("Network.setCookies", { cookies });
|
|
245
323
|
}
|
|
246
|
-
|
|
247
|
-
await
|
|
324
|
+
const evalValue = async (expression, timeoutMs) =>
|
|
325
|
+
(await page("Runtime.evaluate", { expression, awaitPromise: true, returnByValue: true }, timeoutMs)).result?.value;
|
|
248
326
|
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
)
|
|
327
|
+
// Share of a PNG's pixels in its most common colour, so 1 is one flat colour.
|
|
328
|
+
// Chrome writes 8-bit RGB or RGBA, not interlaced; anything else counts as not flat.
|
|
329
|
+
function flatShare(png) {
|
|
330
|
+
if (png.readUInt32BE(12) !== 0x49484452) return 0;
|
|
331
|
+
const w = png.readUInt32BE(16);
|
|
332
|
+
const h = png.readUInt32BE(20);
|
|
333
|
+
const depth = png[24];
|
|
334
|
+
const type = png[25];
|
|
335
|
+
const bpp = type === 6 ? 4 : type === 2 ? 3 : 0;
|
|
336
|
+
if (depth !== 8 || !bpp || png[28] !== 0) return 0;
|
|
337
|
+
const chunks = [];
|
|
338
|
+
for (let at = 8; at < png.length; ) {
|
|
339
|
+
const len = png.readUInt32BE(at);
|
|
340
|
+
const kind = png.toString("latin1", at + 4, at + 8);
|
|
341
|
+
if (kind === "IDAT") chunks.push(png.subarray(at + 8, at + 8 + len));
|
|
342
|
+
at += len + 12;
|
|
343
|
+
}
|
|
344
|
+
const raw = inflateSync(Buffer.concat(chunks));
|
|
345
|
+
const stride = w * bpp;
|
|
346
|
+
let prev = Buffer.alloc(stride);
|
|
347
|
+
const counts = new Map();
|
|
348
|
+
for (let y = 0; y < h; y++) {
|
|
349
|
+
const filter = raw[y * (stride + 1)];
|
|
350
|
+
const line = Buffer.from(raw.subarray(y * (stride + 1) + 1, (y + 1) * (stride + 1)));
|
|
351
|
+
for (let i = 0; i < stride; i++) {
|
|
352
|
+
const a = i >= bpp ? line[i - bpp] : 0;
|
|
353
|
+
const b = prev[i];
|
|
354
|
+
const c = i >= bpp ? prev[i - bpp] : 0;
|
|
355
|
+
let add = 0;
|
|
356
|
+
if (filter === 1) add = a;
|
|
357
|
+
else if (filter === 2) add = b;
|
|
358
|
+
else if (filter === 3) add = (a + b) >> 1;
|
|
359
|
+
else if (filter === 4) {
|
|
360
|
+
const p = a + b - c;
|
|
361
|
+
const pa = Math.abs(p - a);
|
|
362
|
+
const pb = Math.abs(p - b);
|
|
363
|
+
const pc = Math.abs(p - c);
|
|
364
|
+
add = pa <= pb && pa <= pc ? a : pb <= pc ? b : c;
|
|
365
|
+
}
|
|
366
|
+
line[i] = (line[i] + add) & 255;
|
|
367
|
+
}
|
|
368
|
+
for (let i = 0; i < stride; i += bpp) {
|
|
369
|
+
const key = ((line[i] >> 3) << 10) | ((line[i + 1] >> 3) << 5) | (line[i + 2] >> 3);
|
|
370
|
+
counts.set(key, (counts.get(key) || 0) + 1);
|
|
371
|
+
}
|
|
372
|
+
prev = line;
|
|
373
|
+
}
|
|
374
|
+
return Math.max(...counts.values()) / (w * h);
|
|
375
|
+
}
|
|
255
376
|
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
377
|
+
// How flat the screen is: a small PNG of what a person sees right now, or of
|
|
378
|
+
// the top of the full page once it has been laid out for the full shot.
|
|
379
|
+
const FLAT = 0.985;
|
|
380
|
+
const screenFlat = async (beyond = false) => {
|
|
381
|
+
const { data } = await page(
|
|
382
|
+
"Page.captureScreenshot",
|
|
383
|
+
{ format: "png", clip: { x: 0, y: 0, width, height, scale: 0.25 }, captureBeyondViewport: beyond },
|
|
384
|
+
20000,
|
|
385
|
+
);
|
|
386
|
+
return flatShare(Buffer.from(data, "base64"));
|
|
387
|
+
};
|
|
388
|
+
|
|
389
|
+
// A loader is a fixed or absolute layer named like one that covers most of
|
|
390
|
+
// the screen, or a visible count to 100%. A canvas or a video is the page.
|
|
391
|
+
const loaderCheck = `(() => {
|
|
392
|
+
const vw = innerWidth, vh = innerHeight;
|
|
393
|
+
const named = /(^|[-_\\s])(pre-?)?load(er|ing)?([-_\\s]|$)|preload|intro|splash|curtain/i;
|
|
394
|
+
for (const el of document.body ? document.body.querySelectorAll("*") : []) {
|
|
395
|
+
if (el.tagName === "CANVAS" || el.tagName === "VIDEO") continue;
|
|
396
|
+
const label = ((typeof el.className === "string" ? el.className : "") + " " + el.id).trim();
|
|
397
|
+
const count = el.childElementCount === 0 && /^\\s*\\d{1,3}\\s*%\\s*$/.test(el.textContent || "");
|
|
398
|
+
if (!count && !named.test(label)) continue;
|
|
399
|
+
const s = getComputedStyle(el);
|
|
400
|
+
if (s.display === "none" || s.visibility === "hidden" || Number(s.opacity) < 0.05) continue;
|
|
401
|
+
if (!count && s.position !== "fixed" && s.position !== "absolute") continue;
|
|
402
|
+
const r = el.getBoundingClientRect();
|
|
403
|
+
const seen = Math.max(0, Math.min(r.right, vw) - Math.max(r.left, 0)) * Math.max(0, Math.min(r.bottom, vh) - Math.max(r.top, 0));
|
|
404
|
+
if (count ? seen > 0 : seen >= vw * vh * 0.6) return (label || el.tagName).slice(0, 60);
|
|
405
|
+
}
|
|
406
|
+
return "";
|
|
407
|
+
})()`;
|
|
408
|
+
|
|
409
|
+
// Waits for the load event and fonts, then until two checks in a row see no
|
|
410
|
+
// loader, a screen that is not one flat colour (or has stayed flat with the
|
|
411
|
+
// network quiet for 2.5 s), and no request for a second.
|
|
412
|
+
// Returns why it gave up.
|
|
413
|
+
async function settle() {
|
|
414
|
+
const start = Date.now();
|
|
415
|
+
const left = () => Math.max(0, maxWait - (Date.now() - start));
|
|
416
|
+
await evalValue(
|
|
417
|
+
`new Promise((done) => {
|
|
418
|
+
const t = setTimeout(done, ${Math.max(1500, Math.min(maxWait, 20000))});
|
|
419
|
+
const fonts = () => (document.fonts ? document.fonts.ready : Promise.resolve()).then(() => { clearTimeout(t); done(); });
|
|
420
|
+
if (document.readyState === "complete") fonts(); else addEventListener("load", fonts, { once: true });
|
|
421
|
+
})`,
|
|
422
|
+
25000,
|
|
423
|
+
).catch(() => {});
|
|
424
|
+
const busy = () => {
|
|
425
|
+
for (const [id, at] of inflight) if (Date.now() - at > 10000) inflight.delete(id);
|
|
426
|
+
return inflight.size > 0 || Date.now() - lastNetwork < 1000;
|
|
427
|
+
};
|
|
428
|
+
let good = 0;
|
|
429
|
+
let fetched = false;
|
|
430
|
+
let flatSince = 0;
|
|
431
|
+
for (;;) {
|
|
432
|
+
const loader = await evalValue(loaderCheck).catch(() => "");
|
|
433
|
+
const flat = loader ? 1 : await screenFlat().catch(() => 0);
|
|
434
|
+
const fetching = busy();
|
|
435
|
+
fetched ||= fetching;
|
|
436
|
+
// A flat screen with nothing left to fetch for 2.5 s is a sparse page, not a loader.
|
|
437
|
+
flatSince = flat >= FLAT && !loader && !fetching ? flatSince || Date.now() : 0;
|
|
438
|
+
const sparse = flatSince && Date.now() - flatSince >= 2500;
|
|
439
|
+
good = !loader && !fetching && (flat < FLAT || sparse) ? good + 1 : 0;
|
|
440
|
+
if (good >= 2) break;
|
|
441
|
+
if (!left()) {
|
|
442
|
+
return loader
|
|
443
|
+
? `a loader still covers the screen (${loader})`
|
|
444
|
+
: flat >= FLAT
|
|
445
|
+
? "the screen is still one flat colour"
|
|
446
|
+
: "the page is still fetching";
|
|
263
447
|
}
|
|
264
|
-
|
|
265
|
-
|
|
448
|
+
await sleep(Math.min(500, left()));
|
|
449
|
+
}
|
|
450
|
+
// A loader that waited on its assets usually plays a reveal once they land.
|
|
451
|
+
if (fetched && !local) await sleep(Math.min(1500, left()));
|
|
452
|
+
return "";
|
|
453
|
+
}
|
|
454
|
+
|
|
455
|
+
// Clicks a consent button, which can be any element, or else strips a short
|
|
456
|
+
// privacy notice. Rejecting comes first: a script has no use for tracking,
|
|
457
|
+
// and accepting can start scripts that redraw the page.
|
|
458
|
+
// A local page is a state someone set up on purpose, so it is left as it is.
|
|
459
|
+
const dismissConsent = () =>
|
|
460
|
+
local ? Promise.resolve(false) : evalValue(`(() => {
|
|
461
|
+
const accept = /^(accept( all)?( cookies)?|accept (and|&) close|agree|i agree|ok|okay|got it|allow( all)?( cookies)?|i understand|acepto|aceptar)$/i;
|
|
462
|
+
const reject = /^(reject( all)?( cookies)?|decline( all)?|deny|only (essential|necessary)( cookies)?)$/i;
|
|
463
|
+
const clickable = [...document.querySelectorAll("button, [role=button], a, input[type=button], input[type=submit], [onclick], div, span")]
|
|
464
|
+
.filter((el) => el.childElementCount <= 2 && el.getClientRects().length);
|
|
465
|
+
// Only inside a notice about cookies or privacy, so a menu or a gate is never clicked.
|
|
466
|
+
const notice = (el) => {
|
|
467
|
+
for (let up = el, i = 0; up && i < 8; up = up.parentElement, i++) {
|
|
468
|
+
const t = up.innerText || "";
|
|
469
|
+
if (t.length < 1500 && /cookie|consent|privacy|gdpr|datenschutz/i.test(t)) return true;
|
|
470
|
+
}
|
|
471
|
+
return false;
|
|
472
|
+
};
|
|
473
|
+
const text = (el) => (el.innerText || el.value || el.textContent || "").trim().replace(/\\s+/g, " ");
|
|
474
|
+
const pick = (re) => clickable.find((el) => text(el).length < 40 && re.test(text(el)) && notice(el));
|
|
475
|
+
const hit = pick(reject) || pick(accept);
|
|
476
|
+
if (hit) {
|
|
477
|
+
hit.click();
|
|
478
|
+
return true;
|
|
479
|
+
}
|
|
480
|
+
// No button: strip a short privacy notice that floats over the page (e.g.
|
|
481
|
+
// Google Fonts "×" banner). Only a fixed or sticky layer with no canvas or
|
|
482
|
+
// video, because a wrapper whose only text is the notice can hold the page.
|
|
483
|
+
const privacy = /uses cookies|uses essential cookies|privacy and google fonts|cookie settings/i;
|
|
266
484
|
for (const el of [...document.querySelectorAll("div, section, aside, dialog")]) {
|
|
267
485
|
const t = (el.innerText || el.textContent || "").trim();
|
|
268
|
-
|
|
486
|
+
const pos = getComputedStyle(el).position;
|
|
487
|
+
if (t.length > 0 && t.length < 300 && privacy.test(t) && el.children.length < 12 &&
|
|
488
|
+
(pos === "fixed" || pos === "sticky") && !el.querySelector("canvas, video")) {
|
|
269
489
|
el.remove();
|
|
270
490
|
}
|
|
271
491
|
}
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
returnByValue: true,
|
|
275
|
-
});
|
|
276
|
-
await sleep(800);
|
|
492
|
+
return false;
|
|
493
|
+
})()`).catch(() => false);
|
|
277
494
|
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
)
|
|
495
|
+
// A real press and release in the middle of the element, as a person would click.
|
|
496
|
+
async function clickOn(selector) {
|
|
497
|
+
const box = await evalValue(`(() => {
|
|
498
|
+
const el = document.querySelector(${JSON.stringify(selector)});
|
|
499
|
+
if (!el) return null;
|
|
500
|
+
el.scrollIntoView({ block: "center", inline: "center" });
|
|
501
|
+
const r = el.getBoundingClientRect();
|
|
502
|
+
return r.width && r.height ? { x: r.left + r.width / 2, y: r.top + r.height / 2 } : null;
|
|
503
|
+
})()`);
|
|
504
|
+
if (!box) {
|
|
505
|
+
console.error(`no visible element matches ${selector}`);
|
|
506
|
+
drop();
|
|
507
|
+
process.exit(1);
|
|
508
|
+
}
|
|
509
|
+
const at = { x: Math.round(box.x), y: Math.round(box.y), button: "left", clickCount: 1 };
|
|
510
|
+
await page("Input.dispatchMouseEvent", { type: "mouseMoved", x: at.x, y: at.y });
|
|
511
|
+
await page("Input.dispatchMouseEvent", { type: "mousePressed", ...at });
|
|
512
|
+
await page("Input.dispatchMouseEvent", { type: "mouseReleased", ...at });
|
|
513
|
+
}
|
|
514
|
+
|
|
515
|
+
const navigateStarted = Date.now();
|
|
516
|
+
await page("Page.navigate", { url: target }, 40000);
|
|
517
|
+
|
|
518
|
+
if (frames) {
|
|
519
|
+
// The frames are the record of motion, so nothing is paused, unstuck or cleaned up.
|
|
520
|
+
const folder = out.replace(/\.(png|jpe?g|webp)$/i, "");
|
|
521
|
+
mkdirSync(folder, { recursive: true });
|
|
522
|
+
let t0 = navigateStarted;
|
|
523
|
+
if (scrollFrames || clickArg) {
|
|
524
|
+
const why = await settle();
|
|
525
|
+
if (why) console.error(`waited ${maxWait} ms: ${why}`);
|
|
526
|
+
if (await dismissConsent()) {
|
|
527
|
+
await sleep(800);
|
|
528
|
+
await settle();
|
|
529
|
+
}
|
|
530
|
+
await sleep(600);
|
|
531
|
+
t0 = Date.now();
|
|
532
|
+
}
|
|
533
|
+
for (let i = 0; i < frames; i++) {
|
|
534
|
+
const due = t0 + i * every;
|
|
535
|
+
if (Date.now() < due) await sleep(due - Date.now());
|
|
536
|
+
if (scrollFrames && i > 0) {
|
|
537
|
+
await page("Input.dispatchMouseEvent", { type: "mouseWheel", x: Math.round(width / 2), y: Math.round(height / 2), deltaX: 0, deltaY: Math.round(height / 2) });
|
|
538
|
+
}
|
|
539
|
+
// While the page is still swapping in, Chrome refuses a shot for a moment.
|
|
540
|
+
let data = "";
|
|
541
|
+
for (let tries = 0; !data && tries < 10; tries++) {
|
|
542
|
+
data = await page("Page.captureScreenshot", { format: "jpeg", quality: 70 }, 20000).then((r) => r.data, () => "");
|
|
543
|
+
if (!data) await sleep(150);
|
|
544
|
+
}
|
|
545
|
+
if (!data) continue;
|
|
546
|
+
const at = Date.now() - t0;
|
|
547
|
+
const file = join(folder, `${String(i + 1).padStart(2, "0")}-${String(at).padStart(5, "0")}ms.jpg`);
|
|
548
|
+
writeFileSync(file, Buffer.from(data, "base64"));
|
|
549
|
+
// The first frame is the page before the click; the rest are what it does.
|
|
550
|
+
if (clickArg && i === 0) await clickOn(clickArg);
|
|
551
|
+
}
|
|
552
|
+
console.log(folder);
|
|
553
|
+
drop();
|
|
554
|
+
process.exit(0);
|
|
555
|
+
}
|
|
556
|
+
|
|
557
|
+
const waited = await settle();
|
|
558
|
+
if (waited) console.error(`waited ${maxWait} ms: ${waited}`);
|
|
559
|
+
|
|
560
|
+
const beforeHref = await evalValue("location.href");
|
|
561
|
+
// A consent click can make a page redraw from black, so the wait runs again.
|
|
562
|
+
if (await dismissConsent()) {
|
|
563
|
+
await sleep(800);
|
|
564
|
+
const again = await settle();
|
|
565
|
+
if (again) console.error(`after the consent click, waited ${maxWait} ms: ${again}`);
|
|
566
|
+
} else await sleep(800);
|
|
567
|
+
const afterHref = await evalValue("location.href");
|
|
284
568
|
|
|
285
569
|
if (afterHref !== beforeHref) {
|
|
286
570
|
console.error(`navigated away: ${afterHref}`);
|
|
@@ -288,6 +572,26 @@ if (afterHref !== beforeHref) {
|
|
|
288
572
|
process.exit(1);
|
|
289
573
|
}
|
|
290
574
|
|
|
575
|
+
// After a click the page is a new state, often an overlay: the screen is the shot.
|
|
576
|
+
if (clickArg) {
|
|
577
|
+
await clickOn(clickArg);
|
|
578
|
+
await sleep(1500);
|
|
579
|
+
const format = /\.png$/i.test(out) ? "png" : /\.webp$/i.test(out) ? "webp" : "jpeg";
|
|
580
|
+
const { data } = await page("Page.captureScreenshot", { format, ...(format === "png" ? {} : { quality: 70 }) }, 20000);
|
|
581
|
+
mkdirSync(dirname(out), { recursive: true });
|
|
582
|
+
writeFileSync(out, Buffer.from(data, "base64"));
|
|
583
|
+
console.log(out);
|
|
584
|
+
drop();
|
|
585
|
+
process.exit(0);
|
|
586
|
+
}
|
|
587
|
+
|
|
588
|
+
// The screen as a person first sees it, kept in case the full page loses it.
|
|
589
|
+
const firstFormat = /\.png$/i.test(out) ? "png" : /\.webp$/i.test(out) ? "webp" : "jpeg";
|
|
590
|
+
const firstScreen = (
|
|
591
|
+
await page("Page.captureScreenshot", { format: firstFormat, ...(firstFormat === "png" ? {} : { quality: 70 }) }, 20000)
|
|
592
|
+
).data;
|
|
593
|
+
const firstFlat = await screenFlat().catch(() => 1);
|
|
594
|
+
|
|
291
595
|
await page(
|
|
292
596
|
"Runtime.evaluate",
|
|
293
597
|
{
|
|
@@ -300,7 +604,7 @@ await page(
|
|
|
300
604
|
const step = Math.max(480, Math.round(window.innerHeight * 0.65));
|
|
301
605
|
const limit = Math.min(
|
|
302
606
|
Math.max(document.documentElement.scrollHeight, document.body?.scrollHeight || 0),
|
|
303
|
-
|
|
607
|
+
${tallest}
|
|
304
608
|
);
|
|
305
609
|
let y = 0;
|
|
306
610
|
while (y < limit) {
|
|
@@ -396,7 +700,7 @@ if (clipSelector) {
|
|
|
396
700
|
const metrics = await page("Page.getLayoutMetrics");
|
|
397
701
|
const size = metrics.cssContentSize || metrics.contentSize;
|
|
398
702
|
const fullWidth = Math.max(1, Math.ceil(size.width || width));
|
|
399
|
-
const fullHeight = Math.max(1, Math.min(Math.ceil(size.height || height),
|
|
703
|
+
const fullHeight = Math.max(1, Math.min(Math.ceil(size.height || height), tallest));
|
|
400
704
|
const clipBox = clip || (selected ? { ...selected, scale: 1 } : null);
|
|
401
705
|
const shotClip = clipBox
|
|
402
706
|
? {
|
|
@@ -410,7 +714,7 @@ const shotClip = clipBox
|
|
|
410
714
|
|
|
411
715
|
// The file is what its name says, so a reader that trusts the extension is right.
|
|
412
716
|
const format = /\.png$/i.test(out) ? "png" : /\.webp$/i.test(out) ? "webp" : "jpeg";
|
|
413
|
-
|
|
717
|
+
let { data } = await page(
|
|
414
718
|
"Page.captureScreenshot",
|
|
415
719
|
{
|
|
416
720
|
format,
|
|
@@ -421,6 +725,22 @@ const { data } = await page(
|
|
|
421
725
|
60000,
|
|
422
726
|
);
|
|
423
727
|
|
|
728
|
+
// A canvas that draws only what is on screen goes blank when the page is laid
|
|
729
|
+
// out full length. Then the screen seen first is the honest shot, and the
|
|
730
|
+
// full page is kept beside it for what lies below.
|
|
731
|
+
let fullOut = "";
|
|
732
|
+
if (!clipBox && firstFlat < FLAT && (await screenFlat(true).catch(() => 0)) >= FLAT) {
|
|
733
|
+
if (fullHeight > height * 1.2) {
|
|
734
|
+
fullOut = out.replace(/(\.[a-z]+)?$/i, (ext) => `-full${ext || ".jpg"}`);
|
|
735
|
+
mkdirSync(dirname(fullOut), { recursive: true });
|
|
736
|
+
writeFileSync(fullOut, Buffer.from(data, "base64"));
|
|
737
|
+
}
|
|
738
|
+
data = firstScreen;
|
|
739
|
+
console.error(
|
|
740
|
+
`the full page came back blank at the top (a canvas that draws only what is on screen): kept the first screen${fullOut ? `, and the full page as ${fullOut}` : ""}`,
|
|
741
|
+
);
|
|
742
|
+
}
|
|
743
|
+
|
|
424
744
|
mkdirSync(dirname(out), { recursive: true });
|
|
425
745
|
writeFileSync(out, Buffer.from(data, "base64"));
|
|
426
746
|
console.log(out);
|