-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathScreenshotToMap.ts
84 lines (75 loc) · 2.54 KB
/
ScreenshotToMap.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
/**
* This strategy allows to take a screenshot of the page and then parse it to extract the map of the page with selected elements.
* This is akin to what visualping.io does.
*/
import path from "path";
import { ElementHandle, Page } from "playwright";
import { cssPath, xPath as getXPath } from "playwright-dompath";
import { ScrapingStrategy } from "../types";
import { getRandomFileName } from "../utils/utils";
const sizeOf = require("image-size");
const sizeOfBuffer = require("buffer-image-size");
type BoundingBoxReturn = NonNullable<Awaited<ReturnType<ElementHandle["boundingBox"]>>>;
export type ScreenshotMap = BoundingBoxReturn & {
cssSelector?: string;
xPath?: string;
zIndex: number;
};
export type ImageSizeType = {
width: number;
height: number;
type: string;
};
export class ScreenshotToMapStrategy
implements ScrapingStrategy<Promise<[Buffer | string, ImageSizeType, ScreenshotMap[]]>>
{
selectors: string[];
includexpath: boolean = false;
includeCSSSelectors: boolean = true;
fileName?: string;
filePath?: string;
constructor(selectors: string[], includeCSSSelectors?: boolean) {
this.selectors = selectors;
this.includeCSSSelectors = includeCSSSelectors || false;
}
asFile(filePath: string, fileName?: string): this {
this.fileName = fileName || `${getRandomFileName()}.png`;
this.filePath = filePath || "./";
return this;
}
getZIndex(xpath: string): number {
return xpath.split("/").length;
}
async execute(page: Page): Promise<[Buffer | string, ImageSizeType, ScreenshotMap[]]> {
let imgResult: Buffer | string;
let imgSize: ImageSizeType | undefined;
if (this.fileName) {
let savePath = path.join(path.normalize(this.filePath as string), this.fileName);
await page.screenshot({
path: savePath,
fullPage: true,
});
imgResult = savePath;
imgSize = sizeOf(savePath) as ImageSizeType;
} else {
imgResult = await page.screenshot({
fullPage: true,
});
imgSize = sizeOfBuffer(imgResult) as ImageSizeType;
}
const toReturn: ScreenshotMap[] = [];
for (const selector of this.selectors) {
const elements = await page.$$(selector);
for (const element of elements) {
const boundingBox = await element.boundingBox();
if (!boundingBox) continue;
const xPath = await getXPath(element);
if (!xPath || xPath == "") continue;
const cssSelector = this.includeCSSSelectors ? await cssPath(element) : undefined;
const zIndex = this.getZIndex(xPath);
toReturn.push({ ...boundingBox, cssSelector, xPath, zIndex });
}
}
return [imgResult, imgSize, toReturn];
}
}