Files
quest-reader/web/main.ts
T
2022-02-19 23:51:04 +07:00

38 lines
1.4 KiB
TypeScript

class VisitAnalytics {
observer;
constructor() {
window.plausible = window.plausible || function () { (window.plausible.q = window.plausible.q || []).push(arguments); };
if (document.readyState == "interactive")
this.init()
else
document.addEventListener("DOMContentLoaded", () => this.init(), false);
}
init() {
let options = {
root: null,
rootMargin: "0px",
threshold: 1.0
};
this.observer = new IntersectionObserver((entries, observer) => this.handleIntersect(entries, observer), options);
var all = document.querySelectorAll(".chapter-announce");
all.forEach(elem => this.observer.observe(elem));
this.observer.observe(document.querySelector("footer"));
console.log("Intersection observer ready");
}
handleIntersect(entries: IntersectionObserverEntry[], observer: IntersectionObserver) {
entries.filter(e => e.isIntersecting).forEach(e => {
if (e.target.className == "chapter-announce" && !e.target.nextElementSibling.querySelector("img").complete)
return;
window.plausible("landmark", {props: {id: e.target.id ? e.target.id : e.target.tagName}});
observer.unobserve(e.target);
console.log("Reached landmark " + e.target.id ? e.target.id : e.target.tagName);
}
);
}
}
new VisitAnalytics();