Remove Sponsored Content on Nextdoor

Remove divs with Sponsored span on Nextdoor

// ==UserScript==
// @name         Remove Sponsored Content on Nextdoor
// @namespace    http://tampermonkey.net/
// @version      1.0
// @description  Remove divs with Sponsored span on Nextdoor
// @match        https://nextdoor.com/*
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    // Function to remove sponsored divs
    function removeSponsoredDivs() {
        // Select the feed-container div first
        const feedContainer = document.querySelector('div[data-testid="feed-container"]');
        if (!feedContainer) return;

        // Find the first-level V3Wrapper within the feed-container
        const outerV3Wrapper = feedContainer.querySelector('div[data-v3-view-type="V3Wrapper"]');
        if (!outerV3Wrapper) return;

        // Within the outer V3Wrapper, find all inner V3Wrapper divs
        const innerV3Wrappers = outerV3Wrapper.querySelectorAll('div[data-v3-view-type="V3Wrapper"]');
        console.log(`Found ${innerV3Wrappers.length} inner div(s) with data-v3-view-type="V3Wrapper"`); // Log count

        innerV3Wrappers.forEach(div => {
            // Find the span element within each div that contains the text "Sponsored"
            const sponsoredSpan = Array.from(div.getElementsByTagName('span')).find(span => span.textContent.trim() === 'Sponsored');

            // If the span is found, remove the entire div and log the action
            if (sponsoredSpan) {
                console.log('Found "Sponsored" div, removing:', div); // Log the div to be removed
                div.remove();
            }
        });
    }

    // Create a MutationObserver to monitor the document for added nodes
    const observer = new MutationObserver((mutations) => {
        mutations.forEach((mutation) => {
            // Only run the function if new nodes were added
            if (mutation.addedNodes.length) {
                removeSponsoredDivs();
            }
        });
    });

    // Start observing the document for changes in the child list
    observer.observe(document.body, { childList: true, subtree: true });

    // Run the function initially on page load
    removeSponsoredDivs();
})();