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')
 
(33 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"); // Backlink: [[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.
*/
 
$(function()
{
Line 19 ⟶ 10:
{
case "Special":
switch(mw.config.get("wgCanonicalSpecialPageName"))
{
case "Contributions":
$(".mw-contributionschangeslist-listdate").children().each(function()
{
$(this).childrenhtml().first().text(partitionrxReplace($(this).children().first().texthtml()));
});
break;
Line 34 ⟶ 24:
$("ul").children().each(function()
{
$(this).html(regexpReplace($(this).html().replace(/()(\d\d:\d\d)(, 1)/g, 5rxPartition));
});
break;
case "Listusers":
var index, html, time;
$("ul").children().each(function()
{
$(this).html = ($(this).html().replace(/(on \d{1,2} \w+ \d{4} at )(\d\d:\d\d)()/g, rxPartition));
index = html.indexOf("Created on ");
time = partition(html.substring(index));
html = html.substring(0, index);
$(this).html(html + time);
});
break;
Line 61 ⟶ 45:
$("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 a table matching the pattern "dd:dd," where d is a digit.
if(time.match(/\d\d:\d\d,/))
{
$(this).text(partition(text));
}
}
});
break;
Line 89 ⟶ 64:
{
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;
case "view":
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());
$(comp).html(temp.replace(/(\d\d:\d\d)/g, convert(temp.substr(temp.indexOf(":") - 2, 5))));
comp = "#mw-diff-otitle1 > strong > a";
}
$(".diff-currentversion-title").text(partition($(".diff-currentversion-title").text()));
}
Line 119 ⟶ 81:
if($("div").hasClass("mw-revision"))
{
$("#mw-revision-date").html(regexpReplacerxReplace($("#mw-revision-date").html(), 0, 5));
}
// Modify user signatures.
html = $("#mw-content-text").html();
switch(mw.config.get("wgDefaultDateFormat"))
{
case "dmy":
// HH:MM, DD MM YY //
html = html.replace(/(<\/a>\)\s\d\d:\d\d,\s\d{1,2}\s\w+\s\d{4}\s)/g, partition);
break;
case "mdy":
// HH:MM, MM DD YY //
html = html.replace(/(<\/a>\)\s\d\d:\d\d,\s\w+\s\d{1,2}\s\d{4}\s)/g, partition);
break;
case "ymd":
// HH:MM, YY MM DD //
html = html.replace(/(<\/a>\)\s\d\d:\d\d,\s\d{4}\s\w+\s\d{1,2}\s)/g, partition);
break;
}
$("#mw-content-text").html(html);
break;
Line 160 ⟶ 99:
$(".mw-warning-with-logexcerpt.mw-content-ltr > ul").children().each(function()
{
$(this).html(regexpReplacerxReplace($(this).html(), 1, 5));
});
}
Line 167 ⟶ 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");
}