Firefox WebExtension importing Services

The MDN top level page for addons used to give an overview over the different extension types (since FF57 only webextensions are supported). Components.utils.import this is for restartless/XUL (legacy) extensions. const { Cu } = require(“chrome”); this is for SDK extensions. Neither will work in webextensions. Unlike the other extension types webextensions are restrictive, they … Read more

chrome.tabs.executeScript(): How to get result of content script?

chrome.tabs.executeScript() returns an Array with “the result of the script” from each tab/frame in which the script is run. “The result of the script” is the value of the last evaluated statement, which can be the value returned by a function (i.e. an IIFE, using a return statement). Generally, this will be the same thing … Read more

Chrome tabs query returning empty tab array

Sounds like you’re running this code while your active window is devtools, which is a known bug. Another problem takes place in your code: it always accesses the focused (active) tab even though the request may have occurred in a backgrounded (inactive) tab. Solution: Use the tab id provided inside the listener function parameter as … Read more

How to trigger click on a button

Try with this code; it simulates a mouse left click on the element by a quick succession of mousedown, mouseup and click events fired in the center of the button: var simulateMouseEvent = function(element, eventName, coordX, coordY) { element.dispatchEvent(new MouseEvent(eventName, { view: window, bubbles: true, cancelable: true, clientX: coordX, clientY: coordY, button: 0 })); }; … Read more

TypeError: [API] is undefined in content script or Why can’t I do this in a content script?

Content scripts do not have access the API you are using You are attempting to do this from a content script. You need to be doing this from a background script. Content scripts have access to a small subset of the WebExtensions APIs. The available APIs include (from the MDN Content Scripts page): From extension: … Read more

How I can make a browser action button that looks and acts like a toggle

Your code While you added a switch statement to switch on button, you never defined button, nor changed its state. You also did not have a default case, just in case the button variable was not one of the values for which you were testing in your case statements. You should not be using browserAction.setPopup() … Read more

How to create a global hotkey for opening the “browserAction” popup in Firefox (WebExtensions)?

Available natively in Firefox versions >= 52 This functionality will be natively 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 using the _execute_browser_action key within the object supplied for the commands key. For example: “commands”:{ “_execute_browser_action”: { “suggested_key”: { “default”: … Read more

Calling webpage JavaScript methods from browser extension

Your content script is in a different context/scope from that of page scripts (scripts that already exist in the webpage). Your content script has higher privileges than are granted to page scripts. Keeping content scripts separate from page scripts is a normal architecture for browser extensions, which is done for security reasons. Because your content … Read more

Completely lost on how to save extension popup window content

Similar to a web page, the popup’s (or an options/settings page’s) scope is created when it is shown and destroyed when it is no longer visible. This means that there is no state stored within the popup itself between the times that it is shown. Any information which you desire to persist after the popup … Read more

Firefox extension .xpi file structure: description, contents, creation, and installation

.xpi file format (Extension Packaging) The .xpi files that are used as containers for Mozilla (Firefox, Thunderbird, etc.) extensions are merely zip archives that have had the file extension changed to .xpi with the files added to the archive using either “deflate” compression, or uncompressed. If you use any other type of compression, other than … Read more