User:Splarka/diffpreloader.js

/* Difference preloader checker thingy, version [0.0.0] Originally from: http://en.wikipedia.org/wiki/User:Splarka/diffpreloader.js Pre-fetches diff links to s

/* Difference preloader checker thingy, version [0.0.0]
Originally from: http://en.wikipedia.org/wiki/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 = [unknown,checking,small,big]".

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 = ['color:#ffffff;background-color:#000000;','background-color:#eeee00;','background-color:#00ff00;','background-color:#ff0000;'];
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[0] + '} .diffPreload-loading {' + ds[1] + '} .diffPreload-small {' + ds[2] + '} .diffPreload-big {' + ds[3] + '} ');

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

    if(diffr.test(href) && oldidr.test(href)) {
      var as;
      if(!siblink) {
        as = a[i];
      } 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[i]);
      }
      as.className += ' diffPreload-new';
      as.setAttribute('id','diffPreload-' + i);
      as.setAttribute('alt',href);
      as.setAttribute('href','javascript:diffPreloaderClick(' + i + ',"' + href.match(diffr)[1] + '","' + href.match(oldidr)[1] + '");');
    }
  }
}
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['error']) alert('Api error in diff preloader: ' + obj['error']['code'] + '\n' + obj['error']['info'])
  if(!obj['requestid'] || !obj['query'] || !obj['query']['pages'] || !obj['query']['pageids']) return
  var a = document.getElementById('diffPreload-' + obj['requestid']);
  var revs = obj['query']['pages'][obj['query']['pageids'][0]]['revisions'];
  if(!revs || revs.length == 0 || !revs[0]['diff'] || !revs[0]['diff']['*']) return
  var length = revs[0]['diff']['*'].length;
  var size = revs[0]['size'] || 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[1]);
    } catch (e) {
    }
  }
  return null;
}

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

Content Disclaimer

Informasi ini disarikan dari Wikipedia dan disajikan kembali untuk tujuan edukasi. Konten tersedia di bawah lisensi CC BY-SA 3.0. Kami tidak bertanggung jawab atas ketidakakuratan data yang bersumber dari kontribusi publik tersebut.

  1. The information displayed on this website is sourced in part or in whole from Wikipedia and has been adapted for the purpose of restating it. We strive to provide accurate and relevant information, however:
  2. There is no guarantee of absolute accuracy. Wikipedia is an open, collaborative project that can be edited by anyone, so information is subject to change.
  3. It is not intended to constitute professional advice. The content displayed is for informational and educational purposes only. For important decisions (e.g., medical, legal, or financial), please consult a professional.
  4. Content copyright. Wikipedia is licensed under the Creative Commons Attribution-ShareAlike License (CC BY-SA). This means that content may be reused with appropriate attribution and shared under a similar license.
  5. Responsible use. Any risk arising from the use of information from this website is entirely the responsibility of the user.