Skip to content

Instantly share code, notes, and snippets.

@Karmalakas
Last active August 1, 2022 17:24
Show Gist options
  • Save Karmalakas/b0860e39ad26857c285d6bad4884bea6 to your computer and use it in GitHub Desktop.
Save Karmalakas/b0860e39ad26857c285d6bad4884bea6 to your computer and use it in GitHub Desktop.
Remove sponsored posts on Facebook feed
// ==UserScript==
// @name No Sponsored FB posts
// @description Hide Sponsored posts on Facebook
// @namespace http://karmalakas.lt/
// @version 1.5.13
// @author Karmalakas
// @updateURL https://gist.github.com/Karmalakas/b0860e39ad26857c285d6bad4884bea6/raw/FB_no_sponsored.user.js
// @downloadURL https://gist.github.com/Karmalakas/b0860e39ad26857c285d6bad4884bea6/raw/FB_no_sponsored.user.js
// @supportURL https://gist.github.com/Karmalakas/b0860e39ad26857c285d6bad4884bea6
// @match https://www.facebook.com/
// @exclude /^https:\/\/www\.facebook\.com\/.+/
// @require https://code.jquery.com/jquery-2.2.4.min.js
// @run-at document-idle
// @grant none
// ==/UserScript==
(function($) {
'use strict';
var mutationObserver = window.MutationObserver || window.WebKitMutationObserver || window.MozMutationObserver,
body, stream, observer;
var posts_selectors = ['> div'];
function isSponsored(post) {
var span = post.querySelector('span[dir=auto] span[id]');
if (!span) {
return false;
}
var textSpan = span.querySelector('a span');
return headerHasSponsored(textSpan);
}
function headerHasSponsored(textSpan)
{
var string = '',
strings = $(textSpan).children();
while (strings.length) {
strings = strings.children();
}
strings = strings.end();
if (strings.length === 1 && strings[0].innerText.trim().toLowerCase() === 'sponsored') {
return true;
}
strings = strings.toArray();
var text;
$.each(strings, function(i, el) {
el = $(el);
text = el.html();
if (text.indexOf('<') === 0 || !el.is(':visible') || el.width() <= 0 || el.height() <= 0 || el.css('opacity') <= 0 || el.css('font-size') <= 0 || el.css('position') == 'absolute') {
return;
}
string = string + text.toLowerCase();
});
return isStringSponsored(string)
}
function isStringSponsored(checkString)
{
const string = 'sponsored'
const check = checkString.toLowerCase()
if (string.length !== check.length) {
return false
}
const stringArr = string.split('')
const checkArr = checkString.split('')
for (const letter of checkArr) {
let index = stringArr.indexOf(letter)
if (index > -1) {
stringArr.splice(index, 1)
}
}
return stringArr.length === 0
}
function process() {
// Remove rights side sponsored block
clearRightCol();
// Locate the stream every iteration to allow for FB SPA navigation which replaces the stream element
stream = document.querySelector('div[role="feed"]');
if (!stream) {
return;
}
var i, j, posts;
for (i = 0; i < posts_selectors.length; i++) {
posts = stream.querySelectorAll(':scope ' + posts_selectors[i]);
if (!posts.length) {
return;
}
for (j = 0; j < posts.length; j++) {
if (isSponsored(posts[j])) {
posts[j].remove();
}
}
}
}
function clearRightCol() {
var rightCol = $('#ssrb_rhc_start + div');
if (!rightCol.length) {
return false;
}
var text,
firstDiv = rightCol.find('> div').first();
$.each(firstDiv.children(), function(i, el) {
el = $(el);
if (headerHasSponsored(el.find('h3'))) {
el.remove();
}
});
}
if (mutationObserver) {
body = document.querySelector('body');
if (!body) {
return;
}
(new mutationObserver(process)).observe(body, {
'childList': true,
'subtree': true
});
}
})(jQuery);
@Karmalakas
Copy link
Author

Karmalakas commented Jan 2, 2021

Hide Sponsored posts on Facebook stream.

NB: Facebook changes their sponsored posts logic from time to time (sometimes even 5 times per day), so this script might fail at some point until I implement changes


This only works with TamperMonkey (or similar) extension. Supported browsers are Chrome, Firefox, Edge (maybe others too).

TamperMonkey for:

Once you have TamperMonkey installed, just go to the @downloadurl you see in a script.

If later TM fails to update script, just go to @updateURL you see in a script. This should trigger the update in a new window with TM open.

Any questions or suggestions are welcome.


If you by some weird accident found this and like the script, please consider keeping me scripting Keep me scripting 😄

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment