Jump to content

User:Jrp/monobook.js

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Jrp (talk | contribs) at 16:15, 8 June 2008. The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
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.
importScript('User:AndyZ/peerreviewer.js'); //[[User:AndyZ/peerreviewer.js]]

/* Disambiguation lookup script, version [0.1.5b]
Originally from: http://en.wikipedia.org/wiki/User:Splarka/dabfinder.js
 
Notes:
* Uses the API using head/appendchild(script) and callback to avoid ajax.
* Alt command finds and hilights redirects with simple CSS append (class="mw-redirect").
 
Operation:
* Adds portlet button (or works with URI parameter &finddab=true)
** Makes call to MediaWiki:Disambiguationspage links, generates array of these.
*** Makes call to generator=links / prop=templates on page title.
**** Builds array of registered links that link to disambiguation pages (that contain a disambiguation template).
***** Iterates over all <a> link objects on page, matching any that link to disambiguation, and applies red dashed border.
 
This is a bit messy but at the time was the easiest and most thorough way to do it I could think of.
Of course, a bot might be much more efficient, but this is handy for quick on-the-fly live checking.
 
To do:
* test it
* centralized link list in contentSub?
* ?
*/
 
var dabnames = new Array();
if(wgNamespaceNumber != -1) addOnloadHook(findDABsButton)
function findDABsButton() {
  addPortletLink('p-tb','javascript:findDABs()','Find disambiguations','t-dab');
  if(queryString('finddab')=='true') findDABs();
  addPortletLink('p-tb','javascript:findRDRs()','Find redirects','t-rdr');
  if(queryString('findrdr')=='true') findRDRs();
}
 
function findRDRs() {
  var text = '.mw-redirect { background-color: #ffff00;}\n#t-rdr:after {content:" (redirects hilighted)"; background-color:#ffff00; }';
  var s = document.createElement('style');
  s.type = 'text/css';
  s.rel = 'stylesheet';
  if (s.styleSheet) s.styleSheet.cssText = text
  else s.appendChild(document.createTextNode(text + ''))
  document.getElementsByTagName('head')[0].appendChild(s);
}
 
function findDABs() {
  var dab = document.getElementById('t-dab');
  if(dab) {
    dab.getElementsByTagName('a')[0].style.display = 'none';
    var prog = document.createElement('img');
    prog.setAttribute('id','dab-prog');
    prog.setAttribute('src',stylepath + '/common/images/spinner.gif');
    dab.appendChild(document.createTextNode('Searching...'));
    dab.appendChild(prog);
  }
  var url = wgServer + wgScriptPath + '/api.php?action=query&prop=links&tlnamespace=10&format=json&callback=findDABsCB&titles=MediaWiki:Disambiguationspage';
  var scriptElem = document.createElement('script');
  scriptElem.setAttribute('src',url);
  scriptElem.setAttribute('type','text/javascript');
  document.getElementsByTagName('head')[0].appendChild(scriptElem);
}
 
function findDABsCB(obj) {
  if(!obj['query'] || !obj['query']['pages']) return
  for(var i in obj['query']['pages']) var links = obj['query']['pages'][i]['links']
  if(!links) return
  for(var i=0;i<links.length;i++) {
    dabnames[dabnames.length] = links[i]['title'];
  }
  var url = wgServer + wgScriptPath + '/api.php?action=query&redirects&generator=links&prop=templates&format=json&callback=findDABlinksCB&titles=' + encodeURIComponent(wgPageName);
  var scriptElem = document.createElement('script');
  scriptElem.setAttribute('src',url);
  scriptElem.setAttribute('type','text/javascript');
  document.getElementsByTagName('head')[0].appendChild(scriptElem);
}
 
function findDABlinksCB(obj) {
  var dablinks = new Array();
  if(!obj['query'] || !obj['query']['pages']) return
  var links = new Array()
  for(var i in obj['query']['pages']) {
    for(var j in obj['query']['pages'][i]['templates']) {
      var tpl = obj['query']['pages'][i]['templates'][j]['title'];
      for(var k=0;k<dabnames.length;k++) {
        if(tpl == dabnames[k]) {
          dablinks[dablinks.length] = obj['query']['pages'][i]['title'];
        }
      }
    }
  }
  if(obj['query']['redirects']) {
    var dablen = dablinks.length   //don't iterate over additions.
    for(var i in obj['query']['redirects']) {
      for(var j=0;j<dablen;j++) {
        if(obj['query']['redirects'][i]['to'] == dablinks[j]) {
          dablinks[dablinks.length] = obj['query']['redirects'][i]['from'];
        }
      }
    }
  }
  var links = (document.getElementById('content')) ? document.getElementById('content').getElementsByTagName('a') : document.getElementById('mw_content').getElementsByTagName('a')
  var found = 0;
  for(var i=0;i<links.length;i++) {
    for(var j=0;j<dablinks.length;j++) {
      //to match "Foo (bar)" with "/wiki/Foo_%28bar%29", have to do some hacky string manipulation
      var dablink = wgArticlePath.replace(/\$1/,'') + escape(dablinks[j].replace(/ /g,'_'));
      if(links[i].href.replace(wgServer,'') == dablink) {
        links[i].style.border = '2px solid #00ff00';
        found++;
      }
    }
  }
 
  var dab = document.getElementById('t-dab');
  if(dab) {
    document.getElementById('dab-prog').style.display = 'none';
    dab.appendChild(document.createElement('br'));
    if(found > 0) {
      var span = document.createElement('span');
      span.appendChild(document.createTextNode(found + ' links to disambiguation pages found.'));
      span.style.border = '2px solid #00ff00';
      dab.appendChild(span);
    } else {
      dab.appendChild(document.createTextNode('No disambiguation links found.'));
    }
  } else {
    alert(found + ' links to disambiguation pages found.');
  }
}
 
function queryString(p) {
  var re = RegExp('[&?]' + p + '=([^&]*)');
  var matches;
  if (matches = re.exec(document.location)) {
    try { 
      return decodeURI(matches[1]);
    } catch (e) {
    }
  }
  return null;
}