Skip to content

Commit 0cebacc

Browse files
add locators if they are in page's range
1 parent 95a90c3 commit 0cebacc

File tree

1 file changed

+29
-6
lines changed

1 file changed

+29
-6
lines changed

src/lib/processSnapshot.ts

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ export default async function processSnapshot(snapshot: Snapshot, ctx: Context):
5656
}
5757
}
5858
const page = await context.newPage();
59+
let height = 0;
5960

6061
// populate cache with already captured resources
6162
let cache: Record<string, any> = {};
@@ -284,6 +285,17 @@ export default async function processSnapshot(snapshot: Snapshot, ctx: Context):
284285

285286
}
286287
if (ctx.config.cliEnableJavaScript && fullPage) await page.evaluate(scrollToBottomAndBackToTop, { frequency: 100, timing: ctx.config.scrollTime });
288+
// Calculate the height of the content
289+
height = await page.evaluate(() => {
290+
const body = document.body;
291+
const html = document.documentElement;
292+
return Math.max(
293+
body.scrollHeight, body.offsetHeight,
294+
html.clientHeight, html.scrollHeight, html.offsetHeight
295+
);
296+
});
297+
298+
ctx.log.debug(`Calculated content height: ${height}`);
287299

288300
try {
289301
await page.waitForLoadState('networkidle', { timeout: 5000 });
@@ -314,12 +326,23 @@ export default async function processSnapshot(snapshot: Snapshot, ctx: Context):
314326
}
315327
for (const locator of locators) {
316328
let bb = await locator.boundingBox();
317-
if (bb) processedOptions[ignoreOrSelectBoxes][viewportString].push({
318-
left: bb.x,
319-
top: bb.y,
320-
right: bb.x + bb.width,
321-
bottom: bb.y + bb.height
322-
});
329+
if (bb) {
330+
// Calculate top and bottom from the bounding box properties
331+
const top = bb.y;
332+
const bottom = bb.y + bb.height;
333+
334+
// Only push if top and bottom are within the calculated height
335+
if (top <= height && bottom <= height) {
336+
processedOptions[ignoreOrSelectBoxes][viewportString].push({
337+
left: bb.x,
338+
top: top,
339+
right: bb.x + bb.width,
340+
bottom: bottom
341+
});
342+
} else {
343+
ctx.log.debug(`Bounding box for selector skipped due to exceeding height: ${JSON.stringify({ top, bottom, height })}`);
344+
}
345+
}
323346
}
324347
}
325348
ctx.log.debug(`Processed options: ${JSON.stringify(processedOptions)}`);

0 commit comments

Comments
 (0)