I’m trying to create an extension to modify an existing site, and fix a bug in the existing javascript. A simple replace on the text contents of the document before the load would fix the bug — specifically, hard-coded values in a tag in the header — and my first attempt (removing the timestamps from date values) was something like this:
document.documentElement.innerHTML = document.documentElement.innerHTML.replace(/("date_of_birth": new Date\("[A-Z][a-z]{2}, \d{2} [A-Z][a-z]{2} \d{4}) \d{2}:\d{2}:\d{2} GMT/g, "$1");
For example, any instance of "date_of_birth": new Date("Tue, 30 Oct 1984 00:01:00 GMT") becomes "date_of_birth": new Date("Tue, 30 Oct 1984")
The problem is that the only way I know of to access the HTML contents of the document is document.documentElement.innerHTML. Running this at document_start fails, as the DOM hasn’t been loaded yet, so documentElement.innerHTML is an empty string. Running the above code after the DOM loads will be too late, as the initializing scripts have already run, and modifying the document’s html at that point reloads those scripts, causing all kinds of havoc in the script-heavy page.
I cannot simply append to the documentElement before the DOM is loaded, as was the most common suggestion when searching for a solution. This needs to modify the existing scripts before they are loaded and run.
Is there any way to access and preprocess the raw contents of the HTML document before it is loaded?
Thanks in advance.
There is no way to “edit” the contents of
<script>before it is executed: Once the closing</script>tag is encountered, the contents is evaluated right away.Instead of modifying the code itself, an indirect approach can be used: Overwrite the global
Dateobject, which can be done using the code below:contentscript.jsContent scripts run in an isolated environment. That means that you cannot modify global properties directly. To get the code to work in a Content script, you have to inject a
<script>tag, in this way:manifest.jsonHere’s an example of the
manifest.jsonfile. Replace<all_urls>with a more specific match pattern.