Special thanks to diesel for making this possible.
Notes:
-This does not seem to work on phone numbers on Google Maps
-It does not work on phone numbers listed in xxx.xxx.xxxx format
-This also changes dates from dd mm yyyy to mm dd yyyy.
-The script is provided on an external website and I hold no responsibility if it changes. I currently use one with the following source code (this is the same as diesel's version as of March 02, 2009:
// ==UserScript==
// @name Date Correct
// @namespace home
// @description Dates to US format
// @include *
// ==/UserScript==
// Regular expression components
var sMonths = "(January|February|March|April|June|July|August|September|October|November|December|(?:Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sept|Sep|Oct|Nov|Dec)\\.?)(?=\\s|\\b|\\d)";
var sDays = "(3[01]|[0-2][0-9]|[1-9])(?:st|nd|rd|th)?(?=\\b|[^\\d])";
var sSpace = "[\\s\\u00A0]*";
// Regular expressions
var reUSdate = new RegExp("([\\b:]?" + sDays + sSpace + ")?" + sMonths + sSpace + sDays + "(," + sSpace + "(?=[12]\\d{3}))?(?!:|\\d:|\\d\\d\\d?\\b)", "gi");
var reDayMonth = new RegExp("\\b" + sDays + sSpace + sMonths, "gi");
var reISO = /([12]\d{3})([\-\/._])(1[012]|0[1-9])\2(3[01]|[0-2][0-9]|[1-9])/g;
var reUSnumeric = /(3[01]|2[0-9]|1[3-9])([\-\/._])(1[012]|0?[1-9])\2([12]\d{3})/g;
var phnumber = /(\()([0-9]{3})(\)\s*)([0-9]{3}\-[0-9]{4})/g;
//var reTwelveHour = /\b([012]?[0-9])(?:[:\.]([0-5][0-9]))?\s?([apAP])[mM]\b/g;
// Proper case string prototype
String.prototype.toProperCase = function()
{
return this.toLowerCase().replace(/^(.)|\s(.)/g, function($1) {return $1.toUpperCase();});
};
// Takes a number and returns the ordinalized form
var saOrdinals = ["th", "st", "nd", "rd"];
function ordinalize(iNumber, sMonth)
{
var sOrdinal = "";
iNumber = parseInt(iNumber, 10);
if (sMonth.substr(sMonth.length - 1) != ".")
{
if ((sMonth.length > 3) || (sMonth.toLowerCase() == "may"))
{
var iOrdinal = 0;
var iHundredUnit = iNumber % 100;
if ((iHundredUnit <> 13))
{
iOrdinal = iNumber % 10;
if (iOrdinal > 3)
{
iOrdinal = 0;
}
}
sOrdinal = saOrdinals[iOrdinal];
}
}
return iNumber + sOrdinal;
}
// UK date to US (May 4th to May 4th)
function cbUSdate(sMatch, sPrefixOuter, sDayPrefix, sMonth, sDaySuffix, sComma)
{
var sDate;
if (sPrefixOuter && (sPrefixOuter.substr(0, 1) != ":"))
{
sDate = sMatch;
}
else
{
sPrefixOuter = sPrefixOuter || "";
var sDay = ordinalize(sDaySuffix, sMonth);
sMonth = sMonth.toProperCase().replace(".", "");
var sSufix = ((sComma == ", " || sComma == ",") ? " " : "");
sDate = sPrefixOuter + sMonth + " " + sDay + sComma;
}
return sDate;
}
// Ordinalize and capitalize (Jan 3 to Jan 3)
function cbDayMonth(sMatch, sDay, sMonth)
{
return (sMonth == "may" ? sMatch : sMonth.toProperCase() + " " + ordinalize(sDay, sMonth));
}
// ISO date to US (06-28-2008 to 06-28-2008)
function cbISO(sMatch, sYear, sSeparator, sMonth, sDay)
{
return (sDay.length == 1 ? "0" : "") + sMonth + sSeparator + sDay + sSeparator + sYear;
}
// UK separated date to US (04/13/2008 to 4/13/2008)
function cbUSnumeric(sMatch, sDay, sSeparator, sMonth, sYear)
{
return (sMonth.length == 1 ? "0" : "") + sMonth + sSeparator + sDay + sSeparator + sYear;
}
// phone number format
function newnumber(sMatch, lp, ac, rp, num)
{
return (ac + "-" + num);
}
// Localise UK dates to US format
function localiseDates(sText)
{
if (sText.match(/\d/))
{
sText = sText.replace(reUSdate, cbUSdate);
sText = sText.replace(reDayMonth, cbDayMonth);
sText = sText.replace(reISO, cbISO);
sText = sText.replace(reUSnumeric, cbUSnumeric);
sText = sText.replace(phnumber, newnumber);
//sText = sText.replace(reTwelveHour, cbTwelveHour);
}
return sText;
}
// Apply date localisation to all text nodes
var xpathResult = document.evaluate("//text()", document.documentElement, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null);
for (var iNode = 0; iNode < onode =" xpathResult.snapshotItem(iNode);" nodevalue =" localiseDates(oNode.nodeValue);" inode =" 0;" iset =" 0;" onode =" xpathResult.snapshotItem(iNode);" nodevalue =" anglicise(oNode.nodeValue);" iset ="="">
No comments:
Post a Comment