-
Notifications
You must be signed in to change notification settings - Fork 0
/
highlightAndCapture.js
63 lines (54 loc) · 2.01 KB
/
highlightAndCapture.js
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
const highlightClassName = "highlight-tekvision";
export const highlightAndCaptureInit = () => {
try {
// Include HTML2Canvas library by injecting a script tag
const script = document.createElement("script");
script.src = "https://cdnjs.cloudflare.com/ajax/libs/html2canvas/1.4.1/html2canvas.min.js";
document.head.appendChild(script);
script.onload = function () {
console.log("Successfully imported HTML2Canvas library!");
}
} catch (error) {
alert("Unable to automatically capture screenshots on this web page. Please use the manual capture feature ('Alt + 2').\n Error: " + error);
}
};
// Highlight elements
export const highlightElementsAndCaptureScreenshot = async () => {
// Removing existing highlighted elements
unhighlightElements();
const elementSelector = prompt("Enter the CSS selector of the elements you want to highlight:");
const elements = document.querySelectorAll(elementSelector);
if (elements.length > 0) {
elements.forEach(element => element.classList.add(highlightClassName));
elements[0].scrollIntoViewIfNeeded( { behavior: "scroll"} );
// Capture screenshot of the viewport using html2canvas
const canvas = await html2canvas(document.body, {
width: window.innerWidth,
height: window.innerHeight,
x: window.scrollX,
y: window.scrollY,
windowWidth: window.innerWidth,
windowHeight: window.innerHeight
});
// Converting the canvas to a blob
canvas.toBlob(async function (blob) {
try {
await navigator.clipboard.write([
new ClipboardItem({
'image/png': blob
})
]);
alert("Screenshot captured and copied to clipboard!");
} catch (error) {
alert("Failed to copy screenshot to clipboard: ", error);
}
});
} else {
alert("No elements found for the provided selector. Please try again.");
}
};
// Function to unhighlight elements
const unhighlightElements = () => {
const elements = document.querySelectorAll(`.${highlightClassName}`);
elements.forEach(element => element.classList.remove(highlightClassName));
};