Block Facebook Posts with Follow or Join (CSP Safe)

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

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey, το Greasemonkey ή το Violentmonkey για να εγκαταστήσετε αυτόν τον κώδικα.

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey ή το Violentmonkey για να εγκαταστήσετε αυτόν τον κώδικα.

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey ή το Violentmonkey για να εγκαταστήσετε αυτόν τον κώδικα.

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey ή το Userscripts για να εγκαταστήσετε αυτόν τον κώδικα.

You will need to install an extension such as Tampermonkey to install this script.

Θα χρειαστεί να εγκαταστήσετε μια επέκταση διαχείρισης κώδικα χρήστη για να εγκαταστήσετε αυτόν τον κώδικα.

(Έχω ήδη έναν διαχειριστή κώδικα χρήστη, επιτρέψτε μου να τον εγκαταστήσω!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(Έχω ήδη έναν διαχειριστή στυλ χρήστη, επιτρέψτε μου να τον εγκαταστήσω!)

// ==UserScript==
// @name         Block Facebook Posts with Follow or Join (CSP Safe)
// @namespace    http://github.com/sarahbarberuk
// @version      1.3
// @description  Block posts on Facebook newsfeed that have Follow or Join buttons, bypassing CSP restrictions.
// @author       Sarah Barber
// @match        https://www.facebook.com/*
// @grant        none
// @license      MIT
// ==/UserScript==

(function() {
    'use strict';
        // Function to find the nearest parent div with exactly one class
        function findNearestParentWithOneClass(element) {
            let parent = element.closest('div');
            while (parent) {
                if (parent.classList.length === 1) {
                    return parent; // Found the div with exactly one class
                }
                parent = parent.parentElement.closest('div'); // Move to the next parent div
            }
            return null; // Return null if no such div is found
        }

        // Function to block posts with "Follow" or "Join" buttons
        function blockPostsWithFollowOrJoin() {
            // Get the main newsfeed div
            const mainDiv = document.querySelector('div[role="main"]');
            if (!mainDiv) return;

            // Get all div elements with role="button" inside the main div
            const buttons = mainDiv.querySelectorAll('div[role="button"]');

            // Loop through each button and check if it contains "Follow" or "Join"
            buttons.forEach(button => {
                const span = button.querySelector('span');

                if (span && (span.innerText === 'Follow' || span.innerText === 'Join')) {
                    // Find the nearest parent div with exactly one class and hide it
                    const postContainer = findNearestParentWithOneClass(button);
                    if (postContainer) {
                        postContainer.style.display = 'none';
                        console.log('Blocked post with:', span.innerText, postContainer);
                    }
                }

                // this bit blocks reels and short videos
                const innerDiv = button.querySelector('div');
                if (innerDiv) {
                    const innerSpan =  innerDiv.querySelector('span');

                    if (innerSpan && (innerSpan.innerText === 'Reels and short videos')) {
                        // Find the nearest parent div with exactly one class and hide it
                        const postContainer = findNearestParentWithOneClass(button);
                        if (postContainer) {
                            postContainer.style.display = 'none';
                            console.log('Blocked post with:', innerSpan.innerText, postContainer);
                        }
                    }
                }  

                   
            });
        }

        // Run the script periodically to catch new posts
        const observer = new MutationObserver(blockPostsWithFollowOrJoin);
        observer.observe(document.body, { childList: true, subtree: true });


})();