Skip to content

Instantly share code, notes, and snippets.

@inversion
Last active August 29, 2015 14:05
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Embed
What would you like to do?
Hide Facebook News feed entries which are random posts from friends' liked pages. They are matched by looking for the 'Like Page' button within a news feed entry. Instructions for adding user scripts to Chrome here: http://stackoverflow.com/questions/5258989/manually-adding-a-userscript-to-google-chrome or for Firefox use Greasemonkey. It's prob…
// ==UserScript==
// @name Hide Facebook 'Like' News
// @namespace uk.me.amoss
// @include /https?://www.facebook.com/.*/
// @version 1
// @grant none
// ==/UserScript==
// Configurable
var indicateInsteadOfHiding = false;
var newsItemAddedObserver = null;
window.onload = function() {
if( newsItemAddedObserver !== null ) {
return;
}
var newsDivSelection = document.evaluate('//div[starts-with(@id, "topnews_main_stream")]', document, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
if( newsDivSelection.snapshotLength === 0 ) {
console.log('No news stream div found.');
} else if( newsDivSelection.snapshotLength > 1 ) {
console.log('Multiple news stream divs found.');
} else {
newsItemAddedObserver = new MutationObserver(function(mutations) {
mutations.forEach(function(mutation) {
for( var i = 0; i < mutation.addedNodes.length; i++ ) {
var addedNode = mutation.addedNodes[i];
if( addedNode.id.indexOf('jsonp') > -1 ) {
hideMatchingDivs(addedNode);
}
}
});
});
newsItemAddedObserver.observe(newsDivSelection.snapshotItem(0), { subtree: true, childList: true });
hideMatchingDivs(document);
}
function hideMatchingDivs(element) {
var spamDivs = document.evaluate('//div[contains(@id, "jsonp") and .//button[contains(text(), "Like Page")]]', element, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
console.log('Detected ' + spamDivs.snapshotLength + ' matching divs.');
for( var i = 0; i < spamDivs.snapshotLength; i++ ) {
if( indicateInsteadOfHiding ) {
spamDivs.snapshotItem(i).style.border = '3px solid red';
} else {
spamDivs.snapshotItem(i).style.display = 'none';
}
}
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment