Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

7.1 KB
Raw Blame
import { delay } from 'lib/utils/async'
import { BLANK_IMAGE } from 'lib/utils/captureElementImage'

import { toolbarLogger } from '~/toolbar/shared/toolbarLogger'
import { captureToolbarException } from '~/toolbar/shared/toolbarPosthogJS'
import { TOOLBAR_ROOT_ATTRIBUTE, toError } from '~/toolbar/shared/utils'
import { captureElementScreenshot } from '~/toolbar/shared/utils/screenshot'

export const RESPONSIVE_CAPTURE_WIDTHS = [320, 375, 425, 768, 1024, 1440, 1920]

const PER_WIDTH_SETTLE_TIMEOUT_MS = 4000

const CANVAS_SIZE_ATTR = 'data-ph-canvas-size'

export interface WidthCapture {
    width: number
    blob: Blob
}

function buildReflowIframe(width: number): HTMLIFrameElement {
    const iframe = document.createElement('iframe')
    iframe.setAttribute('sandbox', 'allow-same-origin')
    iframe.setAttribute('aria-hidden', 'true')
    iframe.setAttribute('tabindex', '-1')
    iframe.style.cssText = `position:fixed;top:0;left:-100000px;width:${width}px;height:${window.innerHeight}px;border:0;`
    document.body.appendChild(iframe)
    return iframe
}

function populateIframe(iframe: HTMLIFrameElement): void {
    const doc = iframe.contentDocument
    if (!doc) {
        throw new Error('Reflow iframe has no document')
    }

    const importedHtml = doc.importNode(document.documentElement, true)

    importedHtml.querySelectorAll('script, noscript').forEach((node) => node.remove())
    importedHtml.querySelectorAll(`[${TOOLBAR_ROOT_ATTRIBUTE}]`).forEach((node) => node.remove())

    let head = importedHtml.querySelector('head')
    if (!head) {
        head = doc.createElement('head')
        importedHtml.insertBefore(head, importedHtml.firstChild)
    }
    const base = doc.createElement('base')
    base.href = document.baseURI
    head.insertBefore(base, head.firstChild)

    doc.replaceChild(importedHtml, doc.documentElement)

    copyCanvases(doc)
}

function adoptCanvasSizing(doc: Document, rules: string[]): void {
    const view = doc.defaultView as (Window & typeof globalThis) | null
    if (!view || rules.length === 0) {
        return
    }
    try {
        const sheet = new view.CSSStyleSheet()
        sheet.replaceSync(rules.join('\n'))
        doc.adoptedStyleSheets = [...doc.adoptedStyleSheets, sheet]
    } catch {}
}

function copyCanvases(doc: Document): void {
    const liveCanvases = document.querySelectorAll('canvas')
    const clonedCanvases = doc.querySelectorAll('canvas')
    const sizingRules: string[] = []
    clonedCanvases.forEach((cloned, index) => {
        const live = liveCanvases[index] as HTMLCanvasElement | undefined
        if (!live || live.width === 0 || live.height === 0) {
            return
        }
        cloned.setAttribute(CANVAS_SIZE_ATTR, String(index))
        sizingRules.push(
            `:where([${CANVAS_SIZE_ATTR}="${index}"]) { width: ${live.width}px; aspect-ratio: ${live.width} / ${live.height}; }`
        )
        try {
            const clonedCanvas = cloned as HTMLCanvasElement
            clonedCanvas.width = live.width
            clonedCanvas.height = live.height
            clonedCanvas.getContext('2d')?.drawImage(live, 0, 0)
        } catch {}
    })
    adoptCanvasSizing(doc, sizingRules)
}

function waitForResource(el: HTMLLinkElement | HTMLImageElement, isReady: boolean): Promise<void> {
    if (isReady) {
        return Promise.resolve()
    }
    return new Promise<void>((resolve) => {
        el.addEventListener('load', () => resolve(), { once: true })
        el.addEventListener('error', () => resolve(), { once: true })
    })
}

function blankImages(doc: Document, urls: Set<string>): void {
    for (const image of Array.from(doc.images)) {
        if (urls.has(image.src)) {
            image
                .closest('picture')
                ?.querySelectorAll('source')
                .forEach((source) => {
                    source.srcset = ''
                })
            image.srcset = ''
            image.src = BLANK_IMAGE
        }
    }
}

function collectStalledImages(doc: Document, urls: Set<string>): void {
    for (const image of Array.from(doc.images)) {
        if (!image.complete && image.loading !== 'lazy') {
            urls.add(image.src)
        }
    }
}

async function waitForSettle(iframe: HTMLIFrameElement): Promise<void> {
    const doc = iframe.contentDocument
    if (!doc) {
        return
    }

    const settled = (async (): Promise<void> => {
        const links = Array.from(doc.querySelectorAll<HTMLLinkElement>('link[rel~="stylesheet"]'))
        await Promise.all(links.map((link) => waitForResource(link, !!link.sheet)))
        await delay(0)
        try {
            await doc.fonts?.ready
        } catch {}
        iframe.style.height = `${doc.documentElement.scrollHeight}px`
        await delay(150)
        await Promise.all(
            Array.from(doc.images)
                .filter((img) => img.src !== BLANK_IMAGE)
                .map((img) => waitForResource(img, img.complete))
        )
    })()

    await Promise.race([settled, delay(PER_WIDTH_SETTLE_TIMEOUT_MS)])
}

async function captureWidth(width: number, stalledImageUrls: Set<string>): Promise<Blob> {
    const iframe = buildReflowIframe(width)
    try {
        populateIframe(iframe)
        const doc = iframe.contentDocument
        if (!doc) {
            throw new Error('Reflow iframe document went away')
        }
        blankImages(doc, stalledImageUrls)
        await waitForSettle(iframe)
        collectStalledImages(doc, stalledImageUrls)
        blankImages(doc, stalledImageUrls)
        return await captureElementScreenshot(doc.documentElement, {
            pixelRatio: 1,
            width,
            height: doc.documentElement.scrollHeight,
            backgroundColor: '#ffffff',
        })
    } finally {
        iframe.remove()
    }
}

export async function captureResponsiveScreenshots(
    widths: number[] = RESPONSIVE_CAPTURE_WIDTHS,
    onProgress?: (done: number, total: number) => void
): Promise<WidthCapture[]> {
    const captures: WidthCapture[] = []
    const failures: { width: number; error: Error }[] = []
    let attempted = 0
    const stalledImageUrls = new Set<string>()
    for (const width of widths) {
        try {
            const blob = await captureWidth(width, stalledImageUrls)
            captures.push({ width, blob })
        } catch (e) {
            const error = toError(e, `Responsive heatmap capture failed at ${width}px`)
            failures.push({ width, error })
            toolbarLogger.warn('responsive_screenshot', 'Width capture failed', {
                width,
                error: error.message,
            })
        }
        attempted++
        onProgress?.(attempted, widths.length)
    }

    // One exception per run, not one per width, so the occurrence count equals the number of
    // partly-failed captures rather than the number of widths a page happens to fail at.
    if (failures.length > 0) {
        captureToolbarException(failures[0].error, 'responsive_screenshot', {
            failed_widths: failures.map((failure) => failure.width),
            captured_count: captures.length,
            requested_count: widths.length,
        })
    }

    return captures
}