-
Notifications
You must be signed in to change notification settings - Fork 0
/
price-declutter.user.js
43 lines (38 loc) · 1.35 KB
/
price-declutter.user.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
// ==UserScript==
// @name Price declutter-er
// @description Replace e.g. $199.99 with $200.00. Hover over text to see real price.
//
// @run-at document-end
// @include *
// @exclude *halifax-online.co.uk/*
// @grant none
// ==/UserScript==
function removeDuplicates(arr) {
return arr.filter(function (elem, pos) {
return arr.indexOf(elem) === pos;
});
}
function simplify(price) {
var rounded = Number(price).toPrecision(2);
return Number(rounded).toFixed(2);
}
function main() {
var priceRegex = /(?:[£$€]|£|$|€)\d+\.\d\d/g;
var prices = document.body.innerHTML.match(priceRegex);
if (prices !== null) {
var newBody = document.body.innerHTML;
var uniqPrices = removeDuplicates(prices);
uniqPrices.forEach(function (price) {
var numStart = price.search(/\d+\./);
var currencySymbol = price.substr(0, numStart);
var value = price.substr(numStart);
var newPrice = currencySymbol + simplify(value);
if (price !== newPrice) {
var priceSpan = "<span title=\"" + price + "\">" + newPrice + "</span>";
newBody = newBody.split(price).join(priceSpan); //replaceAll(price, priceSpan);
}
} );
document.body.innerHTML = newBody;
}
}
main();