views:

92

answers:

2

What I'm trying to do is write a Chrome extension that inserts a snippet of Javascript that will fire after all the Javascript code on the page has run, but before the onload event fires. (There is some code on the page that has an event listener for the onload event). I've tried all that I've thought of for my extension, but I haven't found a consistent way to do this with a Google Chrome Extension.

I've tried setting the run_at value to both "document_start" and "document_end", along with appending this snippet to both the head and the body, both as a <script></script> with inner html and a <script></script> with a src pointing to a file in the extension. Nothing consistently works.

Has anybody had any luck with this or any thoughts on how to proceed?


UPDATE!

I've made some progress, but now I've hit another snag. I have the extension set to run_at document_start, and it is always firing before the script is loaded. Then I add an event listener for the event DOMContentLoaded, then send a request to my background page (to get the currently selected options so I know how to modify the script on the page).

The issue now is that sometimes, the event fires before I receive my response from the background page. When I receive my response before DOMContentLoaded, everything works. Since this is asynchronous though, I haven't found a way to somehow force a wait for this response.

Does anybody have any thoughts of how to proceed?

+1  A: 

One naive solution would be to put your script just right before you close the body tag. In that way you are sure that all the scripts are loaded and that no onLoad has been called yet

Ed.C
I've attempted this in my extension to no avail. If I use document_start, then I don't have access to the body element to append to. If I do document_end, then sometimes the onload event has already fired before my extension. Thanks for the recommendation though.
Datin
I'm not familiar with chrome extensions tbh. My logic says that `document_end` is probably added after the body tag has closed, meaning that the `onLoad` event for the body has already fired. Good luck! :)
Ed.C
Yeah, that was the conclusion that I came to. After playing around with document_start though, I've made some progress! I've updated the original question with the details.
Datin
A: 

I haven't tried but using the defer attribute should work.

http://dev.w3.org/html5/spec-author-view/scripting-1.html#attr-script-defer

It works in WebKit, but only since last month so it'll take a while until it reaches Chrome stable.

http://webkit.org/blog/1395/running-scripts-in-webkit/

You can try Chrome Canary or a recent Chromium snapshot.

http://tools.google.com/dlpage/chromesxs
http://build.chromium.org/buildbot/snapshots/

It might also require setting a HTML5 doctype.

http://www.w3.org/TR/html5-diff/#doctype

pdknsk
Thank you for the recommendation, but it doesn't appear to be working. From what I can tell, this is an attribute that is supported by Internet Explorer only.http://www.w3schools.com/tags/att_script_defer.asp
Datin
I updated based on your comment.
pdknsk
Thank you for the update pdknsk. Unfortunately, this extension is going to be used by a few people and forcing them to upgrade to the latest snapshop of Chromium isn't really an option. However, I have updated my question as progress has been made. We're almost there!
Datin