-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontent.js
74 lines (56 loc) · 2.24 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
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
const body = document.querySelector('body');
const observerConfig = {
childList: true, // Watch for changes in the child elements of the target
subtree: true, // Watch for changes in the entire subtree, not just the direct children
};
const callback = (mutationList, observer) => {
const h1Element = body.querySelector('h1');
const table = body.querySelector('table');
if (h1Element && h1Element.innerText === "Claim your bounties" && table) {
const rows = table.rows;
if (rows[1].className !== "p-datatable-emptymessage") {
console.log("bountie page reached");
addPriceField();
observer.disconnect();
}
}
};
const observer = new MutationObserver(callback);
observer.observe(body, observerConfig);
function addPriceField() {
const table = document.querySelector("table.p-datatable-table");
const bountiecells = table.querySelectorAll("tbody tr td:nth-child(4) div");
let bounties = [];
bountiecells.forEach(bountie => {
bounties.push(bountie.innerText);
})
const total = sumArray(parsePrices(bounties));
console.log(total);
const totalRow = `
<tr role="row" draggable="false">
<td role="cell" style="width: 20rem;"></td>
<td role="cell" style="width: 20rem;"></td>
<td role="cell" style="width: 20rem;"></td>
<td role="cell" style="width: 20rem;">Total: € ${total}</td>
<td role="cell" style="width: 20rem;"></td>
</tr>`
document.querySelector("table.p-datatable-table tbody").insertAdjacentHTML("beforeend", totalRow);
}
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
function parsePrices(pricesArray) {
const parsedPrices = [];
pricesArray.forEach(priceString => {
const cleanedPrice = parseFloat(priceString.replace(/€\s+/g, '').replace(',', '.'));
if (!isNaN(cleanedPrice)) {
parsedPrices.push(cleanedPrice);
} else {
console.error("a price wasnt parced properly");
}
});
return parsedPrices;
}
function sumArray(arr) {
return arr.reduce((total, currentValue) => total + currentValue, 0);
}