-
Notifications
You must be signed in to change notification settings - Fork 0
/
findreplace.js
67 lines (59 loc) · 2.82 KB
/
findreplace.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
(function() {
function parseMoney(grp) {
return parseFloat(grp[1].replace(/,/g, ''));
}
function roundToPlaces(num, places) {
var scale = Math.pow(10, places);
return Math.round(num * scale) / scale;
}
function randFromArray(arr) {
return arr[Math.floor(Math.random() * arr.length)];
}
var xhr = new XMLHttpRequest();
//http://stackoverflow.com/questions/354044/what-is-the-best-u-s-currency-regex
var re = /\$[+-]?([0-9,]+(?:\.[0-9]+)?)( (million|billion|trillion|thousand))?/i;
var elements = document.getElementsByTagName('*');
chrome.storage.sync.get(['currencyType'], function(storage) {
xhr.onreadystatechange = function() {
if (xhr.readyState != XMLHttpRequest.DONE) {
return;
}
var items = JSON.parse(xhr.responseText);
// XXX: can't we just regex on the whole HTML (i.e. body text contents)?
for (var j = 0; j < elements.length; j++) {
var elt = elements[j];
for (var k = 0; k < elt.childNodes.length; k++) {
var child = elt.childNodes[k];
if (child.nodeType === 3) {
var txt = child.nodeValue;
// find/replace until there are none left
var match;
while (match = txt.match(re)) {
var item = randFromArray(items);
if (storage.currencyType == 'classic') {
item = items[0];
}
var itemAmt = parseMoney(match) / item.price;
if (itemAmt < 0.01) {
itemAmt = 0.01;
}
itemAmt = roundToPlaces(itemAmt, 2);
var written = match[2] ? match[2].trim() : "";
var itemName = item.name;
// contextual capitalization only if millions/billions was matched
if (written !== "" && written[0] === written[0].toUpperCase()) {
itemName = itemName.split(' ').map(function(elt) {
return elt == "of" ? elt : elt[0].toUpperCase() + elt.substring(1);
}).join(' ');
}
txt = txt.replace(re, itemAmt + (written ? " " + written : "") + " " + itemName);
}
elt.replaceChild(document.createTextNode(txt), child);
}
}
}
}; // Implemented elsewhere.
});
xhr.open("GET", chrome.extension.getURL('/things.json'), true);
xhr.send();
})();