The target is get a WebExtension for Firefox that can be activated/deactivated by a user in the toolbar, like an on/off switch.
I'm using a background.js with this code:
browser.browserAction.onClicked.addListener(function (tab) {
switch (button) {
case 'turn-on':
enable();
break;
case 'turn-off':
disable();
break;
}
});
function enable() {
browser.browserAction.setIcon({ path: '/ui/is-on.png', });
browser.browserAction.setPopup({ popup: '/ui/turn-off.js', });
browser.webNavigation.onCommitted.addListener(onTabLoad);
}
function disable() {
browser.browserAction.setIcon({ path: '/ui/is-off.png', });
browser.browserAction.setPopup({ popup: '/ui/turn-on.js', });
browser.webNavigation.onCommitted.removeListener(onTabLoad);
}
function onTabLoad(details) {
browser.tabs.executeScript(details.tabId, {
file: '/gc.js',
allFrames true,
});
}
enable(); // enable or disable by default
Obviously I'm doing something wrong. I'm kind newbie in coding. This is a personal project I'm trying to finish.
Your code
While you added a
switch
statement to switch onbutton
, you never definedbutton
, nor changed its state. You also did not have a default case, just in case thebutton
variable was not one of the values for which you were testing in yourcase
statements.You should not be using
browserAction.setPopup()
to set a popup. Setting a popup will result in the popup being opened instead of your background page receiving aclick
event. In addition, the popup needs to be an HTML page, not JavaScript.See the section below for the Firefox bug which you need to work around in
onTabLoad()
.Listening to
webNavigation.onCommitted
is not sufficient to cover all cases of when your script will need to be injected. In other words,webNavigation.onCommitted
does not fire every time a page is loaded. To fully cover every situation where your script will need to be injected is something that you will need to ask in another question.Workaround for a Firefox
webNavigation.onCommitted
bugThere is a change needed to your
onTabLoad()
code for using awebNavigation.onCommitted
listener to inject scripts usingtabs.executeScript()
in Firefox (this is not needed in Chrome). This is due to a bug in Firefox which causestabs.executeScript()
to fail if executed immediately from awebNavigation.onCommitted
listener. The workaround I use is to inject the script after asetTimeout(function,0)
delay. This allows Firefox to execute the code needed to set up the environment necessary forexecuteScript()
to be functional.Generalized solution for multi-state buttons (e.g. a toggle button)
The code I use to make a Browser Action button behave like a toggle is below. I have modified the
browserButtonStates
Object, which describes both what the buttons do and what they look like, to add and remove yourwebNavigation.onCommitted
listener,onTabLoad()
. See above for the issues withonTabLoad()
.The code below is more complex than what you need. I wrote it intending to be able to move it from project to project with only needing to change the contents of the
browserButtonStates
object. Then, just by changing that object the icon, text, badge text, badge color, and action that is performed in each state (e.g. on/off) can be changed.background.js
manifest.json: