Greasy Fork

Greasy Fork is available in English.

Twitter/X Nitter Redirect

Redirect twitter.com and x.com links to a random healthy Nitter instance.

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

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

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         Twitter/X Nitter Redirect
// @version      1.0
// @description  Redirect twitter.com and x.com links to a random healthy Nitter instance.
// @author       yodaluca23
// @license      GNU GPLv3
// @match        *://twitter.com/*
// @match        *://www.twitter.com/*
// @match        *://x.com/*
// @match        *://www.x.com/*
// @grant        GM_xmlhttpRequest
// @namespace http://tampermonkey.net/
// ==/UserScript==

(function() {
    'use strict';

    const staticURL = ''; // Set this to an instance if you always want to use a single Nitter instance instead of fetching from API.

    const apiUrl = 'https://status.d420.de/api/v1/instances';

    const profileURLPattern = /^https?:\/\/(www\.)?(twitter\.com|x\.com)\/[A-Za-z0-9_]+(\/.*)?$/;

    let currentURL = window.location.href;

    function fetchNitterInstance(callback) {
        GM_xmlhttpRequest({
            method: 'GET',
            url: apiUrl,
            onload: function(response) {
                try {
                    const data = JSON.parse(response.responseText);

                    if (!data.hosts || !Array.isArray(data.hosts)) {
                        console.error('Unexpected API response format:', data);
                        return;
                    }

                    const healthyInstances = data.hosts.filter(host => host.healthy && !host.is_bad_host);

                    if (healthyInstances.length > 0) {
                        const randomInstance = healthyInstances[Math.floor(Math.random() * healthyInstances.length)];
                        callback(randomInstance.domain);
                    } else {
                        console.warn('No healthy Nitter instances found.');
                    }
                } catch (error) {
                    console.error('Failed to parse Nitter instance data:', error, response.responseText);
                }
            },
            onerror: function(error) {
                console.error('Error fetching Nitter instances:', error);
            }
        });
    }

    function redirectToNitter(nitterDomain) {
        if (profileURLPattern.test(currentURL)) {
            let newURL = currentURL.replace(/(twitter\.com|x\.com)/, nitterDomain);
            console.log('Redirecting to:', newURL);
            if (newURL !== currentURL) {
                window.location.replace(newURL);
            }
        } else {
            console.log('URL does not match profile pattern, no redirection.');
        }
    }
    if (staticURL.length > 1) {
      redirectToNitter(staticURL);
    } else {
      fetchNitterInstance(redirectToNitter);
    }
})();