: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.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
import { captureElementImage } from 'lib/utils/captureElementImage'
import { toolbarUploadMedia } from '~/toolbar/shared/toolbarFetch'
import { isToolbarRootElement, toError } from '~/toolbar/shared/utils'
export interface ElementScreenshot {
mediaId: string
}
function screenshotFilter(node: Node): boolean {
return !(node instanceof HTMLElement && isToolbarRootElement(node))
}
export interface CaptureOptions {
pixelRatio?: number
width?: number
height?: number
backgroundColor?: string
}
function describeElement(element: HTMLElement): string {
return `${element.tagName.toLowerCase()}${element.id ? `#${element.id}` : ''}`
}
export async function captureElementScreenshot(element: HTMLElement, options?: CaptureOptions): Promise<Blob> {
try {
return await captureElementImage(element, {
type: 'image/jpeg',
quality: 0.7,
filter: screenshotFilter,
backgroundColor: '#ffffff',
...options,
})
} catch (error) {
// html-to-image rejects with a raw DOM Event when a resource on the page fails to load.
// Rethrow a real Error so the failure reaches error tracking with a message and a stack.
throw toError(error, `Failed to capture screenshot of ${describeElement(element)}`)
}
}
export async function uploadScreenshot(blob: Blob): Promise<ElementScreenshot> {
const file = new File([blob], `screenshot-${Date.now()}.jpg`, { type: 'image/jpeg' })
const { id } = await toolbarUploadMedia(file)
return { mediaId: id }
}
export async function captureAndUploadElementScreenshot(element: HTMLElement): Promise<ElementScreenshot> {
const blob = await captureElementScreenshot(element)
return uploadScreenshot(blob)
}