Skip to content

Instantly share code, notes, and snippets.

@johnsyweb
Created May 16, 2025 08:49
Show Gist options
  • Save johnsyweb/758da87a9a540da9aba04f74c765bc31 to your computer and use it in GitHub Desktop.
Save johnsyweb/758da87a9a540da9aba04f74c765bc31 to your computer and use it in GitHub Desktop.
Redirect X.com and Twitter to Nitter
// ==UserScript==
// @name Redirect X.com and Twitter to Nitter
// @namespace http://tampermonkey.net/
// @version 1.0.1
// @description Redirect Twitter/X (including t.co) URLs to a Nitter instance
// @author Pete Johns (@johnsyweb)
// @match *://x.com/*
// @match *://www.x.com/*
// @match *://mobile.x.com/*
// @match *://twitter.com/*
// @match *://www.twitter.com/*
// @match *://mobile.twitter.com/*
// @match *://t.co/*
// @run-at document-start
// @grant none
// @license MIT
// @icon https://nitter.net/favicon.ico
// ==/UserScript==
(function () {
'use strict';
const nitterInstance = 'https://nitter.net';
const redirectToNitter = () => {
const url = window.location.href;
const twitterRegex = /^https?:\/\/(?:www\.|mobile\.)?(x\.com|twitter\.com)/;
if (twitterRegex.test(url)) {
const nitterUrl = url.replace(twitterRegex, nitterInstance);
window.location.replace(nitterUrl);
}
};
const handleTcoRedirect = () => {
// Wait for the browser to resolve the t.co shortlink
const observer = new MutationObserver(() => {
const finalUrl = window.location.href;
const isTwitter = finalUrl.includes('x.com') || finalUrl.includes('twitter.com');
if (isTwitter) {
const nitterUrl = finalUrl.replace(/^https?:\/\/(?:www\.|mobile\.)?(x\.com|twitter\.com)/, nitterInstance);
window.location.replace(nitterUrl);
}
});
observer.observe(document, { subtree: true, childList: true });
};
const hostname = window.location.hostname;
if (hostname.includes('x.com') || hostname.includes('twitter.com')) {
redirectToNitter();
} else if (hostname === 't.co') {
handleTcoRedirect();
}
})();
@johnsyweb
Copy link
Author

Or if you want it in bookmarklet form:

javascript:(function()%7Bconst%20n=%22https%3A//nitter.net%22,u=location.href,r=/%5Ehttps?:%5C/%5C/(?:www%5C.%7Cmobile%5C.)?(x%5C.com%7Ctwitter%5C.com)/;if(r.test(u))%7Blocation.replace(u.replace(r,n))%7Delse%7Balert(%22Not%20a%20Twitter/X%20page.%22)%7D%7D)();%0A

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment