Supercharge your AI agents with data from the web and beyond. Building the library for superintelligence. 🔥
TypeScript aiai-agentsai-crawlerai-scrapingai-searchcrawlerdata-extractionhtml-to-markdownllmmarkdownscraperscrapingweb-crawlerweb-dataweb-data-extractionweb-scraperweb-scrapingweb-searchwebscraping
fix(branding): measure button positions as on a real screen (#5093)
Abimael Martell11e9dc5e2956
diff --git a/apps/api/src/__tests__/lib/branding/branding-script-buttons.test.ts b/apps/api/src/__tests__/lib/branding/branding-script-buttons.test.tsindex 6f5f5432b..180700ddd 100644--- a/apps/api/src/__tests__/lib/branding/branding-script-buttons.test.ts+++ b/apps/api/src/__tests__/lib/branding/branding-script-buttons.test.ts@@ -5,7 +5,7 @@ import { getBrandingScript } from "../../../scraper/scrapeURL/engines/fire-engin// jsdom has no layout: elements get a box from data-top / data-w / data-h, and// anything without data-top has an empty box (not visible).-function scan(body: string, scrollY = 0) {+function scan(body: string, scrollY = 0, innerHeight = 1080) {const dom = new JSDOM(`<html><head><title>Acme</title></head><body>${body}</body></html>`,{ runScripts: "outside-only", url: "https://acme.test/" },@@ -39,6 +39,7 @@ function scan(body: string, scrollY = 0) {};};Object.defineProperty(window, "scrollY", { value: scrollY });+ Object.defineProperty(window, "innerHeight", { value: innerHeight });const result = window.eval(getBrandingScript());return result.branding.snapshots as Array<{text: string;@@ -112,4 +113,33 @@ describe("branding script button detection", () => {expect(byText(snaps, "Sign up")?.position.top).toBe(10);expect(byText(snaps, "Read more")?.position.top).toBe(900);});++ it("reports positions in a very tall window as they would be on a screen", () => {+ // Branding scrapes render 100,000px tall: a hero sized to the window is+ // that tall, with its centered button halfway down.+ const snaps = scan(+ `<section data-top="0" data-w="1920" data-h="100000">+ <button data-top="40">Log in</button>+ <button data-top="50000">Start free trial</button>+ </section>+ <section data-top="100000" data-w="1920" data-h="2000">+ <button data-top="100400">Read the docs</button>+ </section>+ <div style="position: fixed" data-top="50000" data-w="600" data-h="300">+ <button data-top="50100">Accept</button>+ </div>+ <div style="position: fixed" data-top="900" data-w="600" data-h="60">+ <button data-top="900">Chat with us</button>+ </div>`,+ 0,+ 100_000,+ );++ expect(byText(snaps, "Log in")?.position.top).toBe(40);+ expect(byText(snaps, "Start free trial")?.position.top).toBeCloseTo(540, 0);+ expect(byText(snaps, "Read the docs")?.position.top).toBeCloseTo(1480, 0);+ expect(byText(snaps, "Accept")?.position.top).toBeCloseTo(541, 0);+ // Fixed in pixels, not by the window's height: kept as is.+ expect(byText(snaps, "Chat with us")?.position.top).toBe(900);+ });});diff --git a/apps/api/src/scraper/scrapeURL/engines/fire-engine/branding-script/buttons.ts b/apps/api/src/scraper/scrapeURL/engines/fire-engine/branding-script/buttons.tsindex 7cf398e05..30d7148a1 100644--- a/apps/api/src/scraper/scrapeURL/engines/fire-engine/branding-script/buttons.ts+++ b/apps/api/src/scraper/scrapeURL/engines/fire-engine/branding-script/buttons.ts@@ -42,9 +42,76 @@ export const isViewportAnchored = (el: Element): boolean => {return false;};+// Branding scrapes render in a very tall window (100,000px), which stretches+// anything sized to the window: a hero with `min-height: 100vh` is 100,000px+// tall, with its centered button 50,000px down. In such a window, positions+// are reported as they would be on a screen of this height.+const SCREEN_PX = 1080;+const isTallWindow = (): boolean => window.innerHeight > 10 * SCREEN_PX;++type ScreenBlock = { top: number; height: number };+let screenBlocks: ScreenBlock[] | null = null;++/** The outermost elements sized to the window: on a real screen, one screen tall. */+const getScreenBlocks = (): ScreenBlock[] => {+ if (screenBlocks) return screenBlocks;+ const windowHeight = window.innerHeight;+ const blocks: ScreenBlock[] = [];+ const queue: Element[] = document.body+ ? Array.from(document.body.children)+ : [];+ // Only elements at least half the window tall can contain a block.+ for (let i = 0; i < queue.length && i < 2000; i++) {+ const rect = queue[i].getBoundingClientRect();+ if (rect.height < windowHeight / 2) continue;+ if (rect.height <= windowHeight * 1.01) {+ blocks.push({+ top: rect.top + (window.scrollY || 0),+ height: rect.height,+ });+ } else {+ queue.push(...Array.from(queue[i].children));+ }+ }+ screenBlocks = blocks.sort((a, b) => a.top - b.top);+ return screenBlocks;+};++/**+ * An offset within something as tall as the window. Offsets set in pixels+ * (normal flow, `top: 80px`) are kept; offsets that come from the window's+ * height (centered, pinned to the bottom) scale with it.+ */+const toScreenOffset = (offset: number): number =>+ offset < 0.3 * window.innerHeight+ ? offset+ : (offset * SCREEN_PX) / window.innerHeight;++/** A page position in the tall window, mapped to a page of real screens. */+const toScreenPosition = (top: number): number => {+ const scale = SCREEN_PX / window.innerHeight;+ let removed = 0;+ let end = -Infinity;+ for (const block of getScreenBlocks()) {+ if (block.top < end) continue; // inside an earlier block+ end = block.top + block.height;+ if (top < block.top) break;+ if (top < end) {+ return block.top - removed + toScreenOffset(top - block.top);+ }+ removed += block.height * (1 - scale);+ }+ return top - removed;+};+/** Distance from the top of the page, or of the screen for fixed/sticky elements. */-export const pageTop = (el: Element, rect: DOMRect): number =>- isViewportAnchored(el) ? rect.top : rect.top + (window.scrollY || 0);+export const pageTop = (el: Element, rect: DOMRect): number => {+ if (isViewportAnchored(el)) {+ return isTallWindow() ? toScreenOffset(rect.top) : rect.top;+ }+ const top = rect.top + (window.scrollY || 0);+ return isTallWindow() ? toScreenPosition(top) : top;+};/** The first opaque background behind an element (its ancestors'), white if none. */const getBackgroundBehind = (el: Element): string => {