-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathgoal-tracker.html
More file actions
330 lines (295 loc) · 11.2 KB
/
goal-tracker.html
File metadata and controls
330 lines (295 loc) · 11.2 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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Goal Progress Tracker</title>
<style>
html { -webkit-text-size-adjust: 100%; }
img, video, canvas, svg { max-width: 100%; height: auto; }
@import url("https://fonts.googleapis.com/css2?family=Oswald:wght@200..700&display=swap");
/* --- Base / page --- */
* { box-sizing: border-box; }
body {
margin: 0;
background: #ffffff;
color: #111827; /* near-black */
font-family: ui-sans-serif, system-ui, -apple-system, Segoe UI, Roboto, Ubuntu, Cantarell, Noto Sans, "Helvetica Neue", Arial;
line-height: 1.45;
}
.container {
max-width: 1100px;
margin: 24px auto 56px;
padding: 0 16px;
}
/* --- Section headers (e.g., DICTIONARY / THESAURUS) --- */
h1, h2, h3 {
margin: 0 0 10px;
font-weight: 900;
letter-spacing: .5px;
text-transform: uppercase;
font-family: Oswald, Impact, Haettenschweiler, "Arial Narrow Bold", sans-serif;
}
/* --- Form blocks --- */
.form-block {
margin: 18px 0 30px;
}
label {
display: block;
font-size: 14px;
font-weight: 700;
margin: 0 0 8px;
}
input[type="text"],
input[type="url"],
input[type="number"],
input[type="search"] {
width: 100%;
background: #f3f4f6; /* light gray like screenshot */
color: #111827;
border: 1px solid #e5e7eb; /* subtle border */
border-radius: 8px;
padding: 14px 16px;
outline: none;
transition: border-color .15s ease, box-shadow .15s ease, background .15s ease;
}
input::placeholder { color: #9ca3af; }
input:focus {
background: #ffffff;
border-color: #1d4ed8; /* blue-700 */
box-shadow: 0 0 0 3px rgba(59,130,246,.25);
}
/* --- Buttons --- */
.btn, button, input[type="submit"] {
appearance: none;
border: 0;
cursor: pointer;
display: inline-block;
width: 100%; /* full width like screenshot */
background: #0a84ff; /* bright blue */
color: #ffffff;
text-transform: uppercase;
font-weight: 900;
letter-spacing: .6px;
padding: 14px 18px;
border-radius: 8px;
box-shadow: 0 6px 18px rgba(10,132,255,.35);
transition: filter .15s ease, transform .05s ease, box-shadow .2s ease;
}
.btn:hover, button:hover, input[type="submit"]:hover {
filter: brightness(1.05);
box-shadow: 0 8px 22px rgba(10,132,255,.42);
}
.btn:active, button:active, input[type="submit"]:active {
transform: translateY(1px);
}
/* --- Spacing between input and button like screenshot --- */
.spacer-8 { height: 8px; }
.spacer-16 { height: 16px; }
/* --- Optional wrapper for each tool panel --- */
.panel {
padding: 0; /* screenshot shows no card frame—keep clean */
border: 0;
background: transparent;
}
/* --- Responsive --- */
@media (max-width: 720px) {
.container { padding: 0 12px; }
}
</style>
</head>
<body>
<header>
<h1>Goal Progress Tracker</h1>
<p class="sub">Paste a product link, set the price and how much you've saved. I'll fetch the title & image and track your progress.</p>
</header>
<div class="container">
<div class="panel" style="margin-bottom: 16px;">
<form id="addGoalForm">
<div>
<label for="url">Product URL</label>
<input id="url" type="url" placeholder="https://store.example.com/product" required />
</div>
<div>
<label for="price">Price (USD)</label>
<input id="price" type="number" inputmode="decimal" step="0.01" min="0" placeholder="999.99" required />
</div>
<div>
<label for="saved">Saved So Far (USD)</label>
<input id="saved" type="number" inputmode="decimal" step="0.01" min="0" placeholder="0.00" />
</div>
<div>
<label for="title">(Optional) Title</label>
<input id="title" type="text" placeholder="Auto-fills from link" />
</div>
<div style="display:flex; gap:8px; align-items:end;">
<button type="button" class="btn btn-secondary" id="fetchMetaBtn" title="Fetch title & image from the link">Fetch</button>
<button class="btn" type="submit">Add Goal</button>
</div>
<div style="grid-column: 1 / -1; display:none;" id="imageFieldWrap">
<label for="imageUrl">(Optional) Image URL (auto-filled if found)</label>
<input id="imageUrl" type="url" placeholder="https://...jpg" />
</div>
</form>
<p id="hint" style="margin:10px 2px 0; color:var(--muted); font-size:13px;">
Tip: If a site blocks previews, you can paste a title and/or image manually. Your goals are saved locally in your browser.
</p>
</div>
<div id="goalsWrap" class="grid"></div>
<div id="emptyState" class="empty" style="display:none;">No goals yet. Add your first one above ✨</div>
</div>
<script>
(function(){
const $ = s => document.querySelector(s);
const $$ = s => Array.from(document.querySelectorAll(s));
const fmt = new Intl.NumberFormat('en-US', { style: 'currency', currency: 'USD' });
const storeKey = 'goal_tracker_v1';
const state = {
goals: load()
};
function load(){
try { return JSON.parse(localStorage.getItem(storeKey)) || []; }
catch { return []; }
}
function save(){ localStorage.setItem(storeKey, JSON.stringify(state.goals)); }
function uid(){ return Math.random().toString(36).slice(2) + Date.now().toString(36); }
function percent(saved, price){
if (!price || price <= 0) return 0;
return Math.max(0, Math.min(100, (saved / price) * 100));
}
function render(){
const wrap = $('#goalsWrap');
wrap.innerHTML = '';
if (!state.goals.length){
$('#emptyState').style.display = 'block';
return;
}
$('#emptyState').style.display = 'none';
state.goals.forEach(g => {
const card = document.createElement('div');
card.className = 'card';
card.innerHTML = `
<div class="media">
<img src="${escapeHtml(g.image || placeholderImage(g.title))}" alt="${escapeHtml(g.title || 'Goal image')}" onerror="this.src='${placeholderImage(g.title)}'"/>
<span class="badge">${Math.round(percent(g.saved, g.price))}%</span>
</div>
<div class="body">
<div class="title-row">
<a href="${escapeAttr(g.url)}" target="_blank" rel="noopener">${escapeHtml(g.title || new URL(g.url).hostname)}</a>
<button class="btn btn-danger" data-del="${g.id}" title="Delete">Delete</button>
</div>
<div class="numbers">
<div>Price<strong>${fmt.format(g.price || 0)}</strong></div>
<div>Saved<strong>${fmt.format(g.saved || 0)}</strong></div>
<div>Remaining<strong>${fmt.format(Math.max(0, (g.price||0) - (g.saved||0)))}</strong></div>
</div>
<div class="progress"><div class="bar" style="width:${percent(g.saved, g.price)}%"></div></div>
<div class="actions">
<input type="number" step="0.01" min="0" placeholder="Add or set saved…" value="" data-addsaved="${g.id}" />
<button class="btn btn-secondary" data-add="${g.id}">Add to Saved</button>
<button class="btn" data-set="${g.id}">Set Saved</button>
</div>
</div>
`;
wrap.appendChild(card);
});
// wire actions
$$("[data-del]").forEach(b => b.onclick = () => {
const id = b.getAttribute('data-del');
state.goals = state.goals.filter(x => x.id !== id);
save(); render();
});
$$("[data-add]").forEach(b => b.onclick = () => {
const id = b.getAttribute('data-add');
const input = document.querySelector(`[data-addsaved="${id}"]`);
const val = toNum(input.value);
if (val > 0){
const g = state.goals.find(x => x.id === id);
g.saved = round2((g.saved || 0) + val);
input.value = '';
save(); render();
}
});
$$("[data-set]").forEach(b => b.onclick = () => {
const id = b.getAttribute('data-set');
const input = document.querySelector(`[data-addsaved="${id}"]`);
const val = toNum(input.value);
if (val >= 0){
const g = state.goals.find(x => x.id === id);
g.saved = round2(val);
input.value = '';
save(); render();
}
});
}
function toNum(v){ return Math.max(0, Number.parseFloat(v || '0')) || 0; }
function round2(n){ return Math.round((n + Number.EPSILON) * 100) / 100; }
function placeholderImage(text){
const t = encodeURIComponent((text || 'Goal').slice(0, 16));
// cute gradient placeholder using dummyimage
return `https://dummyimage.com/800x500/0b1220/94a3b8&text=${t}`;
}
function escapeHtml(s){ return (s||'').replace(/[&<>"']/g, c => ({'&':'&','<':'<','>':'>','"':'"','\'':'''}[c])); }
function escapeAttr(s){ return escapeHtml(s).replace(/"/g, '"'); }
async function fetchMetadata(url){
// Try Jina Reader as a CORS-friendly fetcher, then parse OG/Twitter tags
const normalized = normalizeUrl(url);
const readerUrl = `https://r.jina.ai/http://${normalized.replace(/^https?:\/\//,'')}`;
try {
const res = await fetch(readerUrl, { method: 'GET' });
if (!res.ok) throw new Error('Reader failed');
const html = await res.text();
const title = matchFirst(html, /<meta\s+property=["']og:title["']\s+content=["']([^"']+)["'][^>]*>/i)
|| matchFirst(html, /<meta\s+name=["']twitter:title["']\s+content=["']([^"']+)["'][^>]*>/i)
|| matchFirst(html, /<title>([^<]+)<\/title>/i);
const image = matchFirst(html, /<meta\s+property=["']og:image["']\s+content=["']([^"']+)["'][^>]*>/i)
|| matchFirst(html, /<meta\s+name=["']twitter:image["']\s+content=["']([^"']+)["'][^>]*>/i);
return { title, image };
} catch (e){
console.warn('Metadata fetch failed:', e);
return { title: '', image: '' };
}
}
function matchFirst(str, regex){
const m = regex.exec(str);
return m ? m[1].trim() : '';
}
function normalizeUrl(u){
try { return new URL(u).href; } catch { return 'https://' + (u||'').replace(/^\/*/, ''); }
}
// Form handlers
$('#fetchMetaBtn').addEventListener('click', async () => {
const url = $('#url').value.trim();
if (!url) return;
toggleImageField(true);
const { title, image } = await fetchMetadata(url);
if (title) $('#title').value = title;
if (image) $('#imageUrl').value = image;
});
function toggleImageField(show){ $('#imageFieldWrap').style.display = show ? 'block' : 'none'; }
$('#addGoalForm').addEventListener('submit', async (e) => {
e.preventDefault();
const url = $('#url').value.trim();
const price = toNum($('#price').value);
const saved = toNum($('#saved').value);
let title = $('#title').value.trim();
let image = $('#imageUrl').value.trim();
if (!title || !image){
const meta = await fetchMetadata(url);
title = title || meta.title || '';
image = image || meta.image || '';
}
const goal = { id: uid(), url: normalizeUrl(url), title, image, price, saved };
state.goals.unshift(goal);
save();
// reset form
e.target.reset();
toggleImageField(false);
render();
});
// initial
render();
})();
</script>
</body>
</html>