user3399738
user3399738

Reputation: 35

Show an alert from a Firefox extension when a browser window opens

I'm trying to make a very simple Firefox extension. I need it to show an alert box when the Firefox window opens. The message doesn't show up when I open the window but it does when I reload all chrome (through the Extensions Developer Add-On).

My overlay file:

<?xml version="1.0"?>
<overlay xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">
    <script type="application/x-javascript" src="chrome://adTest/content/alert.js" />
</overlay>

My script file:

alert("HI!");

My chrome.manifest file:

content adTest content/ contentaccessible=yes
overlay chrome://browser/content/browser.xul chrome://adTest/content/adTestOverlay.xul

I'm pretty sure the rest of the code is correct because I've added XUL elements for testing purposes and everything worked apart from the alert box.

Upvotes: 3

Views: 3862

Answers (2)

Noitidart
Noitidart

Reputation: 37328

Can also do:

Services.prompt.alert(null, 'title of alert', 'alert msg');

In place of null you can supply window and that will make that window modal and unselectable while that alert is showing (just like normal alert)

Upvotes: 1

nmaier
nmaier

Reputation: 33192

You cannot display alert()s before the browser window is actually loaded and displayed, because the alert dialog has to have a fully-initialized and visible parent window. Your overlay script will however be run during the load/initialization already...

The Browser Console should show an error saying NS_ERROR_NOT_AVAILABLE: Cannot call openModalWindow on a hidden window (but turns out, only when alert is called from within the load event handler).

So, first wait for the load event and then give the event loop a chance to actually show the window, e.g.

addEventListener("load", function() {
    setTimeout(function() { alert("something"); }, 0);
});

Upvotes: 4

Related Questions