Skip to content

Instantly share code, notes, and snippets.

@hugs
Created January 12, 2010 06:19
Show Gist options
  • Save hugs/274958 to your computer and use it in GitHub Desktop.
Save hugs/274958 to your computer and use it in GitHub Desktop.
Killing popup dialogs in Firefox
// 1) Install Firebug
// 2) In a new browser window, open this URL:
// chrome://global/content/commonDialog.xul
// (It doesn't matter the particular .xul file you open, as long as it is a valid XUL file.)
// 3) Launch JavaScript Shell in Firebug
// 4) In the new shell window, type the following commands:
var wm = Components.classes["@mozilla.org/appshell/window-mediator;1"]
.getService(Components.interfaces.nsIWindowMediator);
var enumerator = wm.getEnumerator(null);
// Get a handle to the current window
var win = enumerator.getNext();
// View the "Before" alert.
win.alert("Hello");
var ww = Components.classes["@mozilla.org/embedcomp/window-watcher;1"]
.getService(Components.interfaces.nsIWindowWatcher);
var watcher = {
observe: function(subject, topic, data) {
if (topic == "domwindowopened") {
// unfortunately you can't get anything useful from subject here.
// You need to wait for it to load. The best way to do
// that is to hook it's load event:
subject.addEventListener("load", function() { _firebug.log("hello, there was a load event"); }, false);
// as a proof of concept, I did:
window.setTimeout(function() { _firebug.log(subject.location.href) }, 0);
}
}
};
ww.registerNotification(watcher);
// Now we can "watch" for the alert when it happens...
win.alert("Hello!")
// Don't forget to unregister the watcher before you modify it!
ww.unregisterNotification(watcher)
// Now there shouldn't be a watcher watching...
win.alert("Hello!")
// Let's modify the watcher to close the window right after it appears...
var watcher = {
observe: function(subject, topic, data) {
if (topic == "domwindowopened") {
// unfortunately you can't get anything useful from subject here.
// You need to wait for it to load. The best way to do
// that is to hook it's load event:
subject.addEventListener("load", function() { _firebug.log("hello, there was a load event"); }, false);
// close the modal dialog window
window.setTimeout(function() { _firebug.log("goodbye, now closing the dialog window"); subject.close(); }, 1000);
}
}
};
ww.registerNotification(watcher);
// The alert should now close right after it appears
win.alert("Hello!")
// Yeah!!!
// Don't forget to unregister the watcher, again!
ww.unregisterNotification(watcher)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment