Greasy Fork

Greasy Fork is available in English.

Twitter/X (推特) t.co 直连 & 完整链接还原

将推特推文中的 t.co 链接替换为实际显示的完整 URL(如果可行),并阻止 t.co 跳转跟踪,让点击直接访问目标网站。脚本能够还原被 Twitter 隐藏的完整链接文本。

当前为 2025-12-03 提交的版本,查看 最新版本

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         Twitter/X (推特) t.co 直连 & 完整链接还原
// @name:en      Twitter/X t.co Direct Link & Full URL
// @namespace    http://tampermonkey.net/
// @version      1.0
// @description  将推特推文中的 t.co 链接替换为实际显示的完整 URL(如果可行),并阻止 t.co 跳转跟踪,让点击直接访问目标网站。脚本能够还原被 Twitter 隐藏的完整链接文本。
// @description:en Bypasses the t.co redirection on Twitter/X, replacing the shortened links with the actual full URLs. It also restores truncated links (e.g., "example.com/lon...") to their full original form by retrieving the hidden text from the DOM.
// @author       Yekyos
// @match        https://twitter.com/*
// @match        https://x.com/*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=twitter.com
// @license      MIT
// @grant        none
// @run-at       document-end
// ==/UserScript==

(function() {
    'use strict';

    // 核心处理函数
    function restoreLinks() {
        // 选择所有包含 t.co 的链接,且排除掉卡片类(带有图片的链接)
        const links = document.querySelectorAll('a[href*="t.co"]');

        links.forEach(link => {
            // 1. 检查是否已处理,或是否为非文本链接(如卡片、图片容器)
            if (link.dataset.resolved || link.querySelector('div, img, video')) return;

            // 2. 获取链接的完整文本
            // Twitter 将完整链接拆分为多个 span,部分 hidden,使用 textContent 可以获取所有文本
            let rawText = link.textContent.trim();

            // 3. 清洗文本:移除末尾的省略号 (unicode … 或 ...)
            let cleanUrl = rawText.replace(/…$/, '').replace(/\.\.\.$/, '');

            // 4. 验证是否为有效 URL (简单判断 http 开头)
            if (cleanUrl.startsWith('http')) {
                // 标记已处理
                link.dataset.resolved = "true";

                // --- 修改跳转地址 ---
                link.href = cleanUrl;

                // --- 修改显示文本 ---
                // 强制显示完整链接,并处理换行防止破坏布局
                link.innerText = cleanUrl;
                link.style.wordBreak = "break-all";
                link.style.textDecoration = "underline"; // 加上下划线表示已修改
                link.style.textDecorationColor = "rgba(29, 155, 240, 0.5)";
                link.title = `Original: ${cleanUrl}`;

                // --- 阻止 Twitter 劫持 ---
                // 阻止事件冒泡,防止 Twitter 路由接管点击
                link.addEventListener('click', (e) => {
                    e.stopPropagation();
                }, true);
            }
        });
    }

    // 监听 DOM 变化 (Twitter 是 SPA,内容动态加载)
    const observer = new MutationObserver((mutations) => {
        restoreLinks();
    });

    observer.observe(document.body, {
        childList: true,
        subtree: true
    });

    // 初始执行
    restoreLinks();
})();