-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcontentscript.js
82 lines (66 loc) · 2.3 KB
/
contentscript.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
;(function harvestImprove() {
var lines,
linesPerItemType = new Map();
// Grab data
lines = document.querySelectorAll('.client-doc-items .client-doc-rows tr');
var total_qty = 0;
var total_amount = 0;
lines.forEach(function(line) {
var itemType = line.querySelector('.item-type').innerText;
var qty = parseFloat(line.querySelector('.item-qty').innerText);
var amount = parseFloat(line.querySelector('.item-amount').innerText
.substring(4) // Remove 'CHF '
.replace(/'/, '')) // Remove currency formatting 10'500 -> 10500
;
total_qty += qty;
total_amount += amount;
if (!linesPerItemType.has(itemType)) {
linesPerItemType.set(itemType, {
itemType: itemType,
qty: 0.0,
amount: 0.0,
});
}
var prevObject = linesPerItemType.get(itemType);
linesPerItemType.set(itemType, {
itemType: itemType,
qty: prevObject.qty + qty,
amount: prevObject.amount + amount
});
})
// Build the new Table
var container = document.createElement('div');
var tableHeader = `<h4 class="no-print">Résumé (non visible par le client)</h4>
<table class="client-doc-items no-print" cellspacing="0" cellpadding="0" border="0" style="margin-top: 10px; width: 600px;">
<thead class="client-doc-items-header desktop-only">
<tr>
<th class="item-type">Item Type</th>
<th class="item-qty">Quantité</th>
<th class="item-amount">Montant</th>
</tr>
</thead>
<tbody class="client-doc-rows">
`;
var tableBody = '';
for (var [key, data] of linesPerItemType) {
tableBody += `
<tr>
<td class="item-type desktop-only">${key}</td>
<td class="item-qty desktop-only">${data.qty}</td>
<td class="item-amount">${data.amount}</td>
</tr>
`;
}
tableBody += `</tbody>`;
var tableFooter = `
<tbody class="client-doc-summary">
<tr class="total">
<td class="item-type desktop-only">Totaux de l'estimation</td>
<td class="item-qty desktop-only">${total_qty}</td>
<td class="item-amount">${total_amount}</td>
</tr>
</tbody>`;
tableFooter += `</table>`;
container.innerHTML = tableHeader + tableBody + tableFooter;
document.querySelector('#client-document').insertBefore(container, document.querySelector('.client-doc-notes'));
})();