优化&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,
"name": "渠道应用开发部知识库",
"version": "1.5.2",
"version": "1.5.3",
"description": "通过关键词快速搜索内部文档",
"permissions": ["storage"],
"host_permissions": [

View File

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