This commit is contained in:
XIGE
2026-02-23 21:09:17 +08:00
parent f696e38988
commit bdc98b1844
4 changed files with 1328 additions and 0 deletions

150
assets/who-read-this.js Normal file
View File

@@ -0,0 +1,150 @@
/**
* WhoReadThis 插件专用脚本 - 独立文件避免冲突
* 版本: 1.0.6
*/
(function() {
'use strict';
// 等待DOM加载完成
function initWhoReadThis() {
const containers = document.querySelectorAll('.whoreadthis-container');
containers.forEach(container => {
if (container.dataset.whoReadThisInitialized === 'true') {
return;
}
container.dataset.whoReadThisInitialized = 'true';
const header = container.querySelector('.whoreadthis-header');
const itemsWrapper = container.querySelector('.whoreadthis-items-wrapper');
const toggleBtn = container.querySelector('.whoreadthis-toggle-btn');
// 设置头像大小
const avatarSize = container.getAttribute('data-avatar-size');
if (avatarSize && avatarSize !== '40') {
const avatars = container.querySelectorAll('.whoreadthis-avatar');
const avatarImgs = container.querySelectorAll('.whoreadthis-avatar img');
avatars.forEach(avatar => {
avatar.style.width = avatarSize + 'px';
avatar.style.height = avatarSize + 'px';
});
avatarImgs.forEach(img => {
img.style.width = avatarSize + 'px';
img.style.height = avatarSize + 'px';
});
}
if (!header || !itemsWrapper || !toggleBtn) {
return;
}
// 绑定标题点击事件
header.addEventListener('click', function(e) {
if (e.target.closest('.whoreadthis-toggle-btn')) {
return;
}
toggleWhoReadThis(container);
});
// 绑定按钮点击事件
toggleBtn.addEventListener('click', function(e) {
e.stopPropagation();
toggleWhoReadThis(container);
});
// 处理头像链接点击
const avatarWrappers = container.querySelectorAll('.whoreadthis-avatar-wrapper');
avatarWrappers.forEach(wrapper => {
const link = wrapper.getAttribute('href');
if (link && link !== '#') {
wrapper.addEventListener('click', function(e) {
e.stopPropagation();
window.open(link, '_blank');
});
}
});
// 锚点跳转支持
if (window.location.hash === '#whoreadthis' || window.location.hash === '#whoreadthis-' + container.id.split('-')[1]) {
setTimeout(function() {
container.scrollIntoView({ behavior: 'smooth' });
expandWhoReadThis(container);
}, 300);
}
});
}
function toggleWhoReadThis(container) {
const itemsWrapper = container.querySelector('.whoreadthis-items-wrapper');
const toggleBtn = container.querySelector('.whoreadthis-toggle-btn');
if (!itemsWrapper || !toggleBtn) {
return;
}
if (itemsWrapper.classList.contains('collapsed')) {
expandWhoReadThis(container);
} else {
collapseWhoReadThis(container);
}
}
function expandWhoReadThis(container) {
const itemsWrapper = container.querySelector('.whoreadthis-items-wrapper');
const toggleBtn = container.querySelector('.whoreadthis-toggle-btn');
if (itemsWrapper && toggleBtn) {
itemsWrapper.className = 'whoreadthis-items-wrapper expanded';
toggleBtn.className = 'whoreadthis-toggle-btn expanded';
toggleBtn.querySelector('.toggle-arrow').textContent = '↑';
}
}
function collapseWhoReadThis(container) {
const itemsWrapper = container.querySelector('.whoreadthis-items-wrapper');
const toggleBtn = container.querySelector('.whoreadthis-toggle-btn');
if (itemsWrapper && toggleBtn) {
itemsWrapper.className = 'whoreadthis-items-wrapper collapsed';
toggleBtn.className = 'whoreadthis-toggle-btn collapsed';
toggleBtn.querySelector('.toggle-arrow').textContent = '↓';
}
}
// 页面加载完成后初始化
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', function() {
setTimeout(initWhoReadThis, 100);
});
} else {
setTimeout(initWhoReadThis, 100);
}
// 暴露公共API
window.WhoReadThis = {
init: initWhoReadThis,
expand: function(containerId) {
const container = document.getElementById(containerId);
if (container) {
expandWhoReadThis(container);
}
},
collapse: function(containerId) {
const container = document.getElementById(containerId);
if (container) {
collapseWhoReadThis(container);
}
},
toggle: function(containerId) {
const container = document.getElementById(containerId);
if (container) {
toggleWhoReadThis(container);
}
}
};
})();