User:Splarka/diffpreloader.js

In today's article we are going to delve into the fascinating world of User:Splarka/diffpreloader.js. This topic has been the subject of debate and study for decades, generating great interest among both experts and amateurs. Throughout history, User:Splarka/diffpreloader.js has played a crucial role in different fields, from science to art, politics and popular culture. In this article we will explore the various aspects of User:Splarka/diffpreloader.js, from its origins to its impact on modern society, analyzing its implications and highlighting its relevance in today's world. Get ready to embark on an exciting journey through User:Splarka/diffpreloader.js and discover everything this theme has to offer!
/* Difference preloader checker thingy, version 
Originally from: http://en.wikipedia.orghttps://wikifreehand.com/en/User:Splarka/diffpreloader.js

Pre-fetches diff links to see if they're too big. Some diffs are really big! 
Styles the link indicating the size (more or less than threshhold bytes of diff table html).

Notes:
* Currently only works with oldid= parameter present.
** It is feasable to do it with title= parameter or wgArticlePath match, but this would be a lot of work.
* Currently doesn't load in the main namespace on view actions, this is to prevent excessive javascripting.
* Custom user definitions:
* Style (small/big) threshhold: "var diffPreloaderThreshhold = integer" (default 100000).
* Text of sibling link: "var diffPreloaderSibling = string" (default false, uses diff link instead).
* Your own styles: "var diffPreloaderStyle = ".

To do:
* Should there be an auto-go option? 
** Using document.location.href ruins browser navigation history in many browsers, meh. 
* Should there be an alert option? 
* Should there be a "check all" button?
*/

if(!window.diffPreloaderStyle) var diffPreloaderStyle = ;
if(!window.diffPreloaderSibling) var diffPreloaderSibling = false
if(!window.diffPreloaderThreshhold) var diffPreloaderThreshhold = 100000

function diffPreloader() {
  var docobj = document.getElementById('bodyContent') || document.getElementById('content') || document.body;
  var a = docobj.getElementsByTagName('a');
  if(!a) return
  var ds = diffPreloaderStyle;
  var siblink = diffPreloaderSibling || false;
  appendCSS('.diffPreload-new {' + ds + '} .diffPreload-loading {' + ds + '} .diffPreload-small {' + ds + '} .diffPreload-big {' + ds + '} ');

  for(var i=a.length-1;i>=0;i--) {
    var href = a.getAttribute('href',2);
    if(!href) continue
    var diffr = /diff\=(next|prev|*)/i;
    var oldidr = /oldid\=(\d*)/i;

    if(diffr.test(href) && oldidr.test(href)) {
      var as;
      if(!siblink) {
        as = a;
      } else {
        var small = document.createElement('small');
        var sib = document.createElement('a');
        as = sib;
        sib.appendChild(document.createTextNode(siblink));
        small.appendChild(document.createTextNode(' ('));
        small.appendChild(sib)
        small.appendChild(document.createTextNode(')'));
        insertAfter(small,a);
      }
      as.className += ' diffPreload-new';
      as.setAttribute('id','diffPreload-' + i);
      as.setAttribute('alt',href);
      as.setAttribute('href','javascript:diffPreloaderClick(' + i + ',"' + href.match(diffr) + '","' + href.match(oldidr) + '");');
    }
  }
}
if(wgAction != 'view'|| wgNamespaceNumber != 0 || queryString('diff')) addOnloadHook(diffPreloader)

function diffPreloaderClick(aid,diff,oldid) {
  var a = document.getElementById('diffPreload-' + aid);
  a.setAttribute('class',a.getAttribute('class').replace(/diffPreload\-new/ig,'diffPreload-loading'))
  var url = wgScriptPath + '/api.php?maxage=86400&smaxage=86400&action=query&requestid=' + aid + '&indexpageids&format=json&callback=diffPreloaderCB&prop=revisions&rvprop=size&revids=' + oldid + '&rvdiffto=' + diff;
  mw.loader.load(url);
}

function diffPreloaderCB(obj) {
  if(obj) alert('Api error in diff preloader: ' + obj + '\n' + obj)
  if(!obj || !obj || !obj || !obj) return
  var a = document.getElementById('diffPreload-' + obj);
  var revs = obj];
  if(!revs || revs.length == 0 || !revs || !revs) return
  var length = revs.length;
  var size = revs || 0;
  if(length < diffPreloaderThreshhold) {
    var aclass = 'diffPreload-small';
  } else {
    var aclass = 'diffPreload-big';
  }
  a.setAttribute('class',a.getAttribute('class').replace(/diffPreload\-loading/ig,aclass));
  a.setAttribute('href',a.getAttribute('alt'));
}

function queryString(p) {
  var re = RegExp('' + p + '=(*)');
  var matches;
  if (matches = re.exec(document.location)) {
    try { 
      return decodeURI(matches);
    } catch (e) {
    }
  }
  return null;
}

function insertAfter(nn,ref) {
  if(ref.nextSibling) {
    ref.parentNode.insertBefore(nn,ref.nextSibling);
  } else {
    ref.parentNode.appendChild(nn);
  }
}