Jump to content

User:Daask/sandbox/userwatch.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.
// This script was originally written by Tra and later edited by Jivecat and Daask.
mw.loader.load('https://raw.githubusercontent.com/azatoth/twinkle/master/morebits.js');
function akeytt(){
	// do nothing; obsolete See [[mw:ResourceLoader/Migration guide (users)#Tooltip and accesskeys]]
}

function uwdownload(bundle) {
  // mandatory: bundle.url
  // optional:  bundle.onSuccess (xmlhttprequest, bundle)
  // optional:  bundle.onFailure (xmlhttprequest, bundle)
  // optional:  bundle.otherStuff OK too, passed to onSuccess and onFailure

  var x = window.XMLHttpRequest ? new XMLHttpRequest()
	: window.ActiveXObject ? new ActiveXObject("Microsoft.XMLHTTP")
	: false;

  if (x) {
    x.onreadystatechange=function() {
    if ( x.readyState==4 ) { uwdownloadComplete(x,bundle); }
    };
    x.open("GET",bundle.url,true);
    x.send(null);
  }
  return x; 
}

function uwdownloadComplete(x,bundle) {
  if (x.status==200) { bundle.onSuccess(x,bundle); } else { bundle.onFailure(x,bundle); alert(x.statusText);}
}



function uwgetstartdate(uwdate) {

  var tempvar = ''

  if (navigator.userAgent.indexOf("Firefox")!=-1){
    tempvar = uwdate.getYear() + 1900
  } else {
    tempvar = uwdate.getYear()
  }

  tempvar += ''

  if ((uwdate.getMonth() + 1) < 10 ) {
    tempvar += '0'
  }

  tempvar += (uwdate.getMonth() + 1)

  tempvar += ''

  if (uwdate.getDate() < 10 ) {
    tempvar += '0'
  }

  tempvar += uwdate.getDate()

  tempvar += '000000'

  return tempvar

}



window.gettingWatchlist=false;
uwwatchlist=null;
function uwgetWatchlist() {


  window.gettingWatchlist=true;
  uwdownload({url: 'https://' + document.location.hostname + '/wiki/Special:Watchlist?action=raw',
	      onSuccess: uwprocessWatchlist, onFailure: function () {
 setTimeout(uwgetWatchlist, 15000); return true; }});
}
function uwprocessWatchlist(req, bundle) {

  var watchlist={};
  var lines=req.responseText.split('\n');

  var h1=document.getElementsByTagName('h1')[0]
  var myinfo=document.createElement('ul');
  h1.parentNode.appendChild(myinfo);

  var uwnspg = ''
  var uwinlist = 'no'
  var uwpropline = ''
  var uwdonestart = 'no'
  var uwquerystring = ''
  var uwcountusers = 0

  var uwnow = new Date()




  var h1=document.getElementsByTagName('h1')[0]
  var uwoutput=document.createElement('div');
  h1.parentNode.appendChild(uwoutput);

  uwoutput.innerHTML = '';

  for (var i=0; i<lines.length; ++i) {
//myinfo.innerHTML += 'one'
if (lines[i].indexOf('<textarea') > -1) { uwinlist = 'yes'; }
if (lines[i].indexOf('</textarea') > -1) { uwinlist = 'no'; }

    if (uwinlist == 'yes') {
      uwpropline = lines[i];
      uwnspg=uwpropline.split(':');

      // eliminate all but user or user talk pages
      if ((uwnspg[0] == 'User' || uwnspg[0] == 'User talk') && uwpropline.split('/')[0]==uwpropline && uwcountusers <25) {

        if (uwdonestart == 'yes') {uwquerystring += '|'}
        uwdonestart = 'yes'

        uwquerystring += 'User:' + uwnspg[1]
        uwcountusers += 1

        if (uwcountusers == 25) {

          //send and process stuff

            uwquerystring += '&uclimit=20&ucstart=' + uwgetstartdate(uwnow) + '&uccomments&format=json&dummy=' + parseInt(Math.random()*99999999)



//myinfo.innerHTML += ('https://' + document.location.hostname + '/w/api.php?action=query&list=usercontribs&titles=' + uwquerystring + '<b>stop</b>');
            uwdownload({url: 'https://' + document.location.hostname + '/w/api.php?action=query&list=usercontribs&titles=' + uwquerystring,
	      onSuccess: function (req, bundle) {  uwoutput.innerHTML += uwprocessuserwatchlist(req.responseText) ;   if (typeof(window.setupTooltips)!='undefined') { setupTooltips(uwoutput); } }});

            uwcountusers = 0
            uwquerystring = ''
            uwdonestart = 'no'

        }





      }      
    }
  }

  uwquerystring += '&uclimit=20&ucstart=' + uwgetstartdate(uwnow) + '&uccomments&format=json&dummy=' + parseInt(Math.random()*99999999)
  // myinfo.innerHTML += '<li>' + uwquerystring + '</li>'

//myinfo.innerHTML += ('https://' + document.location.hostname + '/w/api.php?action=query&list=usercontribs&titles=' + uwquerystring);

//if (uwcountusers == 25) {myinfo.innerHTML += 'For performance reasons, only the first 25 users on your watchlist are shown.';}

  uwdownload({url: 'https://' + document.location.hostname + '/w/api.php?action=query&list=usercontribs&titles=' + uwquerystring,
	      onSuccess: function (req, bundle) { uwoutput.innerHTML += uwprocessuserwatchlist(req.responseText) ;   if (typeof(window.setupTooltips)!='undefined') { setupTooltips(uwoutput); } ; uwloadingmsg.innerHTML = ''; }});


// at very end
//  uwoutput.innerHTML = uwtempoutput;
//  if (typeof(window.setupTooltips)!='undefined') { setupTooltips(uwoutput); }

//  uwloadingmsg.innerHTML = ''


}


function uwmid(str, start, len)
  /***
  IN: str - the string we are LEFTing
  start - our string's starting position (0 based!!)
  len - how many characters from start we want to get

  RETVAL: The substring from start to start+len
  ***/
  {
    // Make sure start and len are within proper bounds
    if (start < 0 || len < 0) return "";

    var iEnd, iLen = String(str).length;
    if (start + len > iLen)
      iEnd = iLen;
    else
      iEnd = start + len;

    return String(str).substring(start,iEnd);
}



function uwtimefromdate(uwdate) {

  return uwmid(uwdate, 11, 5)

}

// function uwuwprocessuserwatchlist



function uwprocessuserwatchlist(uwrsp) {

// alert(uwrsp);

  // uwoutput.innerHTML += uwrsp

  var uwtempoutput = ''

  var uwdata = eval('(' + uwrsp + ')');

  var uwusernum = Math.random()*99999999;

  var uwcontribnum = 0;

  var uwobjcontrib

  var uwusername

  uwtempoutput += '';

  // uwtempoutput += uwdata.pages.length;
  // uwtempoutput += uwdata.pages.items[0].title;

  for (prop in uwdata.pages) {

    if (uwdata.pages[prop].contributions.length > 0) {

    uwusername = uwdata.pages[prop].title.split(':')[1]

    uwtempoutput += '<span id="RCM' + uwusernum + '"><a href="javascript:toggleVisibility(\'RCI' + uwusernum + '\',\'RCM'
    uwtempoutput += uwusernum + '\',\'RCL' + uwusernum + '\')"><img src="/skins-1.5/common/images/Arr_r.png"'
    uwtempoutput += ' width="12" height="12" alt="+" /></a></span><span id="RCL' + uwusernum + '" style="display:none">'
    uwtempoutput += '<a href="javascript:toggleVisibility(\'RCI' + uwusernum + '\',\'RCM' + uwusernum
    uwtempoutput += '\',\'RCL' + uwusernum + '\')"><img src="/skins-1.5/common/images/Arr_d.png" width="12"'
    uwtempoutput += ' height="12" alt="-" /></a></span><tt>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</tt>'
  
    uwtempoutput += '<a href="/wiki/User:' + uwusername + '" title="User:' + uwusername + '">' + uwusername
    uwtempoutput += '</a> (<a href="/wiki/User_talk:' + uwusername + '" title="User talk:' + uwusername
    uwtempoutput += '">Talk</a> | <a href="/w/index.php?title=Special:Contributions&amp;target=' + uwusername
    uwtempoutput += '" title="Special:Contributions">contribs</a>)' + '<br />';


    uwtempoutput += '<div id="RCI' + uwusernum + '" style="display:none">'


    for (uwcontribnum = 0; uwcontribnum < uwdata.pages[prop].contributions.length; ++uwcontribnum){

      uwobjcontrib = uwdata.pages[prop].contributions[uwcontribnum]


      uwtempoutput += '<img src="/skins-1.5/common/images/Arr_.png" width="12" height="12" alt=" " />'
      uwtempoutput += '<tt>&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</tt>'

      uwtempoutput += '<tt><a href="/w/index.php?title=' + uwobjcontrib['*'] + '&amp;oldid=' + uwobjcontrib.revid
      uwtempoutput += '" title="' + uwobjcontrib['*'] + '">' + uwtimefromdate(uwobjcontrib.timestamp) + '</a></tt> '


      uwtempoutput +='(<a href="/w/index.php?title=' + uwobjcontrib['*']
      uwtempoutput += '&amp;action=history" title="' + uwobjcontrib['*'] + '">hist</a>) (<a href="/w/index.php?title='
      uwtempoutput += uwobjcontrib['*'] + '&amp;diff=prev&amp;oldid=' + uwobjcontrib.revid + '" title="' + uwobjcontrib['*']
      uwtempoutput += '">diff</a>)  <a href="/wiki/' + uwobjcontrib['*'] + '" title="' + uwobjcontrib['*'] + '">'
      uwtempoutput += uwobjcontrib['*'] + '</a>  ' + (typeof uwobjcontrib['minor']=='undefined' ? '' : '<b>m </b>')
      uwtempoutput += '<span class="comment">(' + uwobjcontrib.comment + ')</span>'
      uwtempoutput += (typeof uwobjcontrib['top']=='undefined' ? '' : ' <b>(top)</b>') + '<br />'

    }


    uwtempoutput += '</div>'
    ++uwusernum

    }

  }

  uwtempoutput += '';


  return uwtempoutput




}

// **************************************************
// Installation
// **************************************************

function uwaddlilink(tabs, url, name, id, title, key){
    var na = document.createElement('a');
    na.href = url;
    na.appendChild(document.createTextNode(name));
    var li = document.createElement('li');
    if(id) li.id = id;
    li.appendChild(na);
    tabs.appendChild(li);
    if(id) {
      if(key && title) ta[id] = [key, title];
      else if(key)     ta[id] = [key, ''];
      else if(title)   ta[id] = ['', title];
    }
    // re-render the title and accesskeys from existing code in wikibits.js
    akeytt();
    return li;
}

function uwaddToolboxLink(url, name, id){
    var tb = document.getElementById('p-tb').getElementsByTagName('ul')[0];
    uwaddlilink(tb, url, name, id);
}


var uwloadingmsg
function uwloadapp() {

  //var h1=document.getElementsByTagName('h1')[0]
  // var uwoutput=document.createElement('div');
  //h1.innerHTML = 'User watchlist';

  uwaddToolboxLink('https://' + document.location.hostname + '/wiki/' + 'User:Kevin_Forsyth/User watchlist', 'User watchlist', 'uw_link');

  if (mw.config.get('wgPageName') == 'User:Kevin_Forsyth/User_watchlist') { //

    var h1=document.getElementsByTagName('h1')[0]
    uwloadingmsg=document.createElement('div');
    h1.parentNode.appendChild(uwloadingmsg);

    uwloadingmsg.innerHTML = 'Loading...'

    uwgetWatchlist()



  } //

}

addOnloadHook(uwloadapp)