Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Remove Sponsored Facebook Elements
// ==UserScript==
// @name Remove Sponsored Facebook Elements
// @description Remove sponsored items from feed.
// @author Turing Eret
// @namespace us.lambdacalcul
// @version 0.1
// @copyright Copyright © 2020 Turing Eret
// @license MIT
// @match https://*.facebook.com/*
// @grant none
// @require https://code.jquery.com/jquery-3.2.1.min.js
// ==/UserScript==
(function() {
'use strict';
var $ = window.jQuery;
// configuration of the observers:
var feedConfig = { childList: true, subtree: true };
var sponsoredConfig = { childList: true };
console.log("Remove Sponsored Facebook Elements started!");
var sponsoredObserver = new MutationObserver(function(mutations)
{
mutations.forEach(function(mutation)
{
if(mutation.type === 'childList' && mutation.addedNodes.length > 0)
{
var offending_nodes = $('div[data-pagelet^="FeedUnit"]:has(a[aria-label="Sponsored"])');
for(var i = 0; i < offending_nodes.length; ++i)
{
console.log("Removing Sponsored Feed Items!");
offending_nodes[i].remove();
}
}
});
});
var feedObserver = new MutationObserver(function(mutations)
{
mutations.forEach(function(mutation)
{
if(mutation.type === 'childList' && mutation.addedNodes.length > 0)
{
var feed_nodes = $('div[role="feed"]');
if(feed_nodes.length > 0)
{
console.log("Installing Sponsored Observer!");
sponsoredObserver.observe(feed_nodes[0], sponsoredConfig);
feedObserver.disconnect();
}
}
});
});
var feed_nodes = $('div[role="feed"]');
if(feed_nodes.length > 0)
{
console.log("Installing Sponsored Observer!");
sponsoredObserver.observe(feed_nodes[0], sponsoredConfig);
}
else
{
console.log("Installing Feed Observer!");
feedObserver.observe(document.querySelector('body'), feedConfig);
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment