document.addEventListener("DOMContentLoaded", () => {
const input = document.getElementById("searchInput");
const searchBtn = document.getElementById("searchBtn");
const clearHistoryBtn = document.getElementById("clearHistoryBtn");
const resultBox = document.getElementById("results");
const historyBox = document.getElementById("historyTags");
const historyWrapper = document.getElementById("historyWrapper");
let currentPage = 1;
let totalPages = 1;
let lastKeywords = [];
function log(...args) {
try {
const version = chrome.runtime?.getManifest?.().version || "dev";
console.log(`[v${version}]`, ...args);
} catch (e) {
console.log("[v?]", ...args);
}
}
function showLoading() {
resultBox.innerHTML = "
加载中...";
}
function clearResults() {
resultBox.innerHTML = "";
historyWrapper.classList.add("hidden");
}
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) 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) {
historyWrapper.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);
});
historyWrapper.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) {
clearResults();
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("input", () => {
if (!input.value.trim()) clearResults();
});
input.addEventListener("blur", () => {
setTimeout(() => historyWrapper.classList.add("hidden"), 200);
});
clearHistoryBtn.addEventListener("click", () => {
chrome.storage.local.remove("searchHistory", () => {
log("历史记录已清空");
renderHistoryTags();
});
});
log("插件初始化完成");
});