Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7646801
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T10:18:05+00:00 2026-05-31T10:18:05+00:00

I’m trying to create an extension to modify an existing site, and fix a

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T10:18:07+00:00Added an answer on May 31, 2026 at 10:18 am

    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 Date object, which can be done using the code below:

    (function(global) {
        var _Date = global.Date; /* Store original Date object */
        var overwriteafterXmatches = 1; /* Overwrite after X matches*/
        function date(year, month, day, hour, minute, second, millisecond) {
            var tmp = /^([A-Z][a-z]{2}, \d{2} [A-Z][a-z]{2} \d{4}) \d{2}:\d{2}:\d{2} GMT$/.exec(year);
            switch(arguments.length) {
                case 0:
                    return new _Date();
                case 1:
                    if (tmp) { /* If match */
                        tmp = new _Date(match[1]);
                        if (--overwriteAfterXmatches <= 0) {
                            /* Set the original Date object*/
                            global.Date = _Date;
                        }
                    }
                    return new _Date(year);
                case 2:
                    return new _Date(year, month);
                case 3:
                    return new _Date(year, month, day);
                case 4:
                    return new _Date(year, month, day, hour);
                case 5:
                    return new _Date(year, month, day, hour, minute);
                case 6:
                    return new _Date(year, month, day, hour, minute, second);
                default:
                    return new _Date(year, month, day, hour, minute, second, millisecond);
            }
        }
        /* Overwrite global Date object*/
        global.Date = date;
    })(window);
    

    contentscript.js

    Content 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:

    var code = "\
    (function(global) {\
        var _Date = global.Date; /* Store original date object*/\
        var overwriteafterXmatches = 1; /* Overwrite after X matches*/\
        function date(year, month, day, hour, minute, second, millisecond) {\
            var tmp = /^([A-Z][a-z]{2}, \d{2} [A-Z][a-z]{2} \d{4}) \d{2}:\d{2}:\d{2} GMT$/.exec(year);\
            switch(arguments.length) {\
                case 0:\
                    return new _Date();\
                case 1:\
                    if (tmp) { /* If match */\
                        tmp = new _Date(match[1]);\
                        if (--overwriteAfterXmatches <= 0) {\
                            /* Set the original Date object*/\
                            global.Date = _Date;\
                        }\
                    }\
                    return new _Date(year);\
                case 2:\
                    return new _Date(year, month);\
                case 3:\
                    return new _Date(year, month, day);\
                case 4:\
                    return new _Date(year, month, day, hour);\
                case 5:\
                    return new _Date(year, month, day, hour, minute);\
                case 6:\
                    return new _Date(year, month, day, hour, minute, second);\
                default:\
                    return new _Date(year, month, day, hour, minute, second, millisecond);\
            }\
        }\
        /* Overwrite global Date object*/\
        global.Date = date;\
    })(window);\
    ";
    var script = document.createElement('script');
    script.appendChild(document.createTextNode(code));
    (document.head||document.documentElement).appendChild(script);
    script.parentNode.removeChild(script);
    

    manifest.json

    Here’s an example of the manifest.json file. Replace <all_urls> with a more specific match pattern.

    {
      "name": "Test",
      "version": "1.0",
      "content_scripts": [
        {
          "matches": ["<all_urls>"],
          "js": ["contentscript.js"],
          "run_at": "document_start"
        }
       ],
       "permissions": ["<all_urls>"]
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Basically, what I'm trying to create is a page of div tags, each has
I am trying to render a haml file in a javascript response like so:
I'm trying to create an if statement in PHP that prevents a single post
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
Is it possible to replace javascript w/ HTML if JavaScript is not enabled on
I am trying to understand how to use SyndicationItem to display feed which is
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I used javascript for loading a picture on my website depending on which small

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.