修改配置文件

This commit is contained in:
2026-06-18 21:59:07 +08:00 Unverified
parent fc76f9949f
commit ed2ef30d22
23 changed files with 270 additions and 137 deletions
+11 -28
View File
@@ -4,28 +4,11 @@
// ============================================================================
// 配置区域 - 请根据实际情况修改
// ============================================================================
const CONFIG = {
apiKey: "2uxQzk7eD2GBCljdSmIiuU3ause7UK9n", // ⚠️ 建议使用 Search-Only API Key
server: {
host: "typesense.biss.click",
port: "443",
protocol: "https"
},
indexName: "blogs",
searchParams: {
query_by: "title,content",
highlight_full_fields: "title,content",
per_page: 8,
num_typos: 1,
typo_tokens_threshold: 1,
prefix: true
},
ui: {
maxRetries: 10,
retryDelay: 100,
animationDuration: 300
}
};
const CONFIG = window.SITE_CONFIG?.search;
if (!CONFIG) {
console.warn('Typesense 搜索配置未加载');
return;
}
// ============================================================================
// 状态管理
@@ -34,7 +17,7 @@
let isInitialized = false;
let isSearchOpen = false;
let initRetryCount = 0;
const MAX_INIT_RETRIES = 30; // 最多重试30次 (3秒)
const MAX_INIT_RETRIES = CONFIG.ui.maxInitRetries || 30; // 最多重试30次 (3秒)
// ============================================================================
// 错误提示函数
@@ -326,7 +309,7 @@
setTimeout(function() {
mask.style.display = 'none';
document.body.style.overflow = '';
}, CONFIG.ui.animationDuration);
}, CONFIG.ui.animationDuration || 300);
}
function focusSearchInput(retryCount) {
@@ -335,10 +318,10 @@
if (input) {
input.focus();
input.select();
} else if (retryCount < CONFIG.ui.maxRetries) {
} else if (retryCount < (CONFIG.ui.maxRetries || 10)) {
setTimeout(function() {
focusSearchInput(retryCount + 1);
}, CONFIG.ui.retryDelay);
}, CONFIG.ui.retryDelay || 100);
}
}
@@ -443,7 +426,7 @@
searchInstance.addWidgets([
instantsearch.widgets.searchBox({
container: '#searchbox',
placeholder: '输入关键词寻找故事...',
placeholder: CONFIG.ui.placeholder || '输入关键词寻找故事...',
autofocus: true,
showReset: true,
showSubmit: false,
@@ -542,4 +525,4 @@
getInstance: function() { return searchInstance; }
};
})();
})();
+8 -6
View File
@@ -1,6 +1,7 @@
function renderTalks() {
const talkContainer = document.querySelector('#talk');
if (!talkContainer) return;
const config = window.SITE_CONFIG?.talks || {};
talkContainer.innerHTML = '';
const generateIconSVG = () => {
return `<svg viewBox="0 0 512 512"xmlns="http://www.w3.org/2000/svg"class="is-badge icon"><path d="m512 268c0 17.9-4.3 34.5-12.9 49.7s-20.1 27.1-34.6 35.4c.4 2.7.6 6.9.6 12.6 0 27.1-9.1 50.1-27.1 69.1-18.1 19.1-39.9 28.6-65.4 28.6-11.4 0-22.3-2.1-32.6-6.3-8 16.4-19.5 29.6-34.6 39.7-15 10.2-31.5 15.2-49.4 15.2-18.3 0-34.9-4.9-49.7-14.9-14.9-9.9-26.3-23.2-34.3-40-10.3 4.2-21.1 6.3-32.6 6.3-25.5 0-47.4-9.5-65.7-28.6-18.3-19-27.4-42.1-27.4-69.1 0-3 .4-7.2 1.1-12.6-14.5-8.4-26-20.2-34.6-35.4-8.5-15.2-12.8-31.8-12.8-49.7 0-19 4.8-36.5 14.3-52.3s22.3-27.5 38.3-35.1c-4.2-11.4-6.3-22.9-6.3-34.3 0-27 9.1-50.1 27.4-69.1s40.2-28.6 65.7-28.6c11.4 0 22.3 2.1 32.6 6.3 8-16.4 19.5-29.6 34.6-39.7 15-10.1 31.5-15.2 49.4-15.2s34.4 5.1 49.4 15.1c15 10.1 26.6 23.3 34.6 39.7 10.3-4.2 21.1-6.3 32.6-6.3 25.5 0 47.3 9.5 65.4 28.6s27.1 42.1 27.1 69.1c0 12.6-1.9 24-5.7 34.3 16 7.6 28.8 19.3 38.3 35.1 9.5 15.9 14.3 33.4 14.3 52.4zm-266.9 77.1 105.7-158.3c2.7-4.2 3.5-8.8 2.6-13.7-1-4.9-3.5-8.8-7.7-11.4-4.2-2.7-8.8-3.6-13.7-2.9-5 .8-9 3.2-12 7.4l-93.1 140-42.9-42.8c-3.8-3.8-8.2-5.6-13.1-5.4-5 .2-9.3 2-13.1 5.4-3.4 3.4-5.1 7.7-5.1 12.9 0 5.1 1.7 9.4 5.1 12.9l58.9 58.9 2.9 2.3c3.4 2.3 6.9 3.4 10.3 3.4 6.7-.1 11.8-2.9 15.2-8.7z"fill="#1da1f2"></path></svg>`;
@@ -76,10 +77,11 @@ function renderTalks() {
};
const fetchAndRenderTalks = () => {
const url = 'https://mm.biss.click/api/echo/page';
const cacheKey = 'talksCache';
const cacheTimeKey = 'talksCacheTime';
const cacheDuration = 30 * 60 * 1000;
const url = config.apiUrl;
if (!url) return;
const cacheKey = config.cacheKey || 'talksCache';
const cacheTimeKey = config.cacheTimeKey || 'talksCacheTime';
const cacheDuration = config.cacheDurationMs || 30 * 60 * 1000;
const cachedData = localStorage.getItem(cacheKey);
const cachedTime = localStorage.getItem(cacheTimeKey);
const now = Date.now();
@@ -90,7 +92,7 @@ function renderTalks() {
fetch(url, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ page: 1, pageSize: 30 })
body: JSON.stringify({ page: 1, pageSize: config.pageSize || 30 })
})
.then(res => res.json())
.then(data => {
@@ -263,7 +265,7 @@ function renderTalks() {
return {
content,
user: item.username || '匿名',
avatar: 'https://free.picui.cn/free/2025/08/10/689845496a283.png',
avatar: config.fallbackAvatar || window.SITE_CONFIG?.author?.avatar || '',
date,
location: '',
tags: Array.isArray(item.tags) && item.tags.length ? item.tags.map(t => t.name) : ['无标签'],
+15 -9
View File
@@ -11,6 +11,10 @@ function indexTalk() {
}
if (!document.getElementById('bber-talk')) return;
const config = window.SITE_CONFIG?.talks || {};
const talksCacheKey = config.cacheKey || cacheKey;
const talksCacheTimeKey = config.cacheTimeKey || cacheTimeKey;
const talksCacheDuration = config.cacheDurationMs || cacheDuration;
function toText(ls) {
return ls.map(item => {
@@ -57,29 +61,31 @@ function indexTalk() {
}, 3000);
}
const cachedData = localStorage.getItem(cacheKey);
const cachedTime = localStorage.getItem(cacheTimeKey);
const cachedData = localStorage.getItem(talksCacheKey);
const cachedTime = localStorage.getItem(talksCacheTimeKey);
const currentTime = new Date().getTime();
// 判断缓存是否有效
if (cachedData && cachedTime && (currentTime - cachedTime < cacheDuration)) {
if (cachedData && cachedTime && (currentTime - cachedTime < talksCacheDuration)) {
const data = toText(JSON.parse(cachedData));
talk(data.slice(0, 6)); // 使用缓存渲染数据
talk(data.slice(0, config.homeLimit || 6)); // 使用缓存渲染数据
} else {
fetch('https://mm.biss.click/api/echo/page', {
if (!config.apiUrl) return;
fetch(config.apiUrl, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ page: 1, pageSize: 30 })
body: JSON.stringify({ page: 1, pageSize: config.pageSize || 30 })
})
.then(res => res.json())
.then(data => {
// 适配新版结构:code=1 且 data.items 存在
if (data.code === 1 && data.data && Array.isArray(data.data.items)) {
localStorage.setItem(cacheKey, JSON.stringify(data.data.items));
localStorage.setItem(cacheTimeKey, currentTime.toString());
localStorage.setItem(talksCacheKey, JSON.stringify(data.data.items));
localStorage.setItem(talksCacheTimeKey, currentTime.toString());
const formattedData = toText(data.data.items);
talk(formattedData.slice(0, 6));
talk(formattedData.slice(0, config.homeLimit || 6));
} else {
console.warn('Unexpected API response format:', data);
}