• 本站招聘各个版块版主共同发展,有意可私聊站长

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

Telegram账号购买找【ITCRY】-顶级号商 https://itcry.com/

itcry

Member
10
消息得分
注册
2025-06-12
消息
1,305
反馈评分
6
// ==UserScript==
// @name Hostloc 有图比 替换为 有图比 并转为超链接
// @namespace
// @version 2.0
// @description 将 hostloc.com 页面中的 " ..." 纯文本转换为可点击的 有图比 链接
// @author You
// @match *://hostloc.com/*
// @match *://*.hostloc.com/*
// @icon
// @grant none
// ==/UserScript==

(function() {
'use strict';

const targetBase = " ";
const replacementBase = " ";

// 使用正则匹配完整链接,允许包含字母、数字及常见的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;

// 如果已经在输入框、代码块,或者本身就已经是 <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[0];
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一下看有没有问题
 
后退
顶部