23

EDIT: Something was wrong with my Chrome browser and creating a conflict with my script, a full reinstall eliminated whatever the problem source was. If I happen to find out what was causing it I will include it in here.

EDIT2: Just to let anyone reading this in 2017 know that I haven't forgotten this and I have never had this problem since my previous edit.

EDIT3: It is 2019 and so far I've never had this problem again.


I have been learning how to create a simple Chrome extension which is a userscript port. The script works perfectly with Tampermonkey with the setting run at to document-start, all the necessary events that need to be caught from the beginning are all captured.

However, when I set the same settings in the Chrome extension I discovered that the same running setting is faster than Tampermonkey's which causes the first function to fail: (Uncaught TypeError: Cannot call method 'appendChild' of null.) since it tries to append a script element to the head section, which doesn't exist until 0.010s later.

My dirty solution so far has been to make use of a setInterval function with the timer set to 10 to check if document.head exists and then proceed with the code if the condition is true.

Is there any way that I can make this work correctly without having to resort to setInterval or maybe replicate Tampermonkey's grant none option which appears to run the userscript on the webpage context?

The following is my manifest.json file:

{
    "manifest_version": 2,
    "content_scripts": [ {
        "js":        [ "simpleuserscript.user.js" ],
        "matches":   [ "https://www.google.com/*"],
        "run_at":    "document_start"
    } ],
    "converted_from_user_script": true,
    "description":  "Chrome extension",
    "name":         "Testing",
    "version":      "1"
}

All of this could be avoided if Chrome would adopt the afterscriptexecute event, but until that happens I am stuck with the load event. I thank in advance any help provided.


EDIT: I have already tried the suggestions in the replies: using a different run at point, using DOMContentLoaded and append to document.documentElement. All were unsuccessful because: 1 and 2 makes the script miss early events, and 3 returns the same TypeError as when trying to append to document.head.

The script has to be inserted/running when document.readyState = loading or else it will miss early necessary events, but not so early to the point of being unable to append childs to either documentElementor head

An example of the code inside simpleuserscript.user.js:

var script = document.createElement("script");
script.textContent = "console.log('success')";
if(document.head) {
    document.head.appendChild(script);
} else if(document.documentElement) {
    document.documentElement.appendChild(script);
}

Console will show TypeError: Cannot call method 'appendChild' of null

Shadow
  • 4,168
  • 5
  • 41
  • 72

2 Answers2

40

Chrome extension Content scripts (run from a manifest.json) that are run at document_start, do fire before document.readyStateDoc has reached interactive -- which is the earliest you want to start messing with most page elements.

However, you can inject most <script> nodes right away if you wish. Just not to document.head or document.body because they don't exist yet.
Append to documentElement instead. For example:

var s = document.createElement ("script");
s.src = "http://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js";
s.async = false;
document.documentElement.appendChild (s);

Or

var s = document.createElement ("script");
s.src = chrome.extension.getURL ("MyPwnCode.js");
s.async = false;
document.documentElement.appendChild (s);

If you are adding or modifying other DOM elements, in a script running at document_start, wait until the DOMContentLoaded event like so:

document.addEventListener('DOMContentLoaded', fireContentLoadedEvent, false);

function fireContentLoadedEvent () {
    console.log ("DOMContentLoaded");
    // PUT YOUR CODE HERE.
    //document.body.textContent = "Changed this!";
}
Brock Adams
  • 90,639
  • 22
  • 233
  • 295
  • 1
    I saw a lot of negative replies so just want to say I was playing around with this for a long time and most of the erroneous results were because I was testing the script with the developer console open which seems to give a different result to when it's closed. I got it to work, injecting jquery source script into pages from an alternative source, I'm in China so Google CDN is blocked, and I used run_at document_start and documentElement to append the script node and it works fine. A little delayed as it tries to load the other script too I may work on this later but for now it works, thanks. – Jay Croghan Nov 21 '17 at 06:19
12

Your problem is that, when using "run_at": "document_start", the only element that is granted to exist in the DOM is the <html> element. If you want to avoid errors relative to page load, like trying to access some element that hasn't been created yet, you'll have to either:

  • Make your script run at "document_idle" or "document_end". Although "document_end" still doesn't grant you that all the elements and resources of the page have been fully loaded (but the DOM has already been parsed), the "document_idle" keyword will give you the certainty that the DOM has been parsed and all the elements and resources have been loaded properly before your script runs.

  • Or, instead, you can continue using "document_start" wrapping your code inside a "DOMContentLoaded" listener, which will make it run when the DOM has completely finished loading and parsing, similarly to "document_idle". Here's an example:

    document.addEventListener("DOMContentLoaded", function() {
        // Run your code here...
    });
    
Pacerier
  • 86,231
  • 106
  • 366
  • 634
Marco Bonelli
  • 63,369
  • 21
  • 118
  • 128
  • 7
    `document_idle` happens **after** `document_end` – Xan Jan 28 '15 at 08:04
  • 1
    @Xan quoting from the doc: "In the case of document_idle, the browser chooses a time to inject scripts between document_end and immediatly after the window.onload event fires [...]" so am I reading wrong or..? – Marco Bonelli Jan 28 '15 at 09:01
  • 2
    You're reading it right, but `window.onload` happens after `document_end`. `window.onload` implies [all resources have been loaded](https://developer.mozilla.org/en-US/docs/Web/API/GlobalEventHandlers.onload), and `document_end` fires already after DOM is complete (which is earlier). – Xan Jan 28 '15 at 09:04
  • 1
    Using `document_idle`or `document_end` make the script load too late, making it unable to capture early events. Using `DOMContentLoaded` yields the same problems, the script is inserted/run too late. – Shadow Jan 28 '15 at 18:57
  • @MarcoBonelli, But couldn't some other script on the page itself remove your listener? Thus using document_idle is better? – Pacerier Jul 15 '17 at 18:59
  • @Xan, If `document_start` is inserted after the last ``, and `document_end` is inserted in `DOMContentLoaded`, Doesn't that mean that if we do not use `document_start`, [`DOMContentLoaded` will not wait for ``](http://archive.is/wk5rW#selection-1013.0-1015.23), **Hence `document_end` will run before the time that `document_start` would have run?** – Pacerier Jul 15 '17 at 19:07
  • @Pacerier 1) you cannot remove a listener if you don't know the name of the function; 2) plus, the page doesn't know anything about your script, since that extension scripts run in a separated context and don't share any variable with the scripts in the page. So the answer is no. – Marco Bonelli Jul 15 '17 at 19:41
  • @MarcoBonelli, But the page can do `document.replaceChild(n=document.createElement('html'),document.documentElement);n.innerHTML='replacer'` if your script is at document_start. On the other hand, if you put at document_idle, he wouldn't be able to replace your injected listeners. – Pacerier Aug 07 '17 at 19:08
  • @Xan, Ah I'v realized now. That "[CSS](http://archive.is/m7For#selection-5667.63-5669.3)" does not refer to – Pacerier Aug 07 '17 at 19:08