MCPcopy Index your code
hub / github.com/hoothin/UserScripts / fetchWithFallback

Function fetchWithFallback

Picviewer CE+/dist.user.js:12467–12500  ·  view source on GitHub ↗
(urls)

Source from the content-addressed store, hash-verified

12465 "https://cdn.jsdelivr.net/npm/video.js@8.23.3/dist/video.min.js"
12466 ];
12467 const fetchWithFallback = (urls) => {
12468 return new Promise((resolve, reject) => {
12469 let lastError = null;
12470 const urlsToTry = [...urls];
12471
12472 const tryNext = () => {
12473 if (urlsToTry.length === 0) {
12474 reject(new Error(`Failed to load resource after trying all sources. Last error: ${lastError}`));
12475 return;
12476 }
12477
12478 const url = urlsToTry.shift();
12479 const xhr = new XMLHttpRequest();
12480 xhr.open('GET', url);
12481 xhr.onload = () => {
12482 if (xhr.status === 200) {
12483 resolve(xhr.responseText);
12484 } else {
12485 lastError = `Status ${xhr.status} from ${url}`;
12486 console.warn(`Failed to load ${url}, trying next source...`);
12487 tryNext();
12488 }
12489 };
12490 xhr.onerror = () => {
12491 lastError = `Network error from ${url}`;
12492 console.warn(`Failed to load ${url}, trying next source...`);
12493 tryNext();
12494 };
12495 xhr.send();
12496 };
12497
12498 tryNext();
12499 });
12500 };
12501 const pCSS = fetchWithFallback(cssSources).then(cssContent => {
12502 const styleElement = document.createElement('style');
12503 styleElement.textContent = cssContent;

Callers 1

loadVideoJsLibraryFunction · 0.70

Calls 1

tryNextFunction · 0.70

Tested by

no test coverage detected