Skip to content

Instantly share code, notes, and snippets.

@lambdaknight
Created December 28, 2020 14:29
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lambdaknight/5879a7c518e89f0c89b6f61de51a1b1d to your computer and use it in GitHub Desktop.
Save lambdaknight/5879a7c518e89f0c89b6f61de51a1b1d to your computer and use it in GitHub Desktop.
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