-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcontent.js
More file actions
113 lines (96 loc) Β· 2.57 KB
/
content.js
File metadata and controls
113 lines (96 loc) Β· 2.57 KB
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
'use strict'
const currencies = [
{
symbol: "Β£",
wage: 0.14,
UKformat: true
} ,
{
symbol: "$",
wage: 0.12,
UKformat: true
} ,
{
symbol: "οΏ₯",
wage: 14.57,
UKformat: true
} ,
{
symbol: "β½",
wage: 202.17,
UKformat: false
} ,
{
symbol: "Rp",
wage: 444.52,
UKformat: false
}
];
const symbols = currencies.map(c => c.symbol);
function priceTime(price, currency) {
if (price == null || isNegative(price)) {
return 'Unable to calculate price'
}
const timeInMinutes = Math.round(price / currency.wage)
const hours = calculateHours(timeInMinutes)
const minutes = calculateMinutes(timeInMinutes, hours)
if (timeInMinutes >= 60) {
return minutes === 0 && hours
? `${generateHours(hours)} on minimum wage`
: `${generateHours(hours)} and ${generateMinutes(minutes)}`
}
return minutes < 1
? 'less than a minute on minimum wage'
: `${generateMinutes(minutes)}`
}
function isNegative(num) {
return Math.sign(num) === -1
}
function calculateMinutes(minutes, hours) {
return hours < 0 ? minutes : minutes - (60 * hours)
}
function calculateHours(minutes) {
return Math.floor(minutes / 60)
}
function generateMinutes(minutes) {
return minutes < 1 ? `less than a minute on minimum wage`
: `${minutes} ${minutes > 1 ? 'minutes' : 'minute'} on minimum wage`
}``
function generateHours(hours) {
return `${hours} ${hours > 1 ? 'hours' : 'hour'}`
}
function treeWalkTextNodes() {
var n, result = [],
walk = document.createTreeWalker(document.getElementsByTagName("body")[0], NodeFilter.SHOW_TEXT, null, false);
while (n = walk.nextNode()) result.push(n);
return result.filter(node => {
return symbols.some(s => node.textContent.includes(s));
}).map(r => r.parentElement);
}
function getPriceNumber(pE,c) {
try {
if (c.UKformat) {
var num = parseFloat(pE.innerText.split(c.symbol)[1].replace(",", ""));
} else {
var num = parseFloat(pE.innerText.split(c.symbol)[1].replace(".", "").replace(",","."));
}
return num;
}
catch(err) {
return 0;
}
}
const prices = treeWalkTextNodes();
for (const priceElement of prices) {
if(priceElement.innerText.length == 1) continue;
const result = [];
priceElement.innerText.split(' ').forEach((text) => {
if(!symbols.some(s => text.startsWith(s))) return result.push(text);
for(let currency of currencies) {
if(text.startsWith(currency.symbol)) {
return result.push(priceTime(getPriceNumber(priceElement,currency), currency));
}
}
});
priceElement.innerText = result.join(' ');
}