take-shot
Version:
Screenshots with JavaScript
50 lines • 2.27 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.parseDocumentSize = exports.parseBounds = exports.Bounds = void 0;
class Bounds {
constructor(left, top, width, height) {
this.left = left;
this.top = top;
this.width = width;
this.height = height;
}
add(x, y, w, h) {
return new Bounds(this.left + x, this.top + y, this.width + w, this.height + h);
}
static fromClientRect(context, clientRect) {
return new Bounds(clientRect.left + context.windowBounds.left, clientRect.top + context.windowBounds.top, clientRect.width, clientRect.height);
}
static fromDOMRectList(context, domRectList) {
const domRect = Array.from(domRectList).find((rect) => rect.width !== 0);
return domRect
? new Bounds(domRect.left + context.windowBounds.left, domRect.top + context.windowBounds.top, domRect.width, domRect.height)
: Bounds.EMPTY;
}
}
exports.Bounds = Bounds;
Bounds.EMPTY = new Bounds(0, 0, 0, 0);
const parseBounds = (context, node) => {
// @ts-ignore
const style = node.style;
const rect = JSON.parse(JSON.stringify(node.getBoundingClientRect()));
const width = parseInt(style?.width, 10);
const height = parseInt(style?.height, 10);
if (!rect.width && width)
rect.width = width;
if (!rect.height && height)
rect.height = height;
return Bounds.fromClientRect(context, rect);
};
exports.parseBounds = parseBounds;
const parseDocumentSize = (document) => {
const body = document.body;
const documentElement = document.documentElement;
if (!body || !documentElement) {
throw new Error(`Unable to get document size`);
}
const width = Math.min(Math.min(body.scrollWidth, documentElement.scrollWidth), Math.min(body.offsetWidth, documentElement.offsetWidth), Math.min(body.clientWidth, documentElement.clientWidth));
const height = Math.min(Math.min(body.scrollHeight, documentElement.scrollHeight), Math.min(body.offsetHeight, documentElement.offsetHeight), Math.min(body.clientHeight, documentElement.clientHeight));
return new Bounds(0, 0, width, height);
};
exports.parseDocumentSize = parseDocumentSize;
//# sourceMappingURL=bounds.js.map