Twitter t.co to Direct Link Converter

Automatically converts Twitter's indirect t.co links into direct, original URLs for a more transparent and streamlined browsing experience.

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name Twitter t.co to Direct Link Converter
// @name:zh-CN 推特 t.co 转直接链接
// @namespace http://tampermonkey.net/
// @version 1.6.0
// @description Automatically converts Twitter's indirect t.co links into direct, original URLs for a more transparent and streamlined browsing experience.
// @description:zh-CN 把推特的 t.co 中转跳转链接改为直接跳转
// @author CLDXiang
// @website https://github.com/CLDXiang/tampermonkey
// @license MIT
// @match *://*twitter.com/*
// @match *://*x.com/*
// @grant none
// @run-at document-end
// ==/UserScript==

"use strict";
(() => {
  // src/twitter-link-converter/main.mts
  function modifyLink(link) {
    if (link.href.includes("t.co")) {
      let urlText = link.innerText;
      if (urlText.endsWith("\u2026"))
        urlText = urlText.slice(0, -1);
      if (urlText.startsWith("http"))
        link.href = urlText;
      else if (!urlText.startsWith("/"))
        link.href = `https://${urlText}`;
    }
  }
  function observeDOM() {
    try {
      new MutationObserver((mutations) => {
        for (const mutation of mutations) {
          if (mutation.type === "childList") {
            mutation.addedNodes.forEach((node) => {
              if (node instanceof HTMLElement) {
                if (node instanceof HTMLAnchorElement)
                  modifyLink(node);
                else
                  node.querySelectorAll("a").forEach(modifyLink);
              }
            });
          }
        }
      }).observe(document.body, { childList: true, subtree: true });
    } catch (e) {
      console.log(e);
    }
  }
  document.querySelectorAll("a").forEach(modifyLink);
  observeDOM();
})();