Skip to content

Instantly share code, notes, and snippets.

@lylejantzi3rd
Created November 11, 2023 20:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lylejantzi3rd/a2e2104406b56986c82833e753880675 to your computer and use it in GitHub Desktop.
Save lylejantzi3rd/a2e2104406b56986c82833e753880675 to your computer and use it in GitHub Desktop.
Block ads on Twitter as they appear
const targetNode = document.querySelector('div[aria-label*="Timeline: Conversation"]')
const config = { attributes: false, childList: true, subtree: true }
function getElementByXpath(path) {
return document.evaluate(path, document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
}
const callback = (mutationList, observer) => {
// We don't care what was mutated. The Xpath has to run at the document level anyway
const foundElement = getElementByXpath("//span[contains (text(), 'Ad')]//ancestor::article")
if (foundElement) {
foundElement.style.display = 'none'
console.log("AD REMOVED")
}
}
const observer = new MutationObserver(callback)
observer.observe(targetNode, config)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment