// ==UserScript== // @name UC网盘免客户端下载 // @namespace http://tampermonkey.net/ // @version 250911 // @description UC网盘分享链接免客户端下载,例外谷歌网盘域名 // @author Magiclyan // @copyright M // @license GPL-3.0 License // @match *://*/* // @grant none // ==/UserScript== (function() { 'use strict'; // 排除Google Drive的正则表达式 const googleDriveRegex = /https?:\/\/(www\.)?drive\.google\.com/gi; // 匹配其他drive相关域名的正则表达式 const driveRegex = /https?:\/\/(www\.)?drive\.(?!google\.com)/gi; const driveDomainRegex = /^https?:\/\/(www\.)?drive\.(?!google\.com)/i; // 检查是否为Google Drive链接 function isGoogleDrive(url) { return googleDriveRegex.test(url); } // 处理当前页面URL,如果是drive域名(非Google)则替换为fast并刷新 function handleCurrentPageUrl() { // 如果是Google Drive则不处理 if (isGoogleDrive(window.location.href)) { return; } if (driveDomainRegex.test(window.location.href)) { const newUrl = window.location.href.replace(driveRegex, 'https://fast.'); // 避免无限循环刷新 if (newUrl !== window.location.href) { window.location.href = newUrl; } } } // 替换页面中所有链接的函数(排除Google Drive) function replaceLinks() { // 替换普通链接 const links = document.getElementsByTagName('a'); for (let link of links) { // 跳过Google Drive链接 if (link.href && isGoogleDrive(link.href)) { continue; } if (link.href && driveRegex.test(link.href)) { link.href = link.href.replace(driveRegex, 'https://fast.'); // 添加视觉提示,标记已替换的链接 link.classList.add('uc-drive-replaced'); link.style.color = '#2196F3'; } } // 替换iframe中的链接(如果有访问权限) const iframes = document.getElementsByTagName('iframe'); for (let iframe of iframes) { try { const iframeDoc = iframe.contentDocument || iframe.contentWindow.document; const iframeLinks = iframeDoc.getElementsByTagName('a'); for (let link of iframeLinks) { // 跳过Google Drive链接 if (link.href && isGoogleDrive(link.href)) { continue; } if (link.href && driveRegex.test(link.href)) { link.href = link.href.replace(driveRegex, 'https://fast.'); } } } catch (e) { // 跨域iframe无法访问,忽略错误 continue; } } // 处理文本中的链接(仅替换纯文本节点,排除Google Drive) function replaceInTextNodes(node) { if (node.nodeType === Node.TEXT_NODE) { // 跳过包含Google Drive的文本 if (googleDriveRegex.test(node.textContent)) { return; } if (driveRegex.test(node.textContent)) { node.textContent = node.textContent.replace(driveRegex, 'https://fast.'); } } else if (node.nodeType === Node.ELEMENT_NODE && !['SCRIPT', 'STYLE', 'NOSCRIPT', 'TEXTAREA'].includes(node.tagName)) { // 跳过可能影响功能的标签 for (let child of node.childNodes) { replaceInTextNodes(child); } } } replaceInTextNodes(document.body); } // 初始化执行 handleCurrentPageUrl(); replaceLinks(); // 页面加载完成后再次执行 window.addEventListener('load', replaceLinks); // 优化动态内容监听,减少性能消耗 let observer = new MutationObserver((mutations) => { // 检查是否有实际需要处理的变化 const needUpdate = mutations.some(mutation => mutation.type === 'childList' || (mutation.type === 'characterData' && driveRegex.test(mutation.target.textContent) && !googleDriveRegex.test(mutation.target.textContent)) ); if (needUpdate) { clearTimeout(window.replaceLinksTimeout); window.replaceLinksTimeout = setTimeout(replaceLinks, 300); } }); // 观察文档变化,限制观察范围以提高性能 observer.observe(document.body, { childList: true, subtree: true, characterData: true, attributes: true, attributeFilter: ['href'] // 只观察href属性变化 }); // 清理函数 window.addEventListener('unload', () => { observer.disconnect(); clearTimeout(window.replaceLinksTimeout); }); })();