🏠 Home 

S1头像替换

在 S1 论坛头像右上角显示按钮,点击后替换为 Dicebear 图像。


Install this script?
// ==UserScript==
// @name         S1头像替换
// @namespace    https://bbs.saraba1st.com
// @version      1.2
// @description  在 S1 论坛头像右上角显示按钮,点击后替换为 Dicebear 图像。
// @author       hexie
// @match        https://*.saraba1st.com/2b/*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=saraba1st.com
// @grant        GM_setValue
// @grant        GM_getValue
// @grant        GM_deleteValue
// @grant        GM_registerMenuCommand
// @license      MIT
// ==/UserScript==
(function() {
'use strict';
// 从 href 中提取 uid
function extractUidFromHref(href) {
const match = href.match(/uid-(\d+)\.html/);
return match ? match[1] : null;
}
// 替换头像函数
function replaceAvatar(uid) {
const allAvatars = document.querySelectorAll(`img[data-uid="${uid}"]`);
allAvatars.forEach((imgElement) => {
const seed = uid; // 使用 uid 作为 seed
const newSrc = `https://api.dicebear.com/9.x/thumbs/svg?seed=${seed}`;
// 设置新的头像链接
imgElement.src = newSrc;
// 保存新头像到本地存储
GM_setValue(uid, newSrc);
});
}
// 恢复头像为默认的原始头像
function resetAvatar(uid) {
GM_deleteValue(uid);
const allAvatars = document.querySelectorAll(`img[data-uid="${uid}"]`);
allAvatars.forEach((imgElement) => {
imgElement.src = imgElement.getAttribute('data-original-src'); // 恢复为原始头像
});
}
function addReplaceButton(avatarElement, imgElement, uid) {
const button = document.createElement('button');
// 使用 Unicode 刷新符号作为按钮内容
button.textContent = '㔢';
button.style.position = 'absolute';
button.style.top = '5px';
button.style.right = '5px';
button.style.zIndex = '1000';
button.style.cursor = 'pointer';
button.style.background = '#007bff'; // 蓝色背景
button.style.color = '#ffffff'; // 白色字体
button.style.border = 'none';
button.style.borderRadius = '50%'; // 圆形按钮
button.style.width = '20px';
button.style.height = '20px';
button.style.fontSize = '14px'; // 调整字体大小
button.style.padding = '0'; // 移除多余的内边距
button.style.display = 'none'; // 默认隐藏按钮
button.style.boxShadow = '0 2px 5px rgba(0, 0, 0, 0.3)'; // 轻微阴影
// 鼠标移到头像上时显示按钮,移开时隐藏按钮
avatarElement.addEventListener('mouseover', function() {
button.style.display = 'block';
});
avatarElement.addEventListener('mouseout', function() {
button.style.display = 'none';
});
button.onclick = function(event) {
event.preventDefault(); // 阻止打开链接
event.stopPropagation(); // 阻止事件冒泡
replaceAvatar(uid);
};
avatarElement.style.position = 'relative'; // 确保父元素的定位是相对的
avatarElement.appendChild(button);
}
// 遍历所有头像并添加按钮
document.querySelectorAll('div.avatar, div.icn.avt').forEach(function(avatarElement) {
const imgElement = avatarElement.querySelector('img');
const linkElement = avatarElement.querySelector('a');
const uid = linkElement ? extractUidFromHref(linkElement.href) : null;
if (imgElement && uid) {
imgElement.style.visibility = 'hidden'; // 隐藏原始头像,避免闪烁
imgElement.setAttribute('data-original-src', imgElement.src); // 存储原始头像链接
imgElement.setAttribute('data-uid', uid); // 存储 UID 以便批量处理
const savedSrc = GM_getValue(uid);
if (savedSrc) {
imgElement.src = savedSrc; // 如果有保存的头像链接,则使用
}
addReplaceButton(avatarElement, imgElement, uid);
imgElement.style.visibility = 'visible'; // 完成替换后显示头像
}
});
// 清除保存数据的功能
GM_registerMenuCommand("清除所有保存的头像数据", function() {
document.querySelectorAll('div.avatar img').forEach(function(imgElement) {
const linkElement = imgElement.closest('div.avatar').querySelector('a.avtm');
const uid = linkElement ? extractUidFromHref(linkElement.href) : null;
if (uid) {
resetAvatar(uid); // 恢复为原始头像
}
});
alert("所有保存的头像数据已清除。");
});
})();