From e148de3560bea253e95c496f9f696bbddb1be90f Mon Sep 17 00:00:00 2001
From: Ashley <iamashley@duck.com>
Date: Sun, 26 Mar 2023 12:02:27 +0000
Subject: [PATCH] add cacher :3

---
 html/poketube.ejs | 45 ++++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 44 insertions(+), 1 deletion(-)

diff --git a/html/poketube.ejs b/html/poketube.ejs
index 2364513b..927e2c04 100644
--- a/html/poketube.ejs
+++ b/html/poketube.ejs
@@ -528,7 +528,9 @@ But Please note that unofficial instances can add the same lock icon, so please
                 <a><i  style="display: block;margin-left: auto;margin-right: auto;visibility: collapse;" class="fa-light fa-server"></i>  </a>
               <a ><i class="fa-light fa-shield" style="visibility: collapse;margin-right: 1.5em;"></i></a>
               <a ><i class="fa-light fa-shield" style="visibility: collapse;margin-right: 1.5em;"></i></a>
-
+<p id="fetch-count">
+                  
+                  </p>
           <div class="icon-button dropdown" style="margin-right: 4.5px;">
 			<input type="checkbox" id="loggedout-dropdown" autocomplete="off">
 			<label for="loggedout-dropdown">
@@ -1511,6 +1513,47 @@ links.forEach(link => {
   });
 });
 
+const urls = document.querySelectorAll('a[href*="/watch?v="]'); // get all links with "/watch?v=" in href attribute
+
+const fetchCount = urls.length;
+let fetchedCount = 0;
+
+const fetchCountElement = document.getElementById('fetch-count');
+fetchCountElement.textContent = ``;
+
+urls.forEach(link => {
+  const url = new URL(link.href);
+
+  if (url.host !== 'www.youtube.com' && url.host !== 'youtube.com') {
+    console.log(`Fetching ${url.origin}`);
+
+    fetch(url.href)
+      .then(response => {
+        if (response.status === 500) {
+          throw new Error('Server Error');
+        }
+        console.log(`Fetched ${url.origin}`);
+        // Only increment fetchedCount if the response is not a 500 error
+        if (response.ok) {
+          fetchedCount++;
+        }
+        fetchCountElement.textContent = ``;
+        // Check if all URLs have been fetched and hide the element if they have
+        if (fetchedCount === fetchCount) {
+          fetchCountElement.style.display = 'none';
+        }
+         console.clear();
+       })
+      .catch(error => {
+        // Ignore network errors
+        if (!(error instanceof TypeError && error.message.includes('Failed to fetch'))) {
+           console.clear();
+        }
+      });
+  }
+});
+
+
 const videoElement = document.getElementById("video");
 
 // Listen for full screen change events on the video element