Closed Bug 424854 Opened 16 years ago Closed 16 years ago

Add a way to "inject" JS into a document earlier than onload

Categories

(Core :: DOM: Events, enhancement)

x86
Windows XP
enhancement
Not set
normal

Tracking

()

RESOLVED DUPLICATE of bug 342715

People

(Reporter: mkaply, Unassigned)

Details

Best way to describe this is a use case.

If you wanted to emulate the JS functionality of other browsers, you would typically create a component and add new JS functionality to the category manager.

However, if you wanted some of that functionality to be site specific, you couldn't do it in the category manager - you'd want it to only be there for a certain site.

To do this, you would want to listen for events and then at the right time, inject the javascript.

Unfortunately, the earliest you can do this today is onload or DOMContentLoaded. This is too late, since a lot of pages check for other browser APIs earlier than that.

There should be a way to listen for an event that is early enough that you can get the page JS context and put new APIs into it before the page starts parsing JS.

Attached is an attempt from OpenSUSE to solve this problem (this is open source and in the source code for OpenSUSE)

It can be utilized with a listener like this:

browser.addProgressListener(foo, I.nsIWebProgress.NOTIFY_STATE_DOCUMENT);

and then in the onStateChange

if (flags & Components.interfaces.nsIWebProgressListener.STATE_TRANSFERRING)
{
  if webprogress.DOMWindow.location.href is a page I want to do this on, 

get webprogress.DOMWindow and do magic with eval, etc.
    
}


Is this a good/bad idea? Is there a better way to do this?
This could be fun for Greasemonkey too.
Didn't realize there was already a bug.
Status: NEW → RESOLVED
Closed: 16 years ago
Resolution: --- → DUPLICATE
You need to log in before you can comment on or make changes to this bug.