function isElementVisible(element) {
const style = window.getComputedStyle(element);
const isDisplayVisible = style.display !== "none";
const isVisibilityVisible = style.visibility !== "hidden";
const isOpacityVisible = style.opacity !== "0";
const rect = element.getBoundingClientRect();
const isInViewport =
rect.top >= 0 &&
rect.left >= 0 &&
rect.bottom <= (window.innerHeight || document.documentElement.clientHeight) &&
rect.right <= (window.innerWidth || document.documentElement.clientWidth);
return isDisplayVisible && isVisibilityVisible && isOpacityVisible && isInViewport;
}
function isParentVisible(element) {
let parent = element.parentElement;
while (parent) {
const style = window.getComputedStyle(parent);
if (style.display === "none" || style.visibility === "hidden" || style.opacity === "0") {
return false;
}
parent = parent.parentElement;
}
return true;
}
function getVisibleElements() {
const elements = document.querySelectorAll("*");
const visibleElements = [];
elements.forEach((element) => {
if (isParentVisible(element) && isElementVisible(element)) {
visibleElements.push(element);
}
});
return visibleElements;
}
const visibleElements = getVisibleElements();
console.log(visibleElements);