User:ObserveOwl/LiveCountdownSW.js

// script derived from User:ToxiBoi/LiveCountdown.js var RefreshingAlready = false; var alreadyScanned = false; var MainLoop = function (index) { setInterval(function {    // Setup again    endDate = new Date(index.getAttribute("data-end")).getTime;    now = new Date.getTime;    distance = endDate - now;    if (!alreadyScanned) {    	alreadyScanned = true;	    var sanitizedCode = encodeURIComponent(index.getAttribute("data-event"));	    $.post( "https:" + mw.config.get( "wgServer" ) + "/w/api.php?action=parse&format=json&title=" + mw.config.get('wgPageName') + "&text=" + sanitizedCode + "&pst=1", function ( res ) { if ( !res || !res.parse || !res.parse.text ) return console.log( "Preview failed" ); var StrippedText = res.parse.text['*']; //Trim off div/p tags var endTrim = StrippedText.search(/<\/p>/g)-1; StrippedText = StrippedText.substring(33,endTrim); index.setAttribute("data-event",StrippedText); } 	   );    }	if (isNaN(distance)) {		// Something went terribly wrong with parsing the dates.		// Display error		index.innerHTML = ' [LiveCountdown] Parsing date from "'+ index.getAttribute("data-end") +'" returned NaN, check parameters "month/day/year/customdate" ';	} else {		// Time calculations for hours, minutes and seconds (copied from W3Schools)	    var hours = Math.floor( (distance / (1000 * 60 * 60)) );	   var minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));	    var seconds = Math.floor((distance % (1000 * 60)) / 1000);		// Display the result	    index.innerHTML =	      "There are " +	      hours +	      " hours, " +	      minutes +	      " minutes, and " +	      seconds +	      " seconds until Steamboat Willie enters the public domain.";	}

// If the count down is finished, refresh if (distance < 0) { index.innerHTML = "Steamboat Willie has entered the public domain. This image will be replaced by a video of the film in a moment."; if (!RefreshingAlready) { RefreshingAlready = true; }   }  }, 1000); }; var counts = document.getElementsByClassName("SWcountdown"); if (counts.length > 0) {  for (var i = 0; i < counts.length; i++) {    counts[i].innerHTML = "Loading countdown...";    var endDate = new Date(counts[i].getAttribute("data-end")).getTime;    var now = new Date.getTime;    var distance = endDate - now;

if (distance < 0) { counts[i].innerHTML = "Steamboat Willie has entered the public domain. This image will be replaced by a video of the film in a moment."; } else { if (counts[i].getAttribute("data-event").search(/<(\/|)script>/g) != -1) { //script tag, do not run counts[i].innerHTML = ' [LiveCountdown] Possible XSS detected in event parameter. As a safety precaution, this countdown is cancelled. Please revert the recent edits immediately. ';   	} else { MainLoop(counts[i]); }   }  } } else { console.log("[LiveCountdown] No countdown widgets detected."); }