-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample.browser.html
More file actions
222 lines (195 loc) · 8.51 KB
/
example.browser.html
File metadata and controls
222 lines (195 loc) · 8.51 KB
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
<!DOCTYPE html>
<html lang="en">
<!-- For readability, the <head> section is placed below the body, which is not standard HTML practice but is done here to highlight the actual demo. -->
<body>
<script src="checkjebon.js"></script>
<h1>Checkjebon Browser Example</h1>
<label for="shopping-list">Enter your shopping list (one item per line):</label><br>
<textarea id="shopping-list">1,5 liter halfvolle melk
Knoflooksaus
400 g shoarma
Pita brood
Kipschnitzel
250 gram kipfilet
1 kilo bananen
1 liter coca cola
kokosbrood
500 ml soep
halvarine</textarea><br>
<button id="check-btn">Check Prices</button>
<button id="open-checkjebon-btn" type="button">Open on Checkjebon</button>
<h2>Prices</h2>
<div id="last-updated"></div>
<div id="supermarket-table"></div>
<h2>Details</h2>
<div id="price-summary"></div>
<hr>
<h1>Multi-Supermarket Shopping Optimizer</h1>
<p>Find the optimal combination of supermarkets to minimize total cost!</p>
<label for="max-stores">Maximum stores to visit:</label>
<input type="number" id="max-stores" value="3" min="1" max="10" style="width: 60px;"><br><br>
<label for="selected-stores">Selected supermarket codes (comma-separated):</label><br>
<input type="text" id="selected-stores" value="ah,aldi,dirk,jumbo,lidl,vomar" style="width: 100%; max-width: 500px;"><br><br>
<button id="optimize-btn">Optimize Shopping List</button>
<h2>Optimal Shopping Plan</h2>
<div id="optimize-result"></div>
<h2>JSON</h2>
<pre id="result"></pre>
<script>
function formatSupermarketPrices(data) {
if (!Array.isArray(data)) return '';
return data.map(sup => {
const total = sup.products.reduce((sum, p) => sum + (typeof p.price === 'number' ? p.price : 0), 0);
let iconHtml = sup.icon ? `<img src="${sup.icon}" alt="icon" style="height:1em;vertical-align:middle;margin-right:0.5em;">` : '';
let html = `<strong>${iconHtml}${sup.name} <small>(${sup.code})</small></strong> · €${total.toFixed(2)}<ul>`;
html += sup.products.map(p => {
let prodText = '';
if (p.isEstimate) {
prodText = `<em>${p.originalQuery} · €${p.price?.toFixed(2) ?? '?'}</em> <small>(geschat)</small>`;
} else {
const name = p.name || p.originalQuery;
const amount = p.amount ? p.amount + ' ' : '';
if (p.link) {
prodText = `<a href="${p.link}" target="_blank">${amount}${name}</a> · €${p.price?.toFixed(2) ?? '?'}`;
} else {
prodText = `${amount}${name} · €${p.price?.toFixed(2) ?? '?'}`;
}
}
return `<li>${prodText}</li>`;
}).join('');
html += '</ul>';
return html;
}).join('<hr>');
}
function formatSupermarketTable(data) {
if (!Array.isArray(data)) return '';
// Calculate total price for each supermarket
const rows = data.map(sup => {
const total = sup.products.reduce((sum, p) => sum + (typeof p.price === 'number' ? p.price : 0), 0);
return { name: sup.name, code: sup.code, icon: sup.icon, total };
});
// Sort by total price ascending
rows.sort((a, b) => a.total - b.total);
let html = '<table border="1" cellpadding="6" style="margin-bottom:1em"><thead><tr><th>Supermarkt</th><th>Code</th><th>Icon</th><th>Total</th></tr></thead><tbody>';
html += rows.map(row => `<tr><td>${row.name}</td><td>${row.code}</td><td>${row.icon ? `<img src='${row.icon}' alt='icon' style='height:1em;'>` : ''}</td><td>€${row.total.toFixed(2)}</td></tr>`).join('');
html += '</tbody></table>';
return html;
}
document.getElementById('check-btn').onclick = async function () {
const textarea = document.getElementById('shopping-list');
const result = document.getElementById('result');
const priceSummary = document.getElementById('price-summary');
const supermarketTable = document.getElementById('supermarket-table');
const lastUpdatedDiv = document.getElementById('last-updated');
const items = textarea.value.split(/\n+/).map(s => s.trim()).filter(Boolean);
result.textContent = 'Loading...';
priceSummary.innerHTML = '';
supermarketTable.innerHTML = '';
lastUpdatedDiv.textContent = '';
if (!window.checkjebon || !window.checkjebon.getPricesForProducts) {
result.textContent = 'checkjebon library not loaded.';
return;
}
try {
const data = await window.checkjebon.getPricesForProducts(items);
supermarketTable.innerHTML = formatSupermarketTable(data);
priceSummary.innerHTML = formatSupermarketPrices(data);
result.textContent = JSON.stringify(data, null, 2);
// Show last updated info
const lastUpdated = window.checkjebon.pricesLastUpdated && window.checkjebon.pricesLastUpdated();
if (lastUpdated) {
lastUpdatedDiv.textContent = 'Last update: ' + new Date(lastUpdated).toLocaleString();
} else {
lastUpdatedDiv.textContent = 'Last update: unknown';
}
} catch (e) {
result.textContent = 'Error: ' + e.message;
}
};
document.getElementById('open-checkjebon-btn').onclick = function () {
const textarea = document.getElementById('shopping-list');
const url = window.checkjebon.getCheckjebonLink(textarea.value);
window.open(url, '_blank');
};
document.getElementById('optimize-btn').onclick = async function () {
const textarea = document.getElementById('shopping-list');
const maxStores = parseInt(document.getElementById('max-stores').value);
const selectedStores = document.getElementById('selected-stores').value
.split(',')
.map(s => s.trim())
.filter(Boolean);
const optimizeResult = document.getElementById('optimize-result');
const items = textarea.value.split(/\n+/).map(s => s.trim()).filter(Boolean);
optimizeResult.innerHTML = '<p>Loading...</p>';
if (!window.checkjebon || !window.checkjebon.getOptimalShoppingPlan) {
optimizeResult.innerHTML = '<p style="color:red;">checkjebon library not loaded or function not available.</p>';
return;
}
try {
const result = await window.checkjebon.getOptimalShoppingPlan(items, maxStores, selectedStores);
let html = `<p><strong>Total Cost: €${result.totalCost.toFixed(2)}</strong></p>`;
html += `<p><strong>Stores to Visit: ${result.supermarkets.length}</strong></p>`;
for (const store of result.supermarkets) {
const storeTotal = store.products.reduce((sum, p) => sum + (typeof p.price === 'number' ? p.price : 0), 0);
let iconHtml = store.icon ? `<img src="${store.icon}" alt="icon" style="height:1.5em;vertical-align:middle;margin-right:0.5em;">` : '';
html += `<h3>${iconHtml}${store.name} (${store.code}) - €${storeTotal.toFixed(2)}</h3>`;
html += '<ul>';
for (const product of store.products) {
console.log(product);
const amount = product.amount ? product.amount + ' ' : '';
const priceStr = typeof product.price === 'number' ? `€${product.price.toFixed(2)}` : '?';
if (product.link) {
html += `<li><a href="${product.link}" target="_blank">${amount}${product.name}</a> - ${priceStr}</li>`;
} else if (!product.isEstimate) {
html += `<li>${amount}${product.name} - ${priceStr}</li>`;
} else {
html += `<li><em>${product.originalQuery} - ${priceStr} (geschat)</em></li>`;
}
}
html += '</ul>';
}
optimizeResult.innerHTML = html;
} catch (e) {
optimizeResult.innerHTML = `<p style="color:red;">Error: ${e.message}</p>`;
}
};
</script>
</body>
<!-- For readability, the <head> section is placed below the body, which is not standard HTML practice but is done here to highlight the actual demo. -->
<head>
<meta charset="UTF-8">
<title>Checkjebon Browser Example</title>
<style>
body {
font-family: sans-serif;
margin: 2em;
}
textarea {
width: 100%;
height: 100px;
}
button {
margin: 1em 0;
}
pre {
background: #f4f4f4;
padding: 1em;
border-radius: 4px;
}
table {
width: 100%;
border-collapse: collapse;
margin: 1em 0;
}
th,
td {
padding: 0.75em;
text-align: left;
border: 1px solid #ddd;
}
th {
background: #f2f2f2;
}
</style>
</head>
</html>