Greasy Fork

修复裂开的小尾巴

很多佬友在签名栏设置了自己的博客链接,或者是自己的座右铭。但是自己没开启签名展示没意识到不能正常显示,让我们一起来修复它们吧

目前为 2024-03-16 提交的版本。查看 最新版本

// ==UserScript==
// @name         修复裂开的小尾巴
// @namespace    http://tampermonkey.net/
// @version      2024-03-16 11:30
// @description  很多佬友在签名栏设置了自己的博客链接,或者是自己的座右铭。但是自己没开启签名展示没意识到不能正常显示,让我们一起来修复它们吧
// @author       ygmjjdev
// @match        https://linux.do/*
// @icon         https://linux.do/uploads/default/optimized/1X/3a18b4b0da3e8cf96f7eea15241c3d251f28a39b_2_180x180.png
// @grant        none
// ==/UserScript==

(function () {
  "use strict";

  function replaceImgToP(img) {
    var p = document.createElement("p");

    var imgSrc = img.getAttribute("src");
    console.log("imgSrc:     " + imgSrc);
    if (imgSrc?.startsWith("http")) {
      var a = document.createElement("a");
      a.href = imgSrc;
      a.textContent = imgSrc;
      p.appendChild(a);
    } else {
      p.innerText = imgSrc;
    }

    img.parentNode.insertBefore(p, img.nextSibling);
    img.parentNode.removeChild(img);
  }

  // 定义处理函数
  function handleImgError(img) {
    if (img.complete && img.naturalWidth === 0 && img.naturalHeight === 0) {
      setTimeout(() => {
        replaceImgToP(img);
      }, 300);
    }
    img.onerror = function () {
      setTimeout(() => {
        replaceImgToP(img);
      }, 300);
    };
  }

  // 监听 DOM 变化
  var observer = new MutationObserver(function (mutations) {
    mutations.forEach(function (mutation) {
      if (mutation.type === "childList") {
        mutation.addedNodes.forEach(function (node) {
          if (node.nodeType === 1 && node.classList.contains("signature-img")) {
            handleImgError(node);
          }
        });
      }
    });
  });

  // 配置观察选项
  var config = { attributes: true, childList: true, subtree: true };

  // 开始观察
  observer.observe(document.body, config);

  // 为已存在的 img 元素添加 onError 事件处理函数

  document.querySelectorAll(".signature-img").forEach(function (img) {
    handleImgError(img);
  });
})();