-
Notifications
You must be signed in to change notification settings - Fork 0
/
woolies-bill-splitter.user.js
145 lines (127 loc) · 5.63 KB
/
woolies-bill-splitter.user.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
// ==UserScript==
// @name Woolworths Bill Splitter
// @namespace http://tampermonkey.net/
// @version 0.2
// @description Add checkboxes to Woolworths order items to calculate shared costs, handling substitutions
// @author lemontea
// @match https://www.woolworths.com.au/shop/myaccount/myorders/*
// @grant none
// ==/UserScript==
(function() {
'use strict';
// Function to add checkboxes and calculate the total
function setupCheckboxes() {
document.querySelectorAll('wow-my-order-groceries-list-item-v2').forEach((item, index) => {
if (!item.querySelector('input[type="checkbox"]')) {
const priceElement = getPriceElement(item);
const qtyElement = item.querySelector('.item-qty-unit').textContent.trim();
const totalQty = parseInt(qtyElement.split(' of ')[1] || qtyElement);
const checkbox = document.createElement('input');
checkbox.type = 'checkbox';
checkbox.id = 'checkbox' + index;
checkbox.style.margin = '0 10px';
checkbox.style.cursor = 'pointer';
checkbox.style.transform = 'scale(1.5)';
const quantityInput = document.createElement('input');
quantityInput.type = 'number';
quantityInput.value = 0;
quantityInput.min = 0;
quantityInput.max = totalQty;
quantityInput.id = 'quantity' + index;
quantityInput.style.width = '50px';
quantityInput.style.margin = '0 10px';
quantityInput.disabled = true;
quantityInput.onchange = () => {
calculateTotal();
};
checkbox.onchange = () => {
if (checkbox.checked) {
quantityInput.disabled = false;
quantityInput.value = totalQty;
} else {
quantityInput.disabled = true;
quantityInput.value = 0;
}
calculateTotal();
};
const container = document.createElement('div');
container.style.display = 'flex';
container.style.alignItems = 'center';
if (totalQty === 1) {
const halfButton = document.createElement('button');
halfButton.textContent = 'Just Half';
halfButton.style.margin = '0 10px';
halfButton.onclick = () => {
quantityInput.value = 0.5;
calculateTotal();
};
container.appendChild(halfButton);
}
container.appendChild(quantityInput);
container.appendChild(priceElement.cloneNode(true));
container.appendChild(checkbox);
priceElement.parentNode.insertBefore(container, priceElement);
priceElement.remove();
}
});
}
// Function to get the correct price element, considering substitutions
function getPriceElement(item) {
const refundSection = item.querySelector('.refund-section');
if (refundSection) {
const paidPriceElement = refundSection.querySelector('.price:not(.refund-price)');
if (paidPriceElement) {
return paidPriceElement;
}
}
return item.querySelector('.price');
}
// Function to calculate the total cost of selected items
function calculateTotal() {
let total = 0;
document.querySelectorAll('wow-my-order-groceries-list-item-v2').forEach((item, index) => {
const checkbox = document.getElementById('checkbox' + index);
if (checkbox && checkbox.checked) {
const quantityInput = document.getElementById('quantity' + index);
const purchasedQty = parseFloat(quantityInput.value);
const qtyElement = item.querySelector('.item-qty-unit').textContent.trim();
const totalQty = parseInt(qtyElement.split(' of ')[1] || qtyElement);
const priceElement = getPriceElement(item);
const priceText = priceElement.textContent.trim();
const price = parseFloat(priceText.replace('$', ''));
if (totalQty > 0) {
const pricePerUnit = price / totalQty;
total += pricePerUnit * purchasedQty;
}
}
});
updateTotalDisplay(total);
}
// Function to update the display of the total cost
function updateTotalDisplay(total) {
let display = document.getElementById('totalDisplay');
if (!display) {
display = document.createElement('div');
display.id = 'totalDisplay';
display.style.position = 'fixed';
display.style.bottom = '20px';
display.style.right = '20px';
display.style.backgroundColor = 'white';
display.style.border = '1px solid black';
display.style.padding = '10px';
document.body.appendChild(display);
}
display.textContent = 'Total: $' + total.toFixed(2);
}
// Observer to monitor changes in the DOM
const observer = new MutationObserver(function() {
if (document.querySelector('wow-my-order-groceries-list-item-v2')) {
setupCheckboxes();
}
});
// Start observing the document
observer.observe(document, {
childList: true,
subtree: true
});
})();