Jump to content

User:ProgrammingGeek/afc-diff.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.
function getElementsByClass(searchClass, node, tag) {
  var classElements = new Array();
  if ( node == null )
    node = document;
  if ( tag == null )
    tag = '*';
  var els = node.getElementsByTagName(tag);
  var elsLen = els.length;
  var pattern = new RegExp("(^|\\s)"+searchClass+"(\\s|$)");
  for (i = 0, j = 0; i < elsLen; i++) {
    if ( pattern.test(els[i].className) ) {
      classElements[j] = els[i];
      j++;
    }
  }
  return classElements;
}

function getDiff() {
  var pathname = window.location.pathname.replace('wiki/', 'index.php?title=');
  var diffUri = 'https://en.wikipedia.org/'+pathname+'&action=history';
  var link = getElementsByClass('mw-changeslist-date', diffUri, 'li')[0].href;
  var deleted = getElementsByClass('diff-deletedline', link, 'td');
  var added = getElementsByClass('diff-addedline', link, 'td');
  var addtext = '<tr>';
  var deltext = '<tr>';
  deleted.forEach(function(element){
    deltext += '<td><p>'+element+'</p></td>';
  });
  deltext += '</tr>';
  added.forEach(function(element){
    addtext += '<td><p>'+element+'</p></td>';
  });
  addtext += '</tr>';
  document.getElementById('contentSub').innerHTML = '<table>'+deltext+addtext+'</table>';
}
addOnloadHook(function() {
  if (location.href.match('Draft:') || location.href.match('User:')) {
    getDiff();
  }
});