User:TJLittle123/common.js

//Mediawiki API wrapper from https://github.com/brettz9/mediawiki-js (function {'use strict';

/**	* @static * @private */	var JSONP = (function (global) {		// (C) WebReflection Essential - Mit Style		// cleaned up by Brett Zamir for JSLint and avoiding additional globals and need for conventional [?&]callback= in URL) // 'use strict'; // Added above var id = 0, ns = 'MediaWikiJS', prefix = '__JSONP__', document = global.document, documentElement = document.documentElement; return function (uri, callback) { var src = prefix + id++, script = document.createElement('script'), JSONPResponse = function { try { delete global[ns][src]; } catch(e) { global[ns][src] = null; } documentElement.removeChild(script); callback.apply(this, arguments); };			global[ns][src] = JSONPResponse; documentElement.insertBefore(				script,				documentElement.lastChild			).src = uri + (uri.indexOf('?') > -1 ? '&' : '?') + 'callback=' + ns + '.' + src; };	}(window));

/**	* @constructor * @param {object|string} opts The options (currently "baseURL" and "apiPath" args only); if a string is supplied, it will be used as the baseURL * @param {object} argObj Object of key-value pairs to be serialized * @param {function} cb The callback to execute upon server (JSONP) reply */	function MediaWikiJS(opts, argObj, cb) { if (!(this instanceof MediaWikiJS)) { return new MediaWikiJS(opts, argObj, cb); }		if (typeof opts === 'string') { this.baseURL = opts; }		else { this.apiPath = opts.apiPath; this.baseURL = opts.baseURL; }		if (!this.apiPath) { this.apiPath = '/w/api.php'; }		if (argObj) { this.send(argObj, cb); }	}

/**	* Send the API request to the server * @param {object} argObj Object of arguments to be serialized * @param	 {function} cb The callback to execute upon server (JSONP) reply */	MediaWikiJS.prototype.send = function MediaWikiJS__send (argObj, cb) { cb = cb || function {}; // Are there API calls with side effects? var uri, arg, args = ''; for (arg in argObj) { if (argObj.hasOwnProperty(arg)) { args += '&' + arg + '=' + encodeURIComponent(argObj[arg]); }		}		uri = this.baseURL + this.apiPath + '?format=json' + args; JSONP(uri, cb); };

// EXPORTS window.MediaWikiJS = MediaWikiJS; });

function RevisionInfo(callback) { /*globals MediaWikiJS*/ var mwjs = new MediaWikiJS('https://en.wikipedia.org', {action: 'query', prop: 'revisions', titles: location.pathname.split("wiki/")[1], rvprop: 'timestamp|user|comment|content', rvlimit: '1000'}, function output (data) {			'use strict';			var pages = data.query.pages;			callback(pages[Object.keys(pages)[0]].revisions);		}); }

var data = $.Deferred; var month = [ "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec" ];

function Pad(i) { if (i < 10) { i = "0" + i;	   } return i;	}

$.when( data ).done( function ( value ) {		$( "small:contains('Last Updated:')" ).remove;

var heading=$("#content h2, #content h3").each( function {			var commentString = "\/* " + $( this ).text.replace("[edit]", "")  + " *\/";			var commentIndex = -1

for (var i = 0; i < value.length; i += 1) { var thisComment = value[i].comment; thisComment = thisComment.substring(0, thisComment.indexOf("*\/") + 2)

if (thisComment == commentString) { commentIndex = i;					i = value.length; }			}

if (commentIndex == -1) { commentIndex = value.length - 1; }			var thisBlock = $( this ).nextUntil( ":header" );

if(thisBlock.length) { thisBlock = thisBlock[thisBlock.length - 1]; } else { thisBlock = this; }

var dateStamp = new Date(value[commentIndex].timestamp); var userName = value[commentIndex].user;

var formatDateStamp = String(Pad(dateStamp.getUTCHours)) + String(Pad(dateStamp.getUTCMinutes)) + "Z, " + dateStamp.getUTCDate + " " + month[dateStamp.getUTCMonth] + " " + dateStamp.getUTCFullYear

$( thisBlock ).after(" Last Updated: " + userName + " " + formatDateStamp + " "); });	});	$(document).ready(function{		RevisionInfo( function (value) { data.resolve(value); } );	});