Skip to content

Instantly share code, notes, and snippets.

@rrthomas
Created September 6, 2013 12:58
Show Gist options
  • Save rrthomas/6463426 to your computer and use it in GitHub Desktop.
Save rrthomas/6463426 to your computer and use it in GitHub Desktop.
A userscript for Greasemonkey to add Roman numerals to Facebook, intended for Latin users
// ==UserScript==
// @name Roman numerals for Facebook
// @namespace http://userscripts.org
// @description Convert numbers on Facebook to Roman numerals (for Latin language setting)
// @include http*://*facebook.com/*
// @grant none
// @version 1
// ==/UserScript==
// Tweaked from http://stackoverflow.com/questions/9083037/convert-a-number-into-a-roman-numeral-in-javascript
function toRoman(num) {
if (num > 3999999) { return num; }
var result = '',
ref = ['M', 'CM', 'D', 'CD', 'C', 'XC', 'L', 'XL', 'X', 'IX', 'V', 'IV', 'I'],
xis = [1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1];
if (num <= 3999999 && num >= 4000) {
result = '<span style="text-decoration: overline;">'+toRoman(num.substr(0, num.length - 3))+'</span>';
num = num.substr(num.length - 3);
}
for (var x = 0; x < ref.length; x++) {
while (num >= xis[x]) { result = result + ref[x]; num = num - xis[x]; }
}
return result;
}
function fixRoman(num) {
num = num.replace('.', '');
return toRoman(num);
}
var numberRE = new RegExp('\\b[\\d.]+\\b', 'g');
// Based on code from http://userscripts.org/scripts/review/83753
function replaceByClass(className, obj) {
if (obj.getElementsByClassName) {
var nodes = obj.getElementsByClassName(className);
for (i in nodes) {
if (typeof(nodes[i].innerHTML) == "string") {
nodes[i].innerHTML = nodes[i].innerHTML.replace(numberRE, fixRoman);
}
}
}
}
function fixup(obj) {
if (typeof(obj) == "object") {
replaceByClass('fsm', obj);
replaceByClass('item', obj);
replaceByClass('timestamp', obj);
replaceByClass('commentContent', obj);
replaceByClass('mobile_status', obj);
replaceByClass('uiStreamMessage', obj);
replaceByClass('GBThreadMessageRow_Body_Content', obj);
replaceByClass('UIStory_Message', obj);
}
}
function listener(event) {
fixup(event.target);
}
fixup(document);
document.addEventListener("DOMNodeInserted", listener, true);
//document.addEventListener("DOMCharacterDataModified", listener, true);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment