• Skip to main content
  • Select language
  • Skip to search
MDN Web Docs
  • Technologies
    • HTML
    • CSS
    • JavaScript
    • Graphics
    • HTTP
    • APIs / DOM
    • WebExtensions
    • MathML
  • References & Guides
    • Learn web development
    • Tutorials
    • References
    • Developer Guides
    • Accessibility
    • Game development
    • ...more docs
Add-ons
  1. MDN
  2. Mozilla
  3. Add-ons
  4. Add-on SDK
  5. Guides
  6. Contributor's Guide
  7. Content Processes

Content Processes

In This Article
  1. Content Scripts
  2. Event Emitters
  3. Content Workers
  4. Accessing the DOM
  5. A few Notes on Security

Add-ons using the techniques described in this document are considered a legacy technology in Firefox. Don't use these techniques to develop new add-ons. Use WebExtensions instead. If you maintain an add-on which uses the techniques described here, consider migrating it to use WebExtensions.

From Firefox 53 onwards, no new legacy add-ons will be accepted on addons.mozilla.org (AMO).

From Firefox 57 onwards, WebExtensions will be the only supported extension type, and Firefox will not load other types.

Even before Firefox 57, changes coming up in the Firefox platform will break many legacy extensions. These changes include multiprocess Firefox (e10s), sandboxing, and multiple content processes. Legacy extensions that are affected by these changes should migrate to WebExtensions if they can. See the "Compatibility Milestones" document for more.

A wiki page containing resources, migration paths, office hours, and more, is available to help developers transition to the new technologies.

A content process was supposed to run all the code associated with a single tab. Conversely, an add-on process was supposed to run all the code associated with a single add-on. Neither content or add-on proceses were ever actually implemented, but by the time they were cancelled, the SDK was already designed with them in mind. To understand this article, it's probably best to read it as if content and add-on processes actually exist.

To communicate between add-on and content processes, the SDK uses something called content scripts. These are explained in the first section. Content scripts communicate with add-on code using something called event emitters. These are explained in the next section. Content workers combine these ideas, allowing you to inject a content script into a content process, and automatically set up a communication channel between them. These are explained in the third section.

In the next section, we will look at how content scripts interact with the DOM in a content process. There are several caveats here, all of them related to security, that might cause things to not behave in the way you might expect.

The final section explains why the SDK still uses the notion of content scripts and message passing, even though the multiprocess model for which they were designed never materialized. This, too, is primarily related to security.

Content Scripts

When the SDK was first designed, Firefox was being refactored towards a multiprocess model. In this model, the UI would be rendered in one process (called the chrome process), whereas each tab and each add-on would run in their own dedicated process (called content and add-on processes, respectively). The project behind this refactor was known as Electrolysis, or E10s. Although E10s has now been suspended, the SDK was designed with this multiprocess model in mind. Afterwards, it was decided to keep the design the way it is: even though it's no longer necessary, it turns out that from a security point of view there are several important advantages to thinking about content and add-on code as living in different processes.

Many add-ons have to interact with content. The problem with the multiprocess model is that add-ons and content are now in different processes, and scripts in one process cannot interact directly with scripts in another. We can, however, pass JSON messages between scripts in different processes. The solution we've come up with is to introduce the notion of content scripts. A content script is a script that is injected into a content process by the main script running in the add-on process. Content scripts differ from scripts that are loaded by the page itself in that they are provided with a messaging API that can be used to send messages back to the add-on script.

Event Emitters

The messaging API we use to send JSON messages between scripts in different processes is based on the use of event emitters. An event emitter maintains a list of callbacks (or listeners) for one or more named events. Each event emitter has several methods: the method on is used to add a listener for an event. Conversely, the method removeListener is used to remove a listener for an event. The method once is a helper function which adds a listener for an event, and automatically removes it the first time it is called.

Each event emitter has two associated emit functions. One emit function is associated with the event emitter itself. When this function is called with a given event name, it calls all the listeners currently associated with that event. The other emit function is associated with another event emitter: it was passed as an argument to the constructor of this event emitter, and made into a method. Calling this method causes an event to be emitted on the other event emitter.

Suppose we have two event emitters in different processes, and we want them to be able to emit events to each other. In this case, we would replace the emit function passed to the constructor of each emitter with a function that sends a message to the other process. We can then hook up a listener to be called when this message arrives at the other process, which in turn calls the emit function on the other event emitter. The combination of this function and the corresponding listener is referred to as a pipe.

Content Workers

A content worker is an object that is used to inject content scripts into a content process, and to provide a pipe between each content script and the main add-on script. The idea is to use a single content worker for each content process. The constructor for the content worker takes an object containing one or more named options. Among other things, this allows us to specify one or more content scripts to be loaded.

When a content script is first loaded, the content worker automatically imports a messaging API that allows it to emit messages over a pipe. On the add-on side, this pipe is exposed via the the port property on the worker. In addition to the port property, workers also support the web worker API, which allows scripts to send messages to each other using the postMessage function. This function uses the same pipe internally, and causes a 'message' event to be emitted on the other side.

As explained earlier, Firefox doesn't yet use separate processes for tabs or add-ons, so instead, each content script is loaded in a sandbox. Sandboxes were explained this article.

Accessing the DOM

The global for the content sandbox has the window object as its prototype. This allows the content script to access any property on the window object, even though that object lives outside the sandbox. Recall that the   window   object inside the sandbox is actually a wrapper to  the real object. A potential problem with the content script having access to the  window  object is that a malicious page could override methods on the window object that   it knows are being used by the add-on, in order to trick the add-on into doing something it does not expect. Similarly, if the content script defines any values on the window object, a malicious page could potentially steal that information. 

To avoid problems like this, content scripts should always see the built-in properties of the window object, even when they are overridden by another script. Conversely, other scripts should not see any properties added to the window object by the content script. This is where xray wrappers come in. Xray wrappers automatically wrap native objects like the window object, and only exposes their native properties, even if they have been overridden on the wrapped object. Conversely, any properties defined on the wrapper are not visible from the wrapped object. This avoids both problems we mentioned earlier.

The fact that you can't override the properties of the window object via a content script is sometimes inconvenient, so it is possible to circumvent this: by defining the property on window.wrappedObject, the property is defined on the underlying object, rather than the wrapper itself. This feature should only be used when you really need it, however.

A few Notes on Security

As we stated earlier, the SDK was designed with multiprocess support in mind, despite the fact that work on implementing this in Firefox has currently been suspended. Since both add-on modules and content scripts are currently loaded in sandboxes rather than separate processes, and sandboxes can communicate with each other directly (using imports/exports), you might be wondering why we have to go through all the trouble of passing messages between add-on and content scripts. The reason for this extra complexity is that the code for add-on modules and content scripts have different privileges. Every add-on module can get chrome privileges simply by asking for them, whereas content scripts have the same privileges as the page it is running on.

When two sandboxes have the same privileges, a wrapper in one sandbox provides transparent access to an object in the other sandbox. When the two sandboxes have different privileges, things become more complicated, however. Code with content privileges should not be able to acces code with chrome privileges, so we use specialized wrappers, called security wrappers, to limit access to the object in the other sandbox. The xray wrappers we saw earlier are an example of such a security wrapper. Security wrappers are created automatically, by the underlying host application.

A full discussion of the different kinds of security wrappers and how they work is out of scope for this document, but the main point is this: security wrappers are very complex, and very error-prone. They are subject to change, in order to fix some security leak that recently popped up. As a result, code that worked just fine last week suddenly does not work the way you expect. By only passing messages between add-on modules and content scripts, these problems can be avoided, making your add-on both easier to debug and to maintain.

Document Tags and Contributors

Tags: 
  • Add-on SDK
 Contributors to this page: wbamberg, didoarellano, evold
 Last updated by: wbamberg, Nov 30, 2016, 2:06:38 PM
See also
  1. WebExtensions
  2. Getting started
    1. What are WebExtensions?
    2. Your first WebExtension
    3. Your second WebExtension
    4. Anatomy of a WebExtension
    5. Example WebExtensions
  3. How to
    1. Intercept HTTP requests
    2. Modify a web page
    3. Add a button to the toolbar
    4. Implement a settings page
  4. Concepts
    1. Using the JavaScript APIs
    2. User interface components
    3. Content scripts
    4. Match patterns
    5. Internationalization
    6. Content Security Policy
    7. Native messaging
  5. Porting
    1. Porting a Google Chrome extension
    2. Porting a legacy Firefox add-on
    3. Embedded WebExtensions
    4. Comparison with the Add-on SDK
    5. Comparison with XUL/XPCOM extensions
    6. Chrome incompatibilities
  6. Firefox workflow
    1. Temporary Installation in Firefox
    2. Debugging
    3. Getting started with web-ext
    4. web-ext command reference
    5. WebExtensions and the Add-on ID
    6. Publishing your WebExtension
  7. JavaScript APIs
    1. Browser support for JavaScript APIs
    2. alarms
    3. bookmarks
    4. browserAction
    5. browsingData
    6. commands
    7. contextMenus
    8. contextualIdentities
    9. cookies
    10. downloads
    11. events
    12. extension
    13. extensionTypes
    14. history
    15. i18n
    16. identity
    17. idle
    18. management
    19. notifications
    20. omnibox
    21. pageAction
    22. runtime
    23. sessions
    24. sidebarAction
    25. storage
    26. tabs
    27. topSites
    28. webNavigation
    29. webRequest
    30. windows
  8. Manifest keys
    1. applications
    2. author
    3. background
    4. browser_action
    5. chrome_url_overrides
    6. commands
    7. content_scripts
    8. content_security_policy
    9. default_locale
    10. description
    11. developer
    12. homepage_url
    13. icons
    14. manifest_version
    15. name
    16. omnibox
    17. options_ui
    18. page_action
    19. permissions
    20. short_name
    21. sidebar_action
    22. version
    23. web_accessible_resources
  9. Add-on SDK
  10. Getting started
    1. Installation
    2. Getting started
    3. Troubleshooting
  11. High-Level APIs
    1. addon-page
    2. base64
    3. clipboard
    4. context-menu
    5. hotkeys
    6. indexed-db
    7. l10n
    8. notifications
    9. page-mod
    10. page-worker
    11. panel
    12. passwords
    13. private-browsing
    14. querystring
    15. request
    16. selection
    17. self
    18. simple-prefs
    19. simple-storage
    20. system
    21. tabs
    22. timers
    23. ui
    24. url
    25. webextension
    26. widget
    27. windows
  12. Low-Level APIs
    1. /loader
    2. chrome
    3. console/plain-text
    4. console/traceback
    5. content/content
    6. content/loader
    7. content/mod
    8. content/symbiont
    9. content/worker
    10. core/heritage
    11. core/namespace
    12. core/promise
    13. dev/panel
    14. event/core
    15. event/target
    16. frame/hidden-frame
    17. frame/utils
    18. fs/path
    19. io/byte-streams
    20. io/file
    21. io/text-streams
    22. lang/functional
    23. lang/type
    24. loader/cuddlefish
    25. loader/sandbox
    26. net/url
    27. net/xhr
    28. places/bookmarks
    29. places/favicon
    30. places/history
    31. platform/xpcom
    32. preferences/event-target
    33. preferences/service
    34. remote/child
    35. remote/parent
    36. stylesheet/style
    37. stylesheet/utils
    38. system/child_process
    39. system/environment
    40. system/events
    41. system/runtime
    42. system/unload
    43. system/xul-app
    44. tabs/utils
    45. test/assert
    46. test/harness
    47. test/httpd
    48. test/runner
    49. test/utils
    50. ui/button/action
    51. ui/button/toggle
    52. ui/frame
    53. ui/id
    54. ui/sidebar
    55. ui/toolbar
    56. util/array
    57. util/collection
    58. util/deprecate
    59. util/list
    60. util/match-pattern
    61. util/object
    62. util/uuid
    63. window/utils
  13. Firefox for Android
  14. Getting started
    1. Walkthrough
    2. Debugging
    3. Code snippets
  15. APIs
    1. Accounts.jsm
    2. BrowserApp
    3. HelperApps.jsm
    4. Home.jsm
    5. HomeProvider.jsm
    6. JavaAddonManager.jsm
    7. NativeWindow
    8. Notifications.jsm
    9. PageActions.jsm
    10. Prompt.jsm
    11. RuntimePermissions.jsm
    12. Snackbars.jsm
    13. Sound.jsm
    14. Tab
  16. Legacy
  17. Restartless extensions
    1. Overview
  18. Overlay extensions
    1. Overview
  19. Themes
  20. Lightweight themes
    1. Overview
  21. Complete themes
    1. Overview
  22. Publishing add-ons
  23. Guides
    1. Signing and distribution overview
    2. Submit an add-on
    3. Review policies
    4. Developer agreement
    5. Featured add-ons
    6. Contact addons.mozilla.org
  24. Community and support
  25. Channels
    1. Add-ons blog
    2. Add-on forums
    3. Stack Overflow
    4. Development newsgroup
    5. IRC Channel