/*
* To test this script, visit Template:Collapse and click on "show" links.
*/
var events = [], k = 0;
$(function()
{
switch(mw.config.get("wgAction"))
{
case "view":
// Modify user signatures.
modifySignatures();
break;
}
});
function modifySignatures()
{
// (1): Store event handlers. //
var elements = $("span");
for(k = 0; k < elements.length; k++)
{
events[k] = [];
$.each($._data(elements[k], "events"), getEventInfo);
}
console.log(events);
// (2): Modify HTML. //
$("#mw-content-text").html($("#mw-content-text").html().replace(/( )(\d\d:\d\d)(, \d{1,2} \w+ \d{4} )/g, rxPartition));
// (3): Reasign event handlers. //
elements = $("span");
for(k = 0; k < elements.length; k++)
{
if(events[k] !== undefined)
{
for(e = 0; e < events[k].length; e++)
{
console.log(k + " length = " + events[k].length);
$(elements[k]).on(events[k][e][0], events[k][e][1]);
console.log("On " + events[k][e][0] + " added.");
}
}
}
}
function getEventInfo(eventType, event)
{
// events =
// [
// [
// [eventType, h.handler],
// [...],
// ...
// ]
// ]
console.log(eventType);
$.each(event, function(j, h)
{
events[k].push([eventType, h.handler]);
});
}
/*
* Used when it is unlikely non-times will be matched.
*/
function rxReplace(html)
{
return html.replace(/(\d\d:\d\d)/g, convert);
}
/*
* 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);
}
function convert(time)
{
var hour = parseFloat(time.substr(0, 2));
if(hour >= 12)
{
if(hour != 12)
{
hour = hour - 12;
}
return (hour + time.substr(2, 3) + " PM");
}
if(hour === 0)
{
hour = 12;
}
return (hour + time.substr(2, 3) + " AM");
}