• SpaceX

    [vip]// ==UserScript==
    // @name Hostloc 有图比 替换为 有图比 并转为超链接
    // @namespace http://tampermonkey.net/
    // @version 2.0
    // @description 将 hostloc.com 页面中的 "https://www.有图比.com/..." 纯文本转换为可点击的 有图比 链接
    // @author You
    // @match ://hostloc.com/
    // @match ://.hostloc.com/*
    // @icon https://www.google.com/s2/favicons?sz=64&domain=hostloc.com
    // @grant none
    // ==/UserScript==
    (function() {
    'use strict';
    const targetBase = "https://www.有图比.com/";
    const replacementBase = "https://www.有图比.com/";

    // 使用正则匹配完整链接,允许包含字母、数字及常见的URL符号,遇到空格或中文字符时自动停止
    const urlRegex = /(https:\/\/www\.有图比\.com\/[A-Za-z0-9\-\._~:/?#\[\]@!$&'()+,;=%])/g;
    function replace有图比Link(node) {
    // 1. 处理纯文本节点
    if (node.nodeType === Node.TEXT_NODE) {
    let parent = node.parentNode;

    // 如果已经在输入框、代码块,或者本身就已经是 标签里了,只需替换文本,不套娃生成新链接
    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[0];
    let newUrl = originalUrl.replace(targetBase, replacementBase);
    // 创建 标签,使其变成可点击链接
    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 标签的内容替换原本的纯文本节点
    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
    });
    })();[/vip]tampermonkey新建脚本,全选删除,然后把以上整个代码贴进去保存即可,不放心可以让gpt review一下看有没有问题

    主楼
  • 雨后天霁

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

    #1
  • rqp

    手搓代码厉害

    #2
  • QQ云

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

    Hostloc 有图比替换为 有图比 并自动转超链接-2.0.user.zip

    #3
  • 卖百度众测礼券

    我记得以前很多年前,就有人干过这个事情。。。。。。。。。

    #4

发表回复

登录后回复