User:Guywan/Scripts/12HourFormat.js: Difference between revisions

Content deleted Content added
No edit summary
Tags: Mobile edit Mobile web edit
maintenance: TypeError: undefined is not an object (evaluating 'html.replace')
 
(44 intermediate revisions by one other user not shown)
Line 1:
// [[Category:Wikipedia scripts]]
/*
// <nowiki>
* Install with:
$(function()
* <code><nowiki>{{subst:Iusc|User:Guywan/Scripts/12HourFormat.js}}</nowiki></code>
* or:
* <code><nowiki>importScript("User:Guywan/Scripts/12HourFormat.js"); // Backling: [[User:Guywan/Scripts/12HoursFormat.js]]</nowiki></code>
*
* This script was originally authored by User:Bility, [[User:Bility/convert24hourtime.js]].
* Considerable contributions by User:DannyS712, [[User:DannyS712/12Hours.js]], whose version I have forked.
* I have mainly streamlined the code and introduced more functionality.
*/
 
var config =
{
////////////////
name : "[[User:Guywan/Scripts/12HourFormat]]",
// NAMESPACES //
version : 3.0,
////////////////
debug : false
};
switch(mw.config.get("wgCanonicalNamespace"))
 
if(mw.config.get("wgAction") == "history"
|| mw.config.get("wgAction") == "view"
|| mw.config.get("wgAction") == "edit"
|| mw.config.get("wgCanonicalNamespace") == "Special")
{
$(function()
{
case "Special":
// Modify time when viewing page history.
if switch(mw.config.get("wgActionwgCanonicalSpecialPageName") == "history")
{
$("span.mw-history-histlinks ~ a").each(function()
{
case "Contributions":
if($(this).attr("class") === "mw-changeslist-date")
$(".mw-changeslist-date").each(function()
{
{
var time = partition($(this).text());
$(this).html(rxReplace($(this).html()));
});
break;
case "Log":
$(this).text(time[0] + time[1] + time[2]);
case "Userrights":
}
case "AbuseLog":
});
$("ul").children().each(function()
}
{
else if(mw.config.get("wgAction") == "edit")
$(this).html($(this).html().replace(/()(\d\d:\d\d)(, )/g, rxPartition));
{
});
if($("div").hasClass("mw-warning-with-logexcerpt mw-content-ltr"))
{ break;
var html = $(".mw-logline-delete").html();
case "Listusers":
$("ul").children().each(function()
$(".mw-logline-delete").html(regexpReplace(html));
{
$(this).html($(this).html().replace(/(on \d{1,2} \w+ \d{4} at )(\d\d:\d\d)()/g, rxPartition));
});
break;
case "Project":
// TODO
break;
}
} break;
else if(mw.config.get("wgCanonicalNamespace") == "File")
case "File":
{
// Modify time in file history table.
$("td > a").each(function()
{
// DMY
var text = $(this).text();
$(this).html($(this).html().replace(/()(\d\d:\d\d)(, \d{1,2} \w+ \d{4})/g, rxPartition));
// MD,Y
var index = text.indexOf(":");
$(this).html($(this).html().replace(/()(\d\d:\d\d)(, \w+ \d{1,2}, \d{4})/g, rxPartition));
if(index > -1)
{
var time = text.substr(index - 2, 6);
// Will convert anything in the table matching the pattern "dd:dd," where d is a digit.
if(time.match(/\d\d:\d\d,/))
{
text = partition(text);
$(this).text(text[0] + text[1] + text[2]);
}
}
});
} break;
else if(mw.config.get("wgCanonicalNamespace") == "Special")
case "User":
{
// TODO
// Modify time when viewing user contributions.
break;
if(mw.config.get("wgCanonicalSpecialPageName") == "Contributions")
}
/////////////
// ACTIONS //
/////////////
switch(mw.config.get("wgAction"))
{
case "history":
$(".mw-changeslist-date").each(function()
{
$(this).text(rxReplace($(this).text()));
$(".mw-contributions-list").children().each(function()
{});
break;
var text = partition($(this).children().first().text());
case "view":
$(this).children().first().text(text[0] + text[1] + text[2]);
if(document.title.indexOf("Difference between revisions") > -1)
});
}
// Modify time when viewing user log info.
else if(mw.config.get("wgCanonicalSpecialPageName") == "Log")
{
$("#mw-diff-ntitle1 > strong > a").html(rxReplace($("#mw-diff-ntitle1 > strong > a").html()));
$("ul").first().children().each(function()
$("#mw-diff-otitle1 > strong > a").html(rxReplace($("#mw-diff-otitle1 > strong > a").html()));
{
$(".diff-currentversion-title").text(rxReplace($(".diff-currentversion-title").text()));
var html = $(this).html();
$(this).html(regexpReplace(html));
});
}
// Modify time in user rights page.
// Modify time in permalink pages.
else if(mw.config.get("wgCanonicalSpecialPageName") == "Userrights")
if($("div").hasClass("mw-revision"))
{
$("#mw-revision-date").html(rxReplace($("#mw-revision-date").html()));
$("ul").children().each(function()
{
var html = $(this).html();
$(this).html(regexpReplace(html));
});
}
}
// Modify time when comparing two revisions.
else if((mw.config.get("wgAction") == "view") && (document.title.indexOf("Difference between revisions") > -1))
{
var comp = "#mw-diff-ntitle1 > strong > a";
var text = "";
for(i = 0; i < 2; i++)
{
text = partition($(comp).text());
$(comp).text(text[0] + text[1] + text[2]);
comp = "#mw-diff-otitle1 > strong > a";
}
break;
case "edit":
text = partition($(".diff-currentversion-title").text());
// TODO
$(".diff-currentversion-title").text(text[0] + text[1] + text[2]);
} break;
}
// Modify the time in the footer of most pages.
if($("#footer-info-lastmod").length)
{
var footer = partition($("#footer-info-lastmod").text());
$("#footer-info-lastmod").text(footer[0] + footer[1] + footer[2]);
}
});
}
 
function partition(text)
{
var front = text.substr(0, text.indexOf(":") - 2);
///////////
var time = text.substr(text.indexOf(":") - 2, 5);
// OTHER //
time = convert(time);
///////////
// Modify time in warning banners.
var end = text.substr(text.indexOf(":") + 3, text.length);
if($("div").hasClass("mw-warning-with-logexcerpt mw-content-ltr"))
{
return new Array(front, time, end);
$(".mw-warning-with-logexcerpt.mw-content-ltr > ul").children().each(function()
}
{
$(this).html(rxReplace($(this).html()));
});
}
 
// Modify the time in the footer of most pages.
function regexpReplace(text)
if($("#footer-info-lastmod").length)
{
{
var time = text.substr(1, 5);
$("#footer-info-lastmod").text(rxReplace($("#footer-info-lastmod").text()));
}
return text.replace(/(\d\d\:\d\d)/g, convert(time));
}
 
/*
function convert(time)
* Used when it is unlikely non-times will be matched.
{
*/
var hour = parseFloat(time.substr(0, 2));
function rxReplace(html)
if(hour > 12 && hour !== 0)
{
// Note a call to html on nonexistent elements returns undefined e.g. $('.sssssss').html(). Guard against this being passed in.
hour = hour - 12;
html = html || '';
return html.replace(/(\d\d:\d\d)/g, convert);
time = (hour + time.substr(2, 5) + " PM");
}
else
/*
* Used when a very specific match needs to be made and
* only a substring of the match should be modified.
*/
function rxPartition(match, p1, p2, p3)
{
time =return (hourp1 + time.substrconvert(2, 5p2) + " AM"p3);
}
returnfunction convert(time;)
{
}
var hour = parseFloat(time.substr(0, 2));
if(hour >= 12)
{
if(hour != 12)
{
hour = hour - 12;
}
return (hour + time.substr(2) + " PM");
}
if(hour === 0)
{
hour = 12;
}
return (hour + time.substr(2) + " AM");
}
});
// </nowiki>