Search code examples
javascriptelement

The browser element does not delete when I give it the code to delete


I am trying to create a simple script with GreaseMonkey to remove the Google search ads. However, the script is not working and I do not know why.

First, I just used the one-line code: document.getElementById("tvcap").remove(); This did not work, so I tried to make sure that the timing was correct and now the code is:

document.addEventListener("DOMContentLoaded", function() {
  
  document.getElementById("tvcap").remove();
});

I have also tried changing remove() to removeChild(), but this is still not working. This code is so simple, I am baffled about why it is not working.


Solution

  • it happens frequently because of before the page has fully loaded, the script may already be running: The script should run after the page has finished loading if the "DOMContentLoaded" event is used to ensure this. But, while the script executes, there can still be other resources loading, like images or scripts. To be certain that everything has loaded completely before the script executes, you might want to consider using the "window.onload" event. and also check there may have syntax error or the id element is overridden somewhere else in your code.