twitter.com to nitter

redirects and rewrites all twitter urls to nitter

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name        twitter.com to nitter
// @description redirects and rewrites all twitter urls to nitter
// @namespace   azzurite
// @match       *://*/*
// @grant       none
// @version     1.0.0
// @license     GPLv3
// @author      -
// @run-at      document-start
// ==/UserScript==

const NITTER_URL = 'nitter.privacydev.net'
const TWITTER_URL = 'twitter.com'

function redirectToNitter () {
    document.querySelectorAll('a[href*="'+ TWITTER_URL +'"]').forEach((element) => {
        element.href = element.href.replace(TWITTER_URL, NITTER_URL)
        element.textContent = element.textContent.replace(TWITTER_URL, NITTER_URL)
    })
}

if (location.hostname.includes(TWITTER_URL)) location.replace(`https://` + NITTER_URL + location.pathname);

document.addEventListener(`DOMContentLoaded`, () => {
    (new MutationObserver((mutations) => {
        let runCheck = false
        for (let mutation of mutations) {
            if (mutation.addedNodes.length || mutation.attributeName === 'href') {
                runCheck = true
                break
            }
        }
        if (runCheck) {
            redirectToNitter()
        }
    })).observe(document.querySelector('body'), {attributeFilter: ['href'], childList: true, subtree: true})

    redirectToNitter();
});