Skip to content

Instantly share code, notes, and snippets.

@asci
Created June 11, 2017 11:18
Show Gist options
  • Save asci/877d13170df8a51e5b759e459780ea19 to your computer and use it in GitHub Desktop.
Save asci/877d13170df8a51e5b759e459780ea19 to your computer and use it in GitHub Desktop.
crystal-feed - remove all non-important cards from your facebook feed
(function() {
'use strict';
function debounce(callback, delay) {
var timeout;
return function() {
var context = this,
args = arguments;
clearTimeout(timeout);
timeout = setTimeout(function() {
callback.apply(context, args);
}, delay);
};
};
function getAds(elems) {
return elems
.filter(elem => elem.innerHTML === 'Suggested Post');
}
function getPeopleYouMayKnow(elems) {
return elems
.filter(elem => elem.innerHTML === 'People you may know');
}
function getReactions(elems) {
return elems
.filter(elem => elem.innerText.includes(' liked this.')
|| elem.innerText.includes(' reacted to this.')
|| elem.innerText.includes(' commented on this.')
|| [...elem.childNodes].filter(node => node instanceof Text && node.wholeText.includes(' likes ')).length
)
.filter(elem => elem.childNodes.length > 1 && elem.childNodes[elem.childNodes.length - 1] instanceof Text)
}
function removeCard(elem) {
let parent = elem.parentNode;
while (parent !== document.body && parent) {
if (parent.getAttribute('role') === 'article') {
parent.parentNode.removeChild(parent);
parent = null;
} else {
parent = parent.parentNode;
}
}
}
function cleanFeed() {
const elems = [...document.querySelectorAll('[role=article] span')];
[
...getAds(elems),
...getPeopleYouMayKnow(elems),
...getReactions(elems)
].forEach(removeCard);
}
const cleanFeedDebounced = debounce(cleanFeed, 20);
(new MutationObserver(function(mutations) {
mutations.forEach(function(mutation) {
if (mutation.addedNodes) {
cleanFeedDebounced();
}
});
}))
.observe(
document.querySelectorAll('[role=feed]')[0],
{ subtree: true, childList: true }
);
cleanFeed();
}());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment