全球主机交流论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

CeraNetworks网络延迟测速工具IP归属甄别会员请立即修改密码
查看: 491|回复: 1
打印 上一主题 下一主题

简单写了个油猴脚本,可以自动识别有图比链接并自动转换

[复制链接]
跳转到指定楼层
1#
发表于 昨天 21:25 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
  1. // ==UserScript==
  2. // [url=home.php?mod=space&uid=5839]@name[/url]         Hostloc 有图比 替换为 有图比 并转为超链接
  3. // @namespace    http://tampermonkey.net/
  4. // [url=home.php?mod=space&uid=73703]@version[/url]      2.0
  5. // @description  将 loc.qiche.eu.org 页面中的 "https://www.有图比.com/..." 纯文本转换为可点击的 有图比 链接
  6. // @author       You
  7. // @match        *://loc.qiche.eu.org/*
  8. // @match        *://*.loc.qiche.eu.org/*
  9. // @icon         https://www.google.com/s2/favicons?sz=64&domain=loc.qiche.eu.org
  10. // @grant        none
  11. // ==/UserScript==

  12. (function() {
  13.     'use strict';

  14.     const targetBase = "https://www.有图比.com/";
  15.     const replacementBase = "https://www.有图比.com/";
  16.    
  17.     // 使用正则匹配完整链接,允许包含字母、数字及常见的URL符号,遇到空格或中文字符时自动停止
  18.     const urlRegex = /(https:\/\/www\.有图比\.com\/[A-Za-z0-9\-\._~:/?#\[\]@!$&'()*+,;=%]*)/g;

  19.     function replace有图比Link(node) {
  20.         // 1. 处理纯文本节点
  21.         if (node.nodeType === Node.TEXT_NODE) {
  22.             let parent = node.parentNode;
  23.             
  24.             // 如果已经在输入框、代码块,或者本身就已经是 <a> 标签里了,只需替换文本,不套娃生成新链接
  25.             if (parent && ['A', 'TEXTAREA', 'CODE', 'PRE', 'SCRIPT', 'STYLE'].includes(parent.tagName)) {
  26.                 if (node.nodeValue.includes(targetBase)) {
  27.                     node.nodeValue = node.nodeValue.replaceAll(targetBase, replacementBase);
  28.                 }
  29.                 return;
  30.             }

  31.             // 如果是普通的纯文本,且匹配到了有图比的链接格式
  32.             if (urlRegex.test(node.nodeValue)) {
  33.                 urlRegex.lastIndex = 0; // 重置正则索引
  34.                 let fragment = document.createDocumentFragment();
  35.                 let lastIndex = 0;
  36.                 let match;

  37.                 while ((match = urlRegex.exec(node.nodeValue)) !== null) {
  38.                     // 截取链接前方的普通文本
  39.                     if (match.index > lastIndex) {
  40.                         fragment.appendChild(document.createTextNode(node.nodeValue.substring(lastIndex, match.index)));
  41.                     }

  42.                     // 获取匹配到的原始完整链接,并替换域名
  43.                     let originalUrl = match[0];
  44.                     let newUrl = originalUrl.replace(targetBase, replacementBase);

  45.                     // 创建 <a> 标签,使其变成可点击链接
  46.                     let a = document.createElement('a');
  47.                     a.href = newUrl;
  48.                     a.textContent = newUrl;
  49.                     a.target = "_blank"; // 在新标签页打开
  50.                     // 可选:加个下划线和颜色,让它看起来更像个链接
  51.                     a.style.color = "#1E90FF";
  52.                     a.style.textDecoration = "underline";

  53.                     fragment.appendChild(a);
  54.                     lastIndex = urlRegex.lastIndex;
  55.                 }

  56.                 // 补齐最后一个链接后面的剩余文本
  57.                 if (lastIndex < node.nodeValue.length) {
  58.                     fragment.appendChild(document.createTextNode(node.nodeValue.substring(lastIndex)));
  59.                 }

  60.                 // 用拼装好的包含 <a> 标签的内容替换原本的纯文本节点
  61.                 if (parent) {
  62.                     parent.replaceChild(fragment, node);
  63.                 }
  64.             }
  65.         }
  66.         // 2. 处理元素节点
  67.         else if (node.nodeType === Node.ELEMENT_NODE) {
  68.             // 跳过一些不该处理的标签,防止破坏网页原有的代码逻辑或排版
  69.             if (['SCRIPT', 'STYLE', 'TEXTAREA', 'CODE', 'PRE'].includes(node.tagName)) return;

  70.             // 处理已经是超链接的标签,防止其 href 跳转地址还是旧的
  71.             if (node.tagName === 'A' && node.hasAttribute('href')) {
  72.                 let href = node.getAttribute('href');
  73.                 if (href.includes(targetBase)) {
  74.                     node.setAttribute('href', href.replaceAll(targetBase, replacementBase));
  75.                 }
  76.             }

  77.             // 递归检查所有子节点 (使用 Array.from 是为了防止下面动态替换文本节点时打乱遍历顺序)
  78.             Array.from(node.childNodes).forEach(replace有图比Link);
  79.         }
  80.     }

  81.     // 初始化:对当前已加载的整个 body 进行一次扫描替换
  82.     replace有图比Link(document.body);

  83.     // 监听器:处理动态加载的内容(如无刷新翻页、展开评论等)
  84.     const observer = new MutationObserver((mutations) => {
  85.         mutations.forEach((mutation) => {
  86.             mutation.addedNodes.forEach((addedNode) => {
  87.                 replace有图比Link(addedNode);
  88.             });
  89.         });
  90.     });

  91.     // 开启监听,观察 body 下所有子节点的变化
  92.     observer.observe(document.body, {
  93.         childList: true,
  94.         subtree: true
  95.     });

  96. })();
复制代码




tampermonkey新建脚本,全选删除,然后把以上整个代码贴进去保存即可,不放心可以让gpt review一下看有没有问题
2#
发表于 昨天 21:30 | 只看该作者
在这个ai大爆炸的时代,依然有老手艺人坚持手搓代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则

Archiver|手机版|小黑屋|全球主机交流论坛

GMT+8, 2026-5-5 02:33 , Processed in 0.057425 second(s), 6 queries , Gzip On, MemCache On.

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表