document.addEventListener("DOMContentLoaded", () => { const input = document.getElementById("searchInput"); const searchBtn = document.getElementById("searchBtn"); const resultBox = document.getElementById("results"); const historyBox = document.getElementById("historyTags"); let currentPage = 1; let totalPages = 1; let lastKeywords = []; function log(...args) { console.log("[v1.0.2]", ...args); } function showLoading() { resultBox.innerHTML = "
  • 加载中...
  • "; } function renderPagination() { const pagination = document.createElement("div"); pagination.className = "pagination"; if (currentPage > 1) { const prevBtn = document.createElement("button"); prevBtn.textContent = "上一页"; prevBtn.onclick = () => fetchResults(lastKeywords, currentPage - 1); pagination.appendChild(prevBtn); } if (currentPage < totalPages) { const nextBtn = document.createElement("button"); nextBtn.textContent = "下一页"; nextBtn.onclick = () => fetchResults(lastKeywords, currentPage + 1); pagination.appendChild(nextBtn); } resultBox.appendChild(pagination); } function updateHistory(keyword) { if (!chrome?.storage?.local) { log("chrome.storage.local 不可用,跳过历史记录"); return; } chrome.storage.local.get(["searchHistory"], (res) => { let history = res.searchHistory || []; history = [keyword, ...history.filter(item => item !== keyword)]; if (history.length > 5) history = history.slice(0, 5); chrome.storage.local.set({ searchHistory: history }); }); } function renderHistoryTags() { if (!chrome?.storage?.local) return; chrome.storage.local.get(["searchHistory"], (res) => { const history = res.searchHistory || []; historyBox.innerHTML = ""; if (history.length === 0) { historyBox.classList.add("hidden"); return; } history.forEach(tagText => { const tag = document.createElement("span"); tag.className = "tag"; tag.textContent = tagText; tag.onclick = () => { input.value = tagText; triggerSearch(); }; historyBox.appendChild(tag); }); historyBox.classList.remove("hidden"); }); } async function fetchResults(keywords, page) { currentPage = page; lastKeywords = keywords; const keywordGroups = keywords.map(k => [k]); const requestBody = { keywordGroups, page: currentPage, size: 10 }; log("触发搜索:", requestBody); showLoading(); try { const response = await fetch("http://app.wisdompulse.cn/search", { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify(requestBody) }); const data = await response.json(); resultBox.innerHTML = ""; if (!data.results || data.results.length === 0) { resultBox.innerHTML = "
  • 未找到相关文档
  • "; return; } totalPages = Math.ceil(data.total / data.size); data.results.forEach(doc => { const li = document.createElement("li"); const cleanPath = doc.filepath.replace(/^import-data\//, "").replace(/\.md$/, ".html"); const link = document.createElement("a"); link.href = `http://share.wisdompulse.cn/public/${cleanPath}`; link.target = "_blank"; link.textContent = doc.filename; const snippet = document.createElement("div"); snippet.className = "summary"; snippet.innerHTML = doc.summary?.slice(0, 300) + "..."; li.appendChild(link); li.appendChild(snippet); resultBox.appendChild(li); }); renderPagination(); } catch (err) { console.error("[ERROR] 搜索失败:", err); resultBox.innerHTML = "
  • 搜索失败,请检查网络或服务状态。
  • "; } } function triggerSearch() { const inputValue = input.value.trim(); if (!inputValue) return; const keywords = inputValue.split(/\s+/).filter(Boolean); updateHistory(inputValue); fetchResults(keywords, 1); } searchBtn.addEventListener("click", triggerSearch); input.addEventListener("keydown", e => { if (e.key === "Enter") triggerSearch(); }); input.addEventListener("focus", renderHistoryTags); input.addEventListener("blur", () => { setTimeout(() => historyBox.classList.add("hidden"), 200); }); log("插件初始化完成"); });