fix: CDX fetch ohne custom headers (CORS preflight vermeiden)

This commit is contained in:
2026-06-12 12:54:55 +02:00
parent 4c909392e5
commit 321f65a802

View File

@@ -13,30 +13,40 @@ function getDomain(url) {
catch { return url; } catch { return url; }
} }
// Wayback CDX direkt im Browser (CORS: * → kein Server-Proxy nötig) // Wayback CDX direkt im Browser
async function searchWayback(targetUrl) { async function searchWayback(targetUrl) {
const cdx = `https://web.archive.org/cdx/search/cdx?url=${encodeURIComponent(targetUrl)}&output=json&limit=1&fl=timestamp&filter=statuscode:200&fastLatest=true`; const cdx = `https://web.archive.org/cdx/search/cdx?url=${encodeURIComponent(targetUrl)}&output=json&limit=1&fl=timestamp&filter=statuscode:200&fastLatest=true`;
const res = await fetch(cdx); let res;
if (!res.ok) throw new Error(`CDX Fehler: ${res.status}`); try {
// Ohne custom headers minimaler Request damit CORS-Preflight entfällt
res = await fetch(cdx);
} catch (e) {
throw new Error('Netzwerkfehler: ' + e.message);
}
if (!res.ok) throw new Error(`CDX ${res.status}`);
const data = await res.json(); const data = await res.json();
if (!Array.isArray(data) || data.length < 2 || !data[1]?.[0]) return null; if (!Array.isArray(data) || data.length < 2 || !data[1]?.[0]) return null;
return `https://web.archive.org/web/${data[1][0]}/${targetUrl}`; return `https://web.archive.org/web/${data[1][0]}/${targetUrl}`;
} }
// Wayback Save direkt im Browser via no-cors fetch (fire-and-forget) // Wayback Save + CDX-Polling
// Dann CDX pollen bis Snapshot erscheint
async function saveAndPollWayback(targetUrl, onStatus) { async function saveAndPollWayback(targetUrl, onStatus) {
// Save abschießen (no-cors: kein Response lesbar, aber Request geht raus) // Save via no-cors (kein lesbarer Response, aber Archivierung wird getriggert)
try { try {
await fetch(`https://web.archive.org/save/${targetUrl}`, { mode: 'no-cors' }); fetch(`https://web.archive.org/save/${targetUrl}`, {
} catch { /* ignorieren no-cors wirft immer */ } method: 'GET',
mode: 'no-cors',
}).catch(() => {});
} catch {}
// CDX pollen: 8x alle 5s = max 40s // CDX pollen: 8x alle 5s = max 40s
for (let i = 0; i < 8; i++) { for (let i = 0; i < 8; i++) {
onStatus(`Warte auf Snapshot... (${(i+1)*5}s)`);
await new Promise(r => setTimeout(r, 5000)); await new Promise(r => setTimeout(r, 5000));
onStatus(`Warte auf Snapshot... (${(i + 1) * 5}s)`);
try {
const url = await searchWayback(targetUrl); const url = await searchWayback(targetUrl);
if (url) return url; if (url) return url;
} catch {}
} }
return null; return null;
} }