Block Facebook Posts with Follow or Join (CSP Safe)

Block posts on Facebook newsfeed that have Follow or Join buttons, bypassing CSP restrictions.

  1. // ==UserScript==
  2. // @name Block Facebook Posts with Follow or Join (CSP Safe)
  3. // @namespace http://github.com/sarahbarberuk
  4. // @version 1.3
  5. // @description Block posts on Facebook newsfeed that have Follow or Join buttons, bypassing CSP restrictions.
  6. // @author Sarah Barber
  7. // @match https://www.facebook.com/*
  8. // @grant none
  9. // @license MIT
  10. // ==/UserScript==
  11.  
  12. (function() {
  13. 'use strict';
  14. // Function to find the nearest parent div with exactly one class
  15. function findNearestParentWithOneClass(element) {
  16. let parent = element.closest('div');
  17. while (parent) {
  18. if (parent.classList.length === 1) {
  19. return parent; // Found the div with exactly one class
  20. }
  21. parent = parent.parentElement.closest('div'); // Move to the next parent div
  22. }
  23. return null; // Return null if no such div is found
  24. }
  25.  
  26. // Function to block posts with "Follow" or "Join" buttons
  27. function blockPostsWithFollowOrJoin() {
  28. // Get the main newsfeed div
  29. const mainDiv = document.querySelector('div[role="main"]');
  30. if (!mainDiv) return;
  31.  
  32. // Get all div elements with role="button" inside the main div
  33. const buttons = mainDiv.querySelectorAll('div[role="button"]');
  34.  
  35. // Loop through each button and check if it contains "Follow" or "Join"
  36. buttons.forEach(button => {
  37. const span = button.querySelector('span');
  38.  
  39. if (span && (span.innerText === 'Follow' || span.innerText === 'Join')) {
  40. // Find the nearest parent div with exactly one class and hide it
  41. const postContainer = findNearestParentWithOneClass(button);
  42. if (postContainer) {
  43. postContainer.style.display = 'none';
  44. console.log('Blocked post with:', span.innerText, postContainer);
  45. }
  46. }
  47.  
  48. // this bit blocks reels and short videos
  49. const innerDiv = button.querySelector('div');
  50. if (innerDiv) {
  51. const innerSpan = innerDiv.querySelector('span');
  52.  
  53. if (innerSpan && (innerSpan.innerText === 'Reels and short videos')) {
  54. // Find the nearest parent div with exactly one class and hide it
  55. const postContainer = findNearestParentWithOneClass(button);
  56. if (postContainer) {
  57. postContainer.style.display = 'none';
  58. console.log('Blocked post with:', innerSpan.innerText, postContainer);
  59. }
  60. }
  61. }
  62.  
  63. });
  64. }
  65.  
  66. // Run the script periodically to catch new posts
  67. const observer = new MutationObserver(blockPostsWithFollowOrJoin);
  68. observer.observe(document.body, { childList: true, subtree: true });
  69.  
  70.  
  71. })();