mirror of
https://github.com/HackTricks-wiki/hacktricks.git
synced 2025-10-10 18:36:50 +00:00
fix local search
This commit is contained in:
parent
1215fdd163
commit
2638185213
@ -470,17 +470,69 @@ window.search = window.search || {};
|
|||||||
// Display results
|
// Display results
|
||||||
showResults(true);
|
showResults(true);
|
||||||
}
|
}
|
||||||
|
async function loadSearchIndex(lang = 'en') {
|
||||||
var branch = lang === "en" ? "master" : lang
|
/* ---------- configuration ---------- */
|
||||||
fetch(`https://raw.githubusercontent.com/HackTricks-wiki/hacktricks/refs/heads/${branch}/searchindex.json`)
|
const branch = lang === 'en' ? 'master' : lang;
|
||||||
.then(response => response.json())
|
const remoteJson = `https://raw.githubusercontent.com/HackTricks-wiki/hacktricks/refs/heads/${branch}/searchindex.json`;
|
||||||
.then(json => init(json))
|
const remoteJs = `https://raw.githubusercontent.com/HackTricks-wiki/hacktricks/refs/heads/${branch}/searchindex.js`;
|
||||||
.catch(error => { // Try to load searchindex.js if fetch failed
|
const localJson = 'searchindex.json';
|
||||||
var script = document.createElement('script');
|
const localJs = 'searchindex.js';
|
||||||
script.src = `https://raw.githubusercontent.com/HackTricks-wiki/hacktricks/refs/heads/${branch}/searchindex.js`;
|
const TIMEOUT_MS = 5_000;
|
||||||
script.onload = () => init(window.search);
|
/* ----------------------------------- */
|
||||||
document.head.appendChild(script);
|
|
||||||
});
|
const fetchWithTimeout = (url, opt = {}) =>
|
||||||
|
Promise.race([
|
||||||
|
fetch(url, opt),
|
||||||
|
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 =>
|
||||||
|
new Promise((resolve, reject) => {
|
||||||
|
const s = document.createElement('script');
|
||||||
|
s.src = src;
|
||||||
|
s.onload = resolve;
|
||||||
|
s.onerror = reject;
|
||||||
|
document.head.appendChild(s);
|
||||||
|
});
|
||||||
|
|
||||||
|
/* 3️⃣ remote JS ----------------------------------------------------- */
|
||||||
|
try {
|
||||||
|
await loadScript(remoteJs);
|
||||||
|
return init(window.search);
|
||||||
|
} catch (err) {
|
||||||
|
console.warn('Remote searchindex.js failed →', err);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* 4️⃣ local JS ------------------------------------------------------ */
|
||||||
|
try {
|
||||||
|
await loadScript(localJs);
|
||||||
|
return init(window.search);
|
||||||
|
} catch (err) {
|
||||||
|
console.error('Local searchindex.js failed →', err);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/* kick things off */
|
||||||
|
loadSearchIndex(window.lang || 'en');
|
||||||
|
|
||||||
// Exported functions
|
// Exported functions
|
||||||
search.hasFocus = hasFocus;
|
search.hasFocus = hasFocus;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user