MozillaZine

Just Before Page Load Event?

Talk about add-ons and extension development.
chrispederick

User avatar
 
Posts: 243
Joined: February 6th, 2003, 1:45 pm

Post Posted February 4th, 2004, 12:34 pm

Okay, another question about events in Mozilla and Firebird...

What I am looking for is an event that occurs just as a new page has been requested - an 'onBeforeLoad' event if you will. Is there something like this?

Basically, I want to be able to work out the URL of the page about to be loaded and then run some code based on the new URL before the page loads.

Any help is greatly appreciated...

Cheers,
Chris
<b>Extensions</b>:
[<a href="http://chrispederick.com/work/useragentswitcher/">User Agent Switcher</a> | <a href="http://chrispederick.com/work/webdeveloper/">Web Developer</a>]

©dn

User avatar
 
Posts: 683
Joined: April 20th, 2003, 1:53 pm
Location: GB

Post Posted April 27th, 2004, 3:29 pm

According to an email :
Johnny Stenback wrote:Yeah, to get something like a onbeforeload event in Mozilla you'd need to hook into nsIWebProgress and listen for "STATE_IS_DOCUMENT | STATE_START". And be sure to unregister your etension when a window is closed to avoid leaks, as usual.

Perhaps we can help each other ?

I have little knowledge of nsI*; although having looked at the .idl files produced by my cvs builds, I think nsIWebProgressListener might be the one to look at ?

Unregistering ?

: ]

MonkeeSage

User avatar
 
Posts: 1011
Joined: December 20th, 2002, 8:15 pm

Post Posted April 27th, 2004, 6:12 pm

Don't hold my feet to the fire on this, but I think the LocationChange event fires before the loading starts, you might give that a try...if not perhaps the StateChange event is what you want...check out nsIWebProgressListener interface reference.


Shelumi`El
Jordan

S.D.G
"[M]en are usually satisfied with bad argument only when their convictions rest on other grounds." --John Oman

raphinou
 
Posts: 33
Joined: January 7th, 2003, 4:04 am

Post Posted April 28th, 2004, 9:01 am

If you get it to work, it would be great if you posted your solution here. I'm interested too! :-)

Raph

Torisugari
 
Posts: 1634
Joined: November 4th, 2002, 8:34 pm
Location: Kyoto, Nippon (GMT +9)

Post Posted June 5th, 2004, 5:25 pm

*bump*

cdn:
By "unregistering" he means nsIWebProgress :: removeProgressListener(aListener).
http://lxr.mozilla.org/mozilla/source/u ... ogress.idl

In terms of XUL, browser and tabbrowser have a nsIWebProgress interface.
http://www.xulplanet.com/references/ele ... owser.html
You can confirm it in this way.
Code: Select all
alert(gBrowser.webProgress);


i.e.
1. Create a javascript object "myListener" that implements nsIWebProgressListener.
2. gBrowser.addProgressListener(myListener);( or gBrowser.webProgress.addProgresListener(myListener))
3. Don't forget to gBrowser.removeProgressListener(myListener) on close the windw.

MonkeySage:
onLocationChange is after loading the page. So that this event will be fired only once if being redirected 10 times.

Sample overlay on Browser/Navigator.
Code: Select all
const NOTIFY_STATE_DOCUMENT =
  Components.interfaces.nsIWebProgress.NOTIFY_STATE_DOCUMENT;
const STATE_IS_DOCUMENT =
  Components.interfaces.nsIWebProgressListener.STATE_IS_DOCUMENT;
const STATE_START =
  Components.interfaces.nsIWebProgressListener.STATE_START;

function registerMyListener()
{
  window.getBrowser().addProgressListener(myListener , NOTIFY_STATE_DOCUMENT);
}

function unregisterMyListener()
{
  window.getBrowser().removeProgressListener(myListener);
}

window.addEventListener("load",registerMyListener,false);
window.addEventListener("unload",unregisterMyListener,false);

var myListener =
{
  onStateChange:function(aProgress,aRequest,aFlag,aStatus)
  {
    if(aFlag & (STATE_IS_DOCUMENT|STATE_START))
    {
      aRequest.QueryInterface(Components.interfaces.nsIChannel);
      alert("Wait a moment!\n"+aRequest.URI.spec);
    }
  },
  onLocationChange:function(a,b,c){},
  onProgressChange:function(a,b,c,d,e,f){},
  onStatusChange:function(a,b,c,d){},
  onSecurityChange:function(a,b,c){},

  /*XXX
    This is not nsIWebProgressListenr method,
    just killing a error in tabbrowser.xml
    Maybe a bug.
  */
  onLinkIconAvailable:function(a){}
}

©dn

User avatar
 
Posts: 683
Joined: April 20th, 2003, 1:53 pm
Location: GB

Post Posted June 6th, 2004, 2:11 am

Will this also work in Thunderbird / Mozmail ?

I've just run this code-snippet in URIid code, and I think it fires too early for my purposes : ]

In URIid I'm trying to add an ID / classes to the body element of a page, but at the stage that this event handler fires there is no body : )

That code should suit OP fine.

Torisugari
 
Posts: 1634
Joined: November 4th, 2002, 8:34 pm
Location: Kyoto, Nippon (GMT +9)

Post Posted June 8th, 2004, 4:40 am

Hmm, TB seems a bit more strict than Fx... the difference between browser and tabbrowser.

myListener version 2.0
Code: Select all
const STATE_START =
  Components.interfaces.nsIWebProgressListener.STATE_START;

var myListener =
{
  QueryInterface : function(aIID)
  {
    if (aIID.equals(Components.interfaces.nsIWebProgressListener) ||
        aIID.equals(Components.interfaces.nsISupportsWeakReference) ||
        aIID.equals(Components.interfaces.nsISupports))
      return this;
    throw Components.results.NS_NOINTERFACE;
  },
  onStateChange:function(aProgress,aRequest,aFlag,aStatus)
  {
    if(aFlag & STATE_START)
    {
      aRequest.QueryInterface(Components.interfaces.nsIChannel);
      alert("Wait a moment!\n"+aRequest.URI.spec);
    }
  },
  onLocationChange:function(a,b,c){},
  onProgressChange:function(a,b,c,d,e,f){},
  onStatusChange:function(a,b,c,d){},
  onSecurityChange:function(a,b,c){},
  onLinkIconAvailable:function(a){}
}


onload TB
Code: Select all
const NOTIFY_STATE_DOCUMENT = Components.interfaces.nsIWebProgress.NOTIFY_STATE_DOCUMENT;

window.getMessageBrowser().webProgress.addProgressListener(myListener,NOTIFY_STATE_DOCUMENT);

onunload TB
Code: Select all
window.getMessageBrowser().webProgress.removeProgressListener(myListener);



In URIid I'm trying to add an ID / classes to the body element of a page, but at the stage that this event handler fires there is no body : )

Ah.

©dn

User avatar
 
Posts: 683
Joined: April 20th, 2003, 1:53 pm
Location: GB

Post Posted July 22nd, 2004, 1:34 pm


kadnan
 
Posts: 30
Joined: November 30th, 2004, 1:18 am
Location: Pakistan

Post Posted May 21st, 2006, 4:42 am

OK i tried and could intercept addressbar url and change it but load event still loads the page which was entered by user.

Is there not anyway to redirect browser to newly set URL in OnState or onLocationChange event?

how cna i navigate new Url?

i tried content.location.href in LOAD event of Eventlistener but it doesnt work at all

Danas
 
Posts: 25
Joined: March 28th, 2008, 5:27 am

Post Posted April 6th, 2008, 10:07 pm

please share the solution to the above issue
Dana

max1million
 
Posts: 2810
Joined: November 15th, 2004, 5:03 am

Post Posted April 7th, 2008, 6:07 am

I usually content.document.location.href but this should work to stop a load and load new uri in current tab.
Code: Select all
var uri = "about:blank";
gBrowser.stop();
gBrowser.loadURI(uri);

brody
 
Posts: 890
Joined: October 20th, 2003, 1:24 pm

Post Posted April 10th, 2008, 12:02 am

Torisugari wrote:Hmm, TB seems a bit more strict than Fx... the difference between browser and tabbrowser.


@Torisugari

I don't have any experience with progress listeners in Thunderbird, but it may be of interest to you to check out tabbrowser.xml from toolkit.jar from any of the browsers to see that using gBrowser.addProgressListener and gBrowser.webProgress.addProgressListener are somewhat different.

gBrowser.webProgress.addProgressListener == function() { [native code] } behaves somewhat differently than gBrowser.addProgressListener which actually calls a method of tabbrowser defined in tabbrowser.xml and adds the new listener to an array with other listeners and then parameters are sequentially passed to each listener in the array when progress occurs while only using only one listener added via gBrowser.webProgress.addProgressListener.

I can't remember right off hand what the specific results are in regards to how you go about adding a listener. I believe that one major difference is that when I used gBrowser.addProgressListener the listener I added fired for document and image requests and the corresponding aRequest was passed to my listener methods. However when using gBrowser.webProgress.addProgressListener I was only notified of document requests and aRequest was always null.

As I said I am unsure as to the specifics (it has been a while) - I just know that it seemed kind of quirky. You can try it out yourself using console dumps and checking to see if you are getting valid requests as a parameter and what type of requests you are getting.

dn wrote:I've just run this code-snippet in URIid code, and I think it fires too early for my purposes.
In URIid I'm trying to add an ID / classes to the body element of a page, but at the stage that this event handler fires there is no body.


@dn

You would probably accomplish this best by adding your styles/elements when the "DOMContentLoaded" event fires (after the DOM is completely loaded and constructed).

You can add/remove your styles like so:
Code: Select all
    addStyle: function(aDocument) {
        if(aDocument.myStyleAdded) return;
        var head = aDocument.getElementsByTagName('head')[0];
        if(head) {
            var style = aDocument.createElement("style");
            style.setAttribute("type","text/css");
            style.setAttribute("title","myElementStyle");
            style.innerHTML = ".myElementStyle { [add style properties here] }";
            head.appendChild(style);
            aDocument.myStyleAdded = true;
        }
    },

    removeStyle: function(aDocument) {
        if(!aDocument.myStyleAdded) return;
        var head = aDocument.getElementsByTagName('head')[0];
        if(head) {
            var styles = aDocument.getElementsByTagName('style');
            var style = null;
            for(var i = 0; i < styles.length; i++) {
                style = styles[i];
                if(style.getAttribute('title') == 'myElementStyle') {
                    head.removeChild(style);
                    delete aDocument.myStyleAdded;
                    break;
                }
            }
        }
    },

There is a thread here discussing this subject (which I remember posting this in), but unfortunately I was unable to locate it in "My Posts". If you can find it there is some discussion there regarding just what you are trying to do. However it was posted more than 3 months ago (which unfortunately is as far back as you can search).

Return to Extension Development


Who is online

Users browsing this forum: No registered users and 0 guests