BraydenMoore commited on
Commit
64da8a6
1 Parent(s): 034b530

Update templates/index.html

Browse files
Files changed (1) hide show
  1. templates/index.html +43 -128
templates/index.html CHANGED
@@ -266,139 +266,54 @@
266
  <script>
267
 
268
  // init
269
-
270
- const feed = document.getElementById("feed");
271
- const infoDiv = document.getElementById("info");
272
- const infoText = document.getElementById("info-text");
273
- const locationName = document.getElementById("location-name");
274
- const countryElement = document.getElementById("country");
275
- let firstLoad = true;
276
- let isJpeg = false;
277
- let isLoading = false;
278
- let controller;
279
-
280
- const loadingGif = "{{ url_for('static', filename='eye.gif') }}";
281
- const newUrl = decodeURIComponent("{{ url|safe }}");
282
-
283
- // Handle feed load and refresh
284
- document.addEventListener("DOMContentLoaded", function() {
285
-
286
- async function refreshImage() {
287
- if(controller) {
288
- controller.abort();
289
- }
290
- controller = new AbortController();
291
- const signal = controller.signal;
292
-
293
- try {
294
- const response = await fetch(newUrl, { method: 'HEAD' , signal: signal});
295
- if (!response.ok) {
296
- console.error(`Fetch failed with status: ${response.status}`);
297
-
298
- const urlParams = new URLSearchParams(window.location.search);
299
- if (urlParams.get('id')) {
300
- feed.style.width = "80px";
301
- feed.style.height = "50px";
302
- feed.src = loadingGif;
303
- feed.style.opacity = "0.15";
304
- countryElement.textContent = "couldn't connect.";
305
- } else if (firstLoad) {
306
- window.location.href = "?new=true";
307
- } else {
308
- console.log("Trying again");
309
- setTimeout(refreshImage, 250);
310
- }
311
- return;
312
- }
313
-
314
- const contentType = response.headers.get('Content-Type');
315
- if (response.ok) {
316
- if (/image\/jpe?g/.test(contentType)) {
317
- feed.src = newUrl + '?t=' + new Date().getTime();
318
- isJpeg = true;
319
- } else {
320
- feed.src = newUrl;
321
- isJpeg = false;
322
- }
323
- }
324
-
325
- } catch (error) {
326
- console.error('Fetch failed:', error);
327
-
328
- }
329
- }
330
- });
331
-
332
- async function init() {
333
- feed.style.width = "80px";
334
- feed.style.height = "50px";
335
- feed.src = loadingGif;
336
- feed.style.opacity = "0.15";
337
- infoText.style.opacity = "0";
338
- locationName.textContent = "{{ page_title|safe }}";
339
-
340
- await refreshImage();
341
-
342
- feed.onload = function() {
343
- isLoading = false;
344
- if (isJpeg) {
345
- setTimeout(async () => {
346
- await refreshImage();
347
- }, 250);
348
- }
349
-
350
- if (firstLoad) {
351
- countryElement.textContent = "connecting...";
352
- infoDiv.style.opacity = "0";
353
- }
354
- firstLoad = false;
355
-
356
- setTimeout(() => {
357
- locationName.textContent = "{{ name|safe }}";
358
- countryElement.textContent = "{{ country|safe }}";
359
- feed.style.width = "100%";
360
- feed.style.height = "70%";
361
- feed.style.opacity = "1";
362
- infoDiv.style.opacity = "1";
363
- }, 100);
364
-
365
  infoText.style.opacity = "1";
 
366
  };
367
-
368
- feed.onerror = function() {
369
- console.error('Error loading feed', this.src);
370
- const urlParams = new URLSearchParams(window.location.search);
371
-
372
- if (urlParams.get('id')) {
373
- firstLoad = false;
374
- feed.style.width = "80px";
375
- feed.style.height = "50px";
376
- feed.src = loadingGif;
377
- feed.style.opacity = "0.15";
378
- countryElement.textContent = "couldn't connect.";
379
- } else if (firstLoad) {
380
- window.location.href = "?new=true";
381
- } else {
382
- firstLoad = false;
383
- console.log("Trying again");
384
- setTimeout(refreshImage, 500);
385
- }
386
  };
387
- }
388
- init();
389
- console.log('Feed load initiated');
390
-
391
- // Stop stream when click button
392
- document.querySelectorAll('.abortButton').forEach(button => {
393
- button.addEventListener('click', () => {
394
- if (controller) {
395
- console.log("Aborting");
396
- controller.abort();
397
- init();
398
- }
399
- });
400
  });
401
 
 
402
  // Time count
403
  document.addEventListener("DOMContentLoaded", function() {
404
  const timezone = "{{ timezone }}";
 
266
  <script>
267
 
268
  // init
269
+ document.addEventListener("DOMContentLoaded", () => {
270
+ const feed = document.getElementById("feed");
271
+ const infoText = document.getElementById("info-text");
272
+ const country = document.getElementById("country");
273
+ const newUrl = "{{ url }}";
274
+ const loadingGif = "{{ url_for('static', filename='eye.gif') }}";
275
+
276
+ const setLoadingState = () => {
277
+ feed.style.width = "80px";
278
+ feed.style.height = "50px";
279
+ feed.src = loadingGif;
280
+ feed.style.opacity = "0.2";
281
+ infoText.style.opacity = "0";
282
+ };
283
+
284
+ const setLoadedState = () => {
285
+ feed.src = `${newUrl}?t=${new Date().getTime()}`;
286
+ feed.style.width = "100%";
287
+ feed.style.height = "70%";
288
+ feed.style.opacity = "1";
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
289
  infoText.style.opacity = "1";
290
+ country.textContent = "{{ country }}";
291
  };
292
+
293
+ const refreshImage = () => {
294
+ let xhr = new XMLHttpRequest();
295
+ xhr.open('HEAD', `${newUrl}?t=${new Date().getTime()}`, true);
296
+ xhr.onreadystatechange = () => {
297
+ if (xhr.readyState === 4 && xhr.status === 200 && xhr.getResponseHeader('Content-Type') === 'image/jpeg') {
298
+ setLoadedState();
299
+ }
300
+ };
301
+ xhr.send();
 
 
 
 
 
 
 
 
 
302
  };
303
+
304
+ setLoadingState();
305
+ feed.onload = () => {
306
+ setLoadedState();
307
+ setTimeout(refreshImage, 1000);
308
+ };
309
+ feed.onerror = () => {
310
+ setLoadingState();
311
+ window.location.href = "?new=true";
312
+ };
313
+ feed.src = `${newUrl}?t=${new Date().getTime()}`;
 
 
314
  });
315
 
316
+
317
  // Time count
318
  document.addEventListener("DOMContentLoaded", function() {
319
  const timezone = "{{ timezone }}";