Need workaround for calling alert() from background script
Asked Answered
T

2

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.

Teetotaler answered 15/8, 2016 at 6:30 Comment(7)
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 which is RESOLVED FIXED with this console output as the result.Colum
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()?Colum
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?Colum
@Colum Give it a shot.Teetotaler
@Colum I know alert() is not supported in bg scripts. That's why the question is asking for a workaround. Clear?Teetotaler
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.Colum
@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.Teetotaler
M
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});
Microcosm answered 9/1, 2017 at 11:8 Comment(2)
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?Tamanaha
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.Normandnormandy
L
1

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.

Lucero answered 12/10, 2022 at 9:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.