grahamcracker1234 grahamcracker1234 - 7 months ago 50
HTML Question

How to delete a script in an HTML Page

So I am making a userscript for a website, which deletes ads. The website added a script which detects, if the ads have been taken out of the HTML. So to fix this again I want to delete the whole script, but it has no identifiers, so I have no clue how to go about it. Say I have this code:


if(hasAds) {
document.write("blah blah blah"); // Act as if this re-ads the ad onto the page
<div id="adsBottom">IMAGINE AD CODE HERE</div>

How could I access the script section and delete it?


Well, since you can presuppose its location in the DOM (and index within #overlay), it's relatively straightforward to remove it using getElementById(), removeChild() and childNodes:

var target = document.getElementById('overlay');
target.removeChild( target.childNodes[0] );

Given the following markup:

<div id="overlay">
    <script>/* AD PREVENTION SCRIPT */</script>

The above will work (although does rely on the <script> being the first child of #overlay).