最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin2浏览0评论

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)

发布评论

评论列表(0)

  1. 暂无评论