Facebook - Hides Suggested and Sponsored Posts

Hides Suggested For You, sponsored posts on the main feed & those silly sponsored ads in Marketplace

目前為 2021-10-17 提交的版本,檢視 最新版本

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

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

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         Facebook - Hides Suggested and Sponsored Posts
// @namespace    http://tampermonkey.net/
// @version      0.2
// @description  Hides Suggested For You, sponsored posts on the main feed & those silly sponsored ads in Marketplace
// @author       ArthurG
// @match        https://www.facebook.com/*
// @require https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js
// @grant        none
// ==/UserScript==

(function() {
    'use strict';
    const pollTimeout = 3000;
    const debounceTimeout = 2000;
    const maxChar = 256;
    const debugHighlightAds = false;

    const debouncedFindAndHide = debounce(findAndHide, debounceTimeout, false);

    function isPresent(s, q)
    {
        // Count occurrences of all
        // characters in s.
        const freq = new Array(maxChar);

        freq.fill(0);

        for (let i = 0; i < s.length; i++)
            freq[s[i]]++;

        // Check if number of occurrences of
        // every character in q is less than
        // or equal to that in s.
        for (let i = 0; i < q.length; i++)
        {
            freq[q[i]]--;
            if (freq[q[i]] < 0)
                return false;
        }

        return true;
    }

    function debounce(func, wait, immediate) {
        var timeout;
        return function() {
            var context = this, args = arguments;
            var later = function() {
                timeout = null;
                if (!immediate) func.apply(context, args);
            };
            var callNow = immediate && !timeout;
            clearTimeout(timeout);
            timeout = window.__fbNativeSetTimeout(later, wait);
            if (callNow) func.apply(context, args);
        };
    };


    function findAndHide(skipPoll) {

        // Suggested For You
        $( "span:contains('Suggested for you')" ).closest('[data-pagelet*="FeedUnit"]').hide();

        //Sponsored Ads in the Feed
        $('a[href*="/ads/"]').closest('[data-pagelet*="FeedUnit"]').hide();
        $('[data-pagelet*="FeedUnit"] a[href="#"]').each((index, el) => {
            const elText = $(el).text();
            if(elText.length < 40 && isPresent('Sponsored', elText)) {
                if(debugHighlightAds) {
                    $(el).closest('[data-pagelet*="FeedUnit"]').get(0).style.border = "thick solid #FF0000";
                } else {
                    $(el).closest('[data-pagelet*="FeedUnit"]').hide();
                }
            }
        });

        $('[data-pagelet*="FeedUnit"] [role="button"]').each((index, el) => {
            const elText = $(el).text();
            if(elText.includes('Sponsored')) {
                if(debugHighlightAds) {
                    $(el).closest('[data-pagelet*="FeedUnit"]').get(0).style.border = "thick solid #FF0000";
                } else {
                    $(el).closest('[data-pagelet*="FeedUnit"]').hide();
                }
            }
        });

        //Sponsored Ads in Marketplace
        $('a[href*="/ads/"]').closest('span > div > a > div > div > div').parent().parent().parent().parent().parent().parent().hide();

        //Hide Sponsored Ads Header in Marketplace
        $(`a[href*="/ads/about"] span:contains('Sponsored')`).parent().parent().hide();

        console.log('Found and hid!');

        if(!skipPoll) {
            window.__fbNativeSetTimeout(function() {
              findAndHide();
           }, pollTimeout);
        }
    }

    $(window).scroll(debounce(() => { findAndHide(true); }, debounceTimeout, true));

    //Kick off the polling
    findAndHide();
})();