-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.js
69 lines (60 loc) · 2.11 KB
/
util.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
'use strict';
function calculatePricePerArea(price, area) {
return (price / area).toFixed(2);
}
function extractValue(itemValueElement) {
const itemValue = itemValueElement.textContent
.replace('€/m²', '')
.replace('€', '')
.replace('m²', '')
.replaceAll('.', '')
.replace(',', '.')
.trim();
return parseFloat(itemValue);
}
function convertPriceToText(pricePerArea) {
return pricePerArea.toString().replace('.', ',') + " €/m²";
}
function applyColorClassOnAllItems() {
const pricePerAreaElements = document.getElementsByClassName('x-pricePerArea');
for (const pricePerAreaElement of pricePerAreaElements) {
const pricePerArea = extractValue(pricePerAreaElement);
applyColorClass(pricePerAreaElement, pricePerArea);
}
}
function applyColorClass(pricePerAreaElement, pricePerArea) {
chrome.storage.sync.get({
colorHighlightFeatureToggle: true,
colorHighlightMethodToggle: true,
colorHighlightLow: 10,
colorHighlightHigh: 15
}, (items) => {
if (!items.colorHighlightFeatureToggle) {
pricePerAreaElement.style.backgroundColor = 'transparent';
pricePerAreaElement.style.color = 'inherit';
pricePerAreaElement.style.paddingLeft = '0';
pricePerAreaElement.style.paddingRight = '0';
return
}
let highlightColor = 'orange';
if (pricePerArea < parseFloat(items.colorHighlightLow)) {
highlightColor = 'green';
} else if (pricePerArea > parseFloat(items.colorHighlightHigh)) {
highlightColor = 'red';
}
if (items.colorHighlightMethodToggle) {
pricePerAreaElement.style.backgroundColor = highlightColor;
pricePerAreaElement.style.color = 'white';
pricePerAreaElement.style.paddingLeft = '0.2em';
pricePerAreaElement.style.paddingRight = '0.2em';
} else {
pricePerAreaElement.style.backgroundColor = 'transparent';
pricePerAreaElement.style.color = highlightColor;
pricePerAreaElement.style.paddingLeft = '0';
pricePerAreaElement.style.paddingRight = '0';
}
});
}
chrome.storage.onChanged.addListener((changes, namespace) => {
applyColorClassOnAllItems();
});