victorwesterlund.com/public/assets/js/modules/Logging.mjs
2021-09-05 18:21:27 +02:00

35 lines
No EOL
701 B
JavaScript

// Copyright © Victor Westerlund - No libraries! 😲
class Logging {
constructor() {
this.endpoint = "/log/";
this.data = new URLSearchParams();
document.addEventListener("visibilitychange",() => {
if(document.visibilityState === "hidden") {
this.send();
}
});
this.log("foo","bar");
}
log(key,value) {
this.data.append(key,value);
}
send() {
const send = navigator.sendBeacon(this.endpoint,this.data);
if(send !== true) {
this.log("mode","fallback");
const url = this.endpoint + this.data.toString();
fetch(url).catch(response => console.log(response));
}
}
}
export default class Log {
constructor(value,key = "u") {
console.log(key,value);
}
}