effect-browser 0.3.0-beta.23 → 0.3.0-beta.24
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/README.md +124 -5
- package/dist/ActionPort.d.mts +1 -1
- package/dist/ActionPort.mjs +1 -1
- package/dist/{Browser-BKPAbyys.mjs → Browser-CAQFhTkM.mjs} +559 -333
- package/dist/Browser-CAQFhTkM.mjs.map +1 -0
- package/dist/{Browser-wZhwZSlG.d.mts → Browser-DGR6ZEYo.d.mts} +9 -3
- package/dist/Browser.d.mts +1 -1
- package/dist/Browser.mjs +1 -1
- package/dist/{BrowserEvent-lLqJ3GJS.d.mts → BrowserEvent-C2p-NwuU.d.mts} +30 -6
- package/dist/BrowserEvent.d.mts +2 -2
- package/dist/BrowserEvent.mjs +20 -1
- package/dist/BrowserEvent.mjs.map +1 -1
- package/dist/{Change-Ba7INmay.d.mts → Change-CxT1mp2e.d.mts} +13 -2
- package/dist/Change.d.mts +1 -1
- package/dist/Change.mjs +7 -1
- package/dist/Change.mjs.map +1 -1
- package/dist/Clock-Du5nP-nK.d.mts +57 -0
- package/dist/Clock.d.mts +2 -57
- package/dist/Layout.d.mts +2 -0
- package/dist/Layout.mjs +81 -0
- package/dist/Layout.mjs.map +1 -0
- package/dist/Matching-MtcWZ_hA.d.mts +266 -0
- package/dist/Matching.d.mts +2 -37
- package/dist/Matching.mjs +22 -17
- package/dist/Matching.mjs.map +1 -1
- package/dist/Overlay-CoGw5tL_.d.mts +27 -0
- package/dist/Overlay.d.mts +2 -0
- package/dist/Overlay.mjs +30 -0
- package/dist/Overlay.mjs.map +1 -0
- package/dist/{Page-BF5izr8R.mjs → Page-BaAOshxo.mjs} +3 -1
- package/dist/Page-BaAOshxo.mjs.map +1 -0
- package/dist/{Page-CvsOEorc.d.mts → Page-CLzh5E9w.d.mts} +158 -6
- package/dist/Page.d.mts +2 -2
- package/dist/Page.mjs +1 -1
- package/dist/PageTime-68UPUZpZ.d.mts +29 -0
- package/dist/PageTime.d.mts +2 -0
- package/dist/PageTime.mjs +7 -0
- package/dist/PageTime.mjs.map +1 -0
- package/dist/Pose-2qqGzPvb.d.mts +32 -0
- package/dist/Pose.d.mts +2 -0
- package/dist/Pose.mjs +44 -0
- package/dist/Pose.mjs.map +1 -0
- package/dist/Site.d.mts +714 -0
- package/dist/Site.mjs +102 -0
- package/dist/Site.mjs.map +1 -0
- package/dist/{Stillness-CNlvayIo.mjs → Stillness-BwP0RdI4.mjs} +27 -6
- package/dist/Stillness-BwP0RdI4.mjs.map +1 -0
- package/dist/{Style-AO4V7Czt.mjs → Style-T3ezDYLI.mjs} +25 -2
- package/dist/{Style-AO4V7Czt.mjs.map → Style-T3ezDYLI.mjs.map} +1 -1
- package/dist/Style.d.mts +15 -2
- package/dist/Style.mjs +2 -2
- package/dist/Url-B7P-TWwL.d.mts +26 -0
- package/dist/Url.d.mts +2 -26
- package/dist/{Window-CMddGMt1.d.mts → Window-BDa8MtNP.d.mts} +4 -4
- package/dist/Window.d.mts +1 -1
- package/dist/{connection-CK83pvqP.mjs → connection-C_jWYIwe.mjs} +2 -2
- package/dist/{connection-CK83pvqP.mjs.map → connection-C_jWYIwe.mjs.map} +1 -1
- package/dist/{connection-2KHI4_KN.d.mts → connection-Db-YtlbE.d.mts} +2 -2
- package/dist/film/Film.d.mts +265 -0
- package/dist/film/Film.mjs +246 -0
- package/dist/film/Film.mjs.map +1 -0
- package/dist/fold.inpage-CyVZQHZL.d.mts +49 -0
- package/dist/index.d.mts +11 -8
- package/dist/index.mjs +8 -4
- package/dist/moment/Moment.d.mts +5 -5
- package/dist/plan/index.d.mts +2 -2
- package/dist/plan/index.mjs +2 -2
- package/dist/policy/Policy.d.mts +1 -1
- package/dist/policy/Policy.mjs +1 -1
- package/dist/pose.inpage-CLq7Mfdr.mjs +2042 -0
- package/dist/pose.inpage-CLq7Mfdr.mjs.map +1 -0
- package/dist/presentation/Presentation.d.mts +8 -1
- package/dist/presentation/Presentation.mjs +3 -1
- package/dist/presentation/Presentation.mjs.map +1 -1
- package/dist/presentation/Stage.d.mts +1 -1
- package/dist/providers/Cdp.d.mts +2 -2
- package/dist/providers/Cdp.mjs +1 -1
- package/dist/providers/Chromium.d.mts +1 -1
- package/dist/providers/Chromium.mjs +1 -1
- package/dist/stillness/Stillness.d.mts +1 -1
- package/dist/stillness/Stillness.mjs +1 -1
- package/dist/testing/index.d.mts +4 -30
- package/dist/testing/index.mjs +8 -2
- package/dist/testing/index.mjs.map +1 -1
- package/dist/workerd/Cdp.d.mts +2 -2
- package/dist/workerd/Cdp.mjs +1 -1
- package/package.json +32 -2
- package/src/Browser.ts +11 -6
- package/src/BrowserEvent.ts +19 -0
- package/src/Change.ts +7 -1
- package/src/Layout.ts +116 -0
- package/src/Matching.ts +24 -15
- package/src/Overlay.ts +37 -0
- package/src/Page.ts +45 -0
- package/src/PageTime.ts +47 -0
- package/src/Pose.ts +50 -0
- package/src/Site.ts +124 -0
- package/src/Style.ts +36 -0
- package/src/film/Film.ts +384 -0
- package/src/index.ts +4 -0
- package/src/internal/input/perform.ts +31 -3
- package/src/internal/input/pointer.ts +7 -4
- package/src/internal/input/targets.ts +1 -1
- package/src/internal/page/bridge.ts +65 -33
- package/src/internal/page/clock.inpage.ts +306 -0
- package/src/internal/page/compose.inpage.ts +101 -0
- package/src/internal/page/context.ts +2 -0
- package/src/internal/page/overlay.inpage.ts +96 -0
- package/src/internal/page/overlay.ts +89 -0
- package/src/internal/page/page.ts +12 -0
- package/src/internal/page/pose.inpage.ts +200 -0
- package/src/internal/page/pose.ts +80 -0
- package/src/internal/page/time.inpage.ts +49 -0
- package/src/internal/page/time.ts +122 -0
- package/src/internal/page/viewport.ts +3 -2
- package/src/internal/pictures/pictures.ts +93 -32
- package/src/internal/reading/layout.inpage.ts +193 -0
- package/src/internal/reading/layout.ts +101 -0
- package/src/internal/reading/names.inpage.ts +15 -0
- package/src/internal/reading/outline.inpage.ts +2 -18
- package/src/internal/reading/reading.ts +22 -2
- package/src/internal/reading/ready.inpage.ts +18 -23
- package/src/internal/reading/ready.ts +23 -3
- package/src/internal/reading/text.inpage.ts +1 -14
- package/src/internal/timeline/changes.inpage.ts +7 -15
- package/src/internal/timeline/changes.ts +14 -1
- package/src/internal/timeline/record.inpage.ts +15 -6
- package/src/presentation/Presentation.ts +13 -0
- package/src/stillness/Pixels.ts +26 -0
- package/src/stillness/Stillness.ts +30 -5
- package/src/testing/page.ts +5 -0
- package/dist/Browser-BKPAbyys.mjs.map +0 -1
- package/dist/Page-BF5izr8R.mjs.map +0 -1
- package/dist/Stillness-CNlvayIo.mjs.map +0 -1
|
@@ -4,14 +4,14 @@
|
|
|
4
4
|
* one call where a device pixel is a CSS pixel and nothing is cropped, and two otherwise, unless
|
|
5
5
|
* Playwright emulates the viewport. The page's first clipped picture also holds the screen it reads.
|
|
6
6
|
*/
|
|
7
|
-
import { Duration, Effect, Option, Result, Schedule, Schema } from "effect";
|
|
7
|
+
import { Duration, Effect, Option, Result, Schedule, Schema, type Scope } from "effect";
|
|
8
8
|
import { Base64 } from "effect/encoding";
|
|
9
9
|
|
|
10
10
|
import { BrowserError, Failed, InvalidRequest } from "../../BrowserError.ts";
|
|
11
11
|
import * as BrowserClock from "../../Clock.ts";
|
|
12
12
|
import { Frame, Image, Screenshot } from "../../Frame.ts";
|
|
13
13
|
import type { Point } from "../../Geometry.ts";
|
|
14
|
-
import { type FrameOptions, Region, type ScreenshotOptions, Zoom } from "../../Page.ts";
|
|
14
|
+
import { type FrameOptions, type Paint, Region, type ScreenshotOptions, Zoom } from "../../Page.ts";
|
|
15
15
|
import { type Bridge, scriptCall } from "../page/bridge.ts";
|
|
16
16
|
import {
|
|
17
17
|
decodeWith,
|
|
@@ -163,8 +163,80 @@ const calibrator = (page: PageContext, bridge: Bridge) => {
|
|
|
163
163
|
);
|
|
164
164
|
};
|
|
165
165
|
|
|
166
|
-
// The screen as the page reads it.
|
|
167
|
-
const ScreenSize = Schema.Struct({ width: Schema.Int, height: Schema.Int });
|
|
166
|
+
// The screen as the page reads it, and its device pixels to a CSS pixel.
|
|
167
|
+
const ScreenSize = Schema.Struct({ width: Schema.Int, height: Schema.Int, ratio: Schema.Finite });
|
|
168
|
+
|
|
169
|
+
// The device as the page's own session emulates it: the screen held for crops (see `camera`), and
|
|
170
|
+
// the device scale factor a paint asks for, 0 for the device's own, in one override that keeps the
|
|
171
|
+
// view's size. Playwright emulates the viewport of a browser this library launched, at the scale of
|
|
172
|
+
// its launch, and puts that back over any other, so there a paint keeps that scale.
|
|
173
|
+
const device = (page: PageContext, bridge: Bridge) => {
|
|
174
|
+
const { playwright, native, within } = page;
|
|
175
|
+
const { send } = page.protocol;
|
|
176
|
+
let screen: Size | undefined;
|
|
177
|
+
let scaled = 0;
|
|
178
|
+
let painting = false;
|
|
179
|
+
|
|
180
|
+
const metrics = (operation: string) =>
|
|
181
|
+
native(operation, () =>
|
|
182
|
+
send("Emulation.setDeviceMetricsOverride", {
|
|
183
|
+
width: 0,
|
|
184
|
+
height: 0,
|
|
185
|
+
deviceScaleFactor: scaled,
|
|
186
|
+
mobile: false,
|
|
187
|
+
...(screen === undefined ? {} : { screenWidth: screen.width, screenHeight: screen.height }),
|
|
188
|
+
}),
|
|
189
|
+
);
|
|
190
|
+
|
|
191
|
+
const screenOf = (operation: string) =>
|
|
192
|
+
bridge
|
|
193
|
+
.evaluate(operation, scriptCall("screenSize"))
|
|
194
|
+
.pipe(Effect.flatMap(decodeWith(operation, ScreenSize)));
|
|
195
|
+
|
|
196
|
+
// The copy has the screen alone: the view keeps its own size, and the device its pixel ratio.
|
|
197
|
+
const holdScreen = Effect.fnUntraced(function* (operation: string) {
|
|
198
|
+
if (screen !== undefined) return;
|
|
199
|
+
const { width, height } = yield* screenOf(operation);
|
|
200
|
+
|
|
201
|
+
screen = { width, height };
|
|
202
|
+
yield* metrics(operation).pipe(Effect.onError(() => Effect.sync(() => (screen = undefined))));
|
|
203
|
+
});
|
|
204
|
+
|
|
205
|
+
const scrollbarsHidden = (hidden: boolean) =>
|
|
206
|
+
native("paint", () => send("Emulation.setScrollbarsHidden", { hidden }));
|
|
207
|
+
|
|
208
|
+
const paint = (options: Paint): Effect.Effect<void, BrowserError, Scope.Scope> =>
|
|
209
|
+
Effect.gen(function* () {
|
|
210
|
+
const { scale, scrollbars = true } = options;
|
|
211
|
+
const launched = playwright.viewportSize() !== null;
|
|
212
|
+
|
|
213
|
+
if (scale !== undefined && !(Number.isFinite(scale) && scale > 0 && scale <= 4))
|
|
214
|
+
return yield* refuse("paint", "a page paints at more than 0 and at most 4 device pixels");
|
|
215
|
+
if (painting) return yield* refuse("paint", "the page is painted for another scope already");
|
|
216
|
+
if (scale !== undefined && launched && (yield* screenOf("paint")).ratio !== scale)
|
|
217
|
+
return yield* refuse(
|
|
218
|
+
"paint",
|
|
219
|
+
"this browser paints at the scale it was launched with, as Chromium's deviceScaleFactor sets",
|
|
220
|
+
);
|
|
221
|
+
const rescaled = scale !== undefined && !launched;
|
|
222
|
+
|
|
223
|
+
painting = true;
|
|
224
|
+
yield* Effect.addFinalizer(() =>
|
|
225
|
+
Effect.gen(function* () {
|
|
226
|
+
scaled = 0;
|
|
227
|
+
if (!scrollbars) yield* scrollbarsHidden(false);
|
|
228
|
+
if (rescaled) yield* metrics("paint");
|
|
229
|
+
}).pipe(Effect.ignore, Effect.ensuring(Effect.sync(() => (painting = false)))),
|
|
230
|
+
);
|
|
231
|
+
if (!scrollbars) yield* scrollbarsHidden(true);
|
|
232
|
+
if (rescaled) {
|
|
233
|
+
scaled = scale;
|
|
234
|
+
yield* metrics("paint");
|
|
235
|
+
}
|
|
236
|
+
}).pipe(within("paint"));
|
|
237
|
+
|
|
238
|
+
return { holdScreen, paint, scaled: () => scaled };
|
|
239
|
+
};
|
|
168
240
|
|
|
169
241
|
// A new picture in CSS pixels. Where a device pixel is not a CSS pixel, or for a crop, it is
|
|
170
242
|
// Playwright's clip over fresh layout metrics: document coordinates from the visual viewport's
|
|
@@ -179,37 +251,20 @@ const ScreenSize = Schema.Struct({ width: Schema.Int, height: Schema.Int });
|
|
|
179
251
|
// one at a time.
|
|
180
252
|
const camera = (
|
|
181
253
|
page: PageContext,
|
|
182
|
-
bridge: Bridge,
|
|
183
254
|
viewport: Viewport,
|
|
184
255
|
capture: Capture.Controller,
|
|
256
|
+
{ holdScreen, scaled }: Pick<ReturnType<typeof device>, "holdScreen" | "scaled">,
|
|
185
257
|
) => {
|
|
186
258
|
const { playwright, native, within } = page;
|
|
187
259
|
const { send } = page.protocol;
|
|
188
260
|
// Device pixels per CSS pixel, as the page's layout metrics last gave it. A picture taken in one
|
|
189
261
|
// call is checked by its size, so a page whose ratio changes is measured again.
|
|
190
262
|
let ratio = 1;
|
|
191
|
-
let screenHeld = false;
|
|
192
|
-
|
|
193
|
-
// The copy has the screen alone: the view keeps its own size, and the device its pixel ratio.
|
|
194
|
-
const holdScreen = Effect.fnUntraced(function* (operation: string) {
|
|
195
|
-
if (screenHeld) return;
|
|
196
263
|
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
yield* native(operation, () =>
|
|
202
|
-
send("Emulation.setDeviceMetricsOverride", {
|
|
203
|
-
width: 0,
|
|
204
|
-
height: 0,
|
|
205
|
-
deviceScaleFactor: 0,
|
|
206
|
-
mobile: false,
|
|
207
|
-
screenWidth: width,
|
|
208
|
-
screenHeight: height,
|
|
209
|
-
}),
|
|
264
|
+
const layoutMetrics = (operation: string) =>
|
|
265
|
+
native(operation, () => send("Page.getLayoutMetrics")).pipe(
|
|
266
|
+
Effect.flatMap(decodeWith(operation, LayoutMetrics)),
|
|
210
267
|
);
|
|
211
|
-
screenHeld = true;
|
|
212
|
-
});
|
|
213
268
|
|
|
214
269
|
const decoded = (operation: string, data: Uint8Array, format: Format) => {
|
|
215
270
|
const size = format === "png" ? pngSize(data) : jpegSize(data);
|
|
@@ -292,14 +347,19 @@ const camera = (
|
|
|
292
347
|
return yield* emulatedShot(operation, format, quality, pixels, crop);
|
|
293
348
|
}
|
|
294
349
|
|
|
295
|
-
const [
|
|
296
|
-
Effect.flatMap(decodeWith(operation, LayoutMetrics)),
|
|
350
|
+
const [measured] = yield* layoutMetrics(operation).pipe(
|
|
297
351
|
Effect.zip(crop === undefined ? Effect.void : holdScreen(operation), { concurrent: true }),
|
|
298
352
|
);
|
|
299
353
|
|
|
300
|
-
const
|
|
354
|
+
const measuredRatio = measured.contentSize.width / measured.cssContentSize.width;
|
|
301
355
|
|
|
302
|
-
|
|
356
|
+
// The layout metrics report the device's own ratio, never the one a paint emulates.
|
|
357
|
+
ratio =
|
|
358
|
+
scaled() > 0
|
|
359
|
+
? scaled()
|
|
360
|
+
: Number.isFinite(measuredRatio) && measuredRatio > 0
|
|
361
|
+
? measuredRatio
|
|
362
|
+
: 1;
|
|
303
363
|
if (crop === undefined && ratio === 1) {
|
|
304
364
|
const image = whole ?? (yield* shot(operation, format, quality));
|
|
305
365
|
|
|
@@ -308,7 +368,7 @@ const camera = (
|
|
|
308
368
|
return image;
|
|
309
369
|
}
|
|
310
370
|
const rect = crop ?? { x: 0, y: 0, ...(known ?? (yield* viewport.viewportFor(operation))) };
|
|
311
|
-
const { pageX, pageY, scale } =
|
|
371
|
+
const { pageX, pageY, scale } = measured.cssVisualViewport;
|
|
312
372
|
|
|
313
373
|
yield* holdScreen(operation);
|
|
314
374
|
|
|
@@ -412,7 +472,8 @@ export const make = Effect.fnUntraced(function* (
|
|
|
412
472
|
error,
|
|
413
473
|
});
|
|
414
474
|
|
|
415
|
-
const
|
|
475
|
+
const emulated = device(page, bridge);
|
|
476
|
+
const picture = camera(page, viewport, capture, emulated);
|
|
416
477
|
const reusable = reuse(page, bridge, viewport, capture);
|
|
417
478
|
// Pictures in flight are shared, never kept: their callers say how old one may be.
|
|
418
479
|
const screenshots = lane.shared<Image>("screenshot", false);
|
|
@@ -533,5 +594,5 @@ export const make = Effect.fnUntraced(function* (
|
|
|
533
594
|
// change record's times need it.
|
|
534
595
|
const estimate = mapping.current(calibrateClock);
|
|
535
596
|
|
|
536
|
-
return { capture, screenshot, frame, zoom, captureStats, estimate };
|
|
597
|
+
return { capture, screenshot, frame, zoom, captureStats, estimate, paint: emulated.paint };
|
|
537
598
|
});
|
|
@@ -0,0 +1,193 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* In the page: where chosen things are. Each pick is a `find` query; of the elements it finds, one
|
|
3
|
+
* is chosen, and its box is read unrounded, with the boxes of words in it that the pick asks for,
|
|
4
|
+
* each from a `Range` over its text. A pick with a selector takes what the selector selects, an
|
|
5
|
+
* icon drawn in SVG among it, where `find`'s walk leaves drawings out. The viewport, the scroll and the scale are read in the same
|
|
6
|
+
* call, so the boxes and the scroll describe one moment. See `names.inpage.ts` for what a
|
|
7
|
+
* page-side part may use.
|
|
8
|
+
*/
|
|
9
|
+
import type { FindRequest, Match, Wanted } from "../../Matching.ts";
|
|
10
|
+
import type { ContextReader } from "./context.inpage.ts";
|
|
11
|
+
import type { Names } from "./names.inpage.ts";
|
|
12
|
+
import type { Subjects } from "./subjects.inpage.ts";
|
|
13
|
+
import type { Walk } from "./walk.inpage.ts";
|
|
14
|
+
|
|
15
|
+
export interface PickRequest extends FindRequest {
|
|
16
|
+
readonly id: string;
|
|
17
|
+
readonly choose: "first" | "last" | "smallest" | "largest";
|
|
18
|
+
readonly words: Wanted | null;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export const layout = (
|
|
22
|
+
names: Names,
|
|
23
|
+
walked: Walk,
|
|
24
|
+
subjected: Subjects,
|
|
25
|
+
matching: Match,
|
|
26
|
+
placing: ContextReader,
|
|
27
|
+
) => {
|
|
28
|
+
const { isDisabled, isOwn, nameOf, refFor, roleOf, textOf } = names;
|
|
29
|
+
const { boxOf, inView, visible } = walked;
|
|
30
|
+
// A pick's words are bounded, as a page's text is not.
|
|
31
|
+
const most = 200;
|
|
32
|
+
|
|
33
|
+
const area = (element: Element) => {
|
|
34
|
+
const { width, height } = element.getBoundingClientRect();
|
|
35
|
+
|
|
36
|
+
return width * height;
|
|
37
|
+
};
|
|
38
|
+
|
|
39
|
+
const chosen = (elements: ReadonlyArray<Element>, choose: PickRequest["choose"]) =>
|
|
40
|
+
choose === "first"
|
|
41
|
+
? elements[0]
|
|
42
|
+
: choose === "last"
|
|
43
|
+
? elements.at(-1)
|
|
44
|
+
: elements.reduce<Element | undefined>(
|
|
45
|
+
(best, element) =>
|
|
46
|
+
best === undefined ||
|
|
47
|
+
(choose === "smallest" ? area(element) < area(best) : area(element) > area(best))
|
|
48
|
+
? element
|
|
49
|
+
: best,
|
|
50
|
+
undefined,
|
|
51
|
+
);
|
|
52
|
+
|
|
53
|
+
// Whether it keeps its place in the viewport as the page scrolls.
|
|
54
|
+
const fixed = (element: Element) => {
|
|
55
|
+
for (let at: Element | null = element; at !== null; at = at.parentElement)
|
|
56
|
+
if (getComputedStyle(at).position === "fixed") return true;
|
|
57
|
+
|
|
58
|
+
return false;
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
// Each match in the element's text, across the text nodes it is split into, with its box moved
|
|
62
|
+
// by `dx` and `dy` into the top document's viewport. A match that shows nothing is left out.
|
|
63
|
+
const words = (element: Element, wanted: Wanted, dx: number, dy: number) => {
|
|
64
|
+
// Each text node with where its text starts in the element's.
|
|
65
|
+
const parts: Array<{ readonly node: Node; readonly start: number }> = [];
|
|
66
|
+
let text = "";
|
|
67
|
+
const walker = element.ownerDocument.createTreeWalker(element, NodeFilter.SHOW_TEXT);
|
|
68
|
+
|
|
69
|
+
for (let node = walker.nextNode(); node !== null; node = walker.nextNode()) {
|
|
70
|
+
parts.push({ node, start: text.length });
|
|
71
|
+
text += node.nodeValue ?? "";
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
const pattern =
|
|
75
|
+
typeof wanted === "string"
|
|
76
|
+
? new RegExp(wanted.replace(/[.*+?^${}()|[\]\\]/g, "\\$&"), "giu")
|
|
77
|
+
: new RegExp(wanted.source, wanted.flags.replace(/[gy]/g, "") + "g");
|
|
78
|
+
|
|
79
|
+
// The text node holding the character at `index`, or, for an end, the one ending at it.
|
|
80
|
+
const at = (index: number, end: boolean): [Node, number] => {
|
|
81
|
+
const part =
|
|
82
|
+
parts.findLast(({ start }) => (end ? start < index : start <= index)) ?? parts[0];
|
|
83
|
+
|
|
84
|
+
return part === undefined ? [element, 0] : [part.node, index - part.start];
|
|
85
|
+
};
|
|
86
|
+
|
|
87
|
+
const found: Array<{
|
|
88
|
+
text: string;
|
|
89
|
+
box: { x: number; y: number; width: number; height: number };
|
|
90
|
+
}> = [];
|
|
91
|
+
|
|
92
|
+
for (const match of text.matchAll(pattern)) {
|
|
93
|
+
if (found.length === most) break;
|
|
94
|
+
if (match[0] === "") continue;
|
|
95
|
+
const range = element.ownerDocument.createRange();
|
|
96
|
+
|
|
97
|
+
range.setStart(...at(match.index, false));
|
|
98
|
+
range.setEnd(...at(match.index + match[0].length, true));
|
|
99
|
+
const { x, y, width, height } = range.getBoundingClientRect();
|
|
100
|
+
|
|
101
|
+
if (width > 0 || height > 0)
|
|
102
|
+
found.push({ text: match[0], box: { x: x + dx, y: y + dy, width, height } });
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
return found;
|
|
106
|
+
};
|
|
107
|
+
|
|
108
|
+
// What a selector selects that shows and meets the pick's other rules, in tree order.
|
|
109
|
+
const selected = (pick: PickRequest & { readonly selector: string }) => {
|
|
110
|
+
let elements: Array<Element>;
|
|
111
|
+
|
|
112
|
+
try {
|
|
113
|
+
elements = Array.from(document.querySelectorAll(pick.selector));
|
|
114
|
+
} catch {
|
|
115
|
+
return { invalid: `${JSON.stringify(pick.selector)} is not a CSS selector` };
|
|
116
|
+
}
|
|
117
|
+
const rules = matching.compile(pick);
|
|
118
|
+
|
|
119
|
+
return elements.filter((element) => {
|
|
120
|
+
const role = roleOf(element, pick.role !== null);
|
|
121
|
+
const box = boxOf(element);
|
|
122
|
+
|
|
123
|
+
return (
|
|
124
|
+
!isOwn(element) &&
|
|
125
|
+
box.width * box.height > 0 &&
|
|
126
|
+
visible(element) &&
|
|
127
|
+
(pick.scope === "document" || inView(new DOMRect(box.x, box.y, box.width, box.height))) &&
|
|
128
|
+
(pick.state !== "enabled" || !isDisabled(element)) &&
|
|
129
|
+
rules({
|
|
130
|
+
role,
|
|
131
|
+
name: () => nameOf(element, role),
|
|
132
|
+
text: () => textOf(element),
|
|
133
|
+
context: () => placing.contextOf(element),
|
|
134
|
+
})
|
|
135
|
+
);
|
|
136
|
+
});
|
|
137
|
+
};
|
|
138
|
+
|
|
139
|
+
const read = (picks: ReadonlyArray<PickRequest>) => {
|
|
140
|
+
const places = [];
|
|
141
|
+
const missing: Array<string> = [];
|
|
142
|
+
|
|
143
|
+
for (const pick of picks) {
|
|
144
|
+
const elements =
|
|
145
|
+
pick.selector === null
|
|
146
|
+
? subjected.elementsOf(pick)
|
|
147
|
+
: selected({ ...pick, selector: pick.selector });
|
|
148
|
+
|
|
149
|
+
if ("invalid" in elements) return { invalid: `${pick.id}: ${elements.invalid}` };
|
|
150
|
+
const element = chosen(elements, pick.choose);
|
|
151
|
+
|
|
152
|
+
if (element === undefined) {
|
|
153
|
+
missing.push(pick.id);
|
|
154
|
+
continue;
|
|
155
|
+
}
|
|
156
|
+
const role = roleOf(element, pick.role !== null);
|
|
157
|
+
const box = boxOf(element);
|
|
158
|
+
const own = element.getBoundingClientRect();
|
|
159
|
+
|
|
160
|
+
places.push({
|
|
161
|
+
id: pick.id,
|
|
162
|
+
ref: refFor(element),
|
|
163
|
+
subject: {
|
|
164
|
+
role,
|
|
165
|
+
name: nameOf(element, role),
|
|
166
|
+
tag: element.tagName.toLowerCase(),
|
|
167
|
+
context: placing.contextOf(element),
|
|
168
|
+
},
|
|
169
|
+
box,
|
|
170
|
+
fixed: fixed(element),
|
|
171
|
+
words:
|
|
172
|
+
pick.words === null
|
|
173
|
+
? undefined
|
|
174
|
+
: words(element, pick.words, box.x - own.x, box.y - own.y),
|
|
175
|
+
});
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
return {
|
|
179
|
+
layout: {
|
|
180
|
+
url: location.href,
|
|
181
|
+
viewport: { width: innerWidth, height: innerHeight },
|
|
182
|
+
scroll: { x: scrollX, y: scrollY },
|
|
183
|
+
scale: devicePixelRatio,
|
|
184
|
+
places,
|
|
185
|
+
missing,
|
|
186
|
+
},
|
|
187
|
+
};
|
|
188
|
+
};
|
|
189
|
+
|
|
190
|
+
return { read };
|
|
191
|
+
};
|
|
192
|
+
|
|
193
|
+
export type Layouts = ReturnType<typeof layout>;
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Reading where things are: `Page.layout`, in one call to the page that takes its turn as other
|
|
3
|
+
* reads do and shares its work with identical ones. The page gives each place's viewport box; the
|
|
4
|
+
* document box is that box moved by the scroll read with it.
|
|
5
|
+
*/
|
|
6
|
+
import { Effect, Schema } from "effect";
|
|
7
|
+
|
|
8
|
+
import { NotActionable } from "../../BrowserError.ts";
|
|
9
|
+
import { Box, Subject } from "../../BrowserEvent.ts";
|
|
10
|
+
import { Point } from "../../Geometry.ts";
|
|
11
|
+
import { Layout, type Picks, Place, Word } from "../../Layout.ts";
|
|
12
|
+
import { pickRequests } from "../../Matching.ts";
|
|
13
|
+
import * as Url from "../../Url.ts";
|
|
14
|
+
import { type Bridge, scriptCall } from "../page/bridge.ts";
|
|
15
|
+
import { contextGone, decodeWith, failWith, type PageContext, refuse } from "../page/context.ts";
|
|
16
|
+
|
|
17
|
+
// What `layout` reads back, before its document boxes are added.
|
|
18
|
+
const LayoutResult = Schema.Union([
|
|
19
|
+
Schema.Struct({
|
|
20
|
+
layout: Schema.Struct({
|
|
21
|
+
url: Schema.String,
|
|
22
|
+
viewport: Schema.Struct({ width: Schema.Finite, height: Schema.Finite }),
|
|
23
|
+
scroll: Point,
|
|
24
|
+
scale: Schema.Finite,
|
|
25
|
+
missing: Schema.Array(Schema.String),
|
|
26
|
+
places: Schema.Array(
|
|
27
|
+
Schema.Struct({
|
|
28
|
+
id: Schema.String,
|
|
29
|
+
ref: Schema.String,
|
|
30
|
+
subject: Subject,
|
|
31
|
+
box: Box,
|
|
32
|
+
fixed: Schema.Boolean,
|
|
33
|
+
words: Schema.optional(Schema.Array(Schema.Struct({ text: Schema.String, box: Box }))),
|
|
34
|
+
}),
|
|
35
|
+
),
|
|
36
|
+
}),
|
|
37
|
+
}),
|
|
38
|
+
Schema.Struct({ invalid: Schema.String }),
|
|
39
|
+
]);
|
|
40
|
+
|
|
41
|
+
export const make = (page: PageContext, bridge: Bridge) => {
|
|
42
|
+
const { now, span, owned, within, lane } = page;
|
|
43
|
+
const { evaluate } = bridge;
|
|
44
|
+
const layouts = lane.shared<Layout>("layout", true);
|
|
45
|
+
|
|
46
|
+
const layout = (picks: Picks) => {
|
|
47
|
+
const asked = pickRequests(picks);
|
|
48
|
+
|
|
49
|
+
// The boxes and the scroll describe the document the call ran in, which is the one current
|
|
50
|
+
// before and after it; a document that replaced it meanwhile is read again, once.
|
|
51
|
+
const once = Effect.gen(function* () {
|
|
52
|
+
const before = bridge.frameTag().document;
|
|
53
|
+
|
|
54
|
+
const result = yield* evaluate("layout", scriptCall("layout", asked)).pipe(
|
|
55
|
+
Effect.flatMap(decodeWith("layout", LayoutResult)),
|
|
56
|
+
);
|
|
57
|
+
|
|
58
|
+
if ("invalid" in result) return yield* refuse("layout", result.invalid);
|
|
59
|
+
if (bridge.frameTag().document !== before)
|
|
60
|
+
return yield* failWith(
|
|
61
|
+
"layout",
|
|
62
|
+
new NotActionable({ detail: "the page's document changed while it was read" }),
|
|
63
|
+
);
|
|
64
|
+
const { places, scroll, ...read } = result.layout;
|
|
65
|
+
const moved = (box: Box) => ({ ...box, x: box.x + scroll.x, y: box.y + scroll.y });
|
|
66
|
+
|
|
67
|
+
return new Layout({
|
|
68
|
+
...read,
|
|
69
|
+
url: Url.redact(read.url),
|
|
70
|
+
page: page.id,
|
|
71
|
+
connection: page.connection,
|
|
72
|
+
document: before,
|
|
73
|
+
at: now(),
|
|
74
|
+
scroll,
|
|
75
|
+
places: places.map(
|
|
76
|
+
({ words, ...place }) =>
|
|
77
|
+
new Place({
|
|
78
|
+
...place,
|
|
79
|
+
page: moved(place.box),
|
|
80
|
+
words: words?.map((word) => new Word({ ...word, page: moved(word.box) })),
|
|
81
|
+
}),
|
|
82
|
+
),
|
|
83
|
+
});
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
return layouts(
|
|
87
|
+
JSON.stringify(asked),
|
|
88
|
+
once.pipe(
|
|
89
|
+
Effect.retry({
|
|
90
|
+
times: 1,
|
|
91
|
+
while: (error) =>
|
|
92
|
+
contextGone(error) ||
|
|
93
|
+
(error.reason._tag === "NotActionable" && error.operation === "layout"),
|
|
94
|
+
}),
|
|
95
|
+
within("layout"),
|
|
96
|
+
),
|
|
97
|
+
).pipe(span("Page.layout", { picks: asked.length }), owned);
|
|
98
|
+
};
|
|
99
|
+
|
|
100
|
+
return layout;
|
|
101
|
+
};
|
|
@@ -45,6 +45,19 @@ export const elements = () => {
|
|
|
45
45
|
isTextArea(element) ||
|
|
46
46
|
(isHtml(element) && element.isContentEditable);
|
|
47
47
|
|
|
48
|
+
// What the library itself puts in the page, such as its overlay's host. A reader that selects by
|
|
49
|
+
// CSS leaves it out; the walk finds nothing in it, since it never enters a closed shadow root.
|
|
50
|
+
const own = new WeakSet<Node>();
|
|
51
|
+
|
|
52
|
+
const adopt = (node: Node) => own.add(node);
|
|
53
|
+
|
|
54
|
+
/** Whether a node is the library's own, or inside a shadow root of one of its own. */
|
|
55
|
+
const isOwn = (node: Node): boolean => {
|
|
56
|
+
const root = node.getRootNode();
|
|
57
|
+
|
|
58
|
+
return own.has(node) || (root instanceof ShadowRoot && own.has(root.host));
|
|
59
|
+
};
|
|
60
|
+
|
|
48
61
|
const clean = (value: string | null | undefined, max = 160): string => {
|
|
49
62
|
const text = (value ?? "").replace(/\s+/g, " ").trim();
|
|
50
63
|
|
|
@@ -52,6 +65,7 @@ export const elements = () => {
|
|
|
52
65
|
};
|
|
53
66
|
|
|
54
67
|
return {
|
|
68
|
+
adopt,
|
|
55
69
|
clean,
|
|
56
70
|
isInput,
|
|
57
71
|
isTextArea,
|
|
@@ -63,6 +77,7 @@ export const elements = () => {
|
|
|
63
77
|
textOf,
|
|
64
78
|
isElement,
|
|
65
79
|
isDocument,
|
|
80
|
+
isOwn,
|
|
66
81
|
textEntry,
|
|
67
82
|
};
|
|
68
83
|
};
|
|
@@ -2,12 +2,12 @@
|
|
|
2
2
|
* In the page: the outline a model reads, with a ref for each control. See `names.inpage.ts` for
|
|
3
3
|
* what a page-side part may use.
|
|
4
4
|
*/
|
|
5
|
-
import { Schema } from "effect";
|
|
6
5
|
|
|
7
|
-
import {
|
|
6
|
+
import type { Control } from "../../Snapshot.ts";
|
|
8
7
|
import type { addresses } from "../../Url.ts";
|
|
9
8
|
import type { Controls } from "./controls.inpage.ts";
|
|
10
9
|
import type { Names } from "./names.inpage.ts";
|
|
10
|
+
import type { SnapshotResult } from "./reading.ts";
|
|
11
11
|
import type { Subjects } from "./subjects.inpage.ts";
|
|
12
12
|
import type { Texts } from "./text.inpage.ts";
|
|
13
13
|
import type { Walk } from "./walk.inpage.ts";
|
|
@@ -22,12 +22,6 @@ export interface SnapshotRequest {
|
|
|
22
22
|
readonly boxes: boolean;
|
|
23
23
|
}
|
|
24
24
|
|
|
25
|
-
/**
|
|
26
|
-
* The outline as a `Snapshot` reads it, and the next ref the page may give, or why `within` is not
|
|
27
|
-
* a selector.
|
|
28
|
-
*/
|
|
29
|
-
export type SnapshotResult = typeof SnapshotResultSchema.Encoded;
|
|
30
|
-
|
|
31
25
|
/** The rule every address the library reports goes by, which a link's address goes by too. */
|
|
32
26
|
type Url = ReturnType<typeof addresses>;
|
|
33
27
|
|
|
@@ -250,13 +244,3 @@ export const outline = (
|
|
|
250
244
|
};
|
|
251
245
|
|
|
252
246
|
export type Outline = ReturnType<typeof outline>;
|
|
253
|
-
|
|
254
|
-
export const SnapshotResultSchema = Schema.Union([
|
|
255
|
-
Schema.Struct({ snapshot: Snapshot }),
|
|
256
|
-
Schema.Struct({ invalid: Schema.String }),
|
|
257
|
-
]);
|
|
258
|
-
|
|
259
|
-
export const ViewportResultSchema = Schema.Struct({
|
|
260
|
-
width: Schema.Finite,
|
|
261
|
-
height: Schema.Finite,
|
|
262
|
-
});
|
|
@@ -13,8 +13,28 @@ import * as Url from "../../Url.ts";
|
|
|
13
13
|
import { type Bridge, scriptCall } from "../page/bridge.ts";
|
|
14
14
|
import { contextGone, decodeWith, failWith, type PageContext, refuse } from "../page/context.ts";
|
|
15
15
|
import type { Want } from "./expect.inpage.ts";
|
|
16
|
-
import {
|
|
17
|
-
import {
|
|
16
|
+
import type { SnapshotRequest } from "./outline.inpage.ts";
|
|
17
|
+
import type { TextRequest } from "./text.inpage.ts";
|
|
18
|
+
|
|
19
|
+
/** The outline as a `Snapshot` reads it, or why `within` is not a selector. */
|
|
20
|
+
const SnapshotResultSchema = Schema.Union([
|
|
21
|
+
Schema.Struct({ snapshot: Snapshot }),
|
|
22
|
+
Schema.Struct({ invalid: Schema.String }),
|
|
23
|
+
]);
|
|
24
|
+
|
|
25
|
+
export type SnapshotResult = typeof SnapshotResultSchema.Encoded;
|
|
26
|
+
|
|
27
|
+
/** What `text` sends back, or null when its ref no longer names an element. */
|
|
28
|
+
const TextResultSchema = Schema.NullOr(
|
|
29
|
+
Schema.Struct({
|
|
30
|
+
text: Schema.String,
|
|
31
|
+
truncated: Schema.Boolean,
|
|
32
|
+
url: Schema.String,
|
|
33
|
+
title: Schema.String,
|
|
34
|
+
}),
|
|
35
|
+
);
|
|
36
|
+
|
|
37
|
+
export type TextResult = typeof TextResultSchema.Type;
|
|
18
38
|
|
|
19
39
|
// What `find` reads back. The bridge envelope owns ref allocation across documents.
|
|
20
40
|
const FindResults = Schema.Union([
|
|
@@ -6,30 +6,15 @@
|
|
|
6
6
|
* frame. So a canvas mounted blank, or a spinner alone, is still loading. A screen that only says
|
|
7
7
|
* "Loading…" in words reads as ready, and a WebGL canvas drawn once, without keeping its drawing,
|
|
8
8
|
* reads as blank. Asked for a quiet spell, where the page's changes are recorded, nothing in view
|
|
9
|
-
* may have changed for that long either.
|
|
9
|
+
* may have changed for that long either. On a page whose time is held (see `clock.inpage.ts`), an
|
|
10
|
+
* animation paused part way is still animating: held time has yet to finish it. After input, it
|
|
11
|
+
* waits a task and a frame. See `names.inpage.ts` for what a page-side part may use.
|
|
10
12
|
*/
|
|
11
|
-
import { Schema } from "effect";
|
|
12
|
-
|
|
13
13
|
import type { History } from "../timeline/history.inpage.ts";
|
|
14
|
+
import type { Readiness } from "./ready.ts";
|
|
14
15
|
import type { Texts } from "./text.inpage.ts";
|
|
15
16
|
import type { Walk } from "./walk.inpage.ts";
|
|
16
17
|
|
|
17
|
-
/** What the page is still waiting for; none when it is ready. */
|
|
18
|
-
export const ReadinessSchema = Schema.Array(
|
|
19
|
-
Schema.Literals([
|
|
20
|
-
"load",
|
|
21
|
-
"paint",
|
|
22
|
-
"animations",
|
|
23
|
-
"images",
|
|
24
|
-
"fonts",
|
|
25
|
-
"busy",
|
|
26
|
-
"content",
|
|
27
|
-
"changing",
|
|
28
|
-
]),
|
|
29
|
-
);
|
|
30
|
-
|
|
31
|
-
export type Readiness = typeof ReadinessSchema.Type;
|
|
32
|
-
|
|
33
18
|
export const ready = (walked: Walk, texts: Texts, kept: History) => {
|
|
34
19
|
const { inView, visible } = walked;
|
|
35
20
|
// A canvas is read through a small copy: reading it directly would give it a context of ours.
|
|
@@ -85,10 +70,16 @@ export const ready = (walked: Walk, texts: Texts, kept: History) => {
|
|
|
85
70
|
};
|
|
86
71
|
|
|
87
72
|
// In a frame, before it is drawn: a canvas drawn every frame still holds its drawing then.
|
|
88
|
-
const check = (quietMillis: number): Promise<Readiness> =>
|
|
73
|
+
const check = (quietMillis: number, clock: string | null): Promise<Readiness> =>
|
|
89
74
|
painted().then((paints) => {
|
|
90
75
|
const waiting: Array<Readiness[number]> = [];
|
|
91
76
|
|
|
77
|
+
const held =
|
|
78
|
+
clock !== null &&
|
|
79
|
+
!document.dispatchEvent(
|
|
80
|
+
new CustomEvent(clock, { detail: { op: "holds" }, cancelable: true }),
|
|
81
|
+
);
|
|
82
|
+
|
|
92
83
|
if (document.readyState === "loading") waiting.push("load");
|
|
93
84
|
if (!paints) waiting.push("paint");
|
|
94
85
|
// An endless animation, such as a pulsing dot, never ends, so only those that end count.
|
|
@@ -97,7 +88,7 @@ export const ready = (walked: Walk, texts: Texts, kept: History) => {
|
|
|
97
88
|
const effect = animation.effect;
|
|
98
89
|
|
|
99
90
|
return (
|
|
100
|
-
animation.playState === "running" &&
|
|
91
|
+
(animation.playState === "running" || (held && animation.playState === "paused")) &&
|
|
101
92
|
effect !== null &&
|
|
102
93
|
Number.isFinite(effect.getComputedTiming().endTime) &&
|
|
103
94
|
"target" in effect &&
|
|
@@ -122,11 +113,15 @@ export const ready = (walked: Walk, texts: Texts, kept: History) => {
|
|
|
122
113
|
* Check until the page is ready, or for `millis`, and say what it is still waiting for; with
|
|
123
114
|
* `quietMillis`, until nothing the record saw in view has changed for that long, too.
|
|
124
115
|
*/
|
|
125
|
-
const wait = async (
|
|
116
|
+
const wait = async (
|
|
117
|
+
millis: number,
|
|
118
|
+
quietMillis = 0,
|
|
119
|
+
clock: string | null = null,
|
|
120
|
+
): Promise<Readiness> => {
|
|
126
121
|
const until = performance.now() + millis;
|
|
127
122
|
|
|
128
123
|
for (;;) {
|
|
129
|
-
const waiting = await check(quietMillis);
|
|
124
|
+
const waiting = await check(quietMillis, clock);
|
|
130
125
|
|
|
131
126
|
if (waiting.length === 0 || performance.now() >= until) return waiting;
|
|
132
127
|
const { promise, resolve } = Promise.withResolvers<void>();
|