🏠 Home 

Greasy Fork is available in English.

S1头像替换

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


安装此脚本?
  1. // ==UserScript==
  2. // @name S1头像替换
  3. // @namespace https://bbs.saraba1st.com
  4. // @version 1.2
  5. // @description 在 S1 论坛头像右上角显示按钮,点击后替换为 Dicebear 图像。
  6. // @author hexie
  7. // @match https://*.saraba1st.com/2b/*
  8. // @icon https://www.google.com/s2/favicons?sz=64&domain=saraba1st.com
  9. // @grant GM_setValue
  10. // @grant GM_getValue
  11. // @grant GM_deleteValue
  12. // @grant GM_registerMenuCommand
  13. // @license MIT
  14. // ==/UserScript==
  15. (function() {
  16. 'use strict';
  17. // 从 href 中提取 uid
  18. function extractUidFromHref(href) {
  19. const match = href.match(/uid-(\d+)\.html/);
  20. return match ? match[1] : null;
  21. }
  22. // 替换头像函数
  23. function replaceAvatar(uid) {
  24. const allAvatars = document.querySelectorAll(`img[data-uid="${uid}"]`);
  25. allAvatars.forEach((imgElement) => {
  26. const seed = uid; // 使用 uid 作为 seed
  27. const newSrc = `https://api.dicebear.com/9.x/thumbs/svg?seed=${seed}`;
  28. // 设置新的头像链接
  29. imgElement.src = newSrc;
  30. // 保存新头像到本地存储
  31. GM_setValue(uid, newSrc);
  32. });
  33. }
  34. // 恢复头像为默认的原始头像
  35. function resetAvatar(uid) {
  36. GM_deleteValue(uid);
  37. const allAvatars = document.querySelectorAll(`img[data-uid="${uid}"]`);
  38. allAvatars.forEach((imgElement) => {
  39. imgElement.src = imgElement.getAttribute('data-original-src'); // 恢复为原始头像
  40. });
  41. }
  42. function addReplaceButton(avatarElement, imgElement, uid) {
  43. const button = document.createElement('button');
  44. // 使用 Unicode 刷新符号作为按钮内容
  45. button.textContent = '㔢';
  46. button.style.position = 'absolute';
  47. button.style.top = '5px';
  48. button.style.right = '5px';
  49. button.style.zIndex = '1000';
  50. button.style.cursor = 'pointer';
  51. button.style.background = '#007bff'; // 蓝色背景
  52. button.style.color = '#ffffff'; // 白色字体
  53. button.style.border = 'none';
  54. button.style.borderRadius = '50%'; // 圆形按钮
  55. button.style.width = '20px';
  56. button.style.height = '20px';
  57. button.style.fontSize = '14px'; // 调整字体大小
  58. button.style.padding = '0'; // 移除多余的内边距
  59. button.style.display = 'none'; // 默认隐藏按钮
  60. button.style.boxShadow = '0 2px 5px rgba(0, 0, 0, 0.3)'; // 轻微阴影
  61. // 鼠标移到头像上时显示按钮,移开时隐藏按钮
  62. avatarElement.addEventListener('mouseover', function() {
  63. button.style.display = 'block';
  64. });
  65. avatarElement.addEventListener('mouseout', function() {
  66. button.style.display = 'none';
  67. });
  68. button.onclick = function(event) {
  69. event.preventDefault(); // 阻止打开链接
  70. event.stopPropagation(); // 阻止事件冒泡
  71. replaceAvatar(uid);
  72. };
  73. avatarElement.style.position = 'relative'; // 确保父元素的定位是相对的
  74. avatarElement.appendChild(button);
  75. }
  76. // 遍历所有头像并添加按钮
  77. document.querySelectorAll('div.avatar, div.icn.avt').forEach(function(avatarElement) {
  78. const imgElement = avatarElement.querySelector('img');
  79. const linkElement = avatarElement.querySelector('a');
  80. const uid = linkElement ? extractUidFromHref(linkElement.href) : null;
  81. if (imgElement && uid) {
  82. imgElement.style.visibility = 'hidden'; // 隐藏原始头像,避免闪烁
  83. imgElement.setAttribute('data-original-src', imgElement.src); // 存储原始头像链接
  84. imgElement.setAttribute('data-uid', uid); // 存储 UID 以便批量处理
  85. const savedSrc = GM_getValue(uid);
  86. if (savedSrc) {
  87. imgElement.src = savedSrc; // 如果有保存的头像链接,则使用
  88. }
  89. addReplaceButton(avatarElement, imgElement, uid);
  90. imgElement.style.visibility = 'visible'; // 完成替换后显示头像
  91. }
  92. });
  93. // 清除保存数据的功能
  94. GM_registerMenuCommand("清除所有保存的头像数据", function() {
  95. document.querySelectorAll('div.avatar img').forEach(function(imgElement) {
  96. const linkElement = imgElement.closest('div.avatar').querySelector('a.avtm');
  97. const uid = linkElement ? extractUidFromHref(linkElement.href) : null;
  98. if (uid) {
  99. resetAvatar(uid); // 恢复为原始头像
  100. }
  101. });
  102. alert("所有保存的头像数据已清除。");
  103. });
  104. })();