APIScript

The userScripts APIScript is a special kind of extension Content Script.

Like a regular extension Content Script:

  • It runs in the content processes.
  • It has access to the window and document globals related to the webpage it is attached to.
  • It has access to the same subset of WebExtension APIs usually available in a content script.

Unlike a regular extension Content Script:

  • It has to be declared in the manifest using the user_scripts.api_script property.
manifest.json
{
  ...
  "user_scripts": {
     "api_script": "apiscript.js"
  }
}

It is executed automatically on any webpage matched by userScript registered by the same extension, before any matched userScript is executed.

It has access to the API Event browser.userScripts.onBeforeScript which the APIScript can use to subscribe a listener to be called right before a matched userScript is being executed, which allows the APIScript to export a set of custom API methods to make available to the userScript.