Jump to content

User:DannyS712/12Hours.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.
// Install with:
// <code><nowiki>		{{subst:Iusc|User:DannyS712/12Hours.js}}																	</nowiki></code>
// or with
// <code><nowiki>		importScript( 'User:DannyS712/12Hours.js' ); // Backlink: [[User:DannyS712/12Hours.js]] 					</nowiki></code> 
//
// Special thanks to User:Bility and their script, [[User:Bility/convert24hourtime.js]], which I forked.
// If forking this script, please note my contributions / give me credit

var config = {
	name: '[[User:DannyS712/12Hours|12Hours]]',
	version: 2.3,
	debug: false
};

if (mw.config.get('wgAction') === 'history' || mw.config.get('wgCanonicalNamespace') === 'Special' || mw.config.get('wgAction') === 'view') {
	$(document).ready(function() {
		if (mw.config.get('wgAction')=='history') $('span.mw-history-histlinks ~ a').each(function() {
			if (($(this).attr("class") === "mw-changeslist-date") || $(this).attr("class") === "mw-changeslist-date userlink") convertTo12HourTime($(this));
			
		});
		else if (mw.config.get('wgCanonicalNamespace')=='Special') {
			if (mw.config.get('wgCanonicalSpecialPageName')=='Contributions'){
				if ($('div').hasClass('mw-warning-with-logexcerpt mw-content-ltr'))	do_log(true);
   				else $('ul').first().children().each(function() {convertTo12HourTime($(this).children().first());});
			}
			else if (mw.config.get('wgCanonicalSpecialPageName')=='Log') do_log(false);
			else if (mw.config.get('wgCanonicalSpecialPageName')=='Userrights') do_log(false);
		}
		else if (document.title.indexOf("Difference between revisions") > -1) do_dif();
		else if (window.location.href.indexOf("oldid") > -1) {
			convertTo12HourTime ( $('#mw-revision-date') );
		}
	});
}
function do_log ( second ){
	$('ul').first().children().each(function() {
		var html = $(this).html();
		if (config.debug) console.log( html );
		html = html.replace(/(\d\d:\d\d)/g, on_log);
		if (config.debug) console.log(html);
		$(this).html( html );
		//convertTo12HourTime_log($(this));
	});
	if (second){
		if (config.debug) console.log("blocked");
		$('ul').eq(1).children().each(function() {
			var html = $(this).html();
			if (config.debug) console.log( html );
			html = html.replace(/(\d\d:\d\d)/g, on_log);
			if (config.debug) console.log(html);
			$(this).html( html );
		});
	}
}
function on_log ( match, offset, string ){
	if (config.debug) console.log( match, convertTo12HourTime_log(match) );
	return convertTo12HourTime_log(match);
}
function on_diff ( rev_time_act ){
	var just_time = rev_time_act.substr(0, 5);
	var rev_time_rst = rev_time_act.substr(5);
	var new_time = convertFromTime(just_time);
	return ( new_time + rev_time_rst);
}
function convertTo12HourTime(timeElement) {
	var time = timeElement.html().substr(0,5);
	var converted = convertFromTime( time, true );
	timeElement.html(converted + timeElement.html().substr(5));
}
function convertTo12HourTime_log( time ) {
	var converted = convertFromTime( time, true );
	return converted;
}
function convertFromTime( time, space ){
	var hour = parseFloat(time.substr(0,2));
	if (hour<12) {
		hour = (hour===0) ? 12 : hour;
		time = (hour + time.substr(2,5) + ' am');
	} else {
		hour = (hour!=12) ? hour-12 : hour;
		time = (hour + time.substr(2,5) + ' pm');
	}
	if (hour<10) {
		if (space) time = (unescape('%A0%A0')+time);
	}
	return time;
}
function do_dif(){
	var rev_time_text = $("#mw-diff-ntitle1 > strong > a").text();
	var rev_time_pre;
	var rev_time_rest;
	if ( rev_time_text.substr(0, 15) === "Revision as of " ){
		rev_time_pre = "Revision as of ";
		rev_time_rest = rev_time_text.substr(15);
	} else if ( rev_time_text.substr(0, 22) === "Latest revision as of " ){
		rev_time_pre = "Latest revision as of ";
		rev_time_rest = rev_time_text.substr(22);
	}
	$("#mw-diff-ntitle1 > strong > a").text( rev_time_pre + on_diff(rev_time_rest));
	
	var rev_time_text2 = $("#mw-diff-otitle1 > strong > a").text();
	var rev_time_pre2;
	var rev_time_rest2;
	if ( rev_time_text2.substr(0, 15) === "Revision as of " ){
		rev_time_pre2 = "Revision as of ";
		rev_time_rest2 = rev_time_text2.substr(15);
	}
	$("#mw-diff-otitle1 > strong > a").text( rev_time_pre2 + on_diff(rev_time_rest2));
}