/** @typedef {import('../../../typings/reader-chapter-search-strict').Response} SearchResponse */ /** @param {import('../../../typings/reader-chapter-search-strict').Configuration} configuration */ export function createChapterSearch(configuration) { const worker = new Worker("/static/reader/search/workers/reader-chapter-search-worker.mjs", { type: "module" }); let sequence = 0, disposed = false; /** @type {import('../../../typings/reader-chapter-search-strict').Pending | null} */ let pending = null; /** @type {number | null} */ let session = null; /** @type {Error | null} */ let failure = null; const cancelPending = () => { pending?.reject(new DOMException("Search cancelled", "AbortError")); pending = null; }; worker.onmessage = (event) => { /** @type {SearchResponse} */ const data = event.data; if (!pending || pending.id !== data.id) return; if (data.progress) { pending.progress?.(data); return; } const task = pending; pending = null; if ("error" in data && data.error) task.reject(new Error(data.error)); else { const page = /** @type {import('../../../typings/reader-chapter-search-strict').Page} */ (data); session = page.session; task.resolve(page); } }; worker.onerror = event => { event.preventDefault(); failure = new Error("全文搜索组件加载失败,请重试"); pending?.reject(failure); pending = null; }; worker.postMessage({ type: "init", configuration }); /** @param {string} type @param {{query: string} | {offset: number, session: number | null}} values * @param {((value: import('../../../typings/reader-chapter-search-strict').Progress) => void) | null} [progress] * @returns {Promise} */ function request(type, values, progress = null) { if (disposed) return Promise.reject(new DOMException("Reader closed", "AbortError")); if (failure) return Promise.reject(failure); cancelPending(); return new Promise((resolve, reject) => { const id = ++sequence; pending = { id, resolve, reject, progress }; worker.postMessage({ type, id, ...values }); }); } return { get failed() { return !!failure; }, search: (/** @type {string} */ query, /** @type {((value: import('../../../typings/reader-chapter-search-strict').Progress) => void) | null} */ progress = null) => request("search", { query }, progress), page: (/** @type {number} */ offset) => request("page", { offset, session }), cancel() { cancelPending(); session = null; worker.postMessage({ type: "cancel" }); }, dispose() { disposed = true; cancelPending(); worker.terminate(); } }; } // Reproduce the bundle's text extraction: skip script/style/noscript, separate // text nodes and collapse whitespace. Only legacy indexes decode DOM text again. // Runs map normalized offsets back to the exact text nodes being displayed. /** @param {(Node | null | undefined)[]} roots @param {string} [textProfile] */ export function chapterTextMap(roots, textProfile) { if (textProfile !== undefined && textProfile !== "dom-text-v1") throw new Error("全文搜索文本格式不受支持"); const legacy = textProfile === undefined; /** @type {import('../../../typings/reader-chapter-search-strict').Run[]} */ const runs = []; /** @type {string[]} */ const chunks = []; let length = 0; /** @type {import('../../../typings/reader-chapter-search-strict').Space | null} */ let pendingSpace = null; const decoder = legacy ? document.createElement("textarea") : null; /** @param {string} text @param {Text} node @param {number} start @param {number} end @param {boolean} linear */ const append = (text, node, start, end, linear) => { if (!text) return; if (pendingSpace && length) { chunks.push(" "); runs.push({ ...pendingSpace, from: length, to: ++length, linear: false }); } pendingSpace = null; chunks.push(text); runs.push({ node, start, end, linear, from: length, to: length + text.length }); length += text.length; }; for (const root of roots.filter(Boolean)) { if (!root) continue; const walker = document.createTreeWalker(root, NodeFilter.SHOW_TEXT); while (walker.nextNode()) { const node = /** @type {Text} */ (walker.currentNode); if (node.parentElement?.closest("script,style,noscript,.reader-chapter-link-error,.reader-chapter-search-heading")) continue; if (length) pendingSpace = { node: null, start: 0, end: 0 }; // Python's Unicode whitespace also includes NEL and the four separators. const pattern = legacy ? /&(?:#[xX][0-9a-fA-F]+;?|#\d+;?|[a-zA-Z][a-zA-Z0-9]+;?)|[\s\u0085\u001c-\u001f]+|[^&\s\u0085\u001c-\u001f]+|&/gu : /[\s\u0085\u001c-\u001f]+|[^\s\u0085\u001c-\u001f]+/gu; for (const match of node.data.matchAll(pattern)) { const raw = match[0], start = match.index, end = start + raw.length; let text = raw; if (decoder && raw.startsWith("&")) { decoder.innerHTML = raw; text = decoder.value; } if (/^[\s\u0085\u001c-\u001f]+$/u.test(text)) { pendingSpace ||= { node, start, end }; } else append(text, node, start, end, text === raw); } } } return { text: chunks.join(""), /** @param {number} start @param {number} end @param {Node} body */ parts(start, end, body) { /** @type {import('../../../typings/reader-chapter-search-strict').TextPart[]} */ const output = []; // Runs are append-only, strictly ordered ranges. Seek directly to the // first intersecting range rather than rescanning a whole long chapter. let low = 0, high = runs.length; while (low < high) { const middle = (low + high) >> 1; const run = runs[middle]; if (run && run.to <= start) low = middle + 1; else high = middle; } for (let index = low; index < runs.length; index++) { const run = runs[index]; if (!run || run.from >= end) break; if (run.to <= start || !run.node || !body.contains(run.node)) continue; const part = { node: run.node, start: run.linear ? run.start + Math.max(start - run.from, 0) : run.start, end: run.linear ? run.end - Math.max(run.to - end, 0) : run.end }; const last = output[output.length - 1]; if (last?.node === part.node) last.end = part.end; else output.push(part); } return output; } }; }