修復 x.com(twitter)分享鏈接在Discord 或 Telegram 無法正確顯示 嵌入資訊的問題


首先會遇到這問題,主要是伺服器閱讀權限不足,所以無法正確讀取到限制讀取的文章

在 github 上有大佬修復使用了Twitter API 並把訊息資料整合到了他的網域上修復了此問題。

所以我們只要把 x 或是 twitter 的域名替換為大佬的就可以讓資訊正確顯示在Discord 或是Telegram

也就是只要把 x.com 或是 twitter.com 換成 fixvx.com 就好了。

相信大家也不會去記所以我做了個 暴力猴的腳本

==========分隔線==========



// ==UserScript==
// @name         把推特的文章時間改成複製fixvx網址
// @namespace    http://tampermonkey.net/
// @version      0.2
// @description  修改鏈接的 href 並實現點擊後複製功能
// @author       You
// @match        https://x.com/*
// @match        https://twitter.com/*
// @grant        GM_setClipboard
// @grant        GM_addStyle
// ==/UserScript==

(function() {
    'use strict';

    // 目標域名
    const newDomain = 'https://fixvx.com';

    // 通用函數來替換 URL 的域名
    function replaceDomain(urlString, newDomain) {
        try {
            let url = new URL(urlString, window.location.origin);
            url.hostname = newDomain.replace(/^https?:\/\//, '');
            return url.toString();
        } catch (e) {
            console.error('Invalid URL:', urlString);
            return urlString;
        }
    }

    // 修改鏈接並添加點擊事件
    function modifyLink(link) {
        let originalHref = link.getAttribute('href');
        if (originalHref) {
            let newHref = replaceDomain(originalHref, newDomain);
            link.setAttribute('href', newHref);

            // 添加點擊事件來複製新的 URL
            link.addEventListener('click', function(event) {
                event.preventDefault();
                GM_setClipboard(newHref);
                alert('鏈接已複製: ' + newHref);
            });
        }
    }

    // 獲取所有目標鏈接(根據實際網頁結構,調整選擇器)
    function modifyAllLinks() {
        const links = document.querySelectorAll('a[href]');
        links.forEach(link => {
            let href = link.getAttribute('href');
            if (/^\/[^\/]+\/status\/\d+$/.test(href)) {
                modifyLink(link);
            }
        });
    }

    // 初始修改
    modifyAllLinks();

    // 觀察 DOM 變化,以處理動態加載的鏈接
    const observer = new MutationObserver(mutations => {
        mutations.forEach(mutation => {
            mutation.addedNodes.forEach(node => {
                if (node.nodeType === 1) { // 元素節點
                    if (node.matches('a[href]')) {
                        let href = node.getAttribute('href');
                        if (/^\/[^\/]+\/status\/\d+$/.test(href)) {
                            modifyLink(node);
                        }
                    }
                    // 如果新增的節點包含子元素,也需要檢查
                    const newLinks = node.querySelectorAll('a[href]');
                    newLinks.forEach(link => {
                        let href = link.getAttribute('href');
                        if (/^\/[^\/]+\/status\/\d+$/.test(href)) {
                            modifyLink(link);
                        }
                    });
                }
            });
        });
    });

    observer.observe(document.body, {
        childList: true,
        subtree: true
    });
})();
==========分隔線==========
說一下功能 就是點擊日期 被我改成了複製文章網址


這張圖來當範例 這個用戶的文章 旁邊的那個3小時 點下去就可以複製文章鏈結 並且轉成fixvx.com 域名的非常方便呢,點下去會跳出提示說複製成功如下圖
==========分隔線==========
最後再補一下 腳本程式碼來自 Chatgpt 幫忙寫的 不敢使用的也可以拿去GPT問問有沒有問題~~

留言