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

Content deleted Content added
No edit summary
maintenance: TypeError: undefined is not an object (evaluating 'html.replace')
 
(37 intermediate revisions by one other user not shown)
Line 1:
// [[Category:Wikipedia scripts]]
/*
// <nowiki>
* Install with:
* <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]]",
version : 3.0,
debug : false
};
 
$(function()
{
////////////////
if(mw.config.get("wgCanonicalNamespace") == "Special")
// NAMESPACES //
////////////////
switch(mw.config.get("wgCanonicalNamespace"))
{
case "Special":
switch(mw.config.get("wgCanonicalSpecialPageName"))
switch(mw.config.get("wgCanonicalSpecialPageName"))
{
case "Contributions":
$(".mw-contributions-list").children().each(function()
{
$(this).children().first().text(partition($(this).children().first().text()));
});
break;
case "Log":
case "Userrights":
$("ul").children().each(function()
{
$(this).html(regexpReplace($(this).html(), 1, 5));
});
break;
case "Project":
// TODO
break;
}
}
else if(mw.config.get("wgCanonicalNamespace") == "File")
{
// Modify time in file history table.
$("td > a").each(function()
{
var text = $(this).text();
var index = text.indexOf(":");
if(index > -1)
{
case "Contributions":
var time = text.substr(index - 2, 6);
$(".mw-changeslist-date").each(function()
{
// Will convert anything in a table matching the pattern "dd:dd," where d is a digit.
$(this).html(rxReplace($(this).html()));
if(time.match(/\d\d:\d\d,/))
{ });
break;
$(this).text(partition(text));
}
case "Log":
case "Userrights":
case "AbuseLog":
$("ul").children().each(function()
{
$(this).html($(this).html().replace(/()(\d\d:\d\d)(, )/g, rxPartition));
});
break;
case "Listusers":
$("ul").children().each(function()
{
$(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;
}
case "File":
else if(mw.config.get("wgCanonicalNamespace") == "User")
// Modify time in file history table.
{
$("td > a").each(function()
// TODO
{
// DMY
$(this).html($(this).html().replace(/()(\d\d:\d\d)(, \d{1,2} \w+ \d{4})/g, rxPartition));
// MD,Y
$(this).html($(this).html().replace(/()(\d\d:\d\d)(, \w+ \d{1,2}, \d{4})/g, rxPartition));
});
break;
case "User":
// TODO
break;
}
/////////////
// ACTIONS //
/////////////
switch(mw.config.get("wgAction"))
{
case "history":
$("span.mw-historychangeslist-histlinks ~ adate").each(function()
{
if$(this).text(rxReplace($(this).attrtext("class") == "mw-changeslist-date"));
{
$(this).text(partition($(this).text()));
}
});
break;
Line 83 ⟶ 73:
if(document.title.indexOf("Difference between revisions") > -1)
{
var$("#mw-diff-ntitle1 comp> =strong > a").html(rxReplace($("#mw-diff-ntitle1 > strong > a").html()));
$("#mw-diff-otitle1 > strong > a").html(rxReplace($("#mw-diff-otitle1 > strong > a").html()));
var temp;
$(".diff-currentversion-title").text(rxReplace($(".diff-currentversion-title").text()));
for(i = 0; i < 2; i++)
{}
temp = ($(comp).html());
// Modify time in permalink pages.
if($("div").hasClass("mw-revision"))
$(comp).html(temp.replace(/(\d\d:\d\d)/g, convert(temp.substr(temp.indexOf(":") - 2, 5))));
{
$("#mw-revision-date").html(rxReplace($("#mw-revision-date").html()));
comp = "#mw-diff-otitle1 > strong > a";
}
$(".diff-currentversion-title").text(partition($(".diff-currentversion-title").text()));
}
break;
Line 102 ⟶ 89:
break;
}
///////////
// OTHER //
///////////
// Modify time in warning banners.
Line 108 ⟶ 99:
$(".mw-warning-with-logexcerpt.mw-content-ltr > ul").children().each(function()
{
$(this).html(regexpReplacerxReplace($(this).html(), 1, 5));
});
}
Line 115 ⟶ 106:
if($("#footer-info-lastmod").length)
{
$("#footer-info-lastmod").text(partitionrxReplace($("#footer-info-lastmod").text()));
}
});
 
/*
function partition(text)
* Used when it is unlikely non-times will be matched.
{
*/
var front = text.substr(0, text.indexOf(":") - 2);
function rxReplace(html)
var time = convert(text.substr(text.indexOf(":") - 2, 5));
{
var end = text.substr(text.indexOf(":") + 3, text.length);
// Note a call to html on nonexistent elements returns undefined e.g. $('.sssssss').html(). Guard against this being passed in.
html = html || '';
return html.replace(/(\d\d:\d\d)/g, convert);
}
/*
return (front + time + end);
* Used when a very specific match needs to be made and
}
* only a substring of the match should be modified.
 
*/
function regexpReplace(html, start, num)
function rxPartition(match, p1, p2, p3)
{
{
return html.replace(/(\d\d:\d\d)/g, convert(html.substr(start, num)));
return (p1 + convert(p2) + p3);
}
}
 
function convert(time)
{
var hour = parseFloat(time.substr(0, 2));
function convert(time)
if(hour >= 12)
{
if(var hour != 12parseFloat(time.substr(0, 2));
if(hour >= 12)
{
if(hour != hour - 12;)
{
hour = hour - 12;
}
return (hour + time.substr(2) + " PM");
}
return if(hour + time.substr(2, 5) + "=== PM"0);
{
hour = 12;
}
return (hour + time.substr(2) + " AM");
}
});
// </nowiki>
if(hour === 0)
{
hour = 12;
}
return (hour + time.substr(2, 5) + " AM");
}