6

Calling alert() from the background script is allowed in Google's Chrome, but not in Firefox (WebExtensions) which I am porting my Chrome extension to.

So, I need a workaround to get an alert dialog up. I'm not asking for anything else other than THE alert dialog.

Sending a message to a content script to call alert() will not suffice since no content scripts may be loaded at the time the alert call is needed.

AlJo
  • 161
  • 1
  • 13
  • The way that you have worded this it sounds like you will accept no solution other than the **actual** `alert()` dialog, not even something that looks and feels like it. If so, then you are SOL. The text which is output in the console "alert() is not supported in background windows; please use console.log instead." is clear. `alert()` is not supported in background scripts. If you want more information, you might check [Bug 1203394 - alert() does not work in background scripts](https://bugzilla.mozilla.org/show_bug.cgi?id=1203394) which is RESOLVED FIXED with this console output as the result. – Makyen Aug 15 '16 at 14:26
  • To be clear: Will you accept a substitute which works in both Firefox and Google Chrome which comes close to looking and acting like an `alert()`? – Makyen Aug 15 '16 at 14:28
  • Also to be clear: it is not acceptable to you to have as a solution that a content script be loaded to accomplish the `alert()`. This is not saying send a message to an already loaded content script, but actually loading one specifically to handle this issue. This is not an acceptable solution to you, correct? – Makyen Aug 15 '16 at 14:32
  • @Makyen Give it a shot. – AlJo Aug 15 '16 at 16:44
  • 2
    @Makyen I know alert() is not supported in bg scripts. That's why the question is asking for a **workaround**. Clear? – AlJo Aug 15 '16 at 23:39
  • But then you say "*I'm not asking for anything else other than THE alert dialog.*" That statement contradicts looking for a workaround that gives a reasonable feel to the user that is close to an `alert()` dialog. It implies the only thing that is acceptable to you is "THE alert dialog" and you want some way to trigger *that* code within a background script (unlikely to happen). That statement is why I asked for clarification. If you want something that is *close* to an `alert()` dialog, then that is possible. – Makyen Aug 16 '16 at 00:18
  • 2
    @Makyen, you're making rash unimaginative assumptions. If you can't answer the question as stated, don't let that bother you; just move along. – AlJo Aug 16 '16 at 02:52

2 Answers2

5

My workaround is to save the alert code in a string like:

var alertWindow = 'alert(message)';

and let the background script inject that code like:

browser.tabs.executeScript({code : alertWindow});
S.I.
  • 3,250
  • 12
  • 48
  • 77
GrafZahl
  • 59
  • 1
  • 2
  • 4
    This hack does not work, when the active tab is e.g. an "about:" page, new tab and other similar situations, and that's precisely where I need to show an error message and suggestion how to work around the problem. Any other ideas? – gregko Oct 12 '17 at 18:29
  • 1
    Note that you need host permissions to inject into the current page. Thankfully, most of the time `activeTab` permission is sufficient. gregko's use case needs more work, however. An option would be to open a visible extension page for this in case the injection fails; clunky for sure. – Xan Nov 14 '18 at 13:25
0

Background scripts and pages in Firefox unfortunately do not support the prompt(), alert() and confirm() functions of the DOM window object (source). However, a pretty good replacement for the alert() function is the browser.notifications API.

To create a drop-in replacement, add the notifications permission to your manifest.json:

  "permissions": [
      "notifications"
  ]

And add a function like this to your background script:

function alert(msg) {
    browser.notifications.create({
        type : 'basic',
        message : msg,
        title : 'Extension alert'
    });
}

Now, whenever you call alert() in your extensions background script it will show a notification.

Husky
  • 5,757
  • 2
  • 46
  • 41