-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
325 lines (307 loc) · 9.06 KB
/
Copy pathindex.html
File metadata and controls
325 lines (307 loc) · 9.06 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Shopify Counter</title>
<link href="https://fonts.googleapis.com/css2?family=Share+Tech+Mono&display=swap" rel="stylesheet">
<style>
#hamburger {
position: absolute;
top: 10px;
right: 10px;
font-size: 30px;
cursor: pointer;
user-select: none;
}
#nav-menu {
position: absolute;
top: 50px;
right: 10px;
background-color: rgba(255, 255, 255, 0.9);
border: 1px solid #ccc;
padding: 10px;
display: none;
font-size: 1.2em;
}
#nav-menu.open {
display: block;
}
#nav-menu a {
display: block;
text-decoration: none;
color: #333;
margin: 5px 0;
}
body {
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
height: 100vh;
margin: 0;
background-color: #dedede;
font-family: 'Share Tech Mono', monospace;
}
#wrapper {
text-align: center;
}
#counter {
font-size: 20vw;
line-height: 1;
width: 100%;
color: #FF5733;
}
#target {
font-size: 5vw;
}
#progress-container {
position: relative;
height: 25px;
background-color: #ccc;
margin-top: 20px;
width: fit-content;
font-family: 'Share Tech Mono', monospace;
}
#progress-bar {
position: absolute;
top: 0;
left: 0;
height: 100%;
width: 0%;
background-color: #FF5733;
z-index: 0;
}
#progress-text {
position: relative;
z-index: 1;
display: flex;
justify-content: space-between;
align-items: center;
height: 100%;
padding: 0 6px;
font-size: 2vw;
color: #000;
}
#api-controls {
margin-top: 10px;
font-size: 1.2vw;
}
#slider-container {
margin-top: 20px;
}
#goal-label[contenteditable] {
display: inline-block;
min-width: 6ch;
padding: 2px 4px;
border: 1px dashed transparent;
}
#goal-label[contenteditable]:focus {
outline: none;
border-color: #666;
background-color: #fff;
}
</style>
</head>
<body>
<div id="hamburger">☰</div>
<div id="nav-menu">
<a href="index.html">Home</a>
<a href="#">Stats</a>
<a href="settings.html">Settings</a>
</div>
<div id="wrapper">
<div id="counter">0</div>
<div id="target"></div>
<div id="progress-container">
<div id="progress-bar"></div>
<div id="progress-text">
<span id="sales-value"></span>
<span id="remaining-value"></span>
</div>
</div>
<div id="api-controls">
<div id="api-checkboxes"></div>
</div>
<div id="slider-container">
<label for="goal-slider">Monthly goal:</label>
<input type="range" id="goal-slider" min="100000" max="1000000" step="1000">
<span id="goal-label" contenteditable="true"></span>
</div>
</div>
<script>
const slider = document.getElementById('goal-slider');
const goalLabel = document.getElementById('goal-label');
const progressBar = document.getElementById('progress-bar');
const progressContainer = document.getElementById('progress-container');
const progressSales = document.getElementById('sales-value');
const progressRemaining = document.getElementById('remaining-value');
const counterElement = document.getElementById('counter');
const hamburger = document.getElementById('hamburger');
const navMenu = document.getElementById('nav-menu');
const apiContainer = document.getElementById('api-checkboxes');
let apiList = JSON.parse(localStorage.getItem('apiList') || '[]');
if (apiList.length === 0) {
apiList = [
{ url: '', name: 'API 1', enabled: true },
{ url: '', name: 'API 2', enabled: true }
];
}
function renderApiCheckboxes() {
apiContainer.innerHTML = '';
apiList.forEach((api, index) => {
const label = document.createElement('label');
const box = document.createElement('input');
box.type = 'checkbox';
box.checked = api.enabled !== false;
box.addEventListener('change', () => {
api.enabled = box.checked;
localStorage.setItem('apiList', JSON.stringify(apiList));
updateCounter();
});
label.appendChild(box);
label.appendChild(document.createTextNode(' ' + (api.name || `API ${index+1}`)));
apiContainer.appendChild(label);
});
}
renderApiCheckboxes();
const now = new Date();
// Use a YYYY-MM key so each month can store a separate goal
const monthKey = now.toISOString().slice(0,7);
// Load any previously saved goals from localStorage
const monthlyGoals = JSON.parse(localStorage.getItem('monthlyGoals') || '{}');
// Default to 500k if there's no entry for this month yet
let monthlyGoal = Number(monthlyGoals[monthKey]) || 500000;
let currentValue = 0;
hamburger.addEventListener('click', () => {
navMenu.classList.toggle('open');
});
function dailyTarget() {
const now = new Date();
const days = new Date(now.getFullYear(), now.getMonth() + 1, 0).getDate();
return (monthlyGoal / days) * now.getDate();
}
function updateGoalDisplay() {
goalLabel.textContent = monthlyGoal.toLocaleString();
}
function updateTargetDisplay(target) {
document.getElementById('target').textContent = target.toLocaleString();
}
function updateProgress(value, color = getColor(value, dailyTarget())) {
const percent = Math.min(value / monthlyGoal, 1) * 100;
progressBar.style.width = percent + '%';
progressBar.style.backgroundColor = color;
progressSales.textContent = Math.round(value).toLocaleString();
const remaining = Math.max(monthlyGoal - value, 0);
progressRemaining.textContent = remaining.toLocaleString();
syncProgressWidth();
}
function measureSixDigitWidth() {
const span = document.createElement('span');
span.textContent = '888,888';
span.style.position = 'absolute';
span.style.visibility = 'hidden';
span.style.fontFamily = window.getComputedStyle(counterElement).fontFamily;
span.style.fontSize = window.getComputedStyle(counterElement).fontSize;
document.body.appendChild(span);
const width = span.offsetWidth;
document.body.removeChild(span);
return width;
}
function syncProgressWidth() {
progressContainer.style.width = measureSixDigitWidth() + 'px';
}
function getColor(value, target) {
if (value >= target * 1.1) return '#28a745';
if (value >= target * 0.9) return '#FFC300';
return '#FF5733';
}
function animateCounter(endValue, target) {
const element = document.getElementById('counter');
const start = parseInt(element.textContent.replace(/,/g, ''), 10) || 0;
const duration = 2000;
const startTime = performance.now();
function tick(now) {
const progress = Math.min((now - startTime) / duration, 1);
const value = Math.floor(start + (endValue - start) * progress);
element.textContent = value.toLocaleString();
const color = getColor(value, target);
element.style.color = color;
updateProgress(value, color);
if (progress < 1) requestAnimationFrame(tick);
}
requestAnimationFrame(tick);
}
async function updateCounter() {
const target = dailyTarget();
updateTargetDisplay(target);
const currentColor = getColor(currentValue, target);
updateProgress(currentValue, currentColor);
try {
const params = new URLSearchParams();
const enabled = apiList.filter(a => a.enabled !== false);
enabled.forEach(a => {
if (a.url) params.append('url', a.url);
});
if (enabled.length === 0) params.set('source', 'none');
const query = params.toString();
const res = await fetch('/api/shopify-counter' + (query ? '?' + query : ''));
const data = await res.json();
if (data.error) {
console.error('Counter API error:', data.error);
animateCounter(0, target);
currentValue = 0;
return;
}
let value = Number(data.number ?? 0);
if (!Number.isFinite(value) || value < 0 || value > 1000000) {
console.error('Invalid counter value:', data.number);
value = 0;
}
if (value !== currentValue) {
animateCounter(value, target);
currentValue = value;
} else {
const color = getColor(value, target);
document.getElementById('counter').style.color = color;
updateProgress(value, color);
}
} catch (err) {
console.error('Failed to fetch counter:', err);
}
}
slider.value = monthlyGoal;
updateGoalDisplay();
slider.addEventListener('input', () => {
monthlyGoal = Number(slider.value);
updateGoalDisplay();
// Persist the value so the next visit shows the same goal
monthlyGoals[monthKey] = monthlyGoal;
localStorage.setItem('monthlyGoals', JSON.stringify(monthlyGoals));
const target = dailyTarget();
updateProgress(currentValue, getColor(currentValue, target));
updateCounter();
});
goalLabel.addEventListener('blur', () => {
const digits = goalLabel.textContent.replace(/[^0-9]/g, '');
const value = Number(digits);
if (!isNaN(value) && value >= 100000 && value <= 1000000) {
monthlyGoal = value;
slider.value = value;
updateGoalDisplay();
const target = dailyTarget();
updateProgress(currentValue, getColor(currentValue, target));
updateCounter();
} else {
updateGoalDisplay();
}
// Store the edited goal when leaving the field
monthlyGoals[monthKey] = monthlyGoal;
localStorage.setItem('monthlyGoals', JSON.stringify(monthlyGoals));
});
syncProgressWidth();
updateCounter();
setInterval(updateCounter, 5000);
window.addEventListener('resize', syncProgressWidth);
</script>
</body>
</html>