I am trying to create an extension that will get rid of certain Page elements (by id or class) before the page is displayed to the screen. I've tried using an event listener on the document with "DOMContentLoaded" as the event but the javascript seems to execute after the page is displayed to the user and then deletes it so it's not as smooth as I want (not showing the unwanted content at all)
document.addEventListener("DOMContentLoaded", function() {
var elements = document.getElementsByClassName("header-nav-item");
while(elements.length > 0){
elements[0].parentNode.removeChild(elements[0]);
}
var element = document.getElementById("topchapter");
element.parentNode.removeChild(element);
element = document.getElementById("wrapper_header");
element.parentNode.removeChild(element);
});
This is the content script that my extension uses which deletes after the page is loaded. What do I need to use to modify the DOM before the pages is seen by the user?