添加按钮 HuggingFace 主站跳转到镜像站

Automatically re-add the stylish redirect button on HuggingFace pages using MutationObserver

  1. // ==UserScript==
  2. // @name 添加按钮 HuggingFace 主站跳转到镜像站
  3. // @namespace http://tampermonkey.net/
  4. // @version 1.0
  5. // @description Automatically re-add the stylish redirect button on HuggingFace pages using MutationObserver
  6. // @author nobody
  7. // @match *://huggingface.co/*
  8. // @grant none
  9. // @run-at document-end
  10. // @license GPL-3.0 License
  11. // ==/UserScript==
  12.  
  13. (function() {
  14. 'use strict';
  15.  
  16. function addButtonIfNeeded() {
  17. // 检查按钮是否已经存在
  18. if (document.getElementById('mirror-redirect-button')) return;
  19.  
  20. // 找到放置按钮的合适位置,这个选择器可能需要根据页面的实际结构进行调整
  21. const targetElement = document.querySelector('.container.relative h1');
  22. if (!targetElement) return;
  23.  
  24. // 创建按钮并设置样式
  25. const button = document.createElement('button');
  26. button.textContent = '跳转往镜像站🚀';
  27. button.id = 'mirror-redirect-button';
  28. // 应用样式,可以是自定义的,也可以尝试复用页面上已有的样式类
  29. button.className = 'btn cursor-pointer text-sm flex-auto sm:flex-none'; // 假设这是页面上已有的样式类
  30. button.style.marginLeft = '10px'; // 可选的,根据需要调整样式
  31. button.onclick = function() {
  32. window.location.href = window.location.href.replace('huggingface.co', 'hf-mirror.com');
  33. };
  34.  
  35. // 将按钮添加到页面上
  36. targetElement.appendChild(button);
  37. }
  38.  
  39. // 创建MutationObserver实例来监听DOM变化
  40. const observer = new MutationObserver((mutations) => {
  41. mutations.forEach((mutation) => {
  42. if (mutation.addedNodes.length) {
  43. addButtonIfNeeded();
  44. }
  45. });
  46. });
  47.  
  48. // 配置观察器选项:子节点的变动
  49. const config = { childList: true, subtree: true };
  50.  
  51. // 开始监听document.body的变化
  52. observer.observe(document.body, config);
  53.  
  54. // 页面初次加载时尝试添加按钮
  55. addButtonIfNeeded();
  56. })();

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址