-
Notifications
You must be signed in to change notification settings - Fork 0
/
highlight.js
78 lines (63 loc) · 2.83 KB
/
highlight.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
let highlightElement = false;
const highlightClassName = "highlight-tekvision";
const highlightStyle = "outline: 5px solid red";
export const highlightInit = () => {
// Create and append the style element for the highlight class
const styleElementWithHighlightClass = document.createElement("style");
styleElementWithHighlightClass.innerHTML = `.${highlightClassName} { ${highlightStyle} !important; }`;
document.head.appendChild(styleElementWithHighlightClass);
};
// Highlight elements
const highlightElements = () => {
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));
alert(`Successfully highlighted ${elements.length} elements(s).`);
} else {
alert("No elements found for the provided selector. Please try again.");
highlightElement = false;
}
};
// Function to unhighlight elements
const unhighlightElements = () => {
const elements = document.querySelectorAll(`.${highlightClassName}`);
elements.forEach(element => element.classList.remove(highlightClassName));
alert("Highlight removed from all elements.");
};
function getVisibilityPercentage(element) {
const rect = element.getBoundingClientRect();
const viewportHeight = window.innerHeight;
const viewportWidth = window.innerWidth;
// Calculate the intersection area with the viewport
const visibleWidth = Math.max(0, Math.min(rect.right, viewportWidth) - Math.max(rect.left, 0));
const visibleHeight = Math.max(0, Math.min(rect.bottom, viewportHeight) - Math.max(rect.top, 0));
const visibleArea = visibleWidth * visibleHeight;
const totalArea = rect.width * rect.height;
// Calculate the percentage of the element that is visible
return (totalArea > 0) ? (visibleArea / totalArea) * 100 : 0;
}
export function getElementVisibilityDetails() {
const elements = document.querySelectorAll('.' + highlightClassName);
let details = "";
let summary = "";
let visibleElementCount = 0;
elements.forEach((element, index) => {
const visibilityPercentage = getVisibilityPercentage(element);
if (visibilityPercentage > 0) {
visibleElementCount++;
summary += `${visibleElementCount}. Element: "${element.innerText.trim()}" - Visibility: ${visibilityPercentage.toFixed(2)}%\n`;
}
});
if (visibleElementCount === 0) {
details = "No highlighted elements are currently visible in the viewport. Please ensure that you've navigated to the right element.";
} else {
details = `${visibleElementCount} highlighted element(s) are currently visible in the viewport:\n${summary}`;
}
prompt("", details);
};
// Toggle highlight mode
export const toggleHighlight = () => {
highlightElement = !highlightElement;
highlightElement ? highlightElements() : unhighlightElements();
};