KiritoAxinity commited on
Commit
18d1fff
·
verified ·
1 Parent(s): 57d4d36

Update public/script.js

Browse files
Files changed (1) hide show
  1. public/script.js +39 -28
public/script.js CHANGED
@@ -127,13 +127,15 @@ async function fetchVideoDataById(videoId) {
127
 
128
  async function fetchNextValidVideoData(tags, page) {
129
  const targetUrl = `https://www.sakugabooru.com/post.json?limit=1&page=${page}&tags=${tags}`;
130
- const proxyUrl = `https://api.allorigins.win/get?url=${encodeURIComponent(targetUrl)}`;
131
 
132
- const response = await fetch(proxyUrl);
133
- if (!response.ok) return null;
134
-
135
- const data = await response.json();
136
- return JSON.parse(data.contents);
 
 
137
  }
138
 
139
  function videoIsValid(video) {
@@ -297,20 +299,27 @@ function preloadVideo(video, index) {
297
  videoPlayers[index].src = video.file_url;
298
  }
299
 
300
- function playVideo() {
301
- var tags = videoData[CURRENT].tags.split(" ").map(makeReadable);
302
- hideTitle();
303
- showTitleAfterDelay(tags);
304
- addVideoTagsToUi(tags);
305
- saveVideoIdToUrl(videoData[CURRENT]);
306
-
307
- // Handle the play promise to catch interruptions
308
- const playPromise = videoPlayers[CURRENT].play();
309
- if (playPromise !== undefined) {
310
- playPromise.catch(error => {
311
- console.log("Playback was prevented or interrupted.");
312
- });
313
- }
 
 
 
 
 
 
 
314
  }
315
 
316
  function showTitleAfterDelay(tags) {
@@ -537,16 +546,18 @@ activityCheck = setInterval(() => {
537
  }, 250);
538
 
539
  async function getTags() {
540
- // AllOrigins uses a different format: hex/json wrapping
541
  const targetUrl = "https://www.sakugabooru.com/tag.json?limit=1500&order=count";
542
- const proxyUrl = `https://api.allorigins.win/get?url=${encodeURIComponent(targetUrl)}`;
543
 
544
- const response = await fetch(proxyUrl);
545
- if (!response.ok) return [];
546
-
547
- const data = await response.json();
548
- // AllOrigins wraps the result in a .contents string, so we must parse it
549
- return JSON.parse(data.contents);
 
 
550
  }
551
 
552
  function putTagsInForm(tags) {
 
127
 
128
  async function fetchNextValidVideoData(tags, page) {
129
  const targetUrl = `https://www.sakugabooru.com/post.json?limit=1&page=${page}&tags=${tags}`;
130
+ const proxyUrl = `https://api.allorigins.win/raw?url=${encodeURIComponent(targetUrl)}`;
131
 
132
+ try {
133
+ const response = await fetch(proxyUrl);
134
+ if (!response.ok) return null;
135
+ return await response.json();
136
+ } catch (error) {
137
+ return null;
138
+ }
139
  }
140
 
141
  function videoIsValid(video) {
 
299
  videoPlayers[index].src = video.file_url;
300
  }
301
 
302
+ async function playVideo() {
303
+ const video = videoPlayers[CURRENT];
304
+
305
+ // 1. Reset tags/UI first
306
+ var tags = videoData[CURRENT].tags.split(" ").map(makeReadable);
307
+ hideTitle();
308
+ showTitleAfterDelay(tags);
309
+ addVideoTagsToUi(tags);
310
+ saveVideoIdToUrl(videoData[CURRENT]);
311
+
312
+ // 2. Play with a safety catch
313
+ try {
314
+ // Ensure video is unmuted if you want sound, or stay muted for autoplay
315
+ video.muted = true;
316
+ await video.play();
317
+ } catch (err) {
318
+ if (err.name !== "AbortError") {
319
+ console.error("Playback error:", err);
320
+ }
321
+ // AbortErrors are ignored because they just mean the video switched quickly
322
+ }
323
  }
324
 
325
  function showTitleAfterDelay(tags) {
 
546
  }, 250);
547
 
548
  async function getTags() {
549
+ // Switching to a more stable format for AllOrigins
550
  const targetUrl = "https://www.sakugabooru.com/tag.json?limit=1500&order=count";
551
+ const proxyUrl = `https://api.allorigins.win/raw?url=${encodeURIComponent(targetUrl)}`;
552
 
553
+ try {
554
+ const response = await fetch(proxyUrl);
555
+ if (!response.ok) throw new Error('Network response was not ok');
556
+ return await response.json();
557
+ } catch (error) {
558
+ console.error("Fetch failed, trying cache...");
559
+ return JSON.parse(localStorage.getItem("tags")) || [];
560
+ }
561
  }
562
 
563
  function putTagsInForm(tags) {