Block Facebook Posts with Follow or Join (CSP Safe)

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

当前为 2024-09-29 提交的版本,查看 最新版本

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