User:AlexDj94/Wikipedia More Alive.js

From mediawiki.org

Note: After publishing, you may have to bypass your browser's cache to see the changes.

  • Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
  • Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
  • Internet Explorer / Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5
  • Opera: Press Ctrl-F5.
/*
Copyright (C) 2011 Alessio Dionisi <alessio.dionisi94@gmail.com>

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to

Free Software Foundation, Inc.
51 Franklin Street, Fifth Floor
Boston, MA   02110-1301, USA.
*/

var PageTitle = mw.config.get("wgPageName");
var WikiURL = document.location.href.split("/");
var currDate = null;

var lastYear;
var lastMonth;
var lastDay;
var lastHour;
var lastMinute;
var lastSecond;

var lastUser;
var lastComment;

function RealTimeWiki() {
   var d = new Date();
   currDate = new Date(d.getUTCFullYear(), d.getUTCMonth() + 1, d.getUTCDate(), d.getUTCHours(), d.getUTCMinutes(), d.getUTCSeconds());
   //UpdatePage();
   CheckLastRevision();
   UpdateLastEditText();
   //setTimeout("CheckLastRevision()", 5000);

   // Social buttons
   document.getElementById("firstHeading").innerHTML += "<span style=\"float: right;\">" + 
      "<iframe src=\"http://www.facebook.com/plugins/like.php?href=" + document.location.href + 
      "&amp;send=false&amp;layout=button_count&amp;width=450&amp;show_faces=false&amp;action=recommend&amp;" +
      "colorscheme=light&amp;font=arial&amp;height=21&amp;appId=130624193700418\" scrolling=\"no\" frameborder=\"0\"" +
      " style=\"border:none; overflow:hidden; width:130px; height:21px;\" allowTransparency=\"true\"></iframe></span>";
}

function CheckLastRevision() {
   if (window.XMLHttpRequest) // IE7+, Firefox, Chrome, Opera, Safari
   {
      xmlhttp = new XMLHttpRequest();
   }
   else // IE6, IE5
   {
      xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
   }
   
   try {
      var urlText = ""; 
      for (i = 3; i < WikiURL.length; i++) {
         if (i != WikiURL.length) {
            urlText += "../";
         }
      }
      
      xmlhttp.open("GET", urlText + "w/api.php?action=query&prop=revisions&titles=" + PageTitle + "&format=xml", false);
      xmlhttp.send(null);
   }
   catch(e) {
      alert("== Javascript Error ==\n" + e.toString());
   }
   
   xmlDoc = xmlhttp.responseXML;
   var lastTimeStamp = xmlDoc.getElementsByTagName("rev")[0].attributes.getNamedItem("timestamp").nodeValue;

   lastUser = xmlDoc.getElementsByTagName("rev")[0].attributes.getNamedItem("user").nodeValue;
   lastComment = xmlDoc.getElementsByTagName("rev")[0].attributes.getNamedItem("comment").nodeValue;
   
   var lastHead1 = lastTimeStamp.split("T")[0];
   lastYear = lastHead1.substring(0, 4);
   lastMonth = lastHead1.substring(5, 7);
   lastDay = lastHead1.substring(8, 10);

   var lastHead2 = lastTimeStamp.split("T")[1];
   lastHour = lastHead2.substring(0, 2);
   lastMinute = lastHead2.substring(3, 5);
   lastSecond = lastHead2.substring(6, 8);

   var lastDate = new Date(lastYear, lastMonth, lastDay, lastHour, lastMinute, lastSecond);

   //alert("Pagina: " + xmlDoc.getElementsByTagName("page")[0].attributes.getNamedItem("title").nodeValue + 
   //   "\nUltima modifica: " + lastTimeStamp);

   if (lastDate > currDate) {
      var d = new Date();
      currDate = new Date(d.getUTCFullYear(), d.getUTCMonth() + 1, d.getUTCDate(), d.getUTCHours(), d.getUTCMinutes(), d.getUTCSeconds());
      UpdatePage();
   }
   
   setTimeout("CheckLastRevision()", 10000); // 10000 <- millisecondi
}

var hex = 255;
function FadeText() {
   var textDiv = document.getElementsByClassName("mw-content-ltr")[0];
   if (hex > 0) { //If color is not black yet
      hex -= 11; // increase color darkness
      textDiv.style.color = "rgb(" + hex + "," + hex + "," + hex + ")";
      document.getElementById("footer-info-lastmod").style.color = "rgb(" + hex + "," + hex + "," + hex + ")";
      setTimeout("FadeText()", 20);
   }
   else {
      hex = 255; //reset hex value
      document.getElementById("footer-info-lastmod").style.color = "#333";
   }
}

function UpdatePage() {
   if (window.XMLHttpRequest) // IE7+, Firefox, Chrome, Opera, Safari
   {
      xmlhttp = new XMLHttpRequest();
   }
   else // IE6, IE5
   {
      xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
   }
   
   try {
      var urlText = ""; 
      for (i = 3; i < WikiURL.length; i++) {
         if (i != WikiURL.length) {
            urlText += "../";
         }
      }
      
      xmlhttp.open("GET", urlText + "w/index.php?action=render&title=" + PageTitle, false);
      xmlhttp.send(null);
   }
   catch(e) {
      alert("== Javascript Error ==\n" + e.toString());
   }
   
   var text = xmlhttp.responseText;
   //var text = xmlDoc.getElementsByTagName("text")[0].nodeValue;

   var textDiv = document.getElementsByClassName("mw-content-ltr")[0];
   FadeText();
   UpdateLastEditText();
   textDiv.innerHTML = text;
}

function UpdateLastEditText() {
   document.getElementById("footer-info-lastmod").innerHTML = "This page was last modified on " + lastDay + "/" + lastMonth + "/" + 
      lastYear + ", at " + lastHour + ":" + lastMinute + " by <a href=\"http://" + WikiURL[2] + "/wiki/User:" + lastUser + "\">" + lastUser + "</a>.";
      if (lastComment != "") {
         document.getElementById("footer-info-lastmod").innerHTML += " <i>&laquo;" + lastComment + "&raquo;</i>";
      }
   document.getElementById("footer-info-lastmod").innerHTML += "";
}

$(document).ready(RealTimeWiki);
//jQuery(RealTimeWiki);