Skip to content

Instantly share code, notes, and snippets.

@Veticia
Created December 9, 2022 16:34
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Veticia/35fb049f9f749af77a7e2d7cf892db61 to your computer and use it in GitHub Desktop.
Save Veticia/35fb049f9f749af77a7e2d7cf892db61 to your computer and use it in GitHub Desktop.
Convert twitter URLs to Nitter URLs
// ==UserScript==
// @name Twitter links To Nitter
// @namespace brazenvoid
// @description Convert twitter URLs to Nitter URLs
// @author brazenvoid
// @include *
// @exclude https://twitter.com/*
// @run-at document-end
// ==/UserScript==
const NITTER_URL = 'unofficialbird.com'
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)
})
}
(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()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment