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')
 
(2 intermediate revisions by one other user not shown)
Line 1:
// [[Category:Wikipedia scripts]]
// This is the Deployment Version:
// See User:Guywan/Scripts/12HourFormatD.js for annotated code.
// <nowiki>
$(function()
{
////////////////
// NAMESPACES //
////////////////
switch(mw.config.get("wgCanonicalNamespace"))
{
Line 15 ⟶ 18:
});
break;
case "Log":
case "Userrights":
Line 23 ⟶ 27:
});
break;
case "Listusers":
$("ul").children().each(function()
Line 28 ⟶ 33:
$(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":
// Modify time in file history table.
$("td > a").each(function()
{
Line 39 ⟶ 50:
$(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"))
{
Line 49 ⟶ 69:
});
break;
case "view":
if(document.title.indexOf("Difference between revisions") > -1)
Line 56 ⟶ 77:
$(".diff-currentversion-title").text(rxReplace($(".diff-currentversion-title").text()));
}
// Modify time in permalink pages.
if($("div").hasClass("mw-revision"))
{
$("#mw-revision-date").html(rxReplace($("#mw-revision-date").html()));
}
break;
case "edit":
// TODO
break;
}
///////////
// OTHER //
///////////
// Modify time in warning banners.
if($("div").hasClass("mw-warning-with-logexcerpt mw-content-ltr"))
{
Line 69 ⟶ 102:
});
}
 
// Modify the time in the footer of most pages.
if($("#footer-info-lastmod").length)
{
$("#footer-info-lastmod").text(rxReplace($("#footer-info-lastmod").text()));
}
 
});
/*
function rxReplace(html)
* Used when it is unlikely non-times will be matched.
*/
return html.replace(/(\d\d:\d\d)/g, convert);
function rxReplace(html)
function rxPartition(match, p1, p2, p3)
{
return (p1 + convert(p2) + p3);
}
function convert(time)
{
var hour = parseFloat(time.substr(0, 2));
if(hour >= 12)
{
// Note a call to html on nonexistent elements returns undefined e.g. $('.sssssss').html(). Guard against this being passed in.
if(hour != 12)
html = html || '';
{
return html.replace(/(\d\d:\d\d)/g, convert);
hour = hour - 12;
}
return (hour + time.substr(2) + " PM");
}
if(hour === 0)
/*
* 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)
{
return (p1 + convert(p2) + p3);
hour = 12;
}
return (hour + time.substr(2) + " AM");
function 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>