Jump to content

User:Jetstreamer/vector.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.
//<source lang="javascript">
 
$(function(){
  if (location.href.indexOf('Special:Watchlist') == -1) return; //Are we on a watchlist?
  //days = document.getElementById('bodyContent').getElementsByTagName('ul');
  days = document.evaluate( //Hell knows how it works - found in "Dive into Greasemonkey"
    "//ul[@class='special']",
    document,
    null,
    XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE,
    null);
  for (d = 0; d < days.snapshotLength; d++) { //For each day
    day = days.snapshotItem(d);
    newday = document.createElement('ul'); //This will replace the old listing
    while ((diffs = day.getElementsByTagName('li')).length > 0) { //Are there any diffs left?
      //Try to extract the namespace
      As = diffs[0].getElementsByTagName('a');
      if (As[0].innerHTML == 'diff')
        pagename = As[2].innerHTML;
      else
        pagename = As[1].innerHTML;
      if (pagename.indexOf(':') == -1)
        namespace = 'Main';
      else
        namespace = pagename.split(':')[0]; //This will fail for articles which contain ":" in name
      hdrs = newday.getElementsByTagName('h5'); //Get the list of namespace headers
      hdr = null;
      for (j=0; j<hdrs.length; j++) //Find the header
        if (hdrs[j].innerHTML==namespace) {
          hdr = hdrs[j]; break;
        }
      if (hdr==null) { //Not found? Make a new one!
        hdr = document.createElement('h5');
        hdr.innerHTML = namespace;
        newday.appendChild(hdr);
        namespacesub = document.createElement('ul');
        namespacesub.className = "special";
        newday.appendChild(namespacesub);
      }
      hdr.nextSibling.appendChild(diffs[0]); //Move the diff
    }
    newday.appendChild(document.createElement('hr')); //For readablility
    day.parentNode.replaceChild(newday,day);
  }
});
 
//</source>[[Category:Wikipedia scripts|watchlistSorter]]

// <pre>
// Based on [[User:Henrik/js/live-edit-counter]]
function liveEditCounter(username)
{
    if (!document.getElementById('edit-count-id') || !document.getElementById('edit-count-info'))
        return;
    var count="";
    if (mw.config.get('wgUserName') == mw.config.get('wgTitle')) // If a user is viewing their own page, the data has already been loaded, no need to make a XHR
    {
        count = mw.config.get('wgUserEditCount');
    }
    else
    {
        var xhr;
        try { xhr = new XMLHttpRequest(); }
        catch(e)
        {
            xhr = new ActiveXObject(Microsoft.XMLHTTP);
        }
 
        xhr.onreadystatechange  = function()
        {
            if(xhr.readyState  == 4)
            {
                if(xhr.status  == 200) {
                    var doc = xhr.responseXML;
                    count = doc.getElementsByTagName('user')[0].getAttribute('editcount')
                }
            }
        };
 
        xhr.open('GET', "http://en.wikipedia.org/w/api.php?action=query&list=users&usprop=editcount&format=xml&ususers="+username, true);
        xhr.send(null);
    }
    if(!count) count="0"; // Cater to zero edit counts
    count = (count+'').replace(/(?=(?:\d{3})+$)(?!^)/g, ','); // Add commas as thousand separators (hat tip to http://jsperf.com/number-format)
    document.getElementById('edit-count-id').innerHTML=count; // Update "icon"
    document.getElementById('edit-count-info').innerHTML=count; // Update text
}
 
$(function() {
  if (mw.config.get('wgCanonicalNamespace') == "User" || mw.config.get('wgCanonicalNamespace') == "User_talk") {
     var username = encodeURIComponent( mw.config.get('wgTitle').split("/")[0] );
     liveEditCounter(username);
  }
});
//<pre>