抓取前检查内容库是否已存在相同链接
- 抓取前先搜索内容库检查 URL 是否已存在 - 已存在的链接标记为黄色,显示'已存在' - 避免重复抓取和保存 - 添加 checkUrlExists 函数
This commit is contained in:
@@ -217,6 +217,11 @@
|
|||||||
background: #f0fdf4;
|
background: #f0fdf4;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.result-item.exists {
|
||||||
|
border-color: #f59e0b;
|
||||||
|
background: #fffbeb;
|
||||||
|
}
|
||||||
|
|
||||||
.result-item.fetched {
|
.result-item.fetched {
|
||||||
border-color: #3b82f6;
|
border-color: #3b82f6;
|
||||||
}
|
}
|
||||||
|
|||||||
+44
-1
@@ -127,6 +127,21 @@ function displayResults() {
|
|||||||
`).join('');
|
`).join('');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 检查URL是否已在内容库
|
||||||
|
async function checkUrlExists(url) {
|
||||||
|
try {
|
||||||
|
const response = await fetch(`${API_BASE}/api/articles/search?q=${encodeURIComponent(url)}`);
|
||||||
|
const data = await response.json();
|
||||||
|
if (data.success && data.articles && data.articles.length > 0) {
|
||||||
|
// 检查是否有完全匹配的 URL
|
||||||
|
return data.articles.some(a => a.url === url);
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
console.error('检查URL失败:', error);
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
// 抓取单个结果
|
// 抓取单个结果
|
||||||
async function fetchResult(index) {
|
async function fetchResult(index) {
|
||||||
const result = searchResults[index];
|
const result = searchResults[index];
|
||||||
@@ -134,6 +149,28 @@ async function fetchResult(index) {
|
|||||||
|
|
||||||
const btn = document.getElementById(`fetch-btn-${index}`);
|
const btn = document.getElementById(`fetch-btn-${index}`);
|
||||||
btn.disabled = true;
|
btn.disabled = true;
|
||||||
|
btn.innerHTML = '<i class="ri-loader-4-line"></i> 检查中...';
|
||||||
|
|
||||||
|
// 先检查内容库是否已存在
|
||||||
|
const exists = await checkUrlExists(result.url);
|
||||||
|
if (exists) {
|
||||||
|
searchResults[index].fetched = true;
|
||||||
|
searchResults[index].saved = true;
|
||||||
|
searchResults[index].content = '[内容库中已存在此链接]';
|
||||||
|
btn.innerHTML = '<i class="ri-check-line"></i> 已存在';
|
||||||
|
btn.className = 'btn btn-sm btn-warning';
|
||||||
|
|
||||||
|
const saveBtn = document.getElementById(`save-btn-${index}`);
|
||||||
|
saveBtn.disabled = true;
|
||||||
|
saveBtn.innerHTML = '<i class="ri-check-line"></i> 已存在';
|
||||||
|
|
||||||
|
const item = document.getElementById(`result-${index}`);
|
||||||
|
item.classList.add('saved');
|
||||||
|
|
||||||
|
showToast('该链接已在内容库中', 'warning');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
btn.innerHTML = '<i class="ri-loader-4-line"></i> 抓取中...';
|
btn.innerHTML = '<i class="ri-loader-4-line"></i> 抓取中...';
|
||||||
|
|
||||||
try {
|
try {
|
||||||
@@ -178,10 +215,16 @@ async function saveResult(index) {
|
|||||||
const result = searchResults[index];
|
const result = searchResults[index];
|
||||||
if (!result) return;
|
if (!result) return;
|
||||||
|
|
||||||
|
// 如果已经保存(可能是内容库中已存在),直接返回
|
||||||
|
if (result.saved) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
// 如果未抓取,先抓取
|
// 如果未抓取,先抓取
|
||||||
if (!result.fetched) {
|
if (!result.fetched) {
|
||||||
await fetchResult(index);
|
await fetchResult(index);
|
||||||
if (!searchResults[index].fetched) return;
|
// 抓取后如果已标记为 saved(内容库已存在),不继续保存
|
||||||
|
if (searchResults[index].saved) return;
|
||||||
}
|
}
|
||||||
|
|
||||||
const btn = document.getElementById(`save-btn-${index}`);
|
const btn = document.getElementById(`save-btn-${index}`);
|
||||||
|
|||||||
Reference in New Issue
Block a user