javascripteventsdommutation-events

Possible to modify DOM during/before initial DOM parsing?


Is it possible to modify the DOM during or before the initial DOM parsing? Or do I have to wait until the DOM is parsed and built before interacting with it? More specifically, is it possible to hinder a script element in DOM from running using userscripts/content scripts or similar in chrome or firefox?

Tried using eventListeners on DOMNodeInserted before the DOM is parsed, but these are only fired after the DOM is built.


Solution

  • These are two separate questions:

    1. Is it possible to modify the DOM during or before the initial DOM parsing?

    Yes. As soon as the browser builds the root element, then you can start querying and mutating the DOM. Note that when your script runs, some of the page may still yet be unparsed, perhaps even still in transit on the network. Your script generally has access to any element declared in the source before the script tag containing/calling your script. This includes parent elements containing your script tag.

    2. Is it possible to hinder a script element in DOM from running using userscripts/content scripts or similar in chrome or firefox?

    No. All scripts are executed and one script can't prevent another script's initial execution. However, you can perhaps go back through and remove event handlers, and otherwise attempt to counteract the effects of a script. Although this scenario seems a bit shady and/or against the grain of normal JavaScript usage.