Recently, I created a basic code/userscript to receive notifications about any changes on a specific website:
function notifier(){
setTimeout(function () {
location.reload(true);
},60000)
}
function notiCounter() {
console.log("Counting notifications");
var noti = document.getElementsByClassName("notification");
for(var i = 0; i < 2; i++) {
if(noti[i].innerHTML != undefined) {
console.log(noti[i].innerHTML);
notifications++;
console.log("Notifications: " + notifications);
}
}
}
function notification(){
setTimeout(function () {
notiCounter();
if(notifications > 0){
document.title = "(" + notifications + ") new notifcations";
sound.play();
}
notifier();
},50)
}
notification();
However, the issue arises with the dynamic nature of the final number of noti[i]
, which keeps changing. If I replace i < 2
with a higher number, the loop turns into an infinite one. On the other hand, setting it too low (like 2), means that data might be lost if the actual number goes beyond 2.
Do you have any suggestions to address this problem? It's probably quite obvious, but sometimes things tend to get blurry when working late at night!