diff --git a/manifest.json b/manifest.json index a4a430b..447377f 100644 --- a/manifest.json +++ b/manifest.json @@ -1,7 +1,7 @@ { "manifest_version": 3, "name": "渠道应用开发部知识库", - "version": "1.5.2", + "version": "1.5.3", "description": "通过关键词快速搜索内部文档", "permissions": ["storage"], "host_permissions": [ diff --git a/scripts/popup.js b/scripts/popup.js index fa40a5e..a912fce 100644 --- a/scripts/popup.js +++ b/scripts/popup.js @@ -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 = "