Skip to content

Commit fbc91db

Browse files
committed
fix search
1 parent 6126f39 commit fbc91db

1 file changed

Lines changed: 42 additions & 28 deletions

File tree

theme/ht_searcher.js

Lines changed: 42 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -471,39 +471,53 @@ window.search = window.search || {};
471471
showResults(true);
472472
}
473473

474-
(async function loadSearchIndex(lang = window.lang || 'en') {
475-
/* ───────── paths ───────── */
476-
const branch = lang === 'en' ? 'master' : lang;
477-
const baseRemote = `https://raw.githubusercontent.com/HackTricks-wiki/hacktricks-cloud/refs/heads/${branch}`;
478-
const remoteJs = `${baseRemote}/searchindex.js`;
479-
const localJs = '/searchindex.js';
480-
const TIMEOUT_MS = 5_000;
481-
482-
const loadScript = src =>
483-
new Promise((resolve, reject) => {
484-
const s = document.createElement('script');
485-
s.src = src;
486-
s.onload = resolve;
487-
s.onerror = reject;
474+
(async function loadSearchIndex(lang = window.lang || "en") {
475+
const branch = lang === "en" ? "master" : lang;
476+
const rawUrl =
477+
`https://raw.githubusercontent.com/HackTricks-wiki/hacktricks-cloud/refs/heads/${branch}/searchindex.js`;
478+
const localJs = "/searchindex.js";
479+
const TIMEOUT_MS = 5_000;
480+
481+
/* helper: inject a <script src=…> and wait for it */
482+
const injectScript = (src) =>
483+
new Promise((resolve, reject) => {
484+
const s = document.createElement("script");
485+
s.src = src;
486+
s.onload = () => resolve(src);
487+
s.onerror = (e) => reject(e);
488488
document.head.appendChild(s);
489-
});
490-
491-
/* ───────── 1. remote JS ───────── */
489+
});
490+
492491
try {
493-
await loadScript(remoteJs);
494-
return init(window.search);
495-
} catch (e) {
496-
console.warn('Remote JS failed →', e);
492+
/* 1 — download raw JS from GitHub */
493+
const controller = new AbortController();
494+
const timer = setTimeout(() => controller.abort(), TIMEOUT_MS);
495+
496+
const res = await fetch(rawUrl, { signal: controller.signal });
497+
clearTimeout(timer);
498+
if (!res.ok) throw new Error(`HTTP ${res.status}`);
499+
500+
/* 2 — wrap in a Blob so the browser sees application/javascript */
501+
const code = await res.text();
502+
const blobUrl = URL.createObjectURL(
503+
new Blob([code], { type: "application/javascript" })
504+
);
505+
506+
/* 3 — execute it */
507+
await injectScript(blobUrl);
508+
return init(window.search);
509+
} catch (eRemote) {
510+
console.warn("Remote JS failed →", eRemote);
497511
}
498-
499-
/* ───────── 2. local JS ───────── */
512+
513+
/* ───────── fallback: local copy ───────── */
500514
try {
501-
await loadScript(localJs);
502-
return init(window.search);
503-
} catch (e) {
504-
console.error('Local JS failed →', e);
515+
await injectScript(localJs);
516+
return init(window.search);
517+
} catch (eLocal) {
518+
console.error("Local JS failed →", eLocal);
505519
}
506-
})();
520+
})();
507521

508522
// Exported functions
509523
search.hasFocus = hasFocus;

0 commit comments

Comments
 (0)