-
Notifications
You must be signed in to change notification settings - Fork 0
/
wishlist.js
84 lines (64 loc) · 2.19 KB
/
wishlist.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
const checkWishlist = async () => {
let priceHistory;
const displayHistory = (curr) => {
for (idx in priceHistory) {
const priceSelect = document.getElementById(idx);
const priceLst = priceHistory[idx][curr];
if (priceSelect && priceLst) {
const itemPrice = priceSelect.querySelector('.price');
const lastPrice = priceLst[0].price;
let s = '';
for (item of priceLst) {
s += `${item.price}, `;
}
itemPrice.innerHTML += `<br/><u style='font-size: 12px;color:#444;font-weight: normal'>(${s})</u>`;
}
}
};
const extractPrice = (item) => {
const re = /\d+\.\d+/;
const pid = item.id;
const priceTag = item.querySelector('.price');
const priceText = priceTag.innerText;
const priceNum = re.exec(priceText)[0];
const price = parseFloat(priceNum);
return {pid, price};
};
const getPrices = () => {
const wishItems = document.querySelectorAll('#good_tabs_box > .home_board > .wishlist_list_new');
const priceList = [];
for(item of wishItems) {
const price = extractPrice(item);
priceList.push(price);
}
return priceList;
};
const updateHistory = (curr) => {
const priceList = getPrices(); // TODO: refactor, same function as in priceHistory
for (price of priceList) {
if (!priceHistory[price.pid]) {
priceHistory[price.pid] = {};
};
const history = priceHistory[price.pid][curr] || [];
if (!history[0] || history[0].price != price.price) {
history.unshift({date: Date.now(), price: price.price});
priceHistory[price.pid][curr] = history;
}
}
saveHistory(priceHistory);
};
const config = { attributes: false, childList: true, subtree: true };
const targetNode = document.getElementById('good_tabs_box');
const curr = getCurrency();
const observer = new MutationObserver((mutationsList, observer) => {
observer.disconnect();
console.log('wish');
updateHistory(curr);
displayHistory(curr);
observer.observe(targetNode, config);
});
priceHistory = await loadHistory();
updateHistory(curr);
displayHistory(curr);
observer.observe(targetNode, config);
};