Skip to content

Instantly share code, notes, and snippets.

@cou929
Created January 5, 2011 13:52
Show Gist options
  • Save cou929/766332 to your computer and use it in GitHub Desktop.
Save cou929/766332 to your computer and use it in GitHub Desktop.
Sample code for progress listener.
/*
* progress_listener.js
* Kosei Moriyama <[email protected]>
*
* Sample code for Firefox Extension.
* Do something when uri is changed using progress listener.
*/
window.addEventListener('load', function() { myExtension.init(); }, false);
window.addEventListener("unload", function() { myExtension.uninit(); }, false);
var myExtension = {
// store old url
oldUrl: null,
// implement nsIWebProgress interface
urlBarListener: {
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;
},
// called when uri of document of current tab is changed
onLocationChange: function(aProgress, aRequest, aUri) {
myExtension.processNewUrl(aUri);
},
onStateChange: function() {},
onProgressChange: function() {},
onStatusChange: function() {},
onSecurityChange: function() {},
onLinkIconAvailable: function() {}
},
// add progress listener at first
init: function() {
gBrowser.addProgressListener(myExtension.urlBarListener, Components.interfaces.nsIWebProgress.NOTIFY_STATE_DOCUMENT);
},
// remove progress listener at last
uninit: function() {
gBrowser.removeProgressListener(myExtension.urlBarListener);
},
// do something when uri is changed
processNewUrl: function(aUri) {
if (aUri.spec == myExtension.oldUrl)
return;
// write your code here
myExtension.oldUrl = aUri.spec;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment