-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprice2time.js
181 lines (155 loc) · 5.57 KB
/
price2time.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
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
function unit_seconds(a, secs_per_unit) {
if (secs_per_unit == 0) return {unit : 0, remainder : 0 }
const unit = Math.floor(parseInt(a,10) / secs_per_unit);
const remainder = a - (unit * secs_per_unit);
return {unit : unit, remainder : remainder };
}
function days_work(secs, hours_work_week) {
return unit_seconds(secs, 3600 * hours_work_week);
}
function days(secs) {
return unit_seconds(secs, 86400);
}
function hours(secs) {
return unit_seconds(secs, 3600);
}
function minutes(secs) {
return unit_seconds(secs, 60);
}
function concat(value, sufix){
return value !== 0 ? `${value}${sufix} ` : ' ';
}
function is_number(obj) {
return !isNaN(parseFloat(obj))
}
function secs_dwhms(a, hour_work_day) {
if (!is_number(a)) return "";
const d = days_work(a, hour_work_day);
const h = hours(d.remainder);
const m = minutes(h.remainder);
const s = Math.floor(m.remainder);
return (concat(d.unit,'d') + concat(h.unit,'h') + concat(m.unit,'m') + concat(s,'s')).trim();
}
function seconds_currency(salary, hours_work) {
if (hours_work == 0) return 0;
return salary / (hours_work * 3600.0);
}
function get_cents(price_str) {
const regex_cents = new RegExp(/[^0-9,.]/, "gm");
const regex_separator = new RegExp(/[,.]/, "gm");
const regex_have_cents = new RegExp(/[,|.]([0-9]{2})$/, "gm")
let price = parseFloat(price_str.replace(regex_cents,'').replace(regex_separator,''))
return (price_str.match(regex_have_cents) == undefined && price != 0 ? price * 100.0 : price);
}
function has_p2t(node) {
const regex_end_seconds = new RegExp(/[0-9][dhms]/,"gi")
return node.className.indexOf('p2tn') !== -1 || (node.className.indexOf('p2t') !== -1 && node.innerText.match(regex_end_seconds) != undefined);
}
function set_p2t(node) {
node.className += " p2t"
}
function set_p2tn(node) {
node.className += " p2tn"
}
function extract_prices(price_str) {
const regex_percent = new RegExp(/(\(.*\)+)|(\d[\.,\d]+)%/,"gm");
const regex_prices = new RegExp(/(\d[\.,\d]+)/,"gm");
const has_nested_tags = new RegExp(/[<>]/,"gm");
if (price_str.match(has_nested_tags)) {
return [];
}
else {
return Array.from(price_str.replace(regex_percent, '')
.matchAll(regex_prices), x => x[0])
.map(price => get_cents(price))
.filter(price => price != undefined && price != 0)
}
}
function assembly_price_time(prices, time_currency, hour_work_per_day) {
if (time_currency == 0) return "";
return " " + prices.map(
price => `${secs_dwhms(price / time_currency, hour_work_per_day)}`)
.join("-")
}
function price_2_time(nodes, time_currency, hour_work_per_day) {
nodes.filter(node => !has_p2t(node))
.map(node => {
set_p2t(node);
const prices = extract_prices(node.innerText);
node.innerText += assembly_price_time(prices, time_currency, hour_work_per_day);
})
}
function parse_filter(filter_str) {
return filter_str.split(',')
.map(x => x.trim())
.filter(x => x !== "")
}
// extension config
function run_convert(config) {
if (Object.keys(config).length === 0) {
save_default_config(config);
}
else {
const salary = parseInt(extract_prices(config.salary)[0], 10);
const month_hours = parseFloat(config.month_hours);
const hour_work_per_day = parseFloat(config.hour_work_per_day);
let filter = parse_filter(config.filter)
const time_currency = seconds_currency(salary, month_hours);
filter.map(x => {
const nodes = Array.from(document.body.getElementsByClassName(x));
price_2_time(nodes, time_currency, hour_work_per_day)
});
//special case
const nodes = Array.from(document.body.getElementsByClassName('a-price'));
nodes.filter(node => !has_p2t(node))
.map(node => {
set_p2tn(node);
const off_price = node.getElementsByClassName('a-offscreen')[0].innerText;
const off_price_time = assembly_price_time(extract_prices(off_price), time_currency, hour_work_per_day);
node.getElementsByClassName('a-price-fraction')[0].innerText += ` (${off_price_time.trim()})`
})
}
}
function run_change() {
if (chrome != undefined) {
chrome.storage.local.get().then(run_convert, save_default_config);
}
}
function save_default_config(config) {
if (Object.keys(config).length === 0) {
const default_settings = {
salary : "2.500,00",
month_hours : "160",
hour_work_per_day : "8.5",
filter : `a-color-price, offer-price, kfs-price, a-text-strike,price, new-price,
dealPriceText, price_inside_buybox, hl-item__displayPrice,
sales-price, price-tag-fraction, price__fraction, promotion-item__price`
}
if (chrome != undefined) {
chrome.storage.local.set(default_settings)
}
}
}
run_change();
(function loop(i) {
setTimeout(function () { ;
run_change();
if (--i) loop(i);
}, 2500)
})(30);
module.exports = {
unit_seconds: unit_seconds,
days_work: days_work,
days: days,
hours: hours,
minutes: minutes,
concat: concat,
is_number: is_number,
secs_dwhms: secs_dwhms,
seconds_currency: seconds_currency,
get_cents: get_cents,
extract_prices: extract_prices,
parse_filter: parse_filter,
assembly_price_time: assembly_price_time,
has_p2t: has_p2t
}