Jump to content

User:Georgewilliamherbert/monobook.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.
//Interiot's javascript edit counter
if (document.title.indexOf('User:Interiot/Tool2/code.js') != -1) {
  mw.loader.load('https://en.wikipedia.org/w/index.php?title=User:Interiot/Tool2/code.js&action=raw&ctype=text/javascript'); }


/* Templates Used finder, version [0.0.3b]
Originally from: http://en.wikipedia.org/wiki/User:Splarka/temused.js
 
Notes:
* Uses the API using head/appendchild(script) and callback to avoid ajax.
* Redesigned a-la http://en.wikipedia.org/wiki/User:Splarka/templatesabused.js (which is for edit mode)
** This script is for view mode. Similar display, completely different method of operation.
*/
 
if(mw.config.get('wgNamespaceNumber') != -1 && mw.config.get('wgArticleId') > 0 && mw.config.get('wgAction') == 'view') addOnloadHook(findTemButton)
function findTemButton() {
  var portlet = (window.temusedIAmATool) ? 'p-tb' : 'p-cactions';
  mw.util.addPortletLink(portlet,'javascript:findTem()','Templates used','ca-tem');
  if(queryString('findtem')=='true') findTem();
}
 
function findTem() {
  var bar = document.getElementById('contentSub') || document.getElementById('topbar');
  if(!bar) return;
  var div = document.createElement('div');
  div.setAttribute('id','tem-out');
  bar.appendChild(div);
  injectSpinner(div,'tem');
  var url = mw.config.get('wgServer') + mw.config.get('wgScriptPath') + '/api.php?maxage=0&smaxage=0&format=json&callback=findTemCB&action=query&generator=templates&gtllimit=500&prop=revisions&indexpageids&titles=' + encodeURIComponent(mw.config.get('wgPageName')) ;
  mw.loader.load(url);
}
 
function findTemCB(obj) {
  var tem = document.getElementById('tem-out');
  removeSpinner('tem');
  if(!obj['query'] || !obj['query']['pages']) {
    tem.appendChild(document.createTextNode('error, or no templates found'));
    return;
  }
  appendCSS('#ca-tem a {display:none;} #tem-out {border:1px solid #777777;} .tem-week {color:black;} .tem-day {font-weight:bold;} .tem-hour {color:red;}');
  var ul = document.createElement('ul');
  var ids = obj['query']['pageids'];
  for(var i=0;i<ids.length;i++) {
    if(parseInt([ids[i]]) < 0) continue
    var page = obj['query']['pages'][ids[i]];
    var title = page['title'];
    if(!page['revisions']) continue
    var rev = page['revisions'][0];
    var li = document.createElement('li');
 
    var ts = rev['timestamp'] || 0;
    var now = new Date();
    var tsd = new Date();
    tsd.setISO8601(ts);
    var timesince = Math.floor((now - tsd)/1000);
    if(timesince == '') timesince = -1;
 
    addLinkChild(li,mw.config.get('wgScript') + '?curid=' + page['pageid'],title);
    if(timesince > 604800) li.className = 'tem-stale';
    li.appendChild(document.createElement('br'));
    li.appendChild(document.createTextNode('Last edited: '));
    var span = document.createElement('span');
     span.appendChild(document.createTextNode(duration(timesince,2)));
     span.className = 'tem-time'
     if(timesince < 604800) span.className += ' tem-week'
     if(timesince < 86400) span.className += ' tem-day'
     if(timesince < 3600) span.className += ' tem-hour'
    li.appendChild(span)
    li.appendChild(document.createTextNode(' ago by '));
    addLinkChild(li, mw.config.get('wgScript') + '?title=Special:Contributions/' + encodeURIComponent(rev['user']),rev['user']);
    li.appendChild(document.createTextNode(' ('));
    addLinkChild(li, mw.config.get('wgScript') + '?diff=' + rev['revid'],'diff',false,false,'diff=' + rev['revid']);
    li.appendChild(document.createTextNode(') '));
    if(rev['comment']) li.appendChild(document.createTextNode(rev['comment']))
    ul.appendChild(li);  
  }
  tem.appendChild(ul);
}
 
function addLinkChild(obj,href,text,id,classes,title) {
  if(!obj || !href || !text) return false;
  var a = document.createElement('a');
  a.setAttribute('href',href);
  a.appendChild(document.createTextNode(text));
  if(id) a.setAttribute('id',id);
  if(classes) a.setAttribute('class',classes);
  if(title) a.setAttribute('title',title);
  obj.appendChild(a);
  return a;
}
 
function queryString(p) {
  var re = RegExp('[&?]' + p + '=([^&]*)');
  var matches;
  if (matches = re.exec(document.location)) {
    try { 
      return decodeURI(matches[1]);
    } catch (e) {
    }
  }
  return null;
}
 
//ISO 8601 date module by Paul Sowden, licensed under AFL.
Date.prototype.setISO8601 = function(string) {
  if(!string) return
  var regexp = '([0-9]{4})(-([0-9]{2})(-([0-9]{2})(T([0-9]{2}):([0-9]{2})(:([0-9]{2})(\.([0-9]+))?)?(Z|(([-+])([0-9]{2}):([0-9]{2})))?)?)?)?';
  var d = string.match(new RegExp(regexp));
  if(d.length < 1) return
  var offset = 0;
  var date = new Date(d[1], 0, 1);
  if(d[3]) date.setMonth(d[3] - 1)
  if(d[5]) date.setDate(d[5])
  if(d[7]) date.setHours(d[7])
  if(d[8]) date.setMinutes(d[8])
  if(d[10]) date.setSeconds(d[10])
  if(d[12]) date.setMilliseconds(Number('0.' + d[12]) * 1000)
  if(d[14]) {
    offset = (Number(d[16]) * 60) + Number(d[17]);
    offset *= ((d[15] == '-') ? 1 : -1);
  }
  offset -= date.getTimezoneOffset();
  time = (Number(date) + (offset * 60 * 1000));
  this.setTime(Number(time));
}
 
function duration(input,depth) {
  var num = input;
  var out = '';
  var s = num % 60; num = Math.floor(num / 60);
  var m = num % 60; num = Math.floor(num / 60);
  var h = num % 24; num = Math.floor(num / 24);
  var d = num % 7;  num = Math.floor(num / 7);
  var w = num % 52; num = Math.floor(num / 52);
  var y = num
  if(y > 0) out += y + 'yrs '
  if(y + w > 0) out += w + 'wks '
  if(y + w + d > 0) out += d + 'days '
  if(y + w + d + h > 0) out += h + 'hrs '
  if(y + w + d + h + m > 0) out += m + 'mins '
  out += s + 'secs';  
  if(depth && depth < out.split(' ').length) {
    out = out.split(' ').slice(0,depth).join(' ');
  }
  return out;
}