User:Tra/whatlinksherewatchlist.js

From Wikipedia, the free encyclopedia

If a message on your talk page led you here, please be wary of who left it. Code that you insert on this page could contain malicious content capable of compromising your account. If you are unsure whether code you are adding to this page is safe, you can ask at the appropriate village pump. If this is a .js page, the code will be executed when previewing the page.
Note: After saving, you have to bypass your browser's cache to see the changes. In Internet Explorer and Firefox, hold down the Ctrl key and click the Refresh or Reload button. Opera users have to clear their caches through Tools→Preferences, see the instructions for Opera. Konqueror and Safari users can just click the Reload button.
 ///////////////////////////////////////
 //     What links here watchlist     //
 //              by Tra               //
 ///////////////////////////////////////
 
 
function wlhdownload(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() {
      x.readyState==4 && wlhdownloadComplete(x,bundle);
    };
    x.open("GET",bundle.url,true);
    x.send(null);
  }
  return x;
}
 
function wlhdownloadComplete(x,bundle) {
  x.status==200 && ( bundle.onSuccess && bundle.onSuccess(x,bundle) || true )
  || ( bundle.onFailure && bundle.onFailure(x,bundle) || alert(x.statusText));
}
 
 
 
function wlhgetstartdate(wlhdate) {
 
  var tempvar = ''
 
  tempvar = wlhdate.getYear()
 
  tempvar += ''
 
  if ((wlhdate.getMonth() + 1) < 10 ) {
    tempvar += '0'
  }
 
  tempvar += (wlhdate.getMonth() + 1)
 
  tempvar += ''
 
  if (wlhdate.getDate() < 10 ) {
    tempvar += '0'
  }
 
  tempvar += wlhdate.getDate()
 
  tempvar += '000000'
 
  return tempvar
 
}
 
 
 
window.gettingWatchlist=false;
wlhwatchlist=null;
function wlhgetWatchlist() {
  window.gettingWatchlist=true;
  wlhdownload({url: 'http://' + document.location.hostname + '/wiki/Special:Watchlist/edit',
	      onSuccess: wlhprocessWatchlist, onFailure: function () { setTimeout(wlhgetWatchlist, 15000); return true; }});
}
function wlhprocessWatchlist(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 wlhnspg = ''
  var wlhpropline = ''
  var wlhdonestart = 'no'
  var wlhquerystring = 'what=backlinks&blnamespace=0&bltitle='
 
 
  for (var i=0; i<lines.length; ++i) {
    if (lines[i].indexOf('<li><input type="checkbox" name="id[]" value=') > -1) {
      wlhpropline = (lines[i].replace(/.*title="(.*?)">.*/, '$1'));
      wlhnspg=wlhpropline.split('/');
 
      // show only correct sort
  // myinfo.innerHTML += '<li>' + wlhpropline + '</li>'
      if (wlhnspg[0] == 'Talk:Backlinks') {
 
        if (wlhdonestart == 'yes') {wlhquerystring += '|'}
        wlhdonestart = 'yes'
 
        wlhquerystring += wlhnspg[1]
 
      }      
    }
  }
 
  var wlhnow = new Date()
 
  wlhquerystring += '&format=json&dummy=' + parseInt(Math.random()*99999999)
//  myinfo.innerHTML += '<li>' + wlhquerystring + '</li>'
 
 
 
  wlhdownload({url: 'http://' + document.location.hostname + '/w/query.php?' + wlhquerystring,
	      onSuccess: wlhprocessuserwatchlist});
 
 
}
 
 
function wlhmid(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 wlhtimefromdate(wlhdate) {
 
  return wlhmid(wlhdate, 11, 5)
 
}
 
 
 
 
 
function wlhprocessuserwatchlist(req, bundle) {
 
  var h1=document.getElementsByTagName('h1')[0]
  var wlhoutput=document.createElement('div');
  h1.parentNode.appendChild(wlhoutput);
 
  wlhoutput.innerHTML = '';
 
 
  // wlhoutput.innerHTML += req.responseText
 
  var wlhtempoutput = ''
 
  var wlhdata = eval('(' + req.responseText + ')');
 
  var wlhusernum = 0;
 
  // var wlhcontribnum = 0;
 
  var wlhobjcontrib
 
  var wlhusername
 
  wlhtempoutput += '';
 
  // wlhtempoutput += wlhdata.pages.length;
  // wlhtempoutput += wlhdata.pages.items[0].title;
 
  for (prop in wlhdata.pages) {
    if (1==1) {                             //if (wlhdata.pages[prop].backlinks.length > 0) {
// alert ('one');
 
    wlhusername = wlhdata.pages[prop].title //.split(':')[1]
 
    wlhtempoutput += '<h2><a href="http://en.wikipedia.org/wiki/' + wlhusername + '">' + wlhusername + '</a></h2>'
    wlhtempoutput += '<a href="http://en.wikipedia.org/wiki/' + wlhusername + '">article</a> • <a href="http://en.wikipedia.org/wiki/Special:Whatlinkshere/' + wlhusername + '">what links here</a> • <a href="http://en.wikipedia.org/wiki/Talk:' + wlhusername + '">talk</a> • <a href="http://en.wikipedia.org/w/index.php?title=' + wlhusername + '&action=history">history</a><ul>'
 
 
 
// alert ('hello');
    for (prop2 in wlhdata.pages[prop].backlinks){
 
      wlhobjcontrib = wlhdata.pages[prop].backlinks[prop2]
 
 
      wlhtempoutput += '<li>'
      wlhtempoutput += '<a href="http://en.wikipedia.org/wiki/' + wlhobjcontrib['*'] + '">' + wlhobjcontrib['*'] + '</a>'
 
      wlhtempoutput += '</li>'
 
/*
      wlhtempoutput += '" title="' + wlhobjcontrib['*'] + '">' + wlhtimefromdate(wlhobjcontrib.timestamp) + '</a></tt> '
 
 
      wlhtempoutput +='(<a href="/w/index.php?title=' + wlhobjcontrib['*']
      wlhtempoutput += '&amp;action=history" title="' + wlhobjcontrib['*'] + '">hist</a>) (<a href="/w/index.php?title='
      wlhtempoutput += wlhobjcontrib['*'] + '&amp;diff=prev&amp;oldid=' + wlhobjcontrib.revid + '" title="' + wlhobjcontrib['*']
      wlhtempoutput += '">diff</a>)  <a href="/wiki/' + wlhobjcontrib['*'] + '" title="' + wlhobjcontrib['*'] + '">'
      wlhtempoutput += wlhobjcontrib['*'] + '</a>  <span class="comment">(' + wlhobjcontrib.comment + ')</span>' + '<br />'
*/
    }
 
wlhtempoutput += '</ul>'
    ++wlhusernum
 
    }
 
  }
 
  wlhtempoutput += '';
 
 
  wlhloadingmsg.innerHTML = ''
 
  wlhoutput.innerHTML = wlhtempoutput;
  if (typeof(window.setupTooltips)!='undefined') { setupTooltips(wlhoutput); }
 
 
 
}
 
// **************************************************
// Installation
// **************************************************
 
/* function wlhaddlilink(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 wlhaddToolboxLink(url, name, id){
    var tb = document.getElementById('p-tb').getElementsByTagName('ul')[0];
    wlhaddlilink(tb, url, name, id);
}
 
 
var wlhloadingmsg
function wlhloadapp() {
 
  //var h1=document.getElementsByTagName('h1')[0]
  // var wlhoutput=document.createElement('div');
  //h1.innerHTML = 'User watchlist';
 
  wlhaddToolboxLink('http://' + document.location.hostname + '/wiki/' + 'User:Tra/What_links_here_watchlist', 'Backlinks watchlist', 'wlh_link');
 
   if (wgPageName == 'User:Tra/What_links_here_watchlist') {
 
    var h1=document.getElementsByTagName('h1')[0]
    wlhloadingmsg=document.createElement('div');
    h1.parentNode.appendChild(wlhloadingmsg);
 
    wlhloadingmsg.innerHTML = 'Loading...'
 
 
    wlhgetWatchlist()
 
 
 
   }
 
 
if (wgPageName == "Special:WhatLinksHere") {
     wlhaddTab('http://en.wikipedia.org/w/index.php?action=watch&title=Backlinks/' + document.getElementById('specialDeleteTarget').firstChild.innerHTML + '&gonow=yes&gonowto=' + location.href,'watch','addwlhwatchlist','Watch this page for backlinks','w')
 
     wlhaddTab('http://en.wikipedia.org/w/index.php?action=unwatch&title=Backlinks/' + document.getElementById('specialDeleteTarget').firstChild.innerHTML + '&gonow=yes&gonowto=' + location.href,'unwatch','rmwlhwatchlist','Remove this page from your watchlist for backlinks')
 
 
 
 
}
 
if (wlhgetQueryVariable('gonow')=='yes') {
 
   location.href=wlhgetQueryVariable('gonowto');
 
}
 
}
 
 
function wlhgetQueryVariable(variable) {
  var query = window.location.search.substring(1);
  var vars = query.split("&");
  for (var i=0;i<vars.length;i++) {
    var pair = vars[i].split("=");
    if (pair[0] == variable) {
      return pair[1];
    }
  } 
  //alert('Query Variable ' + variable + ' not found');
}
 
function wlhaddTab(url, name, id, title, key){
    var tabs = document.getElementById('p-cactions').getElementsByTagName('ul')[0];
    return wlhaddlilink(tabs, url, name, id, title, key)
};
 
 
 
 
 
function wlhaddlilink(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;
}
 
 
 
addOnloadHook(wlhloadapp)