Last active
July 10, 2024 21:28
-
-
Save angeld23/96e2c1ac7fcba3d087555c80e05c0aaa to your computer and use it in GitHub Desktop.
Remove Twitter Discover More Section: Removes the "Discover More" section on tweet replies
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 Remove Twitter Discover More Section | |
// @namespace https://d23.dev/ | |
// @version 1.1 | |
// @description Removes the "Discover More" section on tweet replies | |
// @author angeld23 | |
// @match *://*.twitter.com/* | |
// @icon https://www.google.com/s2/favicons?sz=64&domain=twitter.com | |
// @grant none | |
// ==/UserScript== | |
"use strict"; | |
(() => { | |
/** | |
* Calls the provided callback when the document is loaded | |
*/ | |
function onReady(fn) { | |
if (document.readyState != "loading") { | |
fn(); | |
} | |
else { | |
document.addEventListener("DOMContentLoaded", fn); | |
} | |
} | |
/** | |
* Waits for Element added as a descendant of `parent` that matches `selector`. | |
*/ | |
function waitForElement(parent, selector, callback, runOnce = true) { | |
const elementNow = parent.querySelector(selector); | |
if (elementNow) { | |
callback(elementNow); | |
if (runOnce) { | |
return; | |
} | |
} | |
const observer = new MutationObserver((records) => { | |
records.forEach((record) => { | |
record.addedNodes.forEach((element) => { | |
if (element instanceof Element) { | |
if (element.matches(selector)) { | |
if (runOnce) { | |
observer.disconnect(); | |
} | |
callback(element); | |
} | |
} | |
}); | |
}); | |
}); | |
observer.observe(parent, { | |
childList: true, | |
subtree: true, | |
}); | |
} | |
function deleteAllNextSiblings(element) { | |
while (true) { | |
const nextElement = element.nextElementSibling; | |
if (!nextElement) | |
break; | |
nextElement.remove(); | |
} | |
} | |
onReady(() => { | |
waitForElement(document, "div[data-testid='cellInnerDiv']", (element) => { | |
if (!(element instanceof HTMLElement)) | |
return; | |
if (!element.outerText.includes("Discover more\nSourced from across Twitter")) | |
return; | |
setTimeout(() => { | |
element.children[0].remove(); | |
deleteAllNextSiblings(element); | |
const loop = setInterval(() => { | |
if (element.parentElement) | |
deleteAllNextSiblings(element); | |
else | |
clearInterval(loop); | |
}, 100); | |
}, 50); | |
}, false); | |
}); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment