Search code examples
javascriptgreasemonkey-4

How to use Greasemonkey to selectively remove content from a website?


I have tried removing content (paid content) from this website with uBlock origin, Greasemonkey and Anti-Adblock Killer script.

I have tried running this script but without success.
The ("paid") content I want to remove looks like this:

<div class="news" info="398825">
    <div class="normal" ...>
        <div class="supertitle">
            <a href="http://www.monitor.hr/marketing/sponzorirana.html" target="_blank">Sponzorirana vijest</a>
        </div>
        ...

I can differentiate "paid content" from rest of the content with this element:

<a href="http://www.monitor.hr/marketing/sponzorirana.html" target="_blank">Sponzorirana vijest</a>

I would like to remove every "paid content" ("Sponzorirana vijest") section from the linked website.


Solution

  • That content appears to be static. So just leverage the page's jQuery like so:

    // ==UserScript==
    // @name     _Remove sponsored content
    // @match    *://www.monitor.hr/*
    // @grant    none
    // ==/UserScript==
    
    $(".supertitle > a[href*='marketing/sponzorirana']").closest (".news").remove ();
    


    If more of those blocks are added dynamically, use waitForKeyElements() as shown in this answer. Something like this (untested in GM4):

    // ==UserScript==
    // @name     _Remove sponsored content
    // @match    *://www.monitor.hr/*
    // @require  https://gist.github.com/raw/2625891/waitForKeyElements.js
    // @grant    none
    // ==/UserScript==
    waitForKeyElements (".supertitle > a[href*='marketing/sponzorirana']", removeNewsNode);
    
    function removeNewsNode (jNode) {
        jNode.closest (".news").remove ();
    }
    



    Finally, per Greasemonkey's own developers, switch to Tampermonkey or Violentmonkey. Greasemonkey 4+ has serious deficiencies.