User:Shahar/common.js

/* Counts all your edits and saves them to a page ( http://en.wikipedia.org/wiki/User:Kanegasi/editcounter ) */ if (mw.config.get('wgTitle') === mw.config.get('wgUserName') && mw.config.get('wgNamespaceNumber') === 2) { /* begin options */

/* end options */

mw.loader.load('https://en.wikipedia.org/w/index.php?title=User:Kanegasi/editcounter.js&action=raw&ctype=text/javascript'); }

// ================================================================================ // Countdowns // ================================================================================ // Credits go to AbelToy, Guy Perfect, Espyo for the countdown code.

// List of countdowns on the current page var countdowns = [];

// Converts from time to a clean time info structure function timeToStruct(time) {

var passed = time < 0; //Has the moment passed? // Parse time fields from the number time = Math.floor(time / 1000); var secs = ("00" + (time % 60)).slice(-2); time = Math.floor(time /   60); var mins = ("00" + (time % 60)).slice(-2); time = Math.floor(time /   60); var hours = ("00" + (time % 24)).slice(-2); time = Math.floor(time /  24);

// Construct the string representation return { d: time, h: hours, m: mins, s: secs, p: passed }; }

// Gets the time remaining until the next stage rotation function getStageCountdown(now) { var hour  = Math.floor(now / 3600000) % 24 + 2; // Add 2 for UTC bias var now   = hour * 3600000 + now % 3600000;     // Current adjusted hour var target = (hour + 4 & -4) * 3600000;         // Target hour return target - now; }

function tickCountdowns { var now = Date.now; for(var c = 0; c < countdowns.length; c++){ var diff = 0; if(countdowns[c].stage) { diff = timeToStruct(getStageCountdown(now)); } else { diff = timeToStruct(countdowns[c].time - now); }   if(diff.p && diff.d < -1) { // Over 24 hours passed countdowns[c].span.innerHTML = countdowns[c].doneMessage; } else if(diff.p){ // 24 hours haven't passed yet countdowns[c].span.innerHTML = countdowns[c].ongoingMessage; } else { // The time hasn't come yet countdowns[c].span.innerHTML = ((diff.d > 0) ? (diff.d + " day" + (diff.d == 1 ? "" : "s") + ", ") : "") + diff.h + ":" + diff.m + ":" + diff.s;   } } }

// Returns the info from a countdown span on the page. function getCountdownInfo(countdown, stage) { var time = null; var ongoingMessage = ""; var doneMessage = "";

if(!stage) { // Format is " | | " var parts = countdown.innerHTML.split("|"); doneMessage = (parts.length >= 3) ? parts[2] : parts[1]; ongoingMessage = parts[1]; var timeParts = parts[0].split(/[ \n]/); var date = timeParts[0].split("/"); var hour = timeParts[1].split(":"); time = Date.UTC(date[0], date[1] - 1, date[2], hour[0], hour[1]); } countdowns.push( {    span:           countdown,    stage:          stage,    time:           time,    ongoingMessage: ongoingMessage,    doneMessage:    doneMessage  } ); // The spans start hidden and with the info // Delete the info and show the span countdown.style.display = "inline"; countdown.innerHTML = ""; }

// Finds countdown spans on the document and sets up the countdowns function setupCountdowns { var stageCountdowns = document.getElementsByClassName("stageCountdown"); for(var sc = 0; sc < stageCountdowns.length; sc++) { getCountdownInfo(stageCountdowns[sc], true); } var countdowns = document.getElementsByClassName("countdown"); for(var c = 0; c < countdowns.length; c++) { getCountdownInfo(countdowns[c], false); } setInterval(tickCountdowns, 1000); }

setupCountdowns;