Nitter Teleporter

Converts Twitter links to Nitter ones

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

You will need to install an extension such as Tampermonkey to install this script.

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name          Nitter Teleporter
// @namespace     lousando
// @match         https://*/*
// @match         http://*/*
// @exclude-match https://nitter.net/*
// @run-at        document-idle
// @version       1.0.4
// @author        lousando
// @description   Converts Twitter links to Nitter ones
// @grant         GM_getValue
// @require       https://cdn.jsdelivr.net/npm/@violentmonkey/dom@2
// ==/UserScript==

// allow for overriding of Nitter instance
const nitterDomain = GM_getValue("nitter_domain", "nitter.net");

VM.observe(document.body, () => {
  const twitterRegex = /https?:\/\/twitter\.com?/i;
  const shortTwitterRegex = /https?:\/\/t\.co/i;

  // links
  Array.from(document.body.querySelectorAll("a[href]")).filter(link => {
    return twitterRegex.test(link.href);
  }).forEach(link => {
    const parsedURL = new URL(link.href);
    parsedURL.hostname = nitterDomain;

    if (twitterRegex.test(link.innerText)) {
        link.innerText = link.innerText.replace(twitterRegex, nitterDomain);
    }

    link.setAttribute("href", parsedURL.toString());
  });

  Array.from(document.body.querySelectorAll("a[href]")).filter(link => {
    return shortTwitterRegex.test(link.href);
  }).forEach(link => {
    // expand link
    fetch(link.href).then(r => r.text())
      .then(response => {
        const domParser = new DOMParser();
        const doc = domparser.parseFromString(response, "text/xml")

        const parsedURL = new URL(link.href);
        parsedURL.hostname = nitterDomain;


        if (shortTwitterRegex.test(link.innerText)) {
            link.innerText = link.innerText.replace(shortTwitterRegex, nitterDomain);
        }

        link.setAttribute("href", parsedURL.toString());
    });

  });
});