@parhelia/capture 0.4.13062 → 0.4.13194
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 +23 -4
- package/dist/cli.js +101 -19
- package/dist/cmsJob.d.ts +9 -0
- package/dist/cmsJob.d.ts.map +1 -1
- package/dist/cmsJob.js +81 -0
- package/dist/index.d.ts +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +1 -1
- package/dist/install/install.d.ts +4 -0
- package/dist/install/install.d.ts.map +1 -1
- package/dist/install/install.js +40 -10
- package/dist/install/layout.d.ts +9 -0
- package/dist/install/layout.d.ts.map +1 -1
- package/dist/install/layout.js +7 -1
- package/dist/install/manifestSignature.d.ts +30 -0
- package/dist/install/manifestSignature.d.ts.map +1 -0
- package/dist/install/manifestSignature.js +93 -0
- package/dist/install/releaseKeys.d.ts +2 -0
- package/dist/install/releaseKeys.d.ts.map +1 -0
- package/dist/install/releaseKeys.js +16 -0
- package/dist/install/services.d.ts +12 -0
- package/dist/install/services.d.ts.map +1 -1
- package/dist/install/services.js +45 -1
- package/dist/install/units.d.ts +8 -0
- package/dist/install/units.d.ts.map +1 -1
- package/dist/install/units.js +39 -3
- package/dist/install/updateLock.d.ts +14 -0
- package/dist/install/updateLock.d.ts.map +1 -0
- package/dist/install/updateLock.js +75 -0
- package/dist/install/updater.d.ts +28 -1
- package/dist/install/updater.d.ts.map +1 -1
- package/dist/install/updater.js +74 -2
- package/dist/install/versions.d.ts +11 -1
- package/dist/install/versions.d.ts.map +1 -1
- package/dist/install/versions.js +16 -2
- package/dist/isolation.d.ts +17 -1
- package/dist/isolation.d.ts.map +1 -1
- package/dist/isolation.js +21 -5
- package/dist/keptFor.d.ts +8 -0
- package/dist/keptFor.d.ts.map +1 -0
- package/dist/keptFor.js +28 -0
- package/dist/parheliaClient.d.ts +13 -0
- package/dist/parheliaClient.d.ts.map +1 -1
- package/dist/parheliaClient.js +7 -0
- package/dist/webIsolation.d.ts +2 -0
- package/dist/webIsolation.d.ts.map +1 -1
- package/dist/webIsolation.js +7 -5
- package/dist/webJob.d.ts +13 -0
- package/dist/webJob.d.ts.map +1 -1
- package/dist/webJob.js +90 -10
- package/dist/webPage.d.ts +30 -0
- package/dist/webPage.d.ts.map +1 -0
- package/dist/webPage.js +257 -0
- package/dist/worker.d.ts.map +1 -1
- package/dist/worker.js +6 -5
- package/package.json +2 -2
package/dist/webJob.js
CHANGED
|
@@ -1,10 +1,17 @@
|
|
|
1
1
|
import { PRECONDITION_CSS } from "./cmsJob.js";
|
|
2
|
+
import { CONSENT_SELECTORS, frameInPage, hideOverlaysInPage } from "./webPage.js";
|
|
2
3
|
export const MAX_IMAGE_HEIGHT = 7680;
|
|
3
4
|
export const PNG_SIZE_FALLBACK_THRESHOLD = 2_500_000;
|
|
4
5
|
export const JPEG_QUALITY = 0.9;
|
|
5
6
|
const NETWORK_QUIET_MS = 5_000;
|
|
6
7
|
const NAVIGATION_TIMEOUT_MS = 30_000;
|
|
7
8
|
const LAZY_SCROLL_MS = 3_000;
|
|
9
|
+
/** The most texts and picture names a job frames a part by, and how long one may be. */
|
|
10
|
+
export const MAX_AROUND = 16;
|
|
11
|
+
export const MAX_AROUND_CHARS = 200;
|
|
12
|
+
/** A part is shown with this much of the page above and below it, and never as a band lower than this. */
|
|
13
|
+
const FRAME_MARGIN = 24;
|
|
14
|
+
const MIN_FRAME_HEIGHT = 240;
|
|
8
15
|
const clamp = (value, fallback, min, max) => typeof value === "number" && Number.isFinite(value) ? Math.min(max, Math.max(min, Math.round(value))) : fallback;
|
|
9
16
|
/** The job's parameters as this sidecar will run them, or the reason it will not. */
|
|
10
17
|
export function normalizeWebParameters(raw) {
|
|
@@ -28,9 +35,27 @@ export function normalizeWebParameters(raw) {
|
|
|
28
35
|
viewportWidth: clamp(parameters.viewportWidth, 1280, 240, 3840),
|
|
29
36
|
viewportHeight: clamp(parameters.viewportHeight, 900, 240, 4320),
|
|
30
37
|
maxImageWidth: clamp(parameters.maxImageWidth, 1280, 64, 3840),
|
|
38
|
+
around: aroundOf(parameters.around),
|
|
39
|
+
hideOverlays: parameters.hideOverlays === true,
|
|
31
40
|
},
|
|
32
41
|
};
|
|
33
42
|
}
|
|
43
|
+
/** The job's `around` as this sidecar reads it: strings only, trimmed, the first MAX_AROUND, none longer than MAX_AROUND_CHARS. */
|
|
44
|
+
function aroundOf(raw) {
|
|
45
|
+
if (!Array.isArray(raw))
|
|
46
|
+
return [];
|
|
47
|
+
const out = [];
|
|
48
|
+
for (const entry of raw) {
|
|
49
|
+
if (typeof entry !== "string")
|
|
50
|
+
continue;
|
|
51
|
+
const text = entry.replace(/\s+/g, " ").trim().slice(0, MAX_AROUND_CHARS);
|
|
52
|
+
if (text && !out.includes(text))
|
|
53
|
+
out.push(text);
|
|
54
|
+
if (out.length >= MAX_AROUND)
|
|
55
|
+
break;
|
|
56
|
+
}
|
|
57
|
+
return out;
|
|
58
|
+
}
|
|
34
59
|
export async function runWebJob(rawParameters, options) {
|
|
35
60
|
const now = options.now ?? Date.now;
|
|
36
61
|
const normalized = normalizeWebParameters(rawParameters);
|
|
@@ -59,7 +84,7 @@ export async function runWebJob(rawParameters, options) {
|
|
|
59
84
|
return { kind: "cancelled" };
|
|
60
85
|
if (captured.kind !== "png")
|
|
61
86
|
return captured;
|
|
62
|
-
const processed = await postProcess(browser, captured.png, parameters.maxImageWidth, parameters.scope);
|
|
87
|
+
const processed = await postProcess(browser, captured.png, parameters.maxImageWidth, captured.framed ? "part" : parameters.scope);
|
|
63
88
|
return {
|
|
64
89
|
kind: "result",
|
|
65
90
|
result: {
|
|
@@ -71,6 +96,8 @@ export async function runWebJob(rawParameters, options) {
|
|
|
71
96
|
finalUrl: captured.finalUrl,
|
|
72
97
|
httpStatus: captured.httpStatus,
|
|
73
98
|
truncated: captured.truncated,
|
|
99
|
+
...(parameters.around.length ? { framed: captured.framed, aroundFound: captured.aroundFound } : {}),
|
|
100
|
+
...(parameters.hideOverlays ? { overlaysHidden: captured.overlaysHidden } : {}),
|
|
74
101
|
},
|
|
75
102
|
};
|
|
76
103
|
}
|
|
@@ -105,20 +132,73 @@ async function capture(page, parameters, remaining) {
|
|
|
105
132
|
const heightCap = Math.floor(MAX_IMAGE_HEIGHT / scale);
|
|
106
133
|
let truncated = false;
|
|
107
134
|
let png;
|
|
135
|
+
let overlaysHidden = 0;
|
|
136
|
+
if (parameters.hideOverlays)
|
|
137
|
+
overlaysHidden += await hideOverlays(page);
|
|
138
|
+
const shot = { type: "png", animations: "allow", caret: "initial", scale: "css" };
|
|
139
|
+
const whole = parameters.scope === "page" || parameters.around.length > 0;
|
|
140
|
+
if (whole) {
|
|
141
|
+
// A part can stand anywhere on the page: its pictures load on the way down, as a whole page's do.
|
|
142
|
+
await scrollForLazyContent(page, Math.min(await pageHeight(page), heightCap), parameters.viewportHeight, remaining);
|
|
143
|
+
// A banner that came in late (after the page's own scripts settled) goes too.
|
|
144
|
+
if (parameters.hideOverlays)
|
|
145
|
+
overlaysHidden += await hideOverlays(page);
|
|
146
|
+
}
|
|
147
|
+
if (parameters.around.length) {
|
|
148
|
+
const band = await frameOf(page, parameters, Math.min(await pageHeight(page), heightCap));
|
|
149
|
+
if (band.clip) {
|
|
150
|
+
png = await page.screenshot({ ...shot, fullPage: true, clip: band.clip, timeout: Math.max(1_000, remaining()) });
|
|
151
|
+
return { kind: "png", png, finalUrl: page.url(), httpStatus: response?.status() ?? null, truncated: false, framed: true, aroundFound: band.found, overlaysHidden };
|
|
152
|
+
}
|
|
153
|
+
// None of them is on the page: the capture is the scope's, and the answer says so (framed false).
|
|
154
|
+
}
|
|
108
155
|
if (parameters.scope === "page") {
|
|
109
|
-
|
|
110
|
-
await scrollForLazyContent(page, Math.min(height, heightCap), parameters.viewportHeight, remaining);
|
|
111
|
-
const settled = await page.evaluate(() => Math.max(document.documentElement.scrollHeight, document.body?.scrollHeight ?? 0));
|
|
112
|
-
truncated = settled > heightCap;
|
|
156
|
+
truncated = (await pageHeight(page)) > heightCap;
|
|
113
157
|
png = truncated
|
|
114
|
-
? await page.screenshot({
|
|
115
|
-
|
|
116
|
-
: await page.screenshot({ type: "png", fullPage: true, animations: "allow", caret: "initial", scale: "css", timeout: Math.max(1_000, remaining()) });
|
|
158
|
+
? await page.screenshot({ ...shot, fullPage: true, clip: { x: 0, y: 0, width: parameters.viewportWidth, height: heightCap }, timeout: Math.max(1_000, remaining()) })
|
|
159
|
+
: await page.screenshot({ ...shot, fullPage: true, timeout: Math.max(1_000, remaining()) });
|
|
117
160
|
}
|
|
118
161
|
else {
|
|
119
|
-
png = await page.screenshot({
|
|
162
|
+
png = await page.screenshot({ ...shot, timeout: Math.max(1_000, remaining()) });
|
|
163
|
+
}
|
|
164
|
+
return { kind: "png", png, finalUrl: page.url(), httpStatus: response?.status() ?? null, truncated, framed: false, aroundFound: 0, overlaysHidden };
|
|
165
|
+
}
|
|
166
|
+
/** The page's height as the page says it: a number of an untrusted page, so finite and positive or it is 0. */
|
|
167
|
+
async function pageHeight(page) {
|
|
168
|
+
const height = await page.evaluate(() => Math.max(document.documentElement.scrollHeight, document.body?.scrollHeight ?? 0)).catch(() => 0);
|
|
169
|
+
return typeof height === "number" && Number.isFinite(height) && height > 0 ? Math.floor(height) : 0;
|
|
170
|
+
}
|
|
171
|
+
/** Takes the consent banners away; how many went (0 when the page did not answer with a small whole number). */
|
|
172
|
+
async function hideOverlays(page) {
|
|
173
|
+
const hidden = await page.evaluate(hideOverlaysInPage, CONSENT_SELECTORS).catch(() => 0);
|
|
174
|
+
return typeof hidden === "number" && Number.isInteger(hidden) && hidden > 0 ? Math.min(hidden, 1_000) : 0;
|
|
175
|
+
}
|
|
176
|
+
/**
|
|
177
|
+
* The band of the page that holds the job's `around`: the full width, from a little above the block to a little below it,
|
|
178
|
+
* inside the page and no taller than a capture holds. No clip when the page holds none of them, or answers anything but
|
|
179
|
+
* numbers that make a band.
|
|
180
|
+
*/
|
|
181
|
+
async function frameOf(page, parameters, height) {
|
|
182
|
+
const maxHeight = Math.max(MIN_FRAME_HEIGHT, parameters.viewportHeight * 3);
|
|
183
|
+
const answer = (await page.evaluate(frameInPage, { anchors: parameters.around, maxHeight }).catch(() => null));
|
|
184
|
+
const number = (value) => (typeof value === "number" && Number.isFinite(value) ? value : null);
|
|
185
|
+
const found = number(answer?.found);
|
|
186
|
+
const top = number(answer?.top);
|
|
187
|
+
const bottom = number(answer?.bottom);
|
|
188
|
+
if (found == null || found < 1 || top == null || bottom == null || bottom <= top || height < 1)
|
|
189
|
+
return { found: 0 };
|
|
190
|
+
let from = Math.max(0, Math.floor(top) - FRAME_MARGIN);
|
|
191
|
+
let to = Math.min(height, Math.ceil(bottom) + FRAME_MARGIN);
|
|
192
|
+
if (to - from < MIN_FRAME_HEIGHT) {
|
|
193
|
+
// A line of text alone says little: it is shown with what stands around it.
|
|
194
|
+
const middle = (from + to) / 2;
|
|
195
|
+
from = Math.max(0, Math.floor(middle - MIN_FRAME_HEIGHT / 2));
|
|
196
|
+
to = Math.min(height, from + MIN_FRAME_HEIGHT);
|
|
197
|
+
from = Math.max(0, to - MIN_FRAME_HEIGHT);
|
|
120
198
|
}
|
|
121
|
-
|
|
199
|
+
if (to - from < 1)
|
|
200
|
+
return { found: 0 };
|
|
201
|
+
return { found: Math.min(Math.floor(found), parameters.around.length), clip: { x: 0, y: from, width: parameters.viewportWidth, height: to - from } };
|
|
122
202
|
}
|
|
123
203
|
/** One bounded pass down the page so lazy images load, then back to the top. */
|
|
124
204
|
async function scrollForLazyContent(page, height, step, remaining) {
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The containers of the consent managers met most often, each hidden whole: their banner, their dialog and their backdrop.
|
|
3
|
+
* A banner none of these names is found by what it says (hideOverlaysInPage).
|
|
4
|
+
*/
|
|
5
|
+
export declare const CONSENT_SELECTORS: string;
|
|
6
|
+
/**
|
|
7
|
+
* Hides the page's cookie and consent banners: the known managers' containers, then every element fixed over the page that
|
|
8
|
+
* speaks of cookies or consent (or frames a document that does) without holding the page itself, then the empty backdrops
|
|
9
|
+
* that covered the page for them, and lets a page scroll again that a banner had locked. Answers how many elements went.
|
|
10
|
+
*/
|
|
11
|
+
export declare function hideOverlaysInPage(known: string): number;
|
|
12
|
+
/**
|
|
13
|
+
* The part of the page that holds the given anchors: texts it shows, file names of its pictures and addresses it links to
|
|
14
|
+
* (an anchor that reads as a file name, "bath.jpg", is looked for in the pictures' addresses, one that reads as an address,
|
|
15
|
+
* "/products/lotion", in the links', any other in the text). Each anchor may stand on the
|
|
16
|
+
* page more than once (a title in the navigation too): the rarest one outside the page's header, navigation and footer is
|
|
17
|
+
* taken as the place, and of every other its match nearest to that one, as long as all of them together are no taller than
|
|
18
|
+
* `maxHeight`. The part is the smallest element that holds them, grown to the block around it: as long as the element is
|
|
19
|
+
* no real block yet (a line, a narrow column), or its parent is little more than itself. Answers how many anchors the page
|
|
20
|
+
* holds and the part's top and bottom on the page, or found 0.
|
|
21
|
+
*/
|
|
22
|
+
export declare function frameInPage(input: {
|
|
23
|
+
anchors: string[];
|
|
24
|
+
maxHeight: number;
|
|
25
|
+
}): {
|
|
26
|
+
found: number;
|
|
27
|
+
top?: number;
|
|
28
|
+
bottom?: number;
|
|
29
|
+
};
|
|
30
|
+
//# sourceMappingURL=webPage.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"webPage.d.ts","sourceRoot":"","sources":["../src/webPage.ts"],"names":[],"mappings":"AAMA;;;GAGG;AACH,eAAO,MAAM,iBAAiB,QASnB,CAAC;AAEZ;;;;GAIG;AACH,wBAAgB,kBAAkB,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,CAiDxD;AAED;;;;;;;;;GASG;AACH,wBAAgB,WAAW,CAAC,KAAK,EAAE;IAAE,OAAO,EAAE,MAAM,EAAE,CAAC;IAAC,SAAS,EAAE,MAAM,CAAA;CAAE,GAAG;IAAE,KAAK,EAAE,MAAM,CAAC;IAAC,GAAG,CAAC,EAAE,MAAM,CAAC;IAAC,MAAM,CAAC,EAAE,MAAM,CAAA;CAAE,CA4I7H"}
|
package/dist/webPage.js
ADDED
|
@@ -0,0 +1,257 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* What a public-web job does inside the page (webJob.ts): take the consent banners away, and find the band a part of the page
|
|
3
|
+
* stands in. Both functions run in the page (Playwright serializes them), so each is self-contained: nothing from this
|
|
4
|
+
* module's scope, no helper outside its own body. The page is untrusted: whatever they answer is checked again by the job.
|
|
5
|
+
*/
|
|
6
|
+
/**
|
|
7
|
+
* The containers of the consent managers met most often, each hidden whole: their banner, their dialog and their backdrop.
|
|
8
|
+
* A banner none of these names is found by what it says (hideOverlaysInPage).
|
|
9
|
+
*/
|
|
10
|
+
export const CONSENT_SELECTORS = [
|
|
11
|
+
"#onetrust-consent-sdk", "#onetrust-banner-sdk", ".onetrust-pc-dark-filter", "#CybotCookiebotDialog", "#CybotCookiebotDialogBodyUnderlay",
|
|
12
|
+
"#usercentrics-root", "#usercentrics-cmp-ui", "#uc-center-container", ".cc-window", ".cc-banner", ".cc-revoke", "#cookie-law-info-bar",
|
|
13
|
+
".cli-modal-backdrop", ".cky-consent-container", ".cky-overlay", "#didomi-host", "#didomi-notice", ".didomi-popup-backdrop",
|
|
14
|
+
"#truste-consent-track", ".truste_overlay", ".truste_box_overlay", "#cmpbox", "#cmpbox2", ".qc-cmp2-container", "[id^='sp_message_container']",
|
|
15
|
+
"#cookiescript_injected", "#cookie-notice", "#cookieConsent", "#cookie-consent", ".cookie-consent", "#cookie-banner", ".cookie-banner",
|
|
16
|
+
".osano-cm-window", "#iubenda-cs-banner", "#klaro", ".klaro", "#tarteaucitronRoot", "#axeptio_overlay", "#_evidon_banner", ".evidon-banner",
|
|
17
|
+
"#consent_blackbar", ".fc-consent-root", "#gdpr-cookie-message", "#cookiebanner", ".cookiebanner", "#CookieBanner", "#borlabs-cookie",
|
|
18
|
+
"#BorlabsCookieBox", ".cmplz-cookiebanner", "#cmplz-cookiebanner-container", "#hs-eu-cookie-confirmation", "#ccc", "#ccc-overlay",
|
|
19
|
+
].join(",");
|
|
20
|
+
/**
|
|
21
|
+
* Hides the page's cookie and consent banners: the known managers' containers, then every element fixed over the page that
|
|
22
|
+
* speaks of cookies or consent (or frames a document that does) without holding the page itself, then the empty backdrops
|
|
23
|
+
* that covered the page for them, and lets a page scroll again that a banner had locked. Answers how many elements went.
|
|
24
|
+
*/
|
|
25
|
+
export function hideOverlaysInPage(known) {
|
|
26
|
+
const body = document.body;
|
|
27
|
+
if (!body)
|
|
28
|
+
return 0;
|
|
29
|
+
const gone = new Set();
|
|
30
|
+
const take = (element) => {
|
|
31
|
+
// What is gone already (an earlier pass of the same job) is not counted again.
|
|
32
|
+
if (gone.has(element) || getComputedStyle(element).display === "none")
|
|
33
|
+
return;
|
|
34
|
+
gone.add(element);
|
|
35
|
+
element.style?.setProperty("display", "none", "important");
|
|
36
|
+
};
|
|
37
|
+
try {
|
|
38
|
+
for (const element of Array.from(document.querySelectorAll(known)))
|
|
39
|
+
take(element);
|
|
40
|
+
}
|
|
41
|
+
catch {
|
|
42
|
+
// A selector this browser does not read: the banners are found by what they say.
|
|
43
|
+
}
|
|
44
|
+
const speaks = /cookie|consent|einwillig|datenschutzeinstellung|privacy (settings|preferences|choices)|gdpr/i;
|
|
45
|
+
const fixed = [];
|
|
46
|
+
for (const element of Array.from(body.querySelectorAll("*"))) {
|
|
47
|
+
if (gone.has(element))
|
|
48
|
+
continue;
|
|
49
|
+
const style = getComputedStyle(element);
|
|
50
|
+
if (style.position !== "fixed" || style.display === "none" || style.visibility === "hidden")
|
|
51
|
+
continue;
|
|
52
|
+
const rect = element.getBoundingClientRect();
|
|
53
|
+
if (rect.width < 1 || rect.height < 1)
|
|
54
|
+
continue;
|
|
55
|
+
fixed.push({ element, width: rect.width, height: rect.height });
|
|
56
|
+
}
|
|
57
|
+
const textOf = (element) => String(element.innerText ?? "");
|
|
58
|
+
for (const { element } of fixed) {
|
|
59
|
+
// A page that lives inside one fixed element (an application shell) is not a banner, whatever its footer links to.
|
|
60
|
+
if (element.querySelector("main, article, h1, nav, [role=main]"))
|
|
61
|
+
continue;
|
|
62
|
+
const text = textOf(element);
|
|
63
|
+
if (text.length > 3_000)
|
|
64
|
+
continue;
|
|
65
|
+
const names = [element.id, element.getAttribute("class"), element.getAttribute("aria-label")].join(" ");
|
|
66
|
+
const frames = Array.from(element.tagName === "IFRAME" ? [element] : element.querySelectorAll("iframe"))
|
|
67
|
+
.map((frame) => [frame.getAttribute("src"), frame.getAttribute("title"), frame.id, frame.getAttribute("name")].join(" "))
|
|
68
|
+
.join(" ");
|
|
69
|
+
if (speaks.test(text) || speaks.test(names) || speaks.test(frames))
|
|
70
|
+
take(element);
|
|
71
|
+
}
|
|
72
|
+
if (!gone.size)
|
|
73
|
+
return 0;
|
|
74
|
+
// The backdrop a banner dims the page with: fixed, as large as the window, and empty.
|
|
75
|
+
for (const { element, width, height } of fixed) {
|
|
76
|
+
if (gone.has(element) || width < window.innerWidth * 0.9 || height < window.innerHeight * 0.9)
|
|
77
|
+
continue;
|
|
78
|
+
if (textOf(element).trim() || element.querySelector("img, video, canvas, iframe, svg, picture"))
|
|
79
|
+
continue;
|
|
80
|
+
take(element);
|
|
81
|
+
}
|
|
82
|
+
for (const element of [document.documentElement, body]) {
|
|
83
|
+
const style = getComputedStyle(element);
|
|
84
|
+
if (style.overflowY === "hidden" || style.overflow === "hidden")
|
|
85
|
+
element.style.setProperty("overflow", "visible", "important");
|
|
86
|
+
}
|
|
87
|
+
return gone.size;
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* The part of the page that holds the given anchors: texts it shows, file names of its pictures and addresses it links to
|
|
91
|
+
* (an anchor that reads as a file name, "bath.jpg", is looked for in the pictures' addresses, one that reads as an address,
|
|
92
|
+
* "/products/lotion", in the links', any other in the text). Each anchor may stand on the
|
|
93
|
+
* page more than once (a title in the navigation too): the rarest one outside the page's header, navigation and footer is
|
|
94
|
+
* taken as the place, and of every other its match nearest to that one, as long as all of them together are no taller than
|
|
95
|
+
* `maxHeight`. The part is the smallest element that holds them, grown to the block around it: as long as the element is
|
|
96
|
+
* no real block yet (a line, a narrow column), or its parent is little more than itself. Answers how many anchors the page
|
|
97
|
+
* holds and the part's top and bottom on the page, or found 0.
|
|
98
|
+
*/
|
|
99
|
+
export function frameInPage(input) {
|
|
100
|
+
const body = document.body;
|
|
101
|
+
if (!body)
|
|
102
|
+
return { found: 0 };
|
|
103
|
+
const root = document.documentElement;
|
|
104
|
+
const pageWidth = Math.max(root.clientWidth, 1);
|
|
105
|
+
const compact = (text) => String(text ?? "").toLowerCase().replace(/[\s -]+/g, "");
|
|
106
|
+
const rectOf = (element) => {
|
|
107
|
+
const rect = element.getBoundingClientRect();
|
|
108
|
+
return { top: rect.top + window.scrollY, bottom: rect.bottom + window.scrollY, width: rect.width, height: rect.height };
|
|
109
|
+
};
|
|
110
|
+
const shown = (element) => {
|
|
111
|
+
const rect = element.getBoundingClientRect();
|
|
112
|
+
if (rect.width < 2 || rect.height < 2)
|
|
113
|
+
return false;
|
|
114
|
+
const style = getComputedStyle(element);
|
|
115
|
+
return style.display !== "none" && style.visibility !== "hidden";
|
|
116
|
+
};
|
|
117
|
+
const chrome = (element) => !!element.closest("header, nav, footer, [role=banner], [role=navigation], [role=contentinfo]");
|
|
118
|
+
const skipped = new Set(["SCRIPT", "STYLE", "NOSCRIPT", "TEMPLATE"]);
|
|
119
|
+
const PER_ANCHOR = 12;
|
|
120
|
+
const textMatches = (needle) => {
|
|
121
|
+
const found = [];
|
|
122
|
+
// The deepest elements whose text holds the needle: an element none of whose children holds it.
|
|
123
|
+
const walk = (element) => {
|
|
124
|
+
for (const child of Array.from(element.children)) {
|
|
125
|
+
if (found.length >= PER_ANCHOR)
|
|
126
|
+
return;
|
|
127
|
+
if (skipped.has(child.tagName) || !compact(child.textContent).includes(needle))
|
|
128
|
+
continue;
|
|
129
|
+
const before = found.length;
|
|
130
|
+
walk(child);
|
|
131
|
+
if (found.length === before && shown(child))
|
|
132
|
+
found.push(child);
|
|
133
|
+
}
|
|
134
|
+
};
|
|
135
|
+
walk(body);
|
|
136
|
+
return found;
|
|
137
|
+
};
|
|
138
|
+
const decoded = (text) => {
|
|
139
|
+
try {
|
|
140
|
+
return decodeURIComponent(text);
|
|
141
|
+
}
|
|
142
|
+
catch {
|
|
143
|
+
// An address that is no valid escape sequence is compared as it stands.
|
|
144
|
+
return text;
|
|
145
|
+
}
|
|
146
|
+
};
|
|
147
|
+
// An address as it is compared: its letters and digits alone ("aq%20main stage.jpg" and "aq-main-stage.jpg" are one file).
|
|
148
|
+
const loose = (text) => decoded(String(text ?? "")).toLowerCase().replace(/[^a-z0-9]+/g, "");
|
|
149
|
+
const attributeMatches = (selector, attributes, needle) => {
|
|
150
|
+
const found = [];
|
|
151
|
+
for (const element of Array.from(body.querySelectorAll(selector))) {
|
|
152
|
+
if (found.length >= PER_ANCHOR)
|
|
153
|
+
break;
|
|
154
|
+
const said = attributes.map((name) => (name === "currentSrc" ? element.currentSrc : element.getAttribute(name))).filter(Boolean).join(" ");
|
|
155
|
+
if (!loose(said).includes(needle))
|
|
156
|
+
continue;
|
|
157
|
+
const target = element.tagName === "SOURCE" ? (element.parentElement?.querySelector("img") ?? element.parentElement) : element;
|
|
158
|
+
if (target && shown(target) && !found.includes(target))
|
|
159
|
+
found.push(target);
|
|
160
|
+
}
|
|
161
|
+
return found;
|
|
162
|
+
};
|
|
163
|
+
const fileName = /^[^/]+\.(jpe?g|png|gif|webp|avif|svg|bmp|ico|mp4|webm|mov|ashx|pdf)$/i;
|
|
164
|
+
const address = /^(https?:\/\/|\/)\S+$/i;
|
|
165
|
+
const groups = [];
|
|
166
|
+
for (const anchor of Array.isArray(input?.anchors) ? input.anchors : []) {
|
|
167
|
+
if (typeof anchor !== "string")
|
|
168
|
+
continue;
|
|
169
|
+
const said = anchor.trim();
|
|
170
|
+
let matches = [];
|
|
171
|
+
if (address.test(said)) {
|
|
172
|
+
// A link's target: the links that lead there, by the address without its origin, query and fragment.
|
|
173
|
+
const needle = loose(said.replace(/^https?:\/\/[^/]+/i, "").replace(/[?#].*$/, ""));
|
|
174
|
+
if (needle.length >= 5)
|
|
175
|
+
matches = attributeMatches("a[href], iframe[src], form[action]", ["href", "src", "action"], needle);
|
|
176
|
+
// A teaser's link is larger than a link inside a sentence that leads to the same page: the largest first.
|
|
177
|
+
const area = (element) => rectOf(element).width * rectOf(element).height;
|
|
178
|
+
matches.sort((a, b) => area(b) - area(a));
|
|
179
|
+
}
|
|
180
|
+
else if (fileName.test(said)) {
|
|
181
|
+
const needle = loose(said.replace(/\.[a-z0-9]{2,5}$/i, ""));
|
|
182
|
+
if (needle.length >= 5)
|
|
183
|
+
matches = attributeMatches("img, source, video, [style*='background'], [data-src], [data-srcset], [data-bg]", ["src", "srcset", "currentSrc", "data-src", "data-srcset", "data-bg", "poster", "style"], needle);
|
|
184
|
+
}
|
|
185
|
+
else {
|
|
186
|
+
const needle = compact(said).slice(0, 80);
|
|
187
|
+
if (needle.length >= 4)
|
|
188
|
+
matches = textMatches(needle);
|
|
189
|
+
}
|
|
190
|
+
if (matches.length)
|
|
191
|
+
groups.push({ size: compact(said).length, matches });
|
|
192
|
+
}
|
|
193
|
+
if (!groups.length)
|
|
194
|
+
return { found: 0 };
|
|
195
|
+
// The place: the rarest anchor (the longest of equally rare ones), where it stands outside the page's own frame.
|
|
196
|
+
groups.sort((a, b) => a.matches.length - b.matches.length || b.size - a.size);
|
|
197
|
+
const seed = groups[0].matches.find((element) => !chrome(element)) ?? groups[0].matches[0];
|
|
198
|
+
const middle = (element) => {
|
|
199
|
+
const rect = rectOf(element);
|
|
200
|
+
return (rect.top + rect.bottom) / 2;
|
|
201
|
+
};
|
|
202
|
+
const near = groups.slice(1).map((group) => {
|
|
203
|
+
let best = group.matches[0];
|
|
204
|
+
let distance = Infinity;
|
|
205
|
+
for (const element of group.matches) {
|
|
206
|
+
const far = Math.abs(middle(element) - middle(seed)) + (chrome(element) ? 100_000 : 0);
|
|
207
|
+
if (far < distance) {
|
|
208
|
+
best = element;
|
|
209
|
+
distance = far;
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
return { element: best, distance };
|
|
213
|
+
});
|
|
214
|
+
near.sort((a, b) => a.distance - b.distance);
|
|
215
|
+
const maxHeight = typeof input?.maxHeight === "number" && input.maxHeight > 0 ? input.maxHeight : 2_700;
|
|
216
|
+
const kept = [seed];
|
|
217
|
+
let top = rectOf(seed).top;
|
|
218
|
+
let bottom = rectOf(seed).bottom;
|
|
219
|
+
for (const { element } of near) {
|
|
220
|
+
const rect = rectOf(element);
|
|
221
|
+
if (Math.max(bottom, rect.bottom) - Math.min(top, rect.top) > maxHeight)
|
|
222
|
+
continue;
|
|
223
|
+
kept.push(element);
|
|
224
|
+
top = Math.min(top, rect.top);
|
|
225
|
+
bottom = Math.max(bottom, rect.bottom);
|
|
226
|
+
}
|
|
227
|
+
let frame = kept[0];
|
|
228
|
+
for (const element of kept.slice(1))
|
|
229
|
+
while (frame && !frame.contains(element))
|
|
230
|
+
frame = frame.parentElement;
|
|
231
|
+
const whole = (element) => !element || element === body || element === root;
|
|
232
|
+
if (!whole(frame) && rectOf(frame).height <= maxHeight) {
|
|
233
|
+
let block = frame;
|
|
234
|
+
for (;;) {
|
|
235
|
+
const parent = block.parentElement;
|
|
236
|
+
if (whole(parent))
|
|
237
|
+
break;
|
|
238
|
+
const own = rectOf(block);
|
|
239
|
+
const outer = rectOf(parent);
|
|
240
|
+
if (outer.height > maxHeight)
|
|
241
|
+
break;
|
|
242
|
+
const real = own.height >= 150 && own.width >= pageWidth * 0.5;
|
|
243
|
+
// A parent much taller than a real block holds other parts of the page too.
|
|
244
|
+
if (real && outer.height > own.height * 1.6 + 80)
|
|
245
|
+
break;
|
|
246
|
+
block = parent;
|
|
247
|
+
if (outer.height >= 2 && outer.width >= 2)
|
|
248
|
+
frame = block;
|
|
249
|
+
}
|
|
250
|
+
const rect = rectOf(frame);
|
|
251
|
+
if (rect.height >= 2) {
|
|
252
|
+
top = Math.min(top, rect.top);
|
|
253
|
+
bottom = Math.max(bottom, rect.bottom);
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
return { found: groups.length, top, bottom };
|
|
257
|
+
}
|
package/dist/worker.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"worker.d.ts","sourceRoot":"","sources":["../src/worker.ts"],"names":[],"mappings":"AAOA,OAAO,EAAY,KAAK,OAAO,EAAE,MAAM,iBAAiB,CAAC;AACzD,OAAO,KAAK,EAAE,eAAe,EAAE,YAAY,EAAE,MAAM,sBAAsB,CAAC;AAE1E,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,kBAAkB,CAAC;
|
|
1
|
+
{"version":3,"file":"worker.d.ts","sourceRoot":"","sources":["../src/worker.ts"],"names":[],"mappings":"AAOA,OAAO,EAAY,KAAK,OAAO,EAAE,MAAM,iBAAiB,CAAC;AACzD,OAAO,KAAK,EAAE,eAAe,EAAE,YAAY,EAAE,MAAM,sBAAsB,CAAC;AAE1E,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,kBAAkB,CAAC;AAEvD,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,gBAAgB,CAAC;AACtD,OAAO,EAA8E,KAAK,iBAAiB,EAA4B,MAAM,qBAAqB,CAAC;AAEnK,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,qBAAqB,CAAC;AAEzD,OAAO,EAAwC,KAAK,mBAAmB,EAAE,MAAM,mBAAmB,CAAC;AAEnG,eAAO,MAAM,cAAc,eAAe,CAAC;AAE3C,MAAM,MAAM,iBAAiB,GAAG;IAC9B,IAAI,EAAE,eAAe,CAAC;IACtB,6FAA6F;IAC7F,SAAS,CAAC,EAAE,IAAI,CAAC,mBAAmB,EAAE,QAAQ,GAAG,MAAM,CAAC,CAAC;IACzD,kFAAkF;IAClF,MAAM,CAAC,EAAE,MAAM,OAAO,CAAC,eAAe,CAAC,CAAC;IACxC,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,uGAAuG;IACvG,WAAW,CAAC,EAAE,IAAI,CAAC,YAAY,EAAE,SAAS,GAAG,WAAW,CAAC,CAAC;IAC1D,SAAS,CAAC,EAAE,CAAC,KAAK,EAAE,cAAc,KAAK,IAAI,CAAC;IAC5C,QAAQ,CAAC,EAAE,CAAC,MAAM,EAAE,eAAe,KAAK,IAAI,CAAC;CAC9C,CAAC;AAEF,MAAM,MAAM,aAAa,GAAG;IAC1B,MAAM,EAAE,MAAM,CAAC;IACf,GAAG,EAAE,MAAM,CAAC;IACZ,OAAO,EAAE,MAAM,CAAC;IAChB,yEAAyE;IACzE,YAAY,CAAC,EAAE,MAAM,EAAE,CAAC;IACxB,qHAAqH;IACrH,GAAG,CAAC,EAAE,iBAAiB,CAAC;IACxB,iBAAiB,CAAC,EAAE,MAAM,CAAC;IAC3B,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,kHAAkH;IAClH,MAAM,CAAC,EAAE,CAAC,IAAI,EAAE,MAAM,EAAE,KAAK,OAAO,CAAC,OAAO,CAAC,CAAC;IAC9C,GAAG,CAAC,EAAE,CAAC,IAAI,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,KAAK,IAAI,CAAC;IAC9C,KAAK,CAAC,EAAE,OAAO,KAAK,CAAC;IACrB,kIAAkI;IAClI,OAAO,CAAC,EAAE;QAAE,MAAM,CAAC,EAAE,aAAa,CAAC;QAAC,UAAU,CAAC,EAAE,MAAM,CAAC;QAAC,iBAAiB,CAAC,EAAE,MAAM,CAAA;KAAE,CAAC;IACtF,GAAG,CAAC,EAAE,MAAM,MAAM,CAAC;CACpB,CAAC;AAEF,+HAA+H;AAC/H,wBAAgB,WAAW,CAAC,OAAO,EAAE,MAAM,EAAE,MAAM,EAAE,aAAa,GAAG,SAAS,EAAE,MAAM,EAAE,IAAI,CAAC,iBAAiB,EAAE,iBAAiB,GAAG,mBAAmB,CAAC,EACtJ,GAAG,EAAE,MAAM,GAAG,MAAM,GAAG,SAAS,CAOjC;AAED,MAAM,MAAM,aAAa,GAAG;IAAE,IAAI,IAAI,OAAO,CAAC,IAAI,CAAC,CAAC;IAAC,IAAI,EAAE,OAAO,CAAC,IAAI,CAAC,CAAC;IAAC,SAAS,IAAI,eAAe,GAAG,SAAS,CAAA;CAAE,CAAC;AAErH,mHAAmH;AACnH,wBAAgB,aAAa,CAAC,cAAc,CAAC,EAAE,MAAM,UACrC,MAAM,EAAE,sBACvB;AAED,wBAAgB,WAAW,CAAC,OAAO,EAAE,aAAa,GAAG,aAAa,CAwJjE"}
|
package/dist/worker.js
CHANGED
|
@@ -7,6 +7,7 @@
|
|
|
7
7
|
*/
|
|
8
8
|
import { chromium } from "playwright-core";
|
|
9
9
|
import { runCmsJob } from "./cmsJob.js";
|
|
10
|
+
import { BRIDGE_KEPT_MS, keptFor } from "./keptFor.js";
|
|
10
11
|
import { ParheliaClient, ParheliaRefusedError, SSO_FRONT_CODE } from "./parheliaClient.js";
|
|
11
12
|
import { compareVersions } from "./version.js";
|
|
12
13
|
import { writeFileSync, readFileSync, renameSync } from "node:fs";
|
|
@@ -41,6 +42,9 @@ export function startWorker(options) {
|
|
|
41
42
|
problems.push(...(updater?.problems ?? []));
|
|
42
43
|
if (hostRefusal)
|
|
43
44
|
problems.push(hostRefusal);
|
|
45
|
+
// Public websites run with a protection the administrator chose to do without: said on every poll, first in the list.
|
|
46
|
+
if (webReport?.ok && webReport.residual)
|
|
47
|
+
problems.unshift(webReport.residual);
|
|
44
48
|
const webOff = !options.web ? undefined : staleness ?? (webReport && !webReport.ok ? `${webReport.code}: ${webReport.reason}` : undefined);
|
|
45
49
|
return { browserRevision: options.install?.record?.browserRevision, browserReleasedUtc: options.install?.record?.browserReleasedUtc,
|
|
46
50
|
problems: problems.slice(0, 8), ...(webOff ? { webOffReason: webOff } : {}) };
|
|
@@ -65,11 +69,8 @@ export function startWorker(options) {
|
|
|
65
69
|
const launch = options.launch ?? defaultLaunch();
|
|
66
70
|
const stopping = new AbortController();
|
|
67
71
|
const running = new Map();
|
|
68
|
-
|
|
69
|
-
const bridgeScript = () =>
|
|
70
|
-
bridge = undefined;
|
|
71
|
-
throw error;
|
|
72
|
-
}));
|
|
72
|
+
// The host's bridge, asked for again after a while: the host is deployed while the worker runs (keptFor.ts).
|
|
73
|
+
const bridgeScript = keptFor(() => client.bridge(), BRIDGE_KEPT_MS);
|
|
73
74
|
async function runJob(offer) {
|
|
74
75
|
const abort = new AbortController();
|
|
75
76
|
const done = (async () => {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@parhelia/capture",
|
|
3
|
-
"version": "0.4.
|
|
3
|
+
"version": "0.4.13194",
|
|
4
4
|
"description": "Parhelia's capture sidecar: real Chromium screenshots of CMS pages without an open editor. Install with `npx @parhelia/capture install`.",
|
|
5
5
|
"publishConfig": {
|
|
6
6
|
"access": "public"
|
|
@@ -25,6 +25,6 @@
|
|
|
25
25
|
"node": ">=22"
|
|
26
26
|
},
|
|
27
27
|
"dependencies": {
|
|
28
|
-
"playwright-core": "1.
|
|
28
|
+
"playwright-core": "1.65.1"
|
|
29
29
|
}
|
|
30
30
|
}
|