Reputation: 171
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.
Upvotes: 6
Views: 2749
Reputation: 6196
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.
Upvotes: 1
Reputation: 59
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});
Upvotes: 5