javascript - Show an alert from a Firefox extension when a browser window opens - Stack Overflow

I'm trying to make a very simple Firefox extension. I need it to show an alert box when the Firefo

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=".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.

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/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.

Share Improve this question edited Jul 10, 2014 at 16:24 nmaier 33.2k5 gold badges65 silver badges79 bronze badges asked Jul 10, 2014 at 15:39 user3399738user3399738 351 silver badge4 bronze badges 2
  • Can you put content of "chrome.manifest" here? – Hassan Khodadadeh Commented Jul 10, 2014 at 15:43
  • I have added chrome.manifest. – user3399738 Commented Jul 10, 2014 at 15:50
Add a ment  | 

2 Answers 2

Reset to default 4

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);
});

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)

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745530377a4631666.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信