UNPKG

@bugspotter/sdk

Version:

Professional bug reporting SDK with screenshots, session replay, and automatic error capture for web applications

53 lines (52 loc) 2.56 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ScreenshotCapture = void 0; const html_to_image_1 = require("html-to-image"); const base_capture_1 = require("./base-capture"); const compress_1 = require("../core/compress"); const DEFAULT_SCREENSHOT_OPTIONS = { quality: 0.8, cacheBust: true, backgroundColor: '#ffffff', excludeAttribute: 'data-bugspotter-exclude', errorPlaceholder: 'SCREENSHOT_FAILED', }; class ScreenshotCapture extends base_capture_1.BaseCapture { constructor(options = {}) { super(options); } getErrorPlaceholder() { var _a; return (_a = this.options.errorPlaceholder) !== null && _a !== void 0 ? _a : DEFAULT_SCREENSHOT_OPTIONS.errorPlaceholder; } shouldIncludeNode(node) { if (!('hasAttribute' in node)) { return true; } const element = node; const excludeAttr = this.options.excludeAttribute || DEFAULT_SCREENSHOT_OPTIONS.excludeAttribute; return !element.hasAttribute(excludeAttr); } buildCaptureOptions() { var _a, _b, _c, _d; return Object.assign(Object.assign(Object.assign({ quality: (_a = this.options.quality) !== null && _a !== void 0 ? _a : DEFAULT_SCREENSHOT_OPTIONS.quality, cacheBust: (_b = this.options.cacheBust) !== null && _b !== void 0 ? _b : DEFAULT_SCREENSHOT_OPTIONS.cacheBust, pixelRatio: (_c = this.options.pixelRatio) !== null && _c !== void 0 ? _c : window.devicePixelRatio, backgroundColor: (_d = this.options.backgroundColor) !== null && _d !== void 0 ? _d : DEFAULT_SCREENSHOT_OPTIONS.backgroundColor }, (this.options.width && { width: this.options.width })), (this.options.height && { height: this.options.height })), { filter: (node) => { return this.shouldIncludeNode(node); } }); } async capture(targetElement) { try { const element = targetElement || this.options.targetElement || document.body; const options = this.buildCaptureOptions(); const dataUrl = await (0, html_to_image_1.toPng)(element, options); // Compress the screenshot to reduce payload size // Converts to WebP if supported, resizes if too large const compressed = await (0, compress_1.compressImage)(dataUrl); return compressed; } catch (error) { this.handleError('capturing screenshot', error); return this.getErrorPlaceholder(); } } } exports.ScreenshotCapture = ScreenshotCapture;