Search code examples
javascriptjquerygreasemonkeytampermonkey

How to remove a div with a changing id via Tampermonkey/Greasemonkey?


I would like to be able to remove a div with an id that changes every time you refresh the page.

The div looks like this:

<div id="8474adblockinfo">
</div>

So there is a regular id called 'adblockinfo' and it always has a set of 4 numbers in the id before 'adblockinfo' (e.g. 1234adblockinfo, 1235adblockinfo). Any idea how I can remove this with a Tampermonkey script?

I've tried code:

$("div[id$='adblockinfo']").remove
$("div[id$='adblockinfo']").css('display', 'none');

Neither seems to work.

I also think that the webpage is inserting the div via AJAX after pageload or something, so that could be an issue? I've tried running the script at: // @run-at document-idle but that didn't do it.

Any tips on this?


Solution

  • The following will work on both static and AJAX-driven pages:

    // ==UserScript==
    // @name     _delete Adblock blocking nodes
    // @match    *://YOUR_SERVER.COM/YOUR_PATH/*
    // @require  https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js
    // @require  https://gist.github.com/raw/2625891/waitForKeyElements.js
    // @grant    GM_addStyle
    // @grant    GM.getValue
    // ==/UserScript==
    //- The @grant directives are needed to restore the proper sandbox.
    /* global $, waitForKeyElements */
    
    waitForKeyElements ("[id$='adblockinfo']", killNode);
    
    function killNode (jNode) {
        jNode.remove ();
    }