Created
May 16, 2025 08:49
-
-
Save johnsyweb/758da87a9a540da9aba04f74c765bc31 to your computer and use it in GitHub Desktop.
Redirect X.com and Twitter to Nitter
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==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(); | |
} | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Or if you want it in bookmarklet form: