fix links

This commit is contained in:
Carlos Polop 2025-04-20 02:54:48 +02:00
parent 2638185213
commit 35bb5d3b69

View File

@ -470,69 +470,67 @@ window.search = window.search || {};
// Display results // Display results
showResults(true); showResults(true);
} }
async function loadSearchIndex(lang = 'en') {
/* ---------- configuration ---------- */ (async function loadSearchIndex(lang = window.lang || 'en') {
const branch = lang === 'en' ? 'master' : lang; /* ───────── paths ───────── */
const remoteJson = `https://raw.githubusercontent.com/HackTricks-wiki/hacktricks/refs/heads/${branch}/searchindex.json`; const branch = lang === 'en' ? 'master' : lang;
const remoteJs = `https://raw.githubusercontent.com/HackTricks-wiki/hacktricks/refs/heads/${branch}/searchindex.js`; const baseRemote = `https://raw.githubusercontent.com/HackTricks-wiki/hacktricks/${branch}`;
const localJson = 'searchindex.json'; const remoteJson = `${baseRemote}/searchindex.json`;
const localJs = 'searchindex.js'; const remoteJs = `${baseRemote}/searchindex.js`;
const TIMEOUT_MS = 5_000; const localJson = './searchindex.json';
/* ----------------------------------- */ const localJs = './searchindex.js';
const TIMEOUT_MS = 5_000;
/* ───────── helpers ───────── */
const fetchWithTimeout = (url, opt = {}) => const fetchWithTimeout = (url, opt = {}) =>
Promise.race([ Promise.race([
fetch(url, opt), fetch(url, opt),
new Promise((_, r) => setTimeout(() => r(new Error('timeout')), TIMEOUT_MS)) new Promise((_, r) => setTimeout(() => r(new Error('timeout')), TIMEOUT_MS))
]); ]);
/* 1 remote JSON -------------------------------------------------- */
try {
const r = await fetchWithTimeout(remoteJson);
if (!r.ok) throw new Error(r.status);
return init(await r.json());
} catch (err) {
console.warn('Remote searchindex.json failed →', err);
}
/* 2 local JSON --------------------------------------------------- */
try {
const r = await fetch(localJson);
if (!r.ok) throw new Error(r.status);
return init(await r.json());
} catch (err) {
console.warn('Local searchindex.json failed →', err);
}
/* helper to load a <script> and wait for it ------------------------ */
const loadScript = src => const loadScript = src =>
new Promise((resolve, reject) => { new Promise((resolve, reject) => {
const s = document.createElement('script'); const s = document.createElement('script');
s.src = src; s.src = src;
s.onload = resolve; s.onload = resolve;
s.onerror = reject; s.onerror = reject;
document.head.appendChild(s); document.head.appendChild(s);
}); });
/* 3 remote JS ----------------------------------------------------- */ /* ───────── 1. remote JSON ───────── */
try { try {
await loadScript(remoteJs); const r = await fetchWithTimeout(remoteJson);
return init(window.search); if (!r.ok) throw new Error(r.status);
} catch (err) { return init(await r.json());
console.warn('Remote searchindex.js failed →', err); } catch (e) {
console.warn('Remote JSON failed →', e);
} }
/* 4 local JS ------------------------------------------------------ */ /* ───────── 2. remote JS ───────── */
try { try {
await loadScript(localJs); await loadScript(remoteJs);
return init(window.search); return init(window.search);
} catch (err) { } catch (e) {
console.error('Local searchindex.js failed →', err); console.warn('Remote JS failed →', e);
} }
}
/* ───────── 3. local JSON ───────── */
/* kick things off */ try {
loadSearchIndex(window.lang || 'en'); const r = await fetch(localJson);
if (!r.ok) throw new Error(r.status);
return init(await r.json());
} catch (e) {
console.warn('Local JSON failed →', e);
}
/* ───────── 4. local JS ───────── */
try {
await loadScript(localJs);
return init(window.search);
} catch (e) {
console.error('Local JS failed →', e);
}
})();
// Exported functions // Exported functions
search.hasFocus = hasFocus; search.hasFocus = hasFocus;