User:ToxiBoi/LiveCountdown.js: Difference between revisions

From Wikipedia, the free encyclopedia
Content deleted Content added
m ?
m Don't wanna spam the API here
Line 1: Line 1:
var RefreshingAlready = false;
var alreadyScanned = false;
var MainLoop = function (index) {
var MainLoop = function (index) {
var RefreshingAlready = false;
setInterval(function () {
setInterval(function () {
// Setup again
// Setup again
Line 7: Line 8:
distance = endDate - now;
distance = endDate - now;
if (!alreadyScanned) {
var sanitizedCode = encodeURIComponent(index.getAttribute("data-event"));
alreadyScanned = true;
$.post( "https:" + mw.config.get( "wgServer" ) +
var sanitizedCode = encodeURIComponent(index.getAttribute("data-event"));
"/w/api.php?action=parse&format=json&title=" + mw.config.get('wgPageName') + "&text=" + sanitizedCode
$.post( "https:" + mw.config.get( "wgServer" ) +
+ "&pst=1",
"/w/api.php?action=parse&format=json&title=" + mw.config.get('wgPageName') + "&text=" + sanitizedCode
function ( res ) {
if ( !res || !res.parse || !res.parse.text ) return console.log( "Preview failed" );
+ "&pst=1",
function ( res ) {
if ( !res || !res.parse || !res.parse.text ) return console.log( "Preview failed" );
var StrippedText = res.parse.text['*'];
var StrippedText = res.parse.text['*'];
//Trim off div/p tags
//Trim off div/p tags
var str = "";
var str = "";
var endTrim = StrippedText.length;
var endTrim = StrippedText.length;
var count = 0;
var count = 0;
var scan = false;
var scan = false;
for (i=33;i<StrippedText.length;i++) {
for (i=33;i<StrippedText.length;i++) {
if (res[i] == "<") {
if (res[i] == "<") {
scan = true;
scan = true;
}
if (scan) {
count++;
str += res[i];
}
if (str == "</p>") {
endTrim = i-3;
}
if (count == 4) {
count = 0;
str = "";
scan = false;
}
}
}
StrippedText = StrippedText.substring(33,endTrim);
if (scan) {
index.setAttribute("data-event",StrippedText);
count++;
}
str += res[i];
);
}
}
if (str == "</p>") {
endTrim = i-3;
}
if (count == 4) {
count = 0;
str = "";
scan = false;
}
}
StrippedText = StrippedText.substring(33,endTrim);
index.setAttribute("data-event",StrippedText);
}
);
if (isNaN(distance)) {
if (isNaN(distance)) {

Revision as of 05:23, 17 April 2020

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 str = "";
				var endTrim = StrippedText.length;
				var count = 0;
				var scan = false;
				for (i=33;i<StrippedText.length;i++) {
					if (res[i] == "<") {
						scan = true;
					}
					if (scan) {
						count++;
						str += res[i];
					}
					if (str == "</p>") {
						endTrim = i-3;
					}
					if (count == 4) {
						count = 0;
						str = "";
						scan = false;
					}
				}
				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 = '<strong class="error">[LiveCountdown] Parsing date from "'+ index.getAttribute("data-end") +'" returned NaN, check parameters "month/day/year/customdate"</strong>';
	} else {
		// Time calculations for days, hours, minutes and seconds (copied from W3Schools)
	    var days = Math.floor(distance / (1000 * 60 * 60 * 24));
	    var hours = Math.floor(
	      (distance % (1000 * 60 * 60 * 24)) / (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 <b>" +
	      days +
	      "</b> days, <b>" +
	      hours +
	      "</b> hours, <b>" +
	      minutes +
	      "</b> minutes, and <b>" +
	      seconds +
	      "</b> seconds until " +
	      index.getAttribute("data-event") +
	      ".";
	}

    // If the count down is finished, refresh
    if (distance < 0) {
      index.innerHTML = "Countdown expired, refreshing...";
      if (!RefreshingAlready) {
        document.location.reload();
        RefreshingAlready = true;
      }
    }
  }, 1000);
};
var counts = document.getElementsByClassName("toxicountdown");
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 = "The countdown finished.";
    } else {
      MainLoop(counts[i]);
    }
  }
} else {
  console.log("[LiveCountdown] No countdown widgets detected.");
}