MediaWiki:Common.js/numarticles.js

in Wikipedia, die vrye ensiklopedie

Let wel: Na die wysiging is dit dalk nodig om u blaaier se kasgeheue te verfris voordat u die veranderinge sal sien:

  • Firefox / Safari: hou Shift en kliek Herlaai, of druk Ctrl-F5 of Ctrl-R (⌘-R op 'n Mac)
  • Google Chrome: Druk Ctrl-Shift-R (⌘-Shift-R op 'n Mac)
  • Internet Explorer / Edge: Hou Ctrl en kliek Refresh, of druk Ctrl-F5
  • Opera: Gaan na Kieslys → Settings (Opera → Preferences op 'n Mac) en dan na Privacy & security → Clear browsing data → Cached images and files.
/* Interwiki {{NUMBEROFARTICLES}} [0.0.2], Original from http://test.wikipedia.org/wiki/MediaWiki:Common.js/numarticles.js
Uses the API to grab the number of articles on an allowed remote project on allowed pagename
Example: <span class="numarticles-iw">en.wikipedia</span>

Possible improvements:
* Better handling of htmlentities than with innerHTML, ick.
* More magicwords?
* less hacky marking of numbers in the rendered text? (fat chance!)
*/

function numArticlesIW() {
  var docobj = document.getElementById('bodyContent') || document.getElementById('content') || document.getElementsByTagName('body')[0];
  var numa = getElementsByClassName(docobj,'span','numarticles-iw');
  if(!numa.length) return;
  for(var i=0;i<numa.length;i++) {
    var txt = getText(numa[i]);
    if(!txt.match(/[a-z0-9-]{2,12}\.(wikipedia|wiktionary|wikibooks|wikinews|wikisource|wikiquote)/)) continue;
    var url = 'http://' + txt + '.org/w/api.php&action=parse&format=json&callback=numartCB&text=id-' + i + '-id%20%20num-{{NUMBEROFARTICLES}}-num';
    mw.loader.load(url);
    remText(numa[i]);
  }
}
$(numArticlesIW);

function numartCB(obj) {
  if(!obj['parse'] || !obj['parse']['text'] || !obj['parse']['text']['*']) return;
  var docobj = document.getElementById('bodyContent') || document.getElementById('content') || document.getElementsByTagName('body')[0];
  var numa = getElementsByClassName(docobj,'span','numarticles-iw');
  var txt = obj['parse']['text']['*'];
  var id = txt.match(/id-\d*-id/i)[0].replace(/id-(\d*)-id/i,'$1');
  var num = txt.match(/num-.*-num/i)[0].replace(/num-(.*)-num/i,'$1');
  if(num.indexOf('&') == -1) {
    numa[id].appendChild(document.createTextNode(num));
  } else {
    //sigh, contains html entities, heck with DOM!
    numa[id].innerHTML = num;
  }
}

function getText(obj) {
  if(obj.nodeType == 3) return obj.nodeValue;
  var txt = [];
  var i = 0;
  while(obj.childNodes[i]) {
    txt[txt.length] = getText(obj.childNodes[i]);
    i++;
  }
  return txt.join('');
}

function remText(obj) {
  while(obj.firstChild) obj.removeChild(obj.firstChild);
}