About

This userscript will add a visibility_off icon to your Facebook header:

Facebook header

Clicking this icon will prompt for a list of keywords to block. Enter these keywords in a comma-separated list.

(Note: regular expressions are supported. As such, be wary of using symbols in your keywords.)

keyword prompt

Once you click OK to save changes, all posts containing the entered keywords should now change to pictures of puppies.

Facebooks puppies

Clicking on a picture of a puppy will show the original, hidden post.

Download & Install

NameFacebook: Block Keywords
Namespacehttps://www.charlesstover.com/userscripts/facebook-block-keywords
AuthorCharles Stover
DescriptionBlocks Facebook posts that contain user-defined keywords.
File Size5,421 bytes
Last Modified09 Nov. 2016
Version1.05
URLs
  • http://www.facebook.com/*
  • https://www.facebook.com/*
Download

Open Source

// ==UserScript==
// @name        Facebook: Block Keywords
// @namespace   https://www.charlesstover.com/userscripts/facebook-block-keywords
// @author      Charles Stover
// @description Blocks Facebook posts that contain user-defined keywords.
// @downloadURL https://www.charlesstover.com/facebook-block-keywords.user.js
// @grant       none
// @icon        http://i.imgur.com/ROCNCWN.png
// @match       http://www.facebook.com/*
// @match       https://www.facebook.com/*
// @updateURL   https://www.charlesstover.com/facebook-block-keywords.meta.js
// @version     1.05
// ==/UserScript==

window.keywords2block = {
	csl:     false,
	puppies: ["1OBbx9K", "30cPJI5", "6UHKvdB", "9Xz5QLY", "A27PUA0", "alhqTEE", "ASxd3Kl",
		"B2pCVTj", "BBM3Uy4", "CwnKOiT", "cYpH6wG", "e24dXFX", "ETCe6nr", "FmkBZXj", "FY3ZMrb",
		"KGxPDHh", "lEMFHLC", "pRbbqjU", "PU0UjBY", "QkVw6Ce", "qrEVkmx", "rOKJZzn", "RuSJCS0",
		"TjjGwMs", "twlnyOw", "WYcoMrA", "y06xxf2", "YhLhU1i", "ywYUAZG", "Zb1npAC", "ZPhEUw0"],
	regExp:  false,

	// Store CSL and create RegExp from that CSL.
	set: function(csl) {
		this.csl    = csl ? csl : false;
		this.regExp = csl ?
			new RegExp("(?:" + csl.replace(/\%3B/g, ";").split(", ").join("|") + ")", "i") :
			false;
		return this.regExp;
	}
};
var cookies = document.cookie.split(";"),
	prefix, x;
for (x = 0; x < cookies.length; x++) {
	if (prefix = cookies[x].match(/^\s*k2b=/)) {
		window.keywords2block.set(cookies[x].substring(prefix[0].length, cookies[x].length));
		break;
	}
}

// Append keyword icon to header.
var jewel = {
		a:    document.createElement("a"),
		img:  document.createElement("img"),
		wrap: document.createElement("div")
	},
	notif = document.getElementById("fbNotificationsJewel");
jewel.wrap.className = notif.parentNode.className;
jewel.wrap.style.setProperty("height",  "42px");
jewel.wrap.style.setProperty("padding", "4px 3px 0 3px");
jewel.wrap.style.setProperty("width",   "27px");
jewel.a.addEventListener(
	"click",
	function(e) {
		e.preventDefault();
		var csl = prompt(
				"What keywords would you like blocked?",
				window.keywords2block.csl ? window.keywords2block.csl : ""
			);
		if (csl != null) {
			window.keywords2block.set(csl);
			document.cookie = "k2b=" + csl.replace(/\;/g, "%3B") + "; " +
				"expires=" + new Date(csl ? new Date().getTime() + 31536000000 : 1) + "; " +
				"path=/";
		}
	}
);
jewel.a.setAttribute("href",  "#");
jewel.a.setAttribute("title", "Hide Posts");
jewel.a.style.setProperty("display",     "inline-block");
jewel.a.style.setProperty("padding-top", "6px");
jewel.img.setAttribute("alt",    "Hide");
jewel.img.setAttribute("height", "24");
jewel.img.setAttribute(
	"src",
	"https://storage.googleapis.com/material-icons/external-assets/" +
	"v4/icons/svg/ic_visibility_off_black_24px.svg"
);
jewel.img.setAttribute("width",  "24");
jewel.a.appendChild(jewel.img);
jewel.wrap.appendChild(jewel.a);
notif.parentNode.parentNode.insertBefore(jewel.wrap, notif.parentNode.parentNode.firstChild);

// Scan for and replace blocked posts.
setInterval(
	function() {
		var posts   = document.getElementsByClassName("userContentWrapper"),
			x;

		// For each post,
		for (x = 0; x < posts.length; x++) {

			// If this post matches the blocked keywords,
			if (

				// If there are keywords to block,
				window.keywords2block.csl &&

				// And this posts matches one of those keywords,
				posts.item(x).innerText.match(window.keywords2block.regExp) &&

				// And this post hasn't already been hidden,
				!posts.item(x).className.match(/fbbk\-tagged/) &&

				// And this post's parent hasn't already been hidden (e.g. "[Name] liked this.").
				!posts.item(x).parentNode.parentNode.parentNode.parentNode.parentNode
					.className.match(/fbbk\-tagged/)
			) {
				posts.item(x).className += " fbbk-tagged";
				var puppy = {
						a:    document.createElement("a"),
						img:  document.createElement("img"),
						wrap: document.createElement("div")
					};

				// Create <div> wrapper
				puppy.wrap.className = "userContentWrapper";

				// Create <a> to click to display original post
				puppy.a.setAttribute("href", "#");
				puppy.a.addEventListener(
					"click",
					function(e) {
						e.preventDefault();
						this.parentNode.style.setProperty("display", "none");
						this.parentNode.parentNode.getElementsByClassName("userContentWrapper").item(0)
							.style.setProperty("display", "");
					}
				);

				// Create <img /> of the puppy.
				puppy.img.setAttribute("alt", "Puppy");
				puppy.img.setAttribute(
					"src",
					"http://i.imgur.com/" +
					window.keywords2block.puppies[
						Math.floor(Math.random() * window.keywords2block.puppies.length)
					] +
					".png"
				);
				puppy.img.style.setProperty("margin-bottom", "-4px");
				puppy.img.style.setProperty("max-width", "100%");

				// Hide current post and append puppy post.
				posts.item(x).style.setProperty("display", "none");
				puppy.a.appendChild(puppy.img);
				puppy.wrap.appendChild(puppy.a);
				posts.item(x).parentNode.appendChild(puppy.wrap);

				// Nice rounded border.
				posts.item(x).parentNode.parentNode.style.setProperty("border-radius", "0.5em");
				posts.item(x).parentNode.parentNode.style.setProperty("overflow",     "hidden");
			}
		}
	},
	500
);