Jump to content

User:Splarka/fetchredirects.js

From Wikipedia, the free encyclopedia
The printable version is no longer supported and may have rendering errors. Please update your browser bookmarks and please use the default browser print function instead.
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.
/* Fetch redirects to a page, version [0.0.3]
Originally from: http://en.wikipedia.org/wiki/User:Splarka/fetchredirects.js
*/

var frBaseURL = wgScriptPath + '/api.php?format=json&maxage=3600&smaxage=3600&rawcontinue=&callback=fetchRedirectsCB&action=query&list=backlinks&blfilterredir=redirects&bllimit=500';
if(wgNamespaceNumber > -1) addOnloadHook(function() {
  mw.util.addPortletLink('p-tb','javascript:fetchRedirects();','Get redirects','t-getredirects','Get a list of pages redirecting to this one');
})

function fetchRedirects() {
  var bar = document.getElementById('contentSub') || document.getElementById('topbar');
  if(!bar) return;
  var div = document.createElement('div');
  div.setAttribute('id','fr-out');
  bar.appendChild(div);
  injectSpinner(div,'fr');
  var url = frBaseURL + '&bltitle=' + encodeURIComponent(wgTitle);
  mw.loader.load(url);
  appendCSS('#t-getredirects {display:none;} #fr-out {border:1px solid #777777;padding:.5em;}');
}

function fetchRedirectsCB(obj) {
  var fr = document.getElementById('fr-out');
  if(!obj['query'] || !obj['query']['backlinks']) {
    fr.appendChild(document.createTextNode('Api error'));
    if(obj['error']) fr.appendChild(document.createTextNode(' - ' + obj['error']['code'] + ' - ' + obj['error']['info']));
    removeSpinner('fr');
    return;
  }
  var bl = obj['query']['backlinks'];
  if(bl.length == 0) {
    fr.appendChild(document.createTextNode('There are no redirects to this page.'));
    removeSpinner('fr');
    return;
  }
  fr.appendChild(document.createTextNode('Found redirects:'));
  fr.appendChild(document.createElement('br'));
 
  for(var i=0;i<bl.length;i++) {
    var title = encodeURIComponent(bl[i]['title']);
    fr.appendChild(document.createTextNode('('));
    addLinkChild(fr,wgScript + '?title=' + title + '&action=history','hist');
    fr.appendChild(document.createTextNode(' | '));
    addLinkChild(fr,wgScript + '?title=' + title + '&action=edit','edit');
    fr.appendChild(document.createTextNode(') '));
    addLinkChild(fr,wgScript + '?title=' + title + '&redirect=no',bl[i]['title'],false,'fr-result','Redirect to ' + wgTitle);
    fr.appendChild(document.createElement('br'));
  }
  if(obj['query-continue']) {
    var url = frBaseURL + '&bltitle=' + encodeURIComponent(wgTitle) + '&blcontinue=' + obj['query-continue']['backlinks']['blcontinue'];
    mw.loader.load(url);
  } else {
    removeSpinner('fr');
  }
}

function addLinkChild(obj,href,text,id,classes,title) {
  if(!obj || !href || !text) return false;
  var a = document.createElement('a');
  a.setAttribute('href',href);
  a.appendChild(document.createTextNode(text));
  if(id) a.setAttribute('id',id);
  if(classes) a.setAttribute('class',classes);
  if(title) a.setAttribute('title',title);
  obj.appendChild(a);
  return a;
}