添加历史记录

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,
"name": "渠道应用开发部知识库",
"version": "1.0.1",
"version": "1.0.2",
"description": "通过关键词快速搜索内部文档",
"permissions": [],
"permissions": ["storage"],
"host_permissions": [
"http://app.wisdompulse.cn/*",
"http://share.wisdompulse.cn/*"

View File

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

109
popup.js
View File

@ -1,16 +1,22 @@
const searchBtn = document.getElementById("searchBtn");
const input = document.getElementById("searchInput");
const resultBox = document.getElementById("results");
document.addEventListener("DOMContentLoaded", () => {
const input = document.getElementById("searchInput");
const searchBtn = document.getElementById("searchBtn");
const resultBox = document.getElementById("results");
const historyBox = document.getElementById("historyTags");
let currentPage = 1;
let totalPages = 1;
let lastKeywords = [];
let currentPage = 1;
let totalPages = 1;
let lastKeywords = [];
function showLoading() {
function log(...args) {
console.log("[v1.0.2]", ...args);
}
function showLoading() {
resultBox.innerHTML = "<li>加载中...</li>";
}
}
function renderPagination() {
function renderPagination() {
const pagination = document.createElement("div");
pagination.className = "pagination";
@ -29,9 +35,47 @@ function renderPagination() {
}
resultBox.appendChild(pagination);
}
}
async function fetchResults(keywords, page) {
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) {
currentPage = page;
lastKeywords = keywords;
@ -42,10 +86,7 @@ async function fetchResults(keywords, page) {
size: 10
};
console.log("[INFO] 请求地址: http://app.wisdompulse.cn/search");
console.log("[INFO] 请求方法: POST");
console.log("[INFO] 请求体:", JSON.stringify(requestBody, null, 2));
log("触发搜索:", requestBody);
showLoading();
try {
@ -57,14 +98,10 @@ async function fetchResults(keywords, page) {
body: JSON.stringify(requestBody)
});
console.log("[INFO] 响应状态:", response.status, response.statusText);
const data = await response.json();
console.log("[INFO] 响应体:", data);
resultBox.innerHTML = "";
if (!data.results || data.results.length === 0) {
console.log("[INFO] 未返回结果数据");
resultBox.innerHTML = "<li>未找到相关文档</li>";
return;
}
@ -73,7 +110,6 @@ async function fetchResults(keywords, page) {
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}`;
@ -95,26 +131,25 @@ async function fetchResults(keywords, page) {
console.error("[ERROR] 搜索失败:", err);
resultBox.innerHTML = "<li>搜索失败,请检查网络或服务状态。</li>";
}
}
}
function triggerSearch() {
function triggerSearch() {
const inputValue = input.value.trim();
console.log("[INFO] 用户输入关键词:", inputValue);
if (!inputValue) {
console.warn("[WARN] 输入为空");
return;
}
if (!inputValue) 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();
}
searchBtn.addEventListener("click", triggerSearch);
input.addEventListener("keydown", e => {
if (e.key === "Enter") 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;
}
.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 {
list-style: none;
padding: 0;