@wallets-e2e/walkthrough 0.0.0-stage → 0.1.0
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/LICENSE +21 -0
- package/README.md +104 -2
- package/dist/cli-args.d.ts +14 -0
- package/dist/cli-args.d.ts.map +1 -0
- package/dist/cli-args.js +51 -0
- package/dist/cli-args.js.map +1 -0
- package/dist/cli.d.ts +3 -0
- package/dist/cli.d.ts.map +1 -0
- package/dist/cli.js +30 -0
- package/dist/cli.js.map +1 -0
- package/dist/encode.d.ts +53 -0
- package/dist/encode.d.ts.map +1 -0
- package/dist/encode.js +84 -0
- package/dist/encode.js.map +1 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +5 -0
- package/dist/index.js.map +1 -0
- package/dist/theme.d.ts +27 -0
- package/dist/theme.d.ts.map +1 -0
- package/dist/theme.js +51 -0
- package/dist/theme.js.map +1 -0
- package/dist/timeline.d.ts +53 -0
- package/dist/timeline.d.ts.map +1 -0
- package/dist/timeline.js +101 -0
- package/dist/timeline.js.map +1 -0
- package/dist/walkthrough.d.ts +139 -0
- package/dist/walkthrough.d.ts.map +1 -0
- package/dist/walkthrough.js +372 -0
- package/dist/walkthrough.js.map +1 -0
- package/package.json +52 -3
- package/src/cli-args.ts +58 -0
- package/src/cli.ts +27 -0
- package/src/encode.ts +116 -0
- package/src/index.ts +12 -0
- package/src/theme.ts +75 -0
- package/src/timeline.ts +135 -0
- package/src/walkthrough.ts +445 -0
|
@@ -0,0 +1,445 @@
|
|
|
1
|
+
import { mkdirSync, writeFileSync } from 'node:fs';
|
|
2
|
+
import { basename, join } from 'node:path';
|
|
3
|
+
import type { Locator, Page } from '@playwright/test';
|
|
4
|
+
import { DEFAULT_THEME, overlayStyle, type WalkthroughTheme } from './theme.js';
|
|
5
|
+
import { concatList, frameDurations, placeChapters, type Chapter, type FrameMark } from './timeline.js';
|
|
6
|
+
|
|
7
|
+
export interface WalkthroughOptions {
|
|
8
|
+
/** Where the run is written: `<outputDir>/frames/` holds the frames, `frames.ffconcat` and `chapters.json`. */
|
|
9
|
+
outputDir: string;
|
|
10
|
+
/** Colours and fonts of everything drawn over the page. Missing fields take the defaults. */
|
|
11
|
+
theme?: Partial<WalkthroughTheme>;
|
|
12
|
+
/** The page's size in CSS pixels. Default 1440×810, which the video shows at `videoWidth`. */
|
|
13
|
+
view?: { width: number; height: number };
|
|
14
|
+
/** Width of the recorded frames in pixels. Default 1920: a 1440×810 view recorded at 1920×1080. */
|
|
15
|
+
videoWidth?: number;
|
|
16
|
+
/** JPEG quality of the recorded frames, 0–100. Default 92. */
|
|
17
|
+
quality?: number;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/** A title card: a full-screen slide between parts of the demo. */
|
|
21
|
+
export interface CardOptions {
|
|
22
|
+
/** The small line above the title, e.g. "Live demo" or "Recap". */
|
|
23
|
+
kicker: string;
|
|
24
|
+
title: string;
|
|
25
|
+
body?: string;
|
|
26
|
+
/** A numbered list under the body. */
|
|
27
|
+
items?: string[];
|
|
28
|
+
/** How long the card stays, in ms. Default 5000. */
|
|
29
|
+
holdMs?: number;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/** What a wallet driver calls with its prompt open: the page of the prompt and the button it is about to press. */
|
|
33
|
+
export type PromptHook = (prompt: Page, button: Locator) => Promise<void>;
|
|
34
|
+
|
|
35
|
+
export interface WalletPromptOptions {
|
|
36
|
+
/** The wallet's name, shown on the mirrored prompt's title bar and as the caption chip: "MetaMask". */
|
|
37
|
+
label: string;
|
|
38
|
+
/** The colour of the dot on the title bar. Default: the theme's accent. */
|
|
39
|
+
accent?: string;
|
|
40
|
+
/** Captions shown one after the other while the viewer reads the prompt. */
|
|
41
|
+
explain?: string[];
|
|
42
|
+
/** How long each of those captions stays, in ms. Default 3800. */
|
|
43
|
+
explainHoldMs?: number;
|
|
44
|
+
/** The size the prompt is rendered at before it is mirrored. Default 380×620, an extension popup. */
|
|
45
|
+
promptSize?: { width: number; height: number };
|
|
46
|
+
/** The action on the page that makes the wallet open its prompt, usually `() => show.press(button)`. */
|
|
47
|
+
trigger: () => Promise<void>;
|
|
48
|
+
/**
|
|
49
|
+
* Hands the trigger and the hook to the wallet driver, e.g.
|
|
50
|
+
* `(trigger, beforeAnswer) => metamaskDriver.confirmTransaction(context, trigger, { beforeAnswer })`.
|
|
51
|
+
* The driver presses the wallet's button when the hook returns.
|
|
52
|
+
*/
|
|
53
|
+
answer: (trigger: () => Promise<void>, beforeAnswer: PromptHook) => Promise<void>;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/** What `stop` leaves on disk. `wallets-e2e-walkthrough` (or `encodeWalkthrough`) turns it into the video. */
|
|
57
|
+
export interface WalkthroughRun {
|
|
58
|
+
outputDir: string;
|
|
59
|
+
framesDir: string;
|
|
60
|
+
concatFile: string;
|
|
61
|
+
chaptersFile: string;
|
|
62
|
+
frames: number;
|
|
63
|
+
chapters: Chapter[];
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
const WALLET_OVERLAY = { right: 28, top: 70, headerHeight: 30 };
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* A presentation-quality recording of one page, for demos of a dapp driven by real wallets:
|
|
70
|
+
* the browser's own screencast, an animated cursor with click ripples, captions with a step chip,
|
|
71
|
+
* full-screen title cards, and each wallet prompt shown inside the same picture.
|
|
72
|
+
*
|
|
73
|
+
* Long waits are not recorded: `pause()` before waiting for a chain, `resume()` after, and the wait
|
|
74
|
+
* becomes a short cut. Captions and cards are logged with their time, which the encoder turns into
|
|
75
|
+
* subtitles and chapters.
|
|
76
|
+
*
|
|
77
|
+
* Record only what may be shown: set the wallets up (the recovery phrase, the password) before
|
|
78
|
+
* `start()`.
|
|
79
|
+
*/
|
|
80
|
+
export class Walkthrough {
|
|
81
|
+
readonly page: Page;
|
|
82
|
+
readonly outputDir: string;
|
|
83
|
+
readonly theme: WalkthroughTheme;
|
|
84
|
+
private readonly view: { width: number; height: number };
|
|
85
|
+
private readonly scale: number;
|
|
86
|
+
private readonly quality: number;
|
|
87
|
+
private readonly framesDir: string;
|
|
88
|
+
private readonly frames: (FrameMark & { file: string })[] = [];
|
|
89
|
+
private readonly chapters: Chapter[] = [];
|
|
90
|
+
private cdp: Awaited<ReturnType<ReturnType<Page['context']>['newCDPSession']>> | null = null;
|
|
91
|
+
private paused = true;
|
|
92
|
+
private resumed = false;
|
|
93
|
+
private started = false;
|
|
94
|
+
|
|
95
|
+
constructor(page: Page, options: WalkthroughOptions) {
|
|
96
|
+
this.page = page;
|
|
97
|
+
this.outputDir = options.outputDir;
|
|
98
|
+
this.framesDir = join(options.outputDir, 'frames');
|
|
99
|
+
this.theme = { ...DEFAULT_THEME, ...options.theme };
|
|
100
|
+
this.view = options.view ?? { width: 1440, height: 810 };
|
|
101
|
+
this.scale = (options.videoWidth ?? 1920) / this.view.width;
|
|
102
|
+
this.quality = options.quality ?? 92;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/** Sizes the page for the recording and mounts the cursor layer. Call once, before `start`. */
|
|
106
|
+
async setup(): Promise<void> {
|
|
107
|
+
const cdp = await this.page.context().newCDPSession(this.page);
|
|
108
|
+
this.cdp = cdp;
|
|
109
|
+
await this.page.setViewportSize(this.view);
|
|
110
|
+
await this.applyMetrics();
|
|
111
|
+
// The overlay layer survives navigations; the cursor position carries over in sessionStorage.
|
|
112
|
+
await this.page.addInitScript(({ style, paper, ink }) => {
|
|
113
|
+
const mount = () => {
|
|
114
|
+
if (document.getElementById('wt-cursor')) return;
|
|
115
|
+
const css = document.createElement('style');
|
|
116
|
+
css.id = 'wt-style';
|
|
117
|
+
css.textContent = style;
|
|
118
|
+
document.head.append(css);
|
|
119
|
+
const cursor = document.createElement('div');
|
|
120
|
+
cursor.id = 'wt-cursor';
|
|
121
|
+
const svg = document.createElementNS('http://www.w3.org/2000/svg', 'svg');
|
|
122
|
+
svg.setAttribute('width', '24');
|
|
123
|
+
svg.setAttribute('height', '24');
|
|
124
|
+
svg.setAttribute('viewBox', '0 0 24 24');
|
|
125
|
+
const path = document.createElementNS('http://www.w3.org/2000/svg', 'path');
|
|
126
|
+
path.setAttribute('d', 'M4 2l16 10-7 1.5L9.5 21z');
|
|
127
|
+
path.setAttribute('fill', paper);
|
|
128
|
+
path.setAttribute('stroke', ink);
|
|
129
|
+
path.setAttribute('stroke-width', '1.6');
|
|
130
|
+
path.setAttribute('stroke-linejoin', 'round');
|
|
131
|
+
svg.append(path);
|
|
132
|
+
cursor.append(svg);
|
|
133
|
+
const [x, y] = (sessionStorage.getItem('wt-cursor') ?? '720,420').split(',').map(Number);
|
|
134
|
+
cursor.style.transition = 'none';
|
|
135
|
+
cursor.style.transform = `translate(${x}px,${y}px)`;
|
|
136
|
+
document.body.append(cursor);
|
|
137
|
+
requestAnimationFrame(() => { cursor.style.transition = ''; });
|
|
138
|
+
};
|
|
139
|
+
if (document.body) mount();
|
|
140
|
+
else document.addEventListener('DOMContentLoaded', mount);
|
|
141
|
+
}, { style: overlayStyle(this.theme), paper: this.theme.paper, ink: this.theme.ink });
|
|
142
|
+
|
|
143
|
+
cdp.on('Page.screencastFrame', (event) => {
|
|
144
|
+
void cdp.send('Page.screencastFrameAck', { sessionId: event.sessionId }).catch(() => undefined);
|
|
145
|
+
if (this.paused || !this.started) return;
|
|
146
|
+
const file = join(this.framesDir, `${String(this.frames.length).padStart(6, '0')}.jpg`);
|
|
147
|
+
writeFileSync(file, Buffer.from(event.data, 'base64'));
|
|
148
|
+
this.frames.push({ file, at: Date.now(), gapBefore: this.resumed });
|
|
149
|
+
this.resumed = false;
|
|
150
|
+
});
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
private async applyMetrics(): Promise<void> {
|
|
154
|
+
await this.cdp?.send('Emulation.setDeviceMetricsOverride', { width: this.view.width, height: this.view.height, deviceScaleFactor: this.scale, mobile: false });
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
/** Makes the page repaint, so the screencast emits a frame even when nothing on it changed. */
|
|
158
|
+
private async nudge(): Promise<void> {
|
|
159
|
+
await this.page.evaluate(() => document.body.style.setProperty('--wt-tick', String(Date.now()))).catch(() => undefined);
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
/** Starts recording. Reload the page first if it was open before `setup`, so the cursor layer is mounted. */
|
|
163
|
+
async start(): Promise<void> {
|
|
164
|
+
if (!this.cdp) await this.setup();
|
|
165
|
+
mkdirSync(this.framesDir, { recursive: true });
|
|
166
|
+
this.started = true;
|
|
167
|
+
this.paused = false;
|
|
168
|
+
await this.cdp?.send('Page.startScreencast', { format: 'jpeg', quality: this.quality, everyNthFrame: 1 });
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
/** Stops keeping frames: what happens until `resume` becomes a short cut in the video. */
|
|
172
|
+
pause(): void {
|
|
173
|
+
this.paused = true;
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
async resume(): Promise<void> {
|
|
177
|
+
this.paused = false;
|
|
178
|
+
this.resumed = true;
|
|
179
|
+
await this.nudge();
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
get isPaused(): boolean {
|
|
183
|
+
return this.paused;
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
/** Re-applies the recording size: another tab taking focus can leave the page rendered at another scale. */
|
|
187
|
+
async refresh(): Promise<void> {
|
|
188
|
+
await this.applyMetrics();
|
|
189
|
+
await this.nudge();
|
|
190
|
+
await this.page.waitForTimeout(300);
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
/** Ends the recording and writes the frame list and the chapters next to the frames. */
|
|
194
|
+
async stop(): Promise<WalkthroughRun> {
|
|
195
|
+
this.paused = true;
|
|
196
|
+
await this.cdp?.send('Page.stopScreencast').catch(() => undefined);
|
|
197
|
+
const durations = frameDurations(this.frames, Date.now());
|
|
198
|
+
const chapters = placeChapters(this.chapters, this.frames, durations);
|
|
199
|
+
mkdirSync(this.framesDir, { recursive: true });
|
|
200
|
+
const concatFile = join(this.framesDir, 'frames.ffconcat');
|
|
201
|
+
const chaptersFile = join(this.framesDir, 'chapters.json');
|
|
202
|
+
writeFileSync(concatFile, concatList(this.frames.map((frame) => basename(frame.file)), durations));
|
|
203
|
+
writeFileSync(chaptersFile, `${JSON.stringify(chapters, null, 2)}\n`);
|
|
204
|
+
return { outputDir: this.outputDir, framesDir: this.framesDir, concatFile, chaptersFile, frames: this.frames.length, chapters };
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
private mark(step: string | null, text: string, kind: Chapter['kind']): void {
|
|
208
|
+
this.chapters.push({ wall: Date.now(), at: 0, step, text, kind });
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
/** A caption at the bottom of the page, with an optional step chip, held for `holdMs` (default 3200). */
|
|
212
|
+
async caption(step: string | null, text: string, holdMs = 3200): Promise<void> {
|
|
213
|
+
this.mark(step, text, 'caption');
|
|
214
|
+
await this.page.evaluate(({ step, text }) => {
|
|
215
|
+
let el = document.getElementById('wt-caption');
|
|
216
|
+
if (!el) {
|
|
217
|
+
el = document.createElement('div');
|
|
218
|
+
el.id = 'wt-caption';
|
|
219
|
+
document.body.append(el);
|
|
220
|
+
}
|
|
221
|
+
el.classList.remove('show');
|
|
222
|
+
el.replaceChildren();
|
|
223
|
+
if (step) {
|
|
224
|
+
const chip = document.createElement('b');
|
|
225
|
+
chip.textContent = step;
|
|
226
|
+
el.append(chip);
|
|
227
|
+
}
|
|
228
|
+
const span = document.createElement('span');
|
|
229
|
+
span.textContent = text;
|
|
230
|
+
el.append(span);
|
|
231
|
+
requestAnimationFrame(() => el?.classList.add('show'));
|
|
232
|
+
}, { step, text });
|
|
233
|
+
await this.page.waitForTimeout(holdMs);
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
async hideCaption(): Promise<void> {
|
|
237
|
+
await this.page.evaluate(() => document.getElementById('wt-caption')?.classList.remove('show')).catch(() => undefined);
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
/** A full-screen title card. It starts a chapter of the video. */
|
|
241
|
+
async card(options: CardOptions): Promise<void> {
|
|
242
|
+
const { kicker, title, body = '', items = [], holdMs = 5000 } = options;
|
|
243
|
+
this.mark(kicker, title, 'card');
|
|
244
|
+
await this.page.evaluate(({ kicker, title, body, items }) => {
|
|
245
|
+
document.getElementById('wt-card')?.remove();
|
|
246
|
+
const el = document.createElement('div');
|
|
247
|
+
el.id = 'wt-card';
|
|
248
|
+
const k = document.createElement('div');
|
|
249
|
+
k.className = 'k';
|
|
250
|
+
k.textContent = kicker;
|
|
251
|
+
const h = document.createElement('h1');
|
|
252
|
+
h.textContent = title;
|
|
253
|
+
el.append(k, h);
|
|
254
|
+
if (body) {
|
|
255
|
+
const p = document.createElement('p');
|
|
256
|
+
p.textContent = body;
|
|
257
|
+
el.append(p);
|
|
258
|
+
}
|
|
259
|
+
if (items.length > 0) {
|
|
260
|
+
const list = document.createElement('ol');
|
|
261
|
+
items.forEach((text, index) => {
|
|
262
|
+
const li = document.createElement('li');
|
|
263
|
+
const number = document.createElement('span');
|
|
264
|
+
number.textContent = String(index + 1).padStart(2, '0');
|
|
265
|
+
li.append(number, document.createTextNode(text));
|
|
266
|
+
list.append(li);
|
|
267
|
+
});
|
|
268
|
+
el.append(list);
|
|
269
|
+
}
|
|
270
|
+
document.body.append(el);
|
|
271
|
+
requestAnimationFrame(() => el.classList.add('show'));
|
|
272
|
+
}, { kicker, title, body, items });
|
|
273
|
+
await this.page.waitForTimeout(holdMs);
|
|
274
|
+
await this.page.evaluate(() => {
|
|
275
|
+
const el = document.getElementById('wt-card');
|
|
276
|
+
if (!el) return;
|
|
277
|
+
el.classList.remove('show');
|
|
278
|
+
setTimeout(() => el.remove(), 600);
|
|
279
|
+
});
|
|
280
|
+
await this.page.waitForTimeout(700);
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
/** Scrolls smoothly to the top or until `target` is in the middle of the page. */
|
|
284
|
+
async scrollTo(target: Locator | 'top'): Promise<void> {
|
|
285
|
+
if (target === 'top') await this.page.evaluate(() => window.scrollTo({ top: 0, behavior: 'smooth' }));
|
|
286
|
+
else await target.evaluate((el) => el.scrollIntoView({ behavior: 'smooth', block: 'center' }));
|
|
287
|
+
await this.page.waitForTimeout(1000);
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
/** Glides the drawn cursor to a point of the page. */
|
|
291
|
+
async moveCursor(x: number, y: number): Promise<void> {
|
|
292
|
+
await this.page.evaluate(({ x, y }) => {
|
|
293
|
+
const cursor = document.getElementById('wt-cursor');
|
|
294
|
+
if (cursor) cursor.style.transform = `translate(${x}px,${y}px)`;
|
|
295
|
+
sessionStorage.setItem('wt-cursor', `${x},${y}`);
|
|
296
|
+
}, { x: Math.round(x), y: Math.round(y) });
|
|
297
|
+
await this.page.waitForTimeout(800);
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
/** A click ripple where the drawn cursor is. */
|
|
301
|
+
async ripple(): Promise<void> {
|
|
302
|
+
await this.page.evaluate(() => {
|
|
303
|
+
const [x, y] = (sessionStorage.getItem('wt-cursor') ?? '0,0').split(',').map(Number);
|
|
304
|
+
const ring = document.createElement('div');
|
|
305
|
+
ring.className = 'wt-ripple';
|
|
306
|
+
ring.style.left = `${x}px`;
|
|
307
|
+
ring.style.top = `${y}px`;
|
|
308
|
+
document.body.append(ring);
|
|
309
|
+
setTimeout(() => ring.remove(), 700);
|
|
310
|
+
});
|
|
311
|
+
}
|
|
312
|
+
|
|
313
|
+
/** Scrolls `target` into view if needed, glides the cursor over it, ripples, clicks. */
|
|
314
|
+
async press(target: Locator): Promise<void> {
|
|
315
|
+
await target.evaluate((el) => {
|
|
316
|
+
const box = el.getBoundingClientRect();
|
|
317
|
+
if (box.top < 90 || box.bottom > window.innerHeight - 120) el.scrollIntoView({ behavior: 'smooth', block: 'center' });
|
|
318
|
+
});
|
|
319
|
+
await this.page.waitForTimeout(700);
|
|
320
|
+
const box = await target.boundingBox();
|
|
321
|
+
if (box) await this.moveCursor(box.x + Math.min(box.width / 2, 60), box.y + box.height / 2);
|
|
322
|
+
await this.ripple();
|
|
323
|
+
await target.click();
|
|
324
|
+
await this.page.waitForTimeout(400);
|
|
325
|
+
}
|
|
326
|
+
|
|
327
|
+
/** Clicks a field and types into it at a human pace. */
|
|
328
|
+
async type(target: Locator, text: string, delayMs = 110): Promise<void> {
|
|
329
|
+
await this.press(target);
|
|
330
|
+
await target.fill('');
|
|
331
|
+
await target.pressSequentially(text, { delay: delayMs });
|
|
332
|
+
await this.page.waitForTimeout(500);
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
/** Clicks a `<select>` and picks an option by value. */
|
|
336
|
+
async choose(target: Locator, value: string): Promise<void> {
|
|
337
|
+
await this.press(target);
|
|
338
|
+
await target.selectOption(value);
|
|
339
|
+
await this.page.waitForTimeout(700);
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
/**
|
|
343
|
+
* Shows a wallet's prompt inside the recording, then lets the driver answer it.
|
|
344
|
+
*
|
|
345
|
+
* The screencast records this page only, and a wallet's prompt is a page of its own. While the
|
|
346
|
+
* prompt is open it is screenshotted a few times a second into an overlay drawn like an extension
|
|
347
|
+
* popup at the top right, so the viewer sees exactly what the wallet shows: the account, the
|
|
348
|
+
* network, the fee. The captions in `explain` run meanwhile, then the drawn cursor glides onto the
|
|
349
|
+
* mirrored button and the driver presses the real one.
|
|
350
|
+
*/
|
|
351
|
+
async walletPrompt(options: WalletPromptOptions): Promise<void> {
|
|
352
|
+
const { label, explain = [], explainHoldMs = 3800, promptSize = { width: 380, height: 620 } } = options;
|
|
353
|
+
const accent = options.accent ?? this.theme.accent;
|
|
354
|
+
let mirroring = false;
|
|
355
|
+
let loop: Promise<void> = Promise.resolve();
|
|
356
|
+
|
|
357
|
+
// The wallet's window takes focus the moment it opens: frames from then on are left out, until
|
|
358
|
+
// this page is back in front with the prompt mirrored on it.
|
|
359
|
+
const trigger = async () => {
|
|
360
|
+
await options.trigger();
|
|
361
|
+
this.pause();
|
|
362
|
+
};
|
|
363
|
+
|
|
364
|
+
const beforeAnswer: PromptHook = async (prompt, button) => {
|
|
365
|
+
await prompt.setViewportSize(promptSize).catch(() => undefined);
|
|
366
|
+
await button.waitFor({ state: 'visible', timeout: 60_000 });
|
|
367
|
+
await this.page.bringToFront();
|
|
368
|
+
await this.refresh();
|
|
369
|
+
await this.resume();
|
|
370
|
+
mirroring = true;
|
|
371
|
+
loop = (async () => {
|
|
372
|
+
while (mirroring) {
|
|
373
|
+
const png = await prompt.screenshot({ type: 'png' }).catch(() => null);
|
|
374
|
+
if (png) await this.showWalletFrame(png, label, accent, promptSize.width).catch(() => undefined);
|
|
375
|
+
await new Promise((resolve) => setTimeout(resolve, 280));
|
|
376
|
+
}
|
|
377
|
+
})();
|
|
378
|
+
await this.page.waitForTimeout(900);
|
|
379
|
+
for (const line of explain) await this.caption(label, line, explainHoldMs);
|
|
380
|
+
|
|
381
|
+
// The mirror is drawn at the prompt's own width, so the button is where the prompt has it.
|
|
382
|
+
const box = await button.boundingBox().catch(() => null);
|
|
383
|
+
if (box) {
|
|
384
|
+
const left = this.view.width - WALLET_OVERLAY.right - promptSize.width;
|
|
385
|
+
await this.moveCursor(left + box.x + box.width / 2, WALLET_OVERLAY.top + WALLET_OVERLAY.headerHeight + box.y + box.height / 2);
|
|
386
|
+
await this.ripple();
|
|
387
|
+
}
|
|
388
|
+
};
|
|
389
|
+
|
|
390
|
+
try {
|
|
391
|
+
await options.answer(trigger, beforeAnswer);
|
|
392
|
+
if (mirroring) await this.page.waitForTimeout(1200);
|
|
393
|
+
} finally {
|
|
394
|
+
mirroring = false;
|
|
395
|
+
await loop;
|
|
396
|
+
await this.hideWalletFrame();
|
|
397
|
+
// A prompt that never came leaves the recording paused by the trigger: pick it up again.
|
|
398
|
+
if (this.started && this.paused) {
|
|
399
|
+
await this.page.bringToFront().catch(() => undefined);
|
|
400
|
+
await this.refresh().catch(() => undefined);
|
|
401
|
+
await this.resume();
|
|
402
|
+
}
|
|
403
|
+
}
|
|
404
|
+
}
|
|
405
|
+
|
|
406
|
+
private async showWalletFrame(png: Buffer, label: string, accent: string, width: number): Promise<void> {
|
|
407
|
+
await this.page.evaluate(({ src, label, accent, width, right, top }) => {
|
|
408
|
+
let el = document.getElementById('wt-wallet');
|
|
409
|
+
if (!el) {
|
|
410
|
+
el = document.createElement('div');
|
|
411
|
+
el.id = 'wt-wallet';
|
|
412
|
+
el.style.right = `${right}px`;
|
|
413
|
+
el.style.top = `${top}px`;
|
|
414
|
+
el.style.width = `${width}px`;
|
|
415
|
+
const header = document.createElement('header');
|
|
416
|
+
const dot = document.createElement('i');
|
|
417
|
+
dot.style.background = accent;
|
|
418
|
+
header.append(dot, document.createTextNode(label));
|
|
419
|
+
const img = document.createElement('img');
|
|
420
|
+
el.append(header, img);
|
|
421
|
+
document.body.append(el);
|
|
422
|
+
requestAnimationFrame(() => el?.classList.add('show'));
|
|
423
|
+
}
|
|
424
|
+
(el.querySelector('img') as HTMLImageElement).src = src;
|
|
425
|
+
}, { src: `data:image/png;base64,${png.toString('base64')}`, label, accent, width, right: WALLET_OVERLAY.right, top: WALLET_OVERLAY.top });
|
|
426
|
+
}
|
|
427
|
+
|
|
428
|
+
private async hideWalletFrame(): Promise<void> {
|
|
429
|
+
const shown = await this.page.evaluate(() => {
|
|
430
|
+
const el = document.getElementById('wt-wallet');
|
|
431
|
+
if (!el) return false;
|
|
432
|
+
el.classList.remove('show');
|
|
433
|
+
setTimeout(() => el.remove(), 400);
|
|
434
|
+
return true;
|
|
435
|
+
}).catch(() => false);
|
|
436
|
+
if (shown) await this.page.waitForTimeout(450);
|
|
437
|
+
}
|
|
438
|
+
}
|
|
439
|
+
|
|
440
|
+
/** Creates a walkthrough for `page` and sets it up. Reload the page, then `start()`. */
|
|
441
|
+
export async function createWalkthrough(page: Page, options: WalkthroughOptions): Promise<Walkthrough> {
|
|
442
|
+
const walkthrough = new Walkthrough(page, options);
|
|
443
|
+
await walkthrough.setup();
|
|
444
|
+
return walkthrough;
|
|
445
|
+
}
|