-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontent.js
40 lines (33 loc) · 1.1 KB
/
content.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
const unitPrices = Array.from(document.querySelectorAll(".unit-price"));
let cheapestPrice = Number.MAX_SAFE_INTEGER;
let cheapestElem = undefined;
let MAX_ALLOWED_HIGHLIGHT_ITEMS = 5
unitPrices
.map(e => ({ price: getPriceFor(e), e }))
.filter(e => e.price !== undefined)
.sort((a, b) => a.price - b.price)
.forEach((e, i) => i + 1 <= MAX_ALLOWED_HIGHLIGHT_ITEMS ? highlightElement(e.e, i) : "")
function getPriceFor(e) {
const unitPrice = e.innerText
const perKgOrLOrPiece = unitPrice.match(/kr ([0-9,]*) per (kg|l|piece|stk|m)/);
if (!perKgOrLOrPiece) {
// something different --> send to backend server to improve
// console.log(unitPrice, document.location)
return undefined
}
try {
const priceExtract = perKgOrLOrPiece[1];
return parseFloat(priceExtract.replace(",", "."))
} catch (error) {
// failed to cast to float
// console.error(`Failed to parse to float -> ${price}`)
}
return undefined
}
function highlightElement(e, i) {
if (!e) {
return
}
e.classList.add("ext-oda-highlight");
e.innerHTML = `<strong>[${i+1}]</strong> ${e.innerText}`
}