capture window.alert, confirm from the extension javascript

Talk about add-ons and extension development.
Post Reply
vsrini
Posts: 4
Joined: October 5th, 2005, 3:11 pm

capture window.alert, confirm from the extension javascript

Post by vsrini »

Is there a way I can capture window.alert and window.confirm raised by the documents loaded in the page?

I tried,
window.alert = function(txt) {
// my code here
}

but this works only for code within my extension. How can I extend this to the page that is loaded?

Thanks,
Vinay
asqueella
Posts: 4019
Joined: November 16th, 2003, 3:05 am
Location: Russia, Moscow

Post by asqueella »

rewrite content.alert each time a page is loaded.
Ihoss
Posts: 376
Joined: July 4th, 2004, 11:11 am

Post by Ihoss »

vsrini
Posts: 4
Joined: October 5th, 2005, 3:11 pm

Post by vsrini »

Thanks for the quick reply,

As a test, I tried to do this,

content.alert = function (txt) {
window.document.location = "http://www.google.com";
}

This seemed to have no effect on the document alerts!
I also tried to just suppress the alert by emptying the function, did not help either.

But if i have the same code in the document's <script> tag, it works fine, In extension's code, it doesn't
asqueella
Posts: 4019
Joined: November 16th, 2003, 3:05 am
Location: Russia, Moscow

Post by asqueella »

Did you do this in a page load handler?
vsrini
Posts: 4
Joined: October 5th, 2005, 3:11 pm

Post by vsrini »

Yes, i have gBrowser.addProgressListener(myobject) set,

when the page loads, i call myotherobject.pageLoaded(), where I try to rewrite alert for that page.
vsrini
Posts: 4
Joined: October 5th, 2005, 3:11 pm

Post by vsrini »

Also, wanted to know if this is possible, this also does not seem to work.

Code: Select all

var promptService = Components.classes["@mozilla.org/embedcomp/prompt-service;1"].getService(Components.interfaces.nsIPromptService);

var promptServiceAlert = promptService.alert;

// override prompt service alert
promptService.alert = function (parent , dialogTitle, text) {
    promptServiceAlert(parent, dialogTitle + "my custom text : ", text + "append");
}
asqueella
Posts: 4019
Joined: November 16th, 2003, 3:05 am
Location: Russia, Moscow

Post by asqueella »

Replacing alert function of prompt service certainly isn't going to work. As far as I can see (using the JS Shell of Extension Developer's Extension), replacing content.alert works. So I think you're just doing something wrong.
Post Reply