优化&fix

This commit is contained in:
luke 2025-06-01 11:50:00 +08:00
parent 792c02b283
commit 4954fee1e9
2 changed files with 83 additions and 38 deletions

View File

@ -1,7 +1,7 @@
{ {
"manifest_version": 3, "manifest_version": 3,
"name": "渠道应用开发部知识库", "name": "渠道应用开发部知识库",
"version": "1.5.2", "version": "1.5.3",
"description": "通过关键词快速搜索内部文档", "description": "通过关键词快速搜索内部文档",
"permissions": ["storage"], "permissions": ["storage"],
"host_permissions": [ "host_permissions": [

View File

@ -1,10 +1,10 @@
document.addEventListener("DOMContentLoaded", async () => { document.addEventListener("DOMContentLoaded", async () => {
// ===== 自适应 / 拖拽 ===== log("DOMContentLoaded - 页面加载完成");
const panel = document.getElementById("draggablePanel"); const panel = document.getElementById("draggablePanel");
const header = document.querySelector(".header"); const header = document.querySelector(".header");
if (window.innerWidth > 900 && window.innerHeight > 700) { if (window.innerWidth > 900 && window.innerHeight > 700) {
log("启用全屏模式");
document.body.classList.add("fullscreen"); document.body.classList.add("fullscreen");
if (panel) { if (panel) {
panel.style.left = ""; panel.style.left = "";
@ -12,6 +12,7 @@ document.addEventListener("DOMContentLoaded", async () => {
panel.style.position = "fixed"; panel.style.position = "fixed";
} }
} else if (panel && header) { } else if (panel && header) {
log("启用可拖拽模式");
let dragging = false, offsetX = 0, offsetY = 0; let dragging = false, offsetX = 0, offsetY = 0;
header.addEventListener("mousedown", (e) => { header.addEventListener("mousedown", (e) => {
dragging = true; dragging = true;
@ -31,7 +32,7 @@ document.addEventListener("DOMContentLoaded", async () => {
}); });
} }
// ===== 元素绑定 ===== // ====== 元素选择 ======
const input = document.getElementById("searchInput"); const input = document.getElementById("searchInput");
const searchBtn = document.getElementById("searchBtn"); const searchBtn = document.getElementById("searchBtn");
const clearBtn = document.getElementById("clearHistoryBtn"); const clearBtn = document.getElementById("clearHistoryBtn");
@ -48,19 +49,21 @@ document.addEventListener("DOMContentLoaded", async () => {
const userDropdown = document.getElementById("userDropdown"); const userDropdown = document.getElementById("userDropdown");
const openTabBtn = document.getElementById("openTabBtn"); const openTabBtn = document.getElementById("openTabBtn");
// ===== 标签切换 =====
const tabEs = document.getElementById("tabEs"); const tabEs = document.getElementById("tabEs");
const tabAi = document.getElementById("tabAi"); const tabAi = document.getElementById("tabAi");
const tabContentEs = document.getElementById("tabContentEs"); const tabContentEs = document.getElementById("tabContentEs");
const tabContentAi = document.getElementById("tabContentAi"); const tabContentAi = document.getElementById("tabContentAi");
// ====== 标签切换 ======
tabEs.onclick = () => { tabEs.onclick = () => {
log("切换到 ES 结果页");
tabEs.classList.add("active"); tabEs.classList.add("active");
tabAi.classList.remove("active"); tabAi.classList.remove("active");
tabContentEs.classList.add("active"); tabContentEs.classList.add("active");
tabContentAi.classList.remove("active"); tabContentAi.classList.remove("active");
}; };
tabAi.onclick = () => { tabAi.onclick = () => {
log("切换到 LLM 结果页");
tabEs.classList.remove("active"); tabEs.classList.remove("active");
tabAi.classList.add("active"); tabAi.classList.add("active");
tabContentEs.classList.remove("active"); tabContentEs.classList.remove("active");
@ -70,47 +73,43 @@ document.addEventListener("DOMContentLoaded", async () => {
}, 100); }, 100);
}; };
// ===== 新标签页打开 popup.html ===== function getQueryParam(name) {
if (openTabBtn) { const url = new URL(window.location.href);
openTabBtn.onclick = () => { return url.searchParams.get(name);
const url = chrome?.runtime?.getURL("pages/popup.html") || "popup.html";
try {
chrome?.tabs?.create({ url });
} catch {
window.open(url, "_blank");
}
};
} }
// ===== 状态变量 ===== const isNewTab = getQueryParam("from") === "newtab";
let token = null; let token = null;
let currentPage = 1; let currentPage = 1;
let totalPages = 1; let totalPages = 1;
let lastKeywords = []; let lastKeywords = [];
// ===== 加载 token带用户名=====
const auth = await loadToken(); const auth = await loadToken();
if (!auth) { if (!auth) {
log("未认证,登出"); log("用户未认证,执行登出");
handleLogout(); return handleLogout();
return;
} }
token = auth.token; token = auth.token;
userNameLabel.textContent = auth.username; userNameLabel.textContent = auth.username;
log("用户认证成功:", auth.username); log("用户认证成功,用户名:", auth.username);
// ===== 搜索触发函数 =====
async function triggerSearch() { async function triggerSearch() {
const raw = input?.value?.trim(); const raw = input?.value?.trim();
if (!token || !raw) return; if (!token || !raw) {
log("取消搜索token 丢失或关键词为空");
return;
}
log("执行 triggerSearch关键词:", raw, "是否新标签页:", isNewTab);
updateHistory(raw); updateHistory(raw);
renderHistoryTags(); renderHistoryTags(true);
if (!isNewTab) {
fetchLLMAnswer(raw, llmResultContainer, llmResultContent); fetchLLMAnswer(raw, llmResultContainer, llmResultContent);
}
fetchResults(raw, 1); fetchResults(raw, 1);
} }
// ===== 搜索实现(含 ES + 分页)=====
function parseKeywordGroups(inputStr) { function parseKeywordGroups(inputStr) {
return inputStr return inputStr
.trim() .trim()
@ -128,6 +127,7 @@ document.addEventListener("DOMContentLoaded", async () => {
} }
function clearResults() { function clearResults() {
log("清空结果和历史区域");
resultBox.innerHTML = ""; resultBox.innerHTML = "";
llmResultContent.innerHTML = ""; llmResultContent.innerHTML = "";
llmResultContainer.classList.add("hidden"); llmResultContainer.classList.add("hidden");
@ -155,6 +155,7 @@ document.addEventListener("DOMContentLoaded", async () => {
async function fetchResults(rawInput, page) { async function fetchResults(rawInput, page) {
currentPage = page; currentPage = page;
lastKeywords = rawInput; lastKeywords = rawInput;
log("调用 fetchResults关键词:", rawInput, "页码:", page);
showLoading(); showLoading();
const keywordGroups = parseKeywordGroups(rawInput); const keywordGroups = parseKeywordGroups(rawInput);
@ -174,12 +175,18 @@ document.addEventListener("DOMContentLoaded", async () => {
body: JSON.stringify(requestBody) body: JSON.stringify(requestBody)
}); });
if (response.status === 401) return handleLogout(); log("搜索接口响应状态:", response.status);
if (response.status === 401) {
log("token 已失效,执行登出");
return handleLogout();
}
const data = await response.json(); const data = await response.json();
resultBox.innerHTML = ""; resultBox.innerHTML = "";
if (!data.results || data.results.length === 0) { if (!data.results || data.results.length === 0) {
log("无搜索结果");
resultBox.innerHTML = "<li>未找到相关文档</li>"; resultBox.innerHTML = "<li>未找到相关文档</li>";
return; return;
} }
@ -205,12 +212,13 @@ document.addEventListener("DOMContentLoaded", async () => {
renderPagination(); renderPagination();
} catch (err) { } catch (err) {
log("搜索请求异常:", err);
resultBox.innerHTML = "<li>搜索失败,请检查网络或服务状态。</li>"; resultBox.innerHTML = "<li>搜索失败,请检查网络或服务状态。</li>";
} }
} }
// ===== 历史记录 =====
function updateHistory(keyword) { function updateHistory(keyword) {
log("更新搜索历史:", keyword);
chrome.storage.local.get(["searchHistory"], (res) => { chrome.storage.local.get(["searchHistory"], (res) => {
let history = res.searchHistory || []; let history = res.searchHistory || [];
history = [keyword, ...history.filter(item => item !== keyword)]; history = [keyword, ...history.filter(item => item !== keyword)];
@ -220,6 +228,7 @@ document.addEventListener("DOMContentLoaded", async () => {
} }
function renderHistoryTags(show = false) { function renderHistoryTags(show = false) {
log("渲染历史标签,是否显示:", show);
chrome.storage.local.get(["searchHistory"], (res) => { chrome.storage.local.get(["searchHistory"], (res) => {
const history = res.searchHistory || []; const history = res.searchHistory || [];
historyBox.innerHTML = ""; historyBox.innerHTML = "";
@ -244,19 +253,40 @@ document.addEventListener("DOMContentLoaded", async () => {
}); });
} }
// ====== DOM 事件绑定 ======
// ===== DOM 事件绑定 ===== searchBtn.addEventListener("click", () => {
searchBtn.addEventListener("click", triggerSearch); log("点击搜索按钮");
input.addEventListener("keydown", e => e.key === "Enter" && triggerSearch()); triggerSearch();
});
input.addEventListener("keydown", e => {
if (e.key === "Enter") {
log("按下回车键");
triggerSearch();
}
});
input.addEventListener("focus", () => renderHistoryTags(true)); input.addEventListener("focus", () => renderHistoryTags(true));
input.addEventListener("input", () => !input.value.trim() && clearResults()); input.addEventListener("input", () => {
if (!input.value.trim()) {
log("输入清空,清除结果");
clearResults();
}
});
input.addEventListener("blur", () => setTimeout(() => historyWrapper.classList.add("hidden"), 200)); input.addEventListener("blur", () => setTimeout(() => historyWrapper.classList.add("hidden"), 200));
clearBtn.addEventListener("click", () => { clearBtn.addEventListener("click", () => {
chrome.storage.local.remove("searchHistory", renderHistoryTags); log("点击清空历史按钮");
chrome.storage.local.remove("searchHistory", () => renderHistoryTags());
});
logoutBtn?.addEventListener("click", () => {
log("点击退出登录按钮");
handleLogout();
});
userAvatar?.addEventListener("click", () => {
log("点击用户头像,切换菜单");
userDropdown.classList.toggle("hidden");
}); });
logoutBtn?.addEventListener("click", handleLogout);
userAvatar?.addEventListener("click", () => userDropdown.classList.toggle("hidden"));
window.addEventListener("click", (e) => { window.addEventListener("click", (e) => {
if (!userDropdown.contains(e.target) && e.target !== userAvatar) { if (!userDropdown.contains(e.target) && e.target !== userAvatar) {
userDropdown.classList.add("hidden"); userDropdown.classList.add("hidden");
@ -267,6 +297,7 @@ document.addEventListener("DOMContentLoaded", async () => {
const val = llmInput.value.trim(); const val = llmInput.value.trim();
if (!val) return; if (!val) return;
llmInput.value = ""; llmInput.value = "";
log("发送 LLM 问题:", val);
fetchLLMAnswer(val, llmResultContainer, llmResultContent); fetchLLMAnswer(val, llmResultContainer, llmResultContent);
}); });
@ -274,6 +305,20 @@ document.addEventListener("DOMContentLoaded", async () => {
if (e.key === "Enter") llmSendBtn.click(); if (e.key === "Enter") llmSendBtn.click();
}); });
// 首次展示历史 openTabBtn.onclick = () => {
const val = input?.value?.trim();
const encoded = encodeURIComponent(val || "");
const url = chrome.runtime.getURL("pages/popup.html?keywords=" + encoded + "&from=newtab");
log("点击新标签页按钮,跳转到:", url);
chrome.tabs?.create({ url });
};
const autoKeywords = getQueryParam("keywords");
if (autoKeywords) {
input.value = decodeURIComponent(autoKeywords);
log("检测到 URL 参数 keywords自动搜索:", input.value);
triggerSearch();
}
renderHistoryTags(); renderHistoryTags();
}); });