Skip to content

Instantly share code, notes, and snippets.

@maury91
Last active February 15, 2019 12:35
Show Gist options
  • Save maury91/b6b30e7158896a69def366663846bdaf to your computer and use it in GitHub Desktop.
Save maury91/b6b30e7158896a69def366663846bdaf to your computer and use it in GitHub Desktop.
function realText(elem) {
if (elem instanceof Text) {
return elem.textContent;
}
const visibleText = [...elem.childNodes]
.filter(elem => elem instanceof Text || window.getComputedStyle(elem, "").display !== 'none')
.flatMap(elem => realText(elem))
.join('')
return visibleText
}
function isSponsored(elem) {
return elem && realText(elem).toLowerCase().startsWith('sponsored')
}
function isArticleSponsored(elem) {
return isSponsored(elem.querySelector('div[id^=feed_subtitle]'))
}
const domInsertionObserver = new MutationObserver(function(mutationsList){
[...mutationsList]
.filter(mutation => mutation.type === 'childList' && mutation.addedNodes.length)
.flatMap(mutation => [...mutation.addedNodes])
.filter(addedNode => typeof addedNode.id === 'string' && addedNode.id.startsWith('u_fetchstream'))
.filter(isArticleSponsored)
.forEach(post => post.parentElement.removeChild(post))
});
domInsertionObserver.observe(document.querySelector('div[id^=feed_stream]'), { childList: true, subtree: true });
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment