mirror of
https://github.com/jakejarvis/jarv.is.git
synced 2025-07-03 17:46:39 -04:00
super simple serverless hit counter for fun (#410)
This commit is contained in:
31
assets/js/counter.js
Normal file
31
assets/js/counter.js
Normal file
@ -0,0 +1,31 @@
|
||||
(function () {
|
||||
// don't continue if there isn't a span#meta-hits element on this page
|
||||
var wrapper = document.getElementById("meta-hits");
|
||||
|
||||
if (wrapper) {
|
||||
wrapper.style.display = "inline-block";
|
||||
|
||||
// deduce a consistent identifier for this page, no matter the URL
|
||||
var canonical = document.createElement("a");
|
||||
canonical.href = document.querySelector("link[rel='canonical']").href;
|
||||
|
||||
// strip beginning and ending forward slash
|
||||
var slug = canonical.pathname.slice(1, -1);
|
||||
|
||||
// this will return an error from the API anyways
|
||||
if (!slug || slug === "/") return;
|
||||
|
||||
fetch("/api/hits?slug=" + slug)
|
||||
.then((response) => response.json())
|
||||
.then((data) => {
|
||||
// finally inject the hits and hide the loading spinner
|
||||
var spinner = document.getElementById("hit-spinner");
|
||||
var counter = document.getElementById("hit-counter");
|
||||
|
||||
spinner.style.display = "none";
|
||||
wrapper.title = data.pretty_hits + " " + data.pretty_unit;
|
||||
counter.appendChild(document.createTextNode(data.pretty_hits));
|
||||
})
|
||||
.catch((error) => {});
|
||||
}
|
||||
})();
|
Reference in New Issue
Block a user