The core WebExtension code lives in the same repository as the Firefox browser. If you've never hacked on Firefox before, there is a quick guide to getting started, which will show you how to check out the code and do your first build.
Code StyleWebExtension JavaScript code follows the Mozilla coding style guide, with the following differences:
===
may be used in favor of ==
when doing so makes sense.function
keyword.let
or const
should be used in place of var
wherever possible.<script>
nodes must begin with "use strict";
catch
guard expressions, the __proto__
property, or the yield
operator in non-star-functions.arguments
object where possible..apply()
where possible.ESLint will enforce most of these rules.
Code GuidelinesFor Chrome APIs we'll support the callback interface to maintain compatibility. For new APIs, not Chrome ones, we'll use the promise interface to all APIs. See the MDN docs for the difference.
Checking your code with ESLintAll WebExtension directories in mozilla-central
are configured with a set of ESLint rules, which all code is required to pass. Many of these rules help to catch serious errors, such as references to non-existent variables, or a missing trailing comma turning an array literal into an array dereference, so it is extremely important that you check you code against them before it lands.
The simplest way to check your code is using the mach eslint
command. This requires that you have NPM installed.
To setup ESLint, you need to run the following once:
./mach eslint --setup
This should install ESLint, along with the necessary plugins. After that's done, you can check the files you've been working on with the following:
./mach eslint path/to/file.js
Or you can check all WebExtension code with:
./mach eslint toolkit/components/extensions browser/components/extensions toolkit/modules/addonsIntegrating ESLint with your editor
Since manually checking your code isn't always practical (and is easy to forget), it's best to integrate automatic checking with your code editor. Many editors have integration plugins, or built-in support.
Some editors may need special settings to work well with our codebase. Feel free to add other editors below, as you see fit.
There is some additional useful information in the developer tools hacking section of the wiki.
VimThe easiest way to integrate ESLint with Vim is using the Syntastic plugin.
The following assumes that you use the Vundle package manager. It should be easy enough to adapt to any other package manager you happen to prefer, though.
mach eslint --setup
installs a specific ESLint version and some ESLint plugins to a subdirectory of the repository (changed in Firefox 55). You should configure Syntastic roughly as follows (change /path/to/mozilla-central
as needed):
" Initialize Vundle. filetype off call vundle#rc() " Enable the Syntastic bundle. Bundle 'scrooloose/syntastic' " Enable the specific ESLint checker for files in mozilla-central/ only. " Enable the HTML plugin, and enable JavaScript linting for HTML files. autocmd FileType javascript,html \ if stridx(expand("%:p"), "/mozilla-central/") != -1 | \ let b:syntastic_checkers = ['eslint'] | \ let b:syntastic_eslint_exec = '/path/to/mozilla-central/node_modules/.bin/eslint' | \ let b:syntastic_html_eslint_args = ['--plugin', 'html'] | \ endif
After you've added this to your configuration (and have installed Vundle, if necessary), launch Vim and run:
:BundleInstall
This will install the Syntastic plugin. After this, you should be good to go.
Version control hooksThe mozilla-central
tree contains a Mercurial plugin to automatically check any changed files, using ESLint, when you commit. This can be enabled by adding the following to your global .hgrc
file, or to .hg/hgrc
in your repository:
[extensions] # Replace "~/src/mozilla-central/" with the full path to your mozilla-central repository. mozeslint = ~/src/mozilla-central/tools/mercurial/eslintvalidate.py
On non-Windows systems, this can be achieved with:
printf '[extensions]\nmozeslint = %s/tools/mercurial/eslintvalidate.py' "$(pwd)" >>.hg/hgrc
Now Mercurial will check your code for you, and show any warnings or errors, every time you commit.
Unit testsAll WebExtension code is required to be covered by comprehensive unit tests. There is some information on MDN about the various test suites used in Mozilla code. WebExtension code primarilly uses:
toolkit/components/extensions/test/mochitest/
, and are used to test most APIs that live in the toolkit/components/extensions/
directory.
browser/components/extensions/test/browser/
, and are used to test most APIs that live in the browser/components/extensions/
directory.
toolkit/components/extensions/test/xpcshell/
, and are used to test low-level modules which do not require a browser UI, including those under toolkit/modules/addons/
, toolkit/components/utils/simpleServices.js
, and various pieces of C++ code.
WebExtensions/Try_Server has more information about using the try server to test WebExtension code.
Test tagAll tests in WebExtensions are now tagged with the tag: webextensions
, so you can pass the --tag
argument to mochitest
, xpcshell-test
and the try server syntax.
Ideally, our test suite should exercise as close to 100% of our codebase as possible. 100% coverage may not always be practical, or desirable, but it's still an ideal we'd like to strive for. To that end, we run code coverage tests weekly, for the above three main test suites. If you land any code in a given week, it's best to check the end-of-week coverage tests to make sure it has adequate test coverage.
The test results are published in three separate sets:
In general, the total code coverage numbers are what we focus on. However, it is extremely important for code which is expected to run in both the main browser process and in a content process to be tested in both. If you know that your code falls into this category, please check that it's tested appropriately.
The above results are generated using this patch, which could generously be described as a fairly gross hack. If you'd like to run the tests yourself, you can do so with something like the following:
# Install the Istanbul code coverage tool npm install -g istanbul
# Apply the code coverage patch hg import https://people.mozilla.org/~kmaglione/webext-coverage.patch
# Instrument all WebExtension code, run the various test suites, and # generate the coverage output files. ./toolkit/components/extensions/test_coverage.shCode layout
WebExtension code lives in several different parts of the tree, depending on its purpose:
toolkit/components/extensions/ext-*.js
toolkit/components/extensions/schemas/
toolkit/components/extensions/ExtensionContent.jsm
browser/components/extensions/ext-*.js
browser/components/extensions/schemas/
toolkit/modules/addons/
toolkit/components/extensions/*.jsm
toolkit/components/utils/simpleServices.js
All API modules are loaded into a single module, toolkit/components/extensions/Extension.jsm
, and share the same global namespace. All global variables in the Extension.jsm
module are automatically available to code in the ext-*.js
files. Code in these files may make variables available to other modules by defining them as properties of the global
object. For instance, if one module defines global.FooBar = {}
, other modules may access it as simply FooBar
.
Each API module must be explicitly registered, in order to be loaded. It must also register a schema if it exports any APIs to extensions.
Starting from Firefox 50 (Bug 1285063, hg commit e9ca8dc4b42e) all the APIs schema and ext-*.js
files must be registered to the category manager through one of the following manifest files:
As an example, here is a snippet of the extension-toolkit.manifest
:
# scripts category webextension-scripts alarms chrome://extensions/content/ext-alarms.js ... # schemas category webextension-schemas alarms chrome://extensions/content/schemas/alarms.json
In previous Firefox versions, schemas and ext-*.js
files were statically registered through Extension.jsm
and nsBrowserGlue.js
source files:
Generic APIs were registered in Extension.jsm
as follows:
ExtensionManagement.registerScript("chrome://extensions/content/ext-foobar.js"); ExtensionManagement.registerSchema("chrome://extensions/content/schemas/foobar.json");
Firefox desktop APIs were registered in nsBrowserGlue.js:
ExtensionManagement.registerScript("chrome://browser/content/ext-bazquux.js"); ExtensionManagement.registerSchema("chrome://browser/content/schemas/bazquux.json");Permission strings
The strings for permissions can be found in browser.properties.
If you add an API that requires permissions, then you'll need to make sure the corresponding permission strings are also landed.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4