Last active
June 2, 2021 12:08
-
-
Save rocka/fe345364e67fba56667034a2b10b44a8 to your computer and use it in GitHub Desktop.
replace t.co with original links
This file contains 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 Twitter remove t.co | |
// @description replace t.co with original links | |
// @match https://twitter.com/* | |
// @version 0.1 | |
// ==/UserScript== | |
const PRIMARY_COLUMN = '[data-testid="primaryColumn"]'; | |
const SIDEBAR_COLUMN = '[data-testid="sidebarColumn"]'; | |
const A_HREF_T_CO = 'a[href^="https://t.co"]'; | |
// observe body and wait for primaryColumn | |
let mo = new MutationObserver((mutationList, observer) => { | |
for (let i = 0; i < mutationList.length; i++) { | |
const mutation = mutationList[i]; | |
for (let j = 0; j < mutation.addedNodes.length; j++) { | |
const node = mutation.addedNodes[j]; | |
if (node.querySelector(PRIMARY_COLUMN)) { | |
replace(node); | |
init(); | |
observer.disconnect(); | |
mo = null; | |
} | |
} | |
} | |
}).observe(document.body, { childList: true, subtree: true }); | |
// replace all href inside the node | |
function replace(node) { | |
const a = node.querySelectorAll(A_HREF_T_CO); | |
for (let i = 0; i < a.length; i++) { | |
const elm = a[i]; | |
const href = elm.textContent; | |
if (href) { | |
elm.setAttribute('href', href.endsWith('…') ? href.substr(0, href.length - 1) : href); | |
} | |
} | |
} | |
// observe primaryColumn and replace href | |
function init() { | |
const target = document.querySelector('main'); | |
new MutationObserver((mutationList, observer) => { | |
for (let i = 0; i < mutationList.length; i++) { | |
const mutation = mutationList[i]; | |
for (let j = 0; j < mutation.addedNodes.length; j++) { | |
const node = mutation.addedNodes[j]; | |
replace(node); | |
} | |
} | |
}).observe(target, { childList: true, subtree: true }); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment