Skip to content

Instantly share code, notes, and snippets.

@elsassph
Created February 17, 2020 17:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save elsassph/80668c74ae02c48b987c345ee269c202 to your computer and use it in GitHub Desktop.
Save elsassph/80668c74ae02c48b987c345ee269c202 to your computer and use it in GitHub Desktop.
Hide Facebook sponsored stories
// ==UserScript==
// @name No sponsor
// @namespace http://tampermonkey.net/
// @version 0.1
// @description Hide Facebook sponsored stories
// @author You
// @match https://www.facebook.com/
// @grant none
// ==/UserScript==
(function() {
'use strict';
function getLabel(elem) {
// get visible letters from the subtitle
return Array.from(elem.querySelectorAll("[data-content]")).filter(e => e.offsetWidth > 0).map(e => e.dataset.content).join("");
}
function getSubtitle(elem) {
return elem.querySelector("[data-testid=fb-test-id-feed-sub-tilte]");
}
function getTitle(elem) {
const e = elem.querySelector("h5");
return e ? e.innerText : "";
}
function getStories() {
return document.querySelectorAll("[data-testid=fbfeed_story]");
}
function hideSponsored() {
const stories = getStories();
stories.forEach(story => {
if (story.__checked__) return;
const sub = getSubtitle(story);
if (sub) {
const title = getTitle(story);
const label = getLabel(sub);
if (label == "") return;
console.log('STORY', title, label);
if (label.startsWith("Sponsor")) {
story.style.display = 'none';
}
}
story.__checked__ = true;
});
}
setInterval(() => hideSponsored(), 2000);
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment