|
60 | 60 | } |
61 | 61 |
|
62 | 62 | return local ? loadIndex(null, local, isCloud) : null; |
63 | | - } (async () => { |
64 | | - const htmlLang = (document.documentElement.lang || 'en').toLowerCase(); |
65 | | - const lang = htmlLang.split('-')[0]; |
66 | | - const mainReleaseBase = 'https://github.com/HackTricks-wiki/hacktricks/releases/download'; |
67 | | - const cloudReleaseBase = 'https://github.com/HackTricks-wiki/hacktricks-cloud/releases/download'; |
| 63 | + } |
| 64 | + |
| 65 | + let built = []; |
| 66 | + const MAX = 30, opts = {bool:'AND', expand:true}; |
| 67 | + |
| 68 | + self.onmessage = async ({data}) => { |
| 69 | + if(data.type === 'init'){ |
| 70 | + const lang = data.lang || 'en'; |
| 71 | + const searchindexBase = 'https://raw.githubusercontent.com/HackTricks-wiki/hacktricks-searchindex/main'; |
68 | 72 |
|
69 | | - const mainTags = Array.from(new Set(['searchindex-' + lang, 'searchindex-en', 'searchindex-master'])); |
70 | | - const cloudTags = Array.from(new Set(['searchindex-' + lang, 'searchindex-en', 'searchindex-master'])); |
| 73 | + const mainFilenames = Array.from(new Set(['searchindex-' + lang + '.js', 'searchindex-en.js'])); |
| 74 | + const cloudFilenames = Array.from(new Set(['searchindex-cloud-' + lang + '.js', 'searchindex-cloud-en.js'])); |
71 | 75 |
|
72 | | - const MAIN_REMOTE_SOURCES = mainTags.map(function(tag) { return mainReleaseBase + '/' + tag + '/searchindex.js'; }); |
73 | | - const CLOUD_REMOTE_SOURCES = cloudTags.map(function(tag) { return cloudReleaseBase + '/' + tag + '/searchindex.js'; }); |
| 76 | + const MAIN_REMOTE_SOURCES = mainFilenames.map(function(filename) { return searchindexBase + '/' + filename; }); |
| 77 | + const CLOUD_REMOTE_SOURCES = cloudFilenames.map(function(filename) { return searchindexBase + '/' + filename; }); |
74 | 78 |
|
75 | | - const indices = []; |
76 | | - const main = await loadWithFallback(MAIN_REMOTE_SOURCES , '/searchindex-book.js', false); if(main) indices.push(main); |
77 | | - const cloud= await loadWithFallback(CLOUD_REMOTE_SOURCES, '/searchindex.js', true ); if(cloud) indices.push(cloud); |
| 79 | + const indices = []; |
| 80 | + const main = await loadWithFallback(MAIN_REMOTE_SOURCES , '/searchindex-book.js', false); if(main) indices.push(main); |
| 81 | + const cloud= await loadWithFallback(CLOUD_REMOTE_SOURCES, '/searchindex.js', true ); if(cloud) indices.push(cloud); |
78 | 82 | if(!indices.length){ postMessage({ready:false, error:'no-index'}); return; } |
79 | 83 | |
80 | 84 | /* build index objects */ |
81 | | - const built = indices.map(d => ({ |
| 85 | + built = indices.map(d => ({ |
82 | 86 | idx : elasticlunr.Index.load(d.json), |
83 | 87 | urls: d.urls, |
84 | 88 | cloud: d.cloud, |
85 | 89 | base: d.cloud ? 'https://cloud.hacktricks.wiki/' : '' |
86 | 90 | })); |
87 | 91 | |
88 | 92 | postMessage({ready:true}); |
89 | | - const MAX = 30, opts = {bool:'AND', expand:true}; |
90 | | - |
91 | | - self.onmessage = ({data:q}) => { |
92 | | - if(!q){ postMessage([]); return; } |
| 93 | + return; |
| 94 | + } |
| 95 | + |
| 96 | + const q = data.query || data; |
| 97 | + if(!q){ postMessage([]); return; } |
93 | 98 | |
94 | 99 | const all = []; |
95 | 100 | for(const s of built){ |
|
110 | 115 | } |
111 | 116 | all.sort((a,b)=>b.norm-a.norm); |
112 | 117 | postMessage(all.slice(0,MAX)); |
113 | | - }; |
114 | | - })(); |
| 118 | + }; |
115 | 119 | `; |
116 | 120 |
|
117 | 121 | /* ───────────── 2. spawn worker ───────────── */ |
118 | 122 | const worker = new Worker(URL.createObjectURL(new Blob([workerCode],{type:'application/javascript'}))); |
| 123 | + |
| 124 | + /* ───────────── 2.1. initialize worker with language ───────────── */ |
| 125 | + const htmlLang = (document.documentElement.lang || 'en').toLowerCase(); |
| 126 | + const lang = htmlLang.split('-')[0]; |
| 127 | + worker.postMessage({type: 'init', lang: lang}); |
119 | 128 |
|
120 | 129 | /* ───────────── 3. DOM refs ─────────────── */ |
121 | 130 | const wrap = document.getElementById('search-wrapper'); |
|
182 | 191 | else if([DOWN,UP,ENTER].includes(e.keyCode) && document.activeElement!==bar){const cur=list.querySelector('li.focus'); if(!cur) return; e.preventDefault(); if(e.keyCode===DOWN){const nxt=cur.nextElementSibling; if(nxt){cur.classList.remove('focus'); nxt.classList.add('focus');}} else if(e.keyCode===UP){const prv=cur.previousElementSibling; cur.classList.remove('focus'); if(prv){prv.classList.add('focus');} else {bar.focus();}} else {const a=cur.querySelector('a'); if(a) window.location.assign(a.href);}} |
183 | 192 | }); |
184 | 193 |
|
185 | | - bar.addEventListener('input',e=>{ clearTimeout(debounce); debounce=setTimeout(()=>worker.postMessage(e.target.value.trim()),120); }); |
| 194 | + bar.addEventListener('input',e=>{ clearTimeout(debounce); debounce=setTimeout(()=>worker.postMessage({query: e.target.value.trim()}),120); }); |
186 | 195 |
|
187 | 196 | /* ───────────── worker messages ───────────── */ |
188 | 197 | worker.onmessage = ({data}) => { |
|
0 commit comments