How do I stop my script from being blocked by Facebook?

I tried making an userscript that removes every presence of the word “elodie” from the feed. Sadly, Faceboom seems to block it, with the following code, and the path leads to the Violentmonkey plugin.

Content-Security-Policy: The page’s settings blocked the loading of a resource at inline (“script-src”). injected.js:1:8983

Content-Security-Policy: The page’s settings observed the loading of a resource at inline (“script-src”). A CSP report is being sent. injected.js:1:8983

// ==UserScript==
// @name         Hide Elodie News
// @namespace   
// @version      0.1.0
// @description  Hide Elodie news*
// @author       brunon
// @match        https://*.facebook.com/*
// @grant        none
// @run-at       document-start
// ==/UserScript==

(function() {
    'use strict';

    const keywords = ["elodie"];

    function hideItemsWithKeywords() {
        const regexPattern = new RegExp(keywords.join('|'), 'i');
        const items = document.querySelectorAll('.x1lliihq');

        items.forEach(item => {
            const containsKeywords = (element) => {
                if (element.nodeType === Node.TEXT_NODE) {
                    const text = element.textContent;
                    return regexPattern.test(text);
                }
                for (const child of element.childNodes) {
                    if (containsKeywords(child)) {
                        return true;
                    }
                }
                return false;
            };

            if (containsKeywords(item)) {
                item.style.display = 'none';
            }
        });
    }

    document.addEventListener('DOMContentLoaded', hideItemsWithKeywords);

    const observer = new MutationObserver(hideItemsWithKeywords);
    observer.observe(document.body, { subtree: true, childList: true });
})();

console.log("Elodie script working");

This script works perfectly on any other website.
It’s just Facebook that hates this particular script. Others seem to work. Can someone try and help out a brother?

  • Try adding // @unwrap to the userscript header.

    – 

Leave a Comment