@gnawbit/tabretake 0.4.1 → 0.5.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/CHANGELOG.md +103 -89
- package/README.md +82 -93
- package/README.zh-CN.md +72 -81
- package/SKILL.md +47 -67
- package/examples/site/style.css +81 -71
- package/examples/story.json +1 -2
- package/package.json +9 -1
- package/references/rendering.md +6 -1
- package/references/story.md +2 -1
- package/schemas/story.schema.json +632 -616
- package/scripts/capture.mjs +610 -0
- package/scripts/cli.mjs +26 -9
- package/scripts/demo.mjs +7 -1
- package/scripts/doctor.mjs +11 -3
- package/scripts/init.mjs +73 -25
- package/scripts/lib/browser.mjs +165 -13
- package/scripts/lib/captions.mjs +8 -3
- package/scripts/lib/options.mjs +71 -0
- package/scripts/lib/report.mjs +1 -0
- package/scripts/lib/system-browser.mjs +30 -0
- package/scripts/lib/themes.mjs +125 -0
- package/scripts/mark.mjs +503 -0
- package/scripts/plan.mjs +35 -35
- package/scripts/record.mjs +67 -40
- package/scripts/render.mjs +128 -55
- package/scripts/run.mjs +55 -0
package/scripts/doctor.mjs
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { existsSync } from "node:fs";
|
|
2
2
|
import { execFile } from "node:child_process";
|
|
3
3
|
import { promisify } from "node:util";
|
|
4
|
+
import { findSystemBrowser } from "./lib/system-browser.mjs";
|
|
4
5
|
import { findCjkFont, findFont } from "./lib/fonts.mjs";
|
|
5
6
|
import { parseCommand } from "./lib/options.mjs";
|
|
6
7
|
import { nodeRange, schemaVersion, supportsNodeVersion, version } from "./lib/metadata.mjs";
|
|
@@ -15,6 +16,7 @@ export async function diagnose({
|
|
|
15
16
|
exists = existsSync,
|
|
16
17
|
run = (file) => exec(file, ["-version"], { windowsHide: true, timeout: 10000 }),
|
|
17
18
|
fonts = { latin: findFont, cjk: findCjkFont },
|
|
19
|
+
systemBrowser = () => findSystemBrowser({ exists }),
|
|
18
20
|
} = {}) {
|
|
19
21
|
const checks = [];
|
|
20
22
|
async function check(id, required, hint, probe) {
|
|
@@ -37,11 +39,17 @@ export async function diagnose({
|
|
|
37
39
|
return (stdout || stderr).split("\n")[0].trim();
|
|
38
40
|
});
|
|
39
41
|
}
|
|
40
|
-
await check("chromium", true, "Run tabretake setup (
|
|
42
|
+
await check("chromium", true, "Run tabretake setup (or install Chrome/Edge for automatic fallback).", async () => {
|
|
41
43
|
const { chromium } = await load("playwright");
|
|
42
44
|
const file = chromium.executablePath();
|
|
43
|
-
if (
|
|
44
|
-
|
|
45
|
+
if (exists(file)) {
|
|
46
|
+
return "Executable found; browser launch and system libraries are not tested.";
|
|
47
|
+
}
|
|
48
|
+
const fallback = systemBrowser();
|
|
49
|
+
if (fallback) {
|
|
50
|
+
return `Using system ${fallback.channel} fallback; run tabretake setup for pinned Chromium.`;
|
|
51
|
+
}
|
|
52
|
+
throw new Error("Chromium executable not found and no Chrome/Edge fallback detected");
|
|
45
53
|
});
|
|
46
54
|
await check("sharp", true, reinstall, async () => {
|
|
47
55
|
const { default: sharp } = await load("sharp");
|
package/scripts/init.mjs
CHANGED
|
@@ -2,37 +2,76 @@ import { mkdir, writeFile } from "node:fs/promises";
|
|
|
2
2
|
import path from "node:path";
|
|
3
3
|
import { parseCommand } from "./lib/options.mjs";
|
|
4
4
|
import { createReporter } from "./lib/report.mjs";
|
|
5
|
+
import { resolveTheme } from "./lib/themes.mjs";
|
|
5
6
|
import { extractPlan } from "./plan.mjs";
|
|
6
7
|
|
|
8
|
+
function scoreElement(el) {
|
|
9
|
+
const hints = el._hints || {};
|
|
10
|
+
let score = 0;
|
|
11
|
+
if (hints.isDestructive) score -= 100;
|
|
12
|
+
if (hints.emptyHref) score -= 80;
|
|
13
|
+
if (hints.hiddenContainer) score -= 90;
|
|
14
|
+
if (hints.inNav) score -= 40;
|
|
15
|
+
if (hints.inMain) score += 30;
|
|
16
|
+
if (hints.inForm) score += 25;
|
|
17
|
+
if (hints.isPrimary) score += 45;
|
|
18
|
+
if (hints.isSearch && !hints.inForm) score -= 15;
|
|
19
|
+
if (el.tag === "button" || el.role === "button") score += 20;
|
|
20
|
+
if (el.tag === "input" || el.tag === "textarea" || el.role === "textbox") score += 25;
|
|
21
|
+
if (el.tag === "select") score += 22;
|
|
22
|
+
if (el.text || el.label) score += 5;
|
|
23
|
+
return score;
|
|
24
|
+
}
|
|
25
|
+
|
|
7
26
|
export function buildAutoSteps(elements) {
|
|
8
27
|
const steps = [{ action: "wait", ms: 600 }];
|
|
9
|
-
const interactive = (elements || [])
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
28
|
+
const interactive = (elements || [])
|
|
29
|
+
.filter((el) => {
|
|
30
|
+
if (el.type === "password" || el.type === "hidden" || el.type === "checkbox" || el.type === "radio") return false;
|
|
31
|
+
if (el._hints?.isDestructive || el._hints?.emptyHref) return false;
|
|
32
|
+
return Boolean(el.selector && (el.text || el.label || el.placeholder || el.name));
|
|
33
|
+
})
|
|
34
|
+
.map((el, index) => ({ el, index, score: scoreElement(el) }));
|
|
35
|
+
|
|
36
|
+
const isTextInput = (el) =>
|
|
37
|
+
el.tag === "input" || el.tag === "textarea" || el.role === "textbox";
|
|
38
|
+
const isSelect = (el) => el.tag === "select" && Array.isArray(el.options) &&
|
|
39
|
+
el.options.some((o) => !o.disabled && o.value !== "");
|
|
40
|
+
const isButton = (el) =>
|
|
41
|
+
el.tag === "button" || el.tag === "a" || el.role === "button" || el.role === "link";
|
|
13
42
|
|
|
14
|
-
const
|
|
15
|
-
const
|
|
43
|
+
const byScoreThenOrder = (a, b) => (b.score - a.score) || (a.index - b.index);
|
|
44
|
+
const inputs = interactive.filter(({ el }) => isTextInput(el)).sort(byScoreThenOrder);
|
|
45
|
+
const selects = interactive.filter(({ el }) => isSelect(el)).sort(byScoreThenOrder);
|
|
46
|
+
const buttons = interactive.filter(({ el }) => isButton(el)).sort(byScoreThenOrder);
|
|
16
47
|
|
|
17
48
|
const chosen = [];
|
|
18
|
-
|
|
19
|
-
chosen.
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
if (chosen.length >= 3) break;
|
|
23
|
-
if (!chosen.some((c) => c.selector === btn.selector)) {
|
|
24
|
-
chosen.push(btn);
|
|
49
|
+
const addChosen = (entry) => {
|
|
50
|
+
if (!entry || chosen.length >= 3) return;
|
|
51
|
+
if (!chosen.some((c) => c.el.selector === entry.el.selector)) {
|
|
52
|
+
chosen.push(entry);
|
|
25
53
|
}
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
// Prefer primary non-search inputs first; if only topbar search exists and a primary CTA exists, put the primary CTA first.
|
|
57
|
+
const primaryBtn = buttons.find(({ el }) => el._hints?.isPrimary && !el._hints?.inNav);
|
|
58
|
+
const nonSearchInput = inputs.find(({ el }) => !el._hints?.isSearch);
|
|
59
|
+
if (nonSearchInput) {
|
|
60
|
+
addChosen(nonSearchInput);
|
|
61
|
+
addChosen(selects[0]);
|
|
62
|
+
addChosen(primaryBtn || buttons[0]);
|
|
63
|
+
} else {
|
|
64
|
+
addChosen(primaryBtn);
|
|
65
|
+
addChosen(selects[0]);
|
|
66
|
+
addChosen(inputs[0]);
|
|
67
|
+
for (const btn of buttons) addChosen(btn);
|
|
26
68
|
}
|
|
27
|
-
for (const
|
|
28
|
-
|
|
29
|
-
if (!chosen.some((c) => c.selector === el.selector)) {
|
|
30
|
-
chosen.push(el);
|
|
31
|
-
}
|
|
69
|
+
for (const item of [...interactive].sort(byScoreThenOrder)) {
|
|
70
|
+
addChosen(item);
|
|
32
71
|
}
|
|
33
72
|
|
|
34
|
-
for (const el of chosen) {
|
|
35
|
-
if (el
|
|
73
|
+
for (const [idx, { el }] of chosen.sort((a, b) => a.index - b.index).entries()) {
|
|
74
|
+
if (isTextInput(el)) {
|
|
36
75
|
const caption = el.label || el.placeholder || (el.name ? `Enter ${el.name}` : "Input demo text");
|
|
37
76
|
steps.push({
|
|
38
77
|
action: "type",
|
|
@@ -41,6 +80,16 @@ export function buildAutoSteps(elements) {
|
|
|
41
80
|
pause: 400,
|
|
42
81
|
caption: `Input: ${caption}`,
|
|
43
82
|
});
|
|
83
|
+
} else if (isSelect(el)) {
|
|
84
|
+
const opt = el.options.find((o) => !o.disabled && o.value !== "") || el.options.find((o) => !o.disabled);
|
|
85
|
+
const caption = el.label || opt?.label || "Choose option";
|
|
86
|
+
steps.push({
|
|
87
|
+
action: "select",
|
|
88
|
+
selector: el.selector,
|
|
89
|
+
value: opt.value,
|
|
90
|
+
pause: 500,
|
|
91
|
+
caption: `Select: ${caption}`,
|
|
92
|
+
});
|
|
44
93
|
} else {
|
|
45
94
|
const caption = el.text || el.label || (el.role ? `${el.role}` : "Click target");
|
|
46
95
|
steps.push({
|
|
@@ -50,13 +99,11 @@ export function buildAutoSteps(elements) {
|
|
|
50
99
|
caption: `Click: ${caption}`,
|
|
51
100
|
});
|
|
52
101
|
}
|
|
53
|
-
steps.push({ action: "wait", ms: 600 });
|
|
102
|
+
steps.push({ action: "wait", ms: idx === chosen.length - 1 ? 1200 : 600 });
|
|
54
103
|
}
|
|
55
104
|
|
|
56
105
|
if (steps.length === 1) {
|
|
57
106
|
steps.push({ action: "wait", ms: 1500 });
|
|
58
|
-
} else {
|
|
59
|
-
steps.push({ action: "wait", ms: 1200 });
|
|
60
107
|
}
|
|
61
108
|
return steps;
|
|
62
109
|
}
|
|
@@ -80,7 +127,7 @@ export async function init(argv) {
|
|
|
80
127
|
if (values.auto) {
|
|
81
128
|
reporter.log("Scanning page to generate interactive steps (--auto)...");
|
|
82
129
|
try {
|
|
83
|
-
const { elements } = await extractPlan(url.href);
|
|
130
|
+
const { elements } = await extractPlan(url.href, { browser: values.browser, includeHints: true });
|
|
84
131
|
steps = buildAutoSteps(elements);
|
|
85
132
|
reporter.log(`Generated ${steps.length} step(s) from discovered interactive controls.`);
|
|
86
133
|
} catch (err) {
|
|
@@ -88,13 +135,14 @@ export async function init(argv) {
|
|
|
88
135
|
}
|
|
89
136
|
}
|
|
90
137
|
|
|
138
|
+
const theme = resolveTheme({}, values.preset);
|
|
91
139
|
const story = {
|
|
92
140
|
url: url.href,
|
|
93
141
|
displayUrl: url.host,
|
|
94
142
|
viewport: { width: 1440, height: 810 },
|
|
95
143
|
initialWait: 700,
|
|
96
144
|
tail: 1200,
|
|
97
|
-
brand: { background:
|
|
145
|
+
brand: { preset: theme.preset, background: theme.background, accent: theme.accent },
|
|
98
146
|
steps,
|
|
99
147
|
};
|
|
100
148
|
const outPath = path.resolve(values.out);
|
package/scripts/lib/browser.mjs
CHANGED
|
@@ -1,27 +1,77 @@
|
|
|
1
|
+
import { existsSync } from "node:fs";
|
|
2
|
+
import { chmod, copyFile, mkdir } from "node:fs/promises";
|
|
3
|
+
import path from "node:path";
|
|
1
4
|
import { chromium, errors } from "playwright";
|
|
5
|
+
import { FFMPEG } from "./ffmpeg.mjs";
|
|
6
|
+
import { navigate } from "./navigation.mjs";
|
|
7
|
+
import { VALID_BROWSERS, findSystemBrowser } from "./system-browser.mjs";
|
|
8
|
+
import { waitForEnter } from "./tty.mjs";
|
|
2
9
|
|
|
3
|
-
export
|
|
4
|
-
|
|
10
|
+
export { VALID_BROWSERS, findSystemBrowser };
|
|
11
|
+
|
|
12
|
+
export async function launchBrowser({
|
|
13
|
+
headless = true,
|
|
14
|
+
proxy,
|
|
15
|
+
browser = "auto",
|
|
16
|
+
scale = 1,
|
|
17
|
+
launch = (opts) => chromium.launch(opts),
|
|
18
|
+
hasBundledChromium = () => existsSync(chromium.executablePath()),
|
|
19
|
+
} = {}) {
|
|
20
|
+
const mode = browser || "auto";
|
|
21
|
+
if (!VALID_BROWSERS.has(mode)) {
|
|
22
|
+
throw new Error(`Unsupported --browser "${mode}". Choose auto, chromium, chrome or msedge.`);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
const args = ["--disable-blink-features=AutomationControlled"];
|
|
26
|
+
if (Number(scale) > 1) {
|
|
27
|
+
args.push(`--force-device-scale-factor=${Number(scale)}`);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
const base = {
|
|
5
31
|
headless,
|
|
6
32
|
proxy: proxy ? { server: proxy } : undefined,
|
|
7
|
-
args
|
|
8
|
-
}
|
|
33
|
+
args,
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
if (mode === "chrome" || mode === "msedge") {
|
|
37
|
+
return launch({ ...base, channel: mode });
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
if (mode === "chromium" || hasBundledChromium()) {
|
|
41
|
+
return launch(base);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
for (const channel of ["chrome", "msedge"]) {
|
|
45
|
+
try {
|
|
46
|
+
return await launch({ ...base, channel });
|
|
47
|
+
} catch {
|
|
48
|
+
// Continue trying next installed system browser channel.
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
return launch(base);
|
|
9
53
|
}
|
|
10
54
|
|
|
11
55
|
export async function newContext(browser, options) {
|
|
12
56
|
const {
|
|
13
57
|
viewport = { width: 1440, height: 810 },
|
|
58
|
+
scale = 1,
|
|
14
59
|
videoDir,
|
|
15
60
|
storageState,
|
|
16
61
|
} = options;
|
|
62
|
+
const deviceScaleFactor = Number(scale) || 1;
|
|
17
63
|
|
|
18
64
|
const context = await browser.newContext({
|
|
19
65
|
viewport,
|
|
66
|
+
deviceScaleFactor,
|
|
20
67
|
storageState: storageState || undefined,
|
|
21
68
|
recordVideo: videoDir
|
|
22
69
|
? {
|
|
23
70
|
dir: videoDir,
|
|
24
|
-
size: {
|
|
71
|
+
size: {
|
|
72
|
+
width: viewport.width * deviceScaleFactor,
|
|
73
|
+
height: viewport.height * deviceScaleFactor,
|
|
74
|
+
},
|
|
25
75
|
}
|
|
26
76
|
: undefined,
|
|
27
77
|
});
|
|
@@ -29,12 +79,48 @@ export async function newContext(browser, options) {
|
|
|
29
79
|
return context;
|
|
30
80
|
}
|
|
31
81
|
|
|
82
|
+
export async function openRecordedPage(
|
|
83
|
+
browser,
|
|
84
|
+
contextOptions,
|
|
85
|
+
{ ffmpegPath = FFMPEG, makeContext = (opts) => newContext(browser, opts) } = {},
|
|
86
|
+
) {
|
|
87
|
+
let context = await makeContext(contextOptions);
|
|
88
|
+
try {
|
|
89
|
+
const page = await context.newPage();
|
|
90
|
+
return { context, page };
|
|
91
|
+
} catch (error) {
|
|
92
|
+
const match = /Executable doesn't exist at ([^\r\n]+)/.exec(String(error?.message || ""));
|
|
93
|
+
if (!match || !/Video rendering requires ffmpeg binary/.test(error.message) || !ffmpegPath || !existsSync(ffmpegPath)) {
|
|
94
|
+
await context.close().catch(() => {});
|
|
95
|
+
throw error;
|
|
96
|
+
}
|
|
97
|
+
const targetPath = match[1].trim();
|
|
98
|
+
await context.close().catch(() => {});
|
|
99
|
+
await mkdir(path.dirname(targetPath), { recursive: true });
|
|
100
|
+
await copyFile(ffmpegPath, targetPath);
|
|
101
|
+
await chmod(targetPath, 0o755).catch(() => {});
|
|
102
|
+
context = await makeContext(contextOptions);
|
|
103
|
+
const page = await context.newPage();
|
|
104
|
+
return { context, page };
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
function accentRgba(hex, alpha) {
|
|
109
|
+
const clean = String(hex || "#6c5ce7").replace("#", "");
|
|
110
|
+
const full = clean.length === 3 ? clean.split("").map((c) => c + c).join("") : clean;
|
|
111
|
+
const n = Number.parseInt(full, 16);
|
|
112
|
+
if (!Number.isFinite(n)) return `rgba(108,92,231,${alpha})`;
|
|
113
|
+
return `rgba(${(n >> 16) & 255},${(n >> 8) & 255},${n & 255},${alpha})`;
|
|
114
|
+
}
|
|
115
|
+
|
|
32
116
|
/**
|
|
33
117
|
* Draws a fake cursor + click ripple on top of the page, so the recording
|
|
34
118
|
* looks like a human is using it.
|
|
35
119
|
*/
|
|
36
|
-
export async function installCursor(page) {
|
|
37
|
-
|
|
120
|
+
export async function installCursor(page, { accent = "#6c5ce7" } = {}) {
|
|
121
|
+
const rippleBg = accentRgba(accent, 0.35);
|
|
122
|
+
const rippleBorder = accentRgba(accent, 0.85);
|
|
123
|
+
await page.addInitScript(({ rippleBg, rippleBorder }) => {
|
|
38
124
|
if (window.__tabretakeCursorInstalled) return;
|
|
39
125
|
window.__tabretakeCursorInstalled = true;
|
|
40
126
|
|
|
@@ -46,7 +132,7 @@ export async function installCursor(page) {
|
|
|
46
132
|
}
|
|
47
133
|
#__tabretake_ripple {
|
|
48
134
|
position: fixed; width: 14px; height: 14px; border-radius: 50%;
|
|
49
|
-
background:
|
|
135
|
+
background: ${rippleBg}; border: 2px solid ${rippleBorder};
|
|
50
136
|
z-index: 2147483646; pointer-events: none;
|
|
51
137
|
transform: translate(-50%,-50%) scale(.2); opacity: 0;
|
|
52
138
|
}
|
|
@@ -54,7 +140,6 @@ export async function installCursor(page) {
|
|
|
54
140
|
|
|
55
141
|
const style = document.createElement("style");
|
|
56
142
|
style.textContent = CSS;
|
|
57
|
-
document.documentElement.appendChild(style);
|
|
58
143
|
|
|
59
144
|
const cursor = document.createElement("div");
|
|
60
145
|
cursor.id = "__tabretake_cursor";
|
|
@@ -63,18 +148,28 @@ export async function installCursor(page) {
|
|
|
63
148
|
<path d="M4 2L20.5 12.2L12.6 13.7L9.1 21.2L4 2Z"
|
|
64
149
|
fill="#fff" stroke="#111" stroke-width="1.4" stroke-linejoin="round"/>
|
|
65
150
|
</svg>`;
|
|
66
|
-
document.documentElement.appendChild(cursor);
|
|
67
151
|
|
|
68
152
|
const move = (x, y) => {
|
|
69
153
|
cursor.style.transform = `translate(${x - 2}px, ${y - 2}px)`;
|
|
70
154
|
};
|
|
71
155
|
|
|
156
|
+
const mount = () => {
|
|
157
|
+
if (!document.documentElement || document.getElementById("__tabretake_cursor")) return;
|
|
158
|
+
document.documentElement.appendChild(style);
|
|
159
|
+
document.documentElement.appendChild(cursor);
|
|
160
|
+
move(window.innerWidth / 2, window.innerHeight / 2);
|
|
161
|
+
};
|
|
162
|
+
if (document.documentElement) mount();
|
|
163
|
+
window.addEventListener("DOMContentLoaded", mount, { once: true });
|
|
164
|
+
|
|
72
165
|
const ripple = (x, y) => {
|
|
166
|
+
const root = document.body || document.documentElement;
|
|
167
|
+
if (!root) return;
|
|
73
168
|
const r = document.createElement("div");
|
|
74
169
|
r.id = "__tabretake_ripple";
|
|
75
170
|
r.style.left = `${x}px`;
|
|
76
171
|
r.style.top = `${y}px`;
|
|
77
|
-
|
|
172
|
+
root.appendChild(r);
|
|
78
173
|
r.animate(
|
|
79
174
|
[
|
|
80
175
|
{ transform: "translate(-50%,-50%) scale(.2)", opacity: 1 },
|
|
@@ -87,8 +182,7 @@ export async function installCursor(page) {
|
|
|
87
182
|
|
|
88
183
|
window.addEventListener("mousemove", (e) => move(e.clientX, e.clientY), true);
|
|
89
184
|
window.addEventListener("mousedown", (e) => ripple(e.clientX, e.clientY), true);
|
|
90
|
-
|
|
91
|
-
});
|
|
185
|
+
}, { rippleBg, rippleBorder });
|
|
92
186
|
}
|
|
93
187
|
|
|
94
188
|
export async function resolveLocator(page, step) {
|
|
@@ -196,6 +290,42 @@ export async function dragBy(page, locator, dx, dy = 0, steps = 24, pause = 600)
|
|
|
196
290
|
return { x: endX, y: endY };
|
|
197
291
|
}
|
|
198
292
|
|
|
293
|
+
export async function selectOption(page, locator, value, pause = 500, timeout = 20000) {
|
|
294
|
+
const deadline = Date.now() + timeout;
|
|
295
|
+
const remaining = () => {
|
|
296
|
+
const ms = deadline - Date.now();
|
|
297
|
+
if (ms <= 0) throw new errors.TimeoutError("Timed out waiting for an actionable select target.");
|
|
298
|
+
return ms;
|
|
299
|
+
};
|
|
300
|
+
await locator.waitFor({ state: "visible", timeout: remaining() });
|
|
301
|
+
await locator.scrollIntoViewIfNeeded({ timeout: remaining() });
|
|
302
|
+
const box = await locator.boundingBox({ timeout: remaining() });
|
|
303
|
+
if (!box) throw new Error("Element has no bounding box");
|
|
304
|
+
|
|
305
|
+
const x = box.x + box.width / 2;
|
|
306
|
+
const y = box.y + box.height / 2;
|
|
307
|
+
await page.mouse.move(x, y, { steps: 16 });
|
|
308
|
+
await page.waitForTimeout(100);
|
|
309
|
+
|
|
310
|
+
const target = String(value ?? "");
|
|
311
|
+
const matched = await locator.evaluate((el, raw) => {
|
|
312
|
+
if (el.tagName !== "SELECT") return null;
|
|
313
|
+
const opts = [...el.options];
|
|
314
|
+
const byValue = opts.find((o) => o.value === raw);
|
|
315
|
+
if (byValue) return { value: byValue.value };
|
|
316
|
+
const byLabel = opts.find((o) => (o.label || o.textContent || "").trim() === raw);
|
|
317
|
+
if (byLabel) return { value: byLabel.value };
|
|
318
|
+
return null;
|
|
319
|
+
}, target);
|
|
320
|
+
|
|
321
|
+
if (!matched) {
|
|
322
|
+
throw Object.assign(new Error("Could not find a matching option for select step."), { code: "TARGET_NOT_FOUND" });
|
|
323
|
+
}
|
|
324
|
+
await locator.selectOption(matched, { timeout: remaining() });
|
|
325
|
+
await page.waitForTimeout(pause);
|
|
326
|
+
return { x, y };
|
|
327
|
+
}
|
|
328
|
+
|
|
199
329
|
export async function hover(page, locator, pause = 500) {
|
|
200
330
|
await locator.scrollIntoViewIfNeeded();
|
|
201
331
|
const box = await locator.boundingBox();
|
|
@@ -206,3 +336,25 @@ export async function hover(page, locator, pause = 500) {
|
|
|
206
336
|
await page.waitForTimeout(pause);
|
|
207
337
|
return { x, y };
|
|
208
338
|
}
|
|
339
|
+
|
|
340
|
+
export async function loginAndSave(url, {
|
|
341
|
+
proxy,
|
|
342
|
+
viewport,
|
|
343
|
+
browser: browserMode,
|
|
344
|
+
statePath = path.resolve("auth.json"),
|
|
345
|
+
} = {}) {
|
|
346
|
+
const browser = await launchBrowser({ headless: false, proxy, browser: browserMode });
|
|
347
|
+
try {
|
|
348
|
+
const context = await newContext(browser, { viewport });
|
|
349
|
+
const page = await context.newPage();
|
|
350
|
+
await navigate(page, url, { allowAuthErrors: true });
|
|
351
|
+
console.log("\nA browser window is open. Log in, then come back here.");
|
|
352
|
+
await waitForEnter("Press Enter when you are logged in... ");
|
|
353
|
+
await context.storageState({ path: statePath });
|
|
354
|
+
console.log(`Saved login state to ${statePath}\n`);
|
|
355
|
+
await context.close();
|
|
356
|
+
return statePath;
|
|
357
|
+
} finally {
|
|
358
|
+
await browser.close();
|
|
359
|
+
}
|
|
360
|
+
}
|
package/scripts/lib/captions.mjs
CHANGED
|
@@ -5,8 +5,13 @@ import { hasCjk } from "./fonts.mjs";
|
|
|
5
5
|
const escapeMarkup = (text) => String(text).replace(/&/g, "&")
|
|
6
6
|
.replace(/</g, "<").replace(/>/g, ">");
|
|
7
7
|
|
|
8
|
-
export async function createCaptionLayers({
|
|
8
|
+
export async function createCaptionLayers({
|
|
9
|
+
events, durationSec, font, cjkFont, layout, assetsDir,
|
|
10
|
+
theme = { caption: { fg: "white", bg: { r: 11, g: 15, b: 25, alpha: 0.74 } } },
|
|
11
|
+
}) {
|
|
9
12
|
if (!font) return [];
|
|
13
|
+
const fg = theme?.caption?.fg || "white";
|
|
14
|
+
const bg = theme?.caption?.bg || { r: 11, g: 15, b: 25, alpha: 0.74 };
|
|
10
15
|
const captions = events.map((event) => ({ text: String(event.caption || ""), start: (event.tEnd ?? event.t) / 1000 }))
|
|
11
16
|
.filter(({ text, start }) => text.trim() && Number.isFinite(start) && start >= 0 && start < durationSec)
|
|
12
17
|
.sort((a, b) => a.start - b.start);
|
|
@@ -17,7 +22,7 @@ export async function createCaptionLayers({ events, durationSec, font, cjkFont,
|
|
|
17
22
|
const end = Math.min(captions[i + 1]?.start ?? durationSec, start + 2.8, durationSec);
|
|
18
23
|
if (end <= start) continue;
|
|
19
24
|
const options = {
|
|
20
|
-
text: `<span foreground="
|
|
25
|
+
text: `<span foreground="${fg}">${escapeMarkup(text)}</span>`,
|
|
21
26
|
font: `sans-serif ${size}`, fontfile: hasCjk(text) ? cjkFont || font : font,
|
|
22
27
|
width: width - box * 2, align: "centre", rgba: true, wrap: "word-char",
|
|
23
28
|
};
|
|
@@ -31,7 +36,7 @@ export async function createCaptionLayers({ events, durationSec, font, cjkFont,
|
|
|
31
36
|
const file = path.join(assetsDir, `caption-${layout.key}-${i}.png`);
|
|
32
37
|
const info = await sharp({ create: {
|
|
33
38
|
width: rendered.info.width + box * 2, height: rendered.info.height + box * 2,
|
|
34
|
-
channels: 4, background:
|
|
39
|
+
channels: 4, background: bg,
|
|
35
40
|
} }).composite([{ input: rendered.data, top: box, left: box }]).png().toFile(file);
|
|
36
41
|
layers.push({ file, start, end, x: Math.floor((layout.width - info.width) / 2), y });
|
|
37
42
|
}
|
package/scripts/lib/options.mjs
CHANGED
|
@@ -7,6 +7,8 @@ export const commandOptions = {
|
|
|
7
7
|
url: { type: "string" },
|
|
8
8
|
out: { type: "string", default: "demo.json" },
|
|
9
9
|
auto: { type: "boolean", default: false },
|
|
10
|
+
browser: { type: "string", default: "auto" },
|
|
11
|
+
preset: { type: "string" },
|
|
10
12
|
},
|
|
11
13
|
plan: {
|
|
12
14
|
json: { type: "boolean", default: false },
|
|
@@ -17,6 +19,7 @@ export const commandOptions = {
|
|
|
17
19
|
proxy: { type: "string" },
|
|
18
20
|
viewport: { type: "string" },
|
|
19
21
|
headless: { type: "boolean", default: true },
|
|
22
|
+
browser: { type: "string", default: "auto" },
|
|
20
23
|
},
|
|
21
24
|
record: {
|
|
22
25
|
json: { type: "boolean", default: false },
|
|
@@ -29,6 +32,62 @@ export const commandOptions = {
|
|
|
29
32
|
proxy: { type: "string" },
|
|
30
33
|
viewport: { type: "string" },
|
|
31
34
|
headed: { type: "boolean", default: false },
|
|
35
|
+
browser: { type: "string", default: "auto" },
|
|
36
|
+
scale: { type: "string" },
|
|
37
|
+
},
|
|
38
|
+
run: {
|
|
39
|
+
json: { type: "boolean", default: false },
|
|
40
|
+
config: { type: "string" },
|
|
41
|
+
url: { type: "string" },
|
|
42
|
+
out: { type: "string" },
|
|
43
|
+
login: { type: "boolean", default: false },
|
|
44
|
+
"storage-state": { type: "string" },
|
|
45
|
+
"save-state": { type: "string" },
|
|
46
|
+
proxy: { type: "string" },
|
|
47
|
+
viewport: { type: "string" },
|
|
48
|
+
headed: { type: "boolean", default: false },
|
|
49
|
+
browser: { type: "string", default: "auto" },
|
|
50
|
+
scale: { type: "string" },
|
|
51
|
+
"no-gif": { type: "boolean", default: false },
|
|
52
|
+
"no-captions": { type: "boolean", default: false },
|
|
53
|
+
vertical: { type: "boolean", default: false },
|
|
54
|
+
zoom: { type: "string", default: "1.28" },
|
|
55
|
+
"vertical-zoom": { type: "string" },
|
|
56
|
+
"zoom-duration": { type: "string" },
|
|
57
|
+
"poster-at": { type: "string" },
|
|
58
|
+
preset: { type: "string" },
|
|
59
|
+
},
|
|
60
|
+
capture: {
|
|
61
|
+
json: { type: "boolean", default: false },
|
|
62
|
+
url: { type: "string" },
|
|
63
|
+
out: { type: "string", default: "demo.json" },
|
|
64
|
+
login: { type: "boolean", default: false },
|
|
65
|
+
"storage-state": { type: "string" },
|
|
66
|
+
"save-state": { type: "string" },
|
|
67
|
+
proxy: { type: "string" },
|
|
68
|
+
viewport: { type: "string" },
|
|
69
|
+
browser: { type: "string", default: "auto" },
|
|
70
|
+
preset: { type: "string" },
|
|
71
|
+
scale: { type: "string" },
|
|
72
|
+
force: { type: "boolean", default: false },
|
|
73
|
+
run: { type: "boolean", default: false },
|
|
74
|
+
"media-out": { type: "string" },
|
|
75
|
+
"no-gif": { type: "boolean", default: false },
|
|
76
|
+
"no-captions": { type: "boolean", default: false },
|
|
77
|
+
vertical: { type: "boolean", default: false },
|
|
78
|
+
zoom: { type: "string", default: "1.28" },
|
|
79
|
+
"vertical-zoom": { type: "string" },
|
|
80
|
+
"zoom-duration": { type: "string" },
|
|
81
|
+
"poster-at": { type: "string" },
|
|
82
|
+
},
|
|
83
|
+
mark: {
|
|
84
|
+
json: { type: "boolean", default: false },
|
|
85
|
+
video: { type: "string" },
|
|
86
|
+
out: { type: "string", default: "story-video.json" },
|
|
87
|
+
preset: { type: "string" },
|
|
88
|
+
port: { type: "string", default: "0" },
|
|
89
|
+
"media-out": { type: "string" },
|
|
90
|
+
"no-open": { type: "boolean", default: false },
|
|
32
91
|
},
|
|
33
92
|
render: {
|
|
34
93
|
json: { type: "boolean", default: false },
|
|
@@ -42,6 +101,7 @@ export const commandOptions = {
|
|
|
42
101
|
"vertical-zoom": { type: "string" },
|
|
43
102
|
"zoom-duration": { type: "string" },
|
|
44
103
|
"poster-at": { type: "string" },
|
|
104
|
+
preset: { type: "string" },
|
|
45
105
|
},
|
|
46
106
|
serve: {
|
|
47
107
|
port: { type: "string", default: "4173" },
|
|
@@ -51,6 +111,8 @@ export const commandOptions = {
|
|
|
51
111
|
out: { type: "string", default: "demo" },
|
|
52
112
|
port: { type: "string", default: "4173" },
|
|
53
113
|
story: { type: "string", default: "onboarding" },
|
|
114
|
+
browser: { type: "string", default: "auto" },
|
|
115
|
+
preset: { type: "string" },
|
|
54
116
|
},
|
|
55
117
|
doctor: { json: { type: "boolean", default: false } },
|
|
56
118
|
capabilities: { json: { type: "boolean", default: false } },
|
|
@@ -65,3 +127,12 @@ commandOptions.preview = {
|
|
|
65
127
|
export function parseCommand(command, argv) {
|
|
66
128
|
return parseArgs({ args: argv, options: commandOptions[command], strict: true, allowPositionals: false });
|
|
67
129
|
}
|
|
130
|
+
|
|
131
|
+
export function parseViewport(value, fallback = { width: 1440, height: 810 }) {
|
|
132
|
+
if (value === undefined) return fallback;
|
|
133
|
+
if (!/^[1-9]\d*x[1-9]\d*$/.test(value)) {
|
|
134
|
+
throw new Error("--viewport must be WIDTHxHEIGHT with positive integers");
|
|
135
|
+
}
|
|
136
|
+
const [width, height] = value.split("x").map(Number);
|
|
137
|
+
return { width, height };
|
|
138
|
+
}
|
package/scripts/lib/report.mjs
CHANGED
|
@@ -17,6 +17,7 @@ export function requireInteractiveLogin(values) {
|
|
|
17
17
|
export function createReporter(command, json) {
|
|
18
18
|
return {
|
|
19
19
|
log: (...args) => (json ? console.error : console.log)(...args),
|
|
20
|
+
warn: (...args) => console.error(...args),
|
|
20
21
|
async success({ artifacts, ...details }) {
|
|
21
22
|
if (!json) return;
|
|
22
23
|
const files = await Promise.all(artifacts.map(async (artifact) => {
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { existsSync } from "node:fs";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
|
|
4
|
+
export const VALID_BROWSERS = new Set(["auto", "chromium", "chrome", "msedge"]);
|
|
5
|
+
|
|
6
|
+
export function findSystemBrowser({
|
|
7
|
+
exists = existsSync,
|
|
8
|
+
platform = process.platform,
|
|
9
|
+
env = process.env,
|
|
10
|
+
} = {}) {
|
|
11
|
+
const candidates = [];
|
|
12
|
+
if (platform === "win32") {
|
|
13
|
+
const roots = [env.PROGRAMFILES, env["PROGRAMFILES(X86)"], env.LOCALAPPDATA].filter(Boolean);
|
|
14
|
+
for (const root of roots) {
|
|
15
|
+
candidates.push({ channel: "chrome", path: path.join(root, "Google", "Chrome", "Application", "chrome.exe") });
|
|
16
|
+
candidates.push({ channel: "msedge", path: path.join(root, "Microsoft", "Edge", "Application", "msedge.exe") });
|
|
17
|
+
}
|
|
18
|
+
} else if (platform === "darwin") {
|
|
19
|
+
candidates.push({ channel: "chrome", path: "/Applications/Google Chrome.app/Contents/MacOS/Google Chrome" });
|
|
20
|
+
candidates.push({ channel: "msedge", path: "/Applications/Microsoft Edge.app/Contents/MacOS/Microsoft Edge" });
|
|
21
|
+
} else {
|
|
22
|
+
for (const bin of ["/usr/bin/google-chrome", "/usr/bin/google-chrome-stable", "/opt/google/chrome/chrome"]) {
|
|
23
|
+
candidates.push({ channel: "chrome", path: bin });
|
|
24
|
+
}
|
|
25
|
+
for (const bin of ["/usr/bin/microsoft-edge", "/usr/bin/microsoft-edge-stable"]) {
|
|
26
|
+
candidates.push({ channel: "msedge", path: bin });
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
return candidates.find((candidate) => exists(candidate.path)) || null;
|
|
30
|
+
}
|