How to run a JavaScript fragment right before the onload event (Google Chrome extension)

What I'm trying to do is write a Chrome extension that will add a Javascript snippet that will fire after all the Javascript code has been run on the page, but before the onload event occurs. (There is a code on the page that has an event for the onload event). I tried everything I thought about my extension, but I did not find a consistent way to do this with the Google Chrome extension.

I tried to set the run_at value to both "document_start" and "document_end" along with adding this fragment to both the head and body, both to <script > </script > with internal html and <script > </script > with using src pointing to the file in the extension. Nothing works sequentially.

Has anyone been lucky with this or with thoughts on how to act?


UPDATE!

I have made some progress, but now I hit another snag. I have a set of extensions for run_at document_start and it always fires before the script loads. Then I add an event listener for the DOMContentLoaded event, and then send a request to my background page (to get the currently selected options so that I know how to change the script on the page).

The problem is that sometimes the event fires before I get my answer from the man page. When I get the answer before DOMContentLoaded , everything works. Since this is asynchronous, I have not found a way to somehow wait for this answer.

Does anyone have any thoughts on how to proceed?

+4
source share
2 answers

One naive solution would be to put your script just before closing the body tag. Thus, you are sure that all scripts are loaded and that no onLoad has not yet been called

+1
source

I have not tried, but using the defer attribute should work.

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

It has been working in WebKit, but only since last month, so it will take some time until it reaches the stability of Chrome.

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

You can try Chrome Canary or a recent snapshot of Chromium.

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

You may also need to install a document such as HTML5.

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

0
source

All Articles