SpaceX 发表于 3 天前

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

// ==UserScript==
// @name         Hostloc 有图比 替换为 有图比 并转为超链接
// @namespace    http://tampermonkey.net/
// @version      2.0
// @description将 hostloc.567899.xyz 页面中的 "https://www.有图比.com/..." 纯文本转换为可点击的 有图比 链接
// @author       You
// @match      *://hostloc.567899.xyz/*
// @match      *://*.hostloc.567899.xyz/*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=hostloc.567899.xyz
// @grant      none
// ==/UserScript==

(function() {
    'use strict';

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

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

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

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

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

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

                  fragment.appendChild(a);
                  lastIndex = urlRegex.lastIndex;
                }

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

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

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

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

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

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

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

})();



tampermonkey新建脚本,全选删除,然后把以上整个代码贴进去保存即可,不放心可以让gpt review一下看有没有问题

雨后天霁 发表于 3 天前

在这个ai大爆炸的时代,依然有老手艺人坚持手搓代码

rqp 发表于 前天 10:43

手搓代码厉害

QQ云 发表于 前天 12:26

本帖最后由 QQ云 于 2026-5-5 12:31 编辑

元信息格式错误:
你的 // @name、// @version 这些字段被多余的 标签包裹了,油猴完全无法解析。
报错,豆包修改了一下

卖百度众测礼券 发表于 前天 13:46

我记得以前很多年前,就有人干过这个事情。。。。。。。。。
页: [1]
查看完整版本: 简单写了个油猴脚本,可以自动识别有图比链接并自动转换