Jump to content

User:Sander Säde/pagesCreated.js

From Wikipedia, the free encyclopedia
Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
/*
Script to show pages in mainspace created by user. Alpha stage.
*/

$(pagesCreatedHook);
var cpUserName = "";
var cpBody = "";
var cpOffset = 2; //time offset

//from User:Lupin/nichalpCustomLinks
function addToolboxLink(url, name, id){
  var tb = document.getElementById('p-tb').getElementsByTagName('ul')[0];
  addlilink(tb, url, name, id);
}

function pagesCreatedHook() {
if ((document.URL.indexOf('/User:') != -1) || (document.URL.indexOf('/User_talk:') != -1)) {
  if (mw.config.get('wgTitle').indexOf("/") != -1) return; //subpages
  addToolboxLink('javascript:pagesCreated()','Created pages', 'pgCreated');
}

if (document.URL.indexOf('Special:Contributions/') != -1) addToolboxLink('javascript:pagesCreated()','Created pages', 'pgCreated');

}

function pagesCreated() {
 if (cpUserName == "") cpUserName = mw.config.get('wgTitle');
  if (cpUserName == 'Contributions') {
    var name = document.getElementById("contentSub").getElementsByTagName("a"); 
    cpUserName = name[0].innerHTML;    
    //IP's    
    if (cpUserName == "Talk") {
     cpUserName = document.getElementById("contentSub").innerHTML.split(" ")[1];
    }
  }

 var cpaj = sajax_init_object(null);
    cpaj.open("GET", "http://en.wikipedia.org/w/query.php?what=usercontribs&titles=User:" + escape(cpUserName) + "&uclimit=500&uccomments&format=json&noprofile", true);
    cpaj.onreadystatechange = function()
    {
      if(cpaj.readyState == 4) cpResultsReceived(cpaj.responseText);
    };
    cpaj.send(null);
}

function anyChild(obj) { 
   for(var key in obj) {
      return obj[key];
   }
   return null; 
}


function cpResultsReceived(jsonObj) {
  eval("var queryResult=" + jsonObj);  
  var page = anyChild(queryResult.pages);
  var links = page.contributions;  
  cnt = 0;
  cpBody = "";
   for (i = 0; i < links.length; i++) {
    if (links[i].new != null) {
    addToBodyContent(links[i]);
    cnt++;
   }
  }

cpt = "<div id='contentSub'>Page creations in last 500 edits for <strong>" +  cpUserName +  '</strong><br />&nbsp;&nbsp;';
if (cnt == 0) cpt += "No";
else cpt += cnt;

cpt += ' pages created.</div>' + "\r\n<ul>";
cpBody = cpt + cpBody + '</ul>';
document.getElementById("bodyContent").innerHTML = cpBody ;  
}

function addToBodyContent(entry) {
cpBody  += '\r\n<li id="' + entry.* +'">' + cpGetTime(entry.timestamp) + '&nbsp;(<a href="/w/index.php?title=' + escape(entry.*) + '&action=history">hist</a>)&nbsp;&nbsp;<a href="/wiki/' + escape(entry.*) + '">' + entry.* + '</a>&nbsp;<span class="comment">(' + entry.comment + ')</span></li>';
}

function cpGetTime(cpTime) {
  var arrDate = new Array();
  arrDate = cpTime.split("T")[0].split("-"); 
  var arrTime = new Array();
  arrTime = cpTime.split("T")[1].split(":");
  arrTime[2] = arrTime[2].replace("Z","");
  
  //cpDate = new Date(Date.UTC(arrDate[0],arrDate[1],arrDate[2],arrTime[0], arrTime[1], arrTime[2]));
  cpDate = new Date();
  cpDate.setFullYear = (arrDate[0],arrDate[1],arrDate[2]);
  cpDate.setHours(Number(arrTime[0]) + cpOffset, arrTime[1], arrTime[2]);  
  return cpDate.toLocaleString();
}