添加历史记录

This commit is contained in:
luke 2025-05-24 17:17:18 +08:00
parent 8e8df8897c
commit 413dc1a4aa
5 changed files with 226 additions and 164 deletions

BIN
.DS_Store vendored Normal file

Binary file not shown.

View File

@ -1,9 +1,9 @@
{ {
"manifest_version": 3, "manifest_version": 3,
"name": "渠道应用开发部知识库", "name": "渠道应用开发部知识库",
"version": "1.0.1", "version": "1.0.2",
"description": "通过关键词快速搜索内部文档", "description": "通过关键词快速搜索内部文档",
"permissions": [], "permissions": ["storage"],
"host_permissions": [ "host_permissions": [
"http://app.wisdompulse.cn/*", "http://app.wisdompulse.cn/*",
"http://share.wisdompulse.cn/*" "http://share.wisdompulse.cn/*"

View File

@ -1,13 +1,13 @@
<!DOCTYPE html> <!DOCTYPE html>
<html lang="zh-CN"> <html lang="zh-CN">
<head> <head>
<meta charset="UTF-8"> <meta charset="UTF-8" />
<title>渠道应用开发部知识库</title> <title>渠道应用开发部知识库</title>
<link rel="stylesheet" href="styles.css"> <link rel="stylesheet" href="styles.css" />
</head> </head>
<body> <body>
<div class="header"> <div class="header">
<img src="icon.png" alt="logo" class="logo"> <img src="icon.png" alt="logo" class="logo" />
<div class="title-box"> <div class="title-box">
<h1>渠道应用开发部知识库</h1> <h1>渠道应用开发部知识库</h1>
<a href="https://oa.ahrcu.com/sys/portal/page.jsp" target="_blank" class="oa-link">跳转OA</a> <a href="https://oa.ahrcu.com/sys/portal/page.jsp" target="_blank" class="oa-link">跳转OA</a>
@ -16,9 +16,12 @@
<div class="container"> <div class="container">
<div class="search-bar"> <div class="search-bar">
<input id="searchInput" type="text" placeholder="请输入关键词(空格分隔)..." /> <input id="searchInput" type="text" autocomplete="off" placeholder="请输入关键词(空格分隔)..." />
<button id="searchBtn">搜索</button> <button id="searchBtn">搜索</button>
</div> </div>
<div id="historyTags" class="tag-history hidden"></div>
<ul id="results"></ul> <ul id="results"></ul>
</div> </div>

View File

@ -1,11 +1,17 @@
const searchBtn = document.getElementById("searchBtn"); document.addEventListener("DOMContentLoaded", () => {
const input = document.getElementById("searchInput"); const input = document.getElementById("searchInput");
const searchBtn = document.getElementById("searchBtn");
const resultBox = document.getElementById("results"); const resultBox = document.getElementById("results");
const historyBox = document.getElementById("historyTags");
let currentPage = 1; let currentPage = 1;
let totalPages = 1; let totalPages = 1;
let lastKeywords = []; let lastKeywords = [];
function log(...args) {
console.log("[v1.0.2]", ...args);
}
function showLoading() { function showLoading() {
resultBox.innerHTML = "<li>加载中...</li>"; resultBox.innerHTML = "<li>加载中...</li>";
} }
@ -31,6 +37,44 @@ function renderPagination() {
resultBox.appendChild(pagination); 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) { async function fetchResults(keywords, page) {
currentPage = page; currentPage = page;
lastKeywords = keywords; lastKeywords = keywords;
@ -42,10 +86,7 @@ async function fetchResults(keywords, page) {
size: 10 size: 10
}; };
console.log("[INFO] 请求地址: http://app.wisdompulse.cn/search"); log("触发搜索:", requestBody);
console.log("[INFO] 请求方法: POST");
console.log("[INFO] 请求体:", JSON.stringify(requestBody, null, 2));
showLoading(); showLoading();
try { try {
@ -57,14 +98,10 @@ async function fetchResults(keywords, page) {
body: JSON.stringify(requestBody) body: JSON.stringify(requestBody)
}); });
console.log("[INFO] 响应状态:", response.status, response.statusText);
const data = await response.json(); const data = await response.json();
console.log("[INFO] 响应体:", data);
resultBox.innerHTML = ""; resultBox.innerHTML = "";
if (!data.results || data.results.length === 0) { if (!data.results || data.results.length === 0) {
console.log("[INFO] 未返回结果数据");
resultBox.innerHTML = "<li>未找到相关文档</li>"; resultBox.innerHTML = "<li>未找到相关文档</li>";
return; return;
} }
@ -73,7 +110,6 @@ async function fetchResults(keywords, page) {
data.results.forEach(doc => { data.results.forEach(doc => {
const li = document.createElement("li"); const li = document.createElement("li");
const cleanPath = doc.filepath.replace(/^import-data\//, "").replace(/\.md$/, ".html"); const cleanPath = doc.filepath.replace(/^import-data\//, "").replace(/\.md$/, ".html");
const link = document.createElement("a"); const link = document.createElement("a");
link.href = `http://share.wisdompulse.cn/public/${cleanPath}`; link.href = `http://share.wisdompulse.cn/public/${cleanPath}`;
@ -99,22 +135,21 @@ async function fetchResults(keywords, page) {
function triggerSearch() { function triggerSearch() {
const inputValue = input.value.trim(); const inputValue = input.value.trim();
console.log("[INFO] 用户输入关键词:", inputValue); if (!inputValue) return;
if (!inputValue) {
console.warn("[WARN] 输入为空");
return;
}
const keywords = inputValue.split(/\s+/).filter(Boolean); const keywords = inputValue.split(/\s+/).filter(Boolean);
updateHistory(inputValue);
fetchResults(keywords, 1); fetchResults(keywords, 1);
} }
searchBtn.addEventListener("click", triggerSearch); searchBtn.addEventListener("click", triggerSearch);
// 支持按回车键直接搜索
input.addEventListener("keydown", e => { input.addEventListener("keydown", e => {
if (e.key === "Enter") { if (e.key === "Enter") triggerSearch();
triggerSearch(); });
}
input.addEventListener("focus", renderHistoryTags);
input.addEventListener("blur", () => {
setTimeout(() => historyBox.classList.add("hidden"), 200);
});
log("插件初始化完成");
}); });

View File

@ -76,6 +76,30 @@ h1 {
background-color: #0056b3; background-color: #0056b3;
} }
.tag-history {
display: flex;
flex-wrap: wrap;
gap: 8px;
margin-bottom: 15px;
}
.tag-history .tag {
background-color: #f0f0f0;
color: #333;
padding: 4px 10px;
border-radius: 12px;
font-size: 12px;
cursor: pointer;
}
.tag-history .tag:hover {
background-color: #dcdcdc;
}
.hidden {
display: none;
}
#results { #results {
list-style: none; list-style: none;
padding: 0; padding: 0;