Available initially in versions of Firefox> = 52
This functionality will be available in Firefox 52, which is currently Firefox Developer Edition (i.e. Firefox 52.0a2). As you know, for WebExtensions you create a global hotkey with the _execute_browser_action
key in the object supplied for the _execute_browser_action
key. For instance:
"commands":{ "_execute_browser_action": { "suggested_key": { "default": "Alt+Shift+J" } } }
Open a pseudo popup (polyfill this feature in older versions of Firefox)
While explicit functionality is not available until Firefox 52, you can polyfill use this function in the current version of Firefox by specifying a user command named "_execute_browser_action"
. It will look a little different than a regular popup, but it will work. It will be in the panel, which you may have to consider with some associated style, which applies only when it is on the panel instead of a popup. There may also be some differences in the fact that the tab is active when your panel is open. However, the code below at least takes this into account when executing queries using chrome.tabs.query()
or browser.tabs.query()
, making the answer as expected if it were opened in a real popup instead of panels.
The same code will continue to work in Firefox 52+. In Firefox 52+, the _execute_browser_action function activates a browser click action or pop-up window.
If you are not using a popup, the main thing is that you do not use an anonymous function for the browserAction.onClicked
listener. This allows functionality to also be invoked by the commands.onCommand
listener. commands.onCommand
was introduced in Firefox 48, so this should work on any version that is 48+.
Using this polyfill may cause problems with rights other than activeTab
. Exactly what is needed, in any case, will depend on your code.
The following is an extension that invokes functionality using a browser action button when you press Alt-Shift-J. It either activates the doActionButton()
function, or, if the popup is defined, it will open your popup in the form of a panel that will behave similarly to the way the popup works, but it is not perfect. It gets the name of the pop-up file from the one that is currently defined for the currently active tab, as it would be when the browserAction
button was browserAction
.
manifest.json:
{ "description": "Polyfill browserAction keyboard shortcut, including popups.", "manifest_version": 2, "name": "Polyfill browserAction keyboard shortcut", "version": "0.1", "background": { "scripts": [ "background.js" ] }, "browser_action": { "default_icon": { "32": "myIcon.png" }, "default_title": "Open popup", "default_popup": "popup.html" }, "commands": { "_execute_browser_action": { "suggested_key": { "default": "Alt+Shift+J" } } } }
background.js:
chrome.browserAction.onClicked.addListener(doActionButton); function doActionButton(tab){ console.log('Action Button clicked. Tab:',tab); } chrome.commands.onCommand.addListener(function(command) {
WebExtensions is still under development:
The WebExtensions API is developing a lot. Which improves the work with each version of Firefox. At the moment, it's probably best for you to develop and test a WebExtension add-in with Firefox Developer Edition or Firefox Nightly (for _execute_browser_action
). You should also carefully monitor which version of Firefox is required for the functionality you want to use. This information is contained in the "Browser Compatibility" section of the MDN documentation pages.
Some parts of the code in this question have been copied / modified from my other other answers.