A while ago I took one of @Simon's scripts and updated it to make it more geared towards normal users (no offense mods) and this Question was created. But the link to the Github is no longer correct and the code has changed quite a bit to get it to work on Firefox and Chrome (and Opera and IceDragon and Safari).
I would like to know if my code is following UserScript and JavaScript standards and if there is anything that I can do to improve the efficiency of the code itself.
Here is the current Next version
Notification.requestPermission();
var KEY_NEXT = 'NextReload';
var DELAY = 120 * 1000; //120,000 milliseconds = 2 Minutes
var currentTime = Date.now ? Date.now() : new Date().getTime();
var lastTime = GM_getValue(KEY_NEXT, 0);
var nextTime = currentTime + DELAY;
GM_setValue(KEY_NEXT, nextTime);
var timeDiff = Math.abs(lastTime - currentTime);
setTimeout(function(){
window.location.reload();
}, DELAY);
var notificationTitle = (document.title.split(' - ')[1] + ' Review Queue').replace(' Stack Exchange', '.SE');
// a way to detect that the script is being executed because of an automatic script reload, not by the user.
if (timeDiff <= DELAY * 2) {
var reviewCount = 0;
var reviewItems = document.getElementsByClassName('dashboard-num');
for (var i = 0; i < reviewItems.length; i++){
if (reviewItems[i].parentNode.className != 'dashboard-count dashboard-faded'){
reviewCount += parseInt((reviewItems[i].getAttribute("title")).replace(',', ''), 10);
console.log(reviewItems[i]);
}
}
console.log(reviewCount);
if (reviewCount > 0) {
var details = {
body: reviewCount + ' Review Items',
icon: 'https://github.com/malachi26/ReviewQueueNotifier/raw/master/Resources/Icon2.jpg'
}
var n = new Notification(notificationTitle, details);
n.onclick = function(){
window.focus();
this.cancel();
}
setTimeout(n.close.bind(n), 100000); // Magic number is time to notification disappear
}
}
You can follow development or participate in development at Github as well.
for
loop with aforEach
, and replace the embeddedif
statement with a returning guard clause for the case you want to ignore. This saves you a level of nesting and improves clarity. If you need old browser support and don't want to include the forEach polyfill, that's a decent recent to keep it as is. \$\endgroup\$