fixed bug 若干
This commit is contained in:
1 parent
e59a772985
commit
830c2d5947
10 files changed
+324
-58
No files matched your search
+114
-20
@@ -104,10 +104,10 @@ function initArtalkAndGetComments() {
|
||||
|
||||
Artalk.init({
|
||||
el: '#Comments', // 绑定元素的 Selector
|
||||
server: 'https://artalk.usj.cc', // 后端地址
|
||||
site: '优世界', // 你的站点名
|
||||
server: window.siteConfig.artalk.server, // 后端地址
|
||||
site: window.siteConfig.artalk.site, // 你的站点名
|
||||
flatMode: true,
|
||||
emoticons: '/emotion/OwO.json'
|
||||
emoticons: window.siteConfig.artalk.emoticons
|
||||
});
|
||||
});
|
||||
}
|
||||
@@ -115,35 +115,63 @@ function initArtalkAndGetComments() {
|
||||
// 异步创建弹幕
|
||||
async function createBarragerFromArtalk(commentsData) {
|
||||
const looperTime = 1000; // 每条弹幕的发送间隔,单位毫秒
|
||||
|
||||
// 1. 处理评论数据(扁平化、乱序)
|
||||
const flattenedComments = flattenComments(commentsData);
|
||||
const totalComments = flattenedComments.length;
|
||||
let currentIndex = 0;
|
||||
shuffleArray(flattenedComments); // 随机打乱评论顺序
|
||||
const selectedComments = flattenedComments; // 使用所有评论
|
||||
|
||||
// 2. 构建弹幕列表
|
||||
let barrageList = selectedComments.map(comment => ({
|
||||
type: 'comment',
|
||||
content: stripHTML(comment.content_marked) || 'No comment',
|
||||
// 修复头像模糊:请求 200px 的图片 (s=200)
|
||||
headImg: window.siteConfig.artalk.gravatar + comment.email_encrypted + "?s=200" || 'static/default-avatar.png',
|
||||
href: comment.link || '#',
|
||||
speed: 35
|
||||
}));
|
||||
|
||||
// 3. 尝试获取在线一言并插入
|
||||
if (window.siteConfig.barrage && window.siteConfig.barrage.enableHitokoto) {
|
||||
try {
|
||||
const hitokoto = await fetchHitokoto();
|
||||
if (hitokoto) {
|
||||
const insertIndex = Math.floor(Math.random() * (barrageList.length + 1));
|
||||
barrageList.splice(insertIndex, 0, {
|
||||
type: 'hitokoto',
|
||||
content: `『一言』${hitokoto}`,
|
||||
headImg: window.siteConfig.author.avatar,
|
||||
href: 'https://hitokoto.cn',
|
||||
speed: 35
|
||||
});
|
||||
}
|
||||
} catch (e) {
|
||||
console.error('获取一言失败:', e);
|
||||
}
|
||||
}
|
||||
|
||||
// 初始化弹幕插件
|
||||
let barrage = new Barrage.default({
|
||||
el: 'body',
|
||||
backColor: "#000000b5",
|
||||
fontSize: "14px",
|
||||
fontSize: "13px", // 字体稍微改小一点点以适配小头像
|
||||
isLeft: true,
|
||||
speed: 0,
|
||||
height: "33px"
|
||||
height: "30px" // 整体高度稍微减小
|
||||
});
|
||||
|
||||
if (totalComments !== 0) {
|
||||
while (currentIndex < totalComments) {
|
||||
const currentComment = flattenedComments[currentIndex];
|
||||
const barragerItem = {
|
||||
headImg: "https://cravatar.cn/avatar/"+currentComment.email_encrypted + "?s=33" || 'static/default-avatar.png',
|
||||
content: stripHTML(currentComment.content_marked) || 'No comment',
|
||||
href: currentComment.link || '#',
|
||||
speed: 10
|
||||
};
|
||||
const totalItems = barrageList.length;
|
||||
let currentIndex = 0;
|
||||
|
||||
if (totalItems !== 0) {
|
||||
while (currentIndex < totalItems) {
|
||||
const item = barrageList[currentIndex];
|
||||
|
||||
// 发送弹幕
|
||||
barrage.start(barragerItem.content, {
|
||||
speed: barragerItem.speed,
|
||||
headImg: barragerItem.headImg,
|
||||
speed: 35
|
||||
barrage.start(item.content, {
|
||||
speed: item.speed,
|
||||
headImg: item.headImg,
|
||||
href: item.href
|
||||
});
|
||||
|
||||
currentIndex++;
|
||||
@@ -152,6 +180,26 @@ async function createBarragerFromArtalk(commentsData) {
|
||||
}
|
||||
}
|
||||
|
||||
// 数组乱序算法 (Fisher-Yates Shuffle)
|
||||
function shuffleArray(array) {
|
||||
for (let i = array.length - 1; i > 0; i--) {
|
||||
const j = Math.floor(Math.random() * (i + 1));
|
||||
[array[i], array[j]] = [array[j], array[i]];
|
||||
}
|
||||
}
|
||||
|
||||
// 获取一言
|
||||
async function fetchHitokoto() {
|
||||
const url = window.siteConfig.barrage.hitokotoUrl || 'https://v1.hitokoto.cn?c=a&c=b&c=c&c=d&c=i';
|
||||
try {
|
||||
const response = await fetch(url);
|
||||
const data = await response.json();
|
||||
return data.hitokoto;
|
||||
} catch (error) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
// 延迟函数,返回一个 Promise,用于等待指定的毫秒数
|
||||
function delay(ms) {
|
||||
return new Promise(resolve => setTimeout(resolve, ms));
|
||||
@@ -276,4 +324,50 @@ function initCodeCopy() {
|
||||
// 页面加载完成后初始化
|
||||
document.addEventListener('DOMContentLoaded', function() {
|
||||
initCodeCopy();
|
||||
initDescriptionHitokoto();
|
||||
});
|
||||
|
||||
// 初始化个人描述一言切换
|
||||
function initDescriptionHitokoto() {
|
||||
const descEl = document.querySelector('.yiyandes');
|
||||
if (!descEl) return;
|
||||
|
||||
descEl.title = '点击切换一言';
|
||||
|
||||
descEl.addEventListener('click', async () => {
|
||||
if (descEl.dataset.loading === 'true') return;
|
||||
|
||||
descEl.dataset.loading = 'true';
|
||||
const originalText = descEl.innerText;
|
||||
descEl.innerText = '正在获取灵感...';
|
||||
|
||||
const hitokoto = await fetchHitokoto();
|
||||
|
||||
if (hitokoto) {
|
||||
typeWriter(descEl, hitokoto);
|
||||
} else {
|
||||
descEl.innerText = originalText;
|
||||
descEl.dataset.loading = 'false';
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// 打字机效果
|
||||
function typeWriter(element, text, speed = 100) {
|
||||
element.innerText = '';
|
||||
element.classList.add('typing');
|
||||
let i = 0;
|
||||
|
||||
function type() {
|
||||
if (i < text.length) {
|
||||
element.innerText += text.charAt(i);
|
||||
i++;
|
||||
setTimeout(type, speed);
|
||||
} else {
|
||||
element.dataset.loading = 'false';
|
||||
element.classList.remove('typing');
|
||||
}
|
||||
}
|
||||
|
||||
type();
|
||||
}
|
||||
Reference in new issue
Block a user