addon.self

Available in userscripts ✔️
Available in popup scripts ✔️
Required manifest permissions None

Description

Allows addons to get information about themselves or the browser.

Examples

Using addon.self.dir

With addon.self.dir, we can get the URL to an image inside the addon’s directory.

  const img = document.createElement("img");
  img.src = addon.self.dir + "/image.svg"; 
  // Will be something like `chrome-extension://aeepldbjfoihffgcaejikpoeppffnlbd/addons/addon-id/image.svg`
  document.body.appendChild(img);

Using addon.self.disabled and events (dynamicDisable)

We use the disabled and reenabled events to hide the image when the addon is disabled, without requiring the user to refresh the page.
We also use addon.self.disabled inside an event listener to avoid running code while the addon is disabled.
This example requires {"dynamicDisable": true} in the manifest.

  addon.self.addEventListener("disabled", () => img.style.visibility = "visible");
  addon.self.addEventListener("reenabled", () => img.style.visibility = "hidden");

  img.onclick = () => {
    if (addon.self.disabled) return;
    // Code starting here will only run if the image is visible
  };

Properties

addon.self.id

Type String
Nullable No

The addon ID for this addon.
The addon ID is the name of the directory inside /addons that identifies the addon.

addon.self.dir

Type String
Nullable No

Path to the addon’s directory, without trailing slash.
Should be used instead of hardcoding URLs. Locally, using a hardcoded URL like chrome-extension://aeepldbjfoihffgcaejikpoeppffnlbd/addons/full-signature/happen.js might appear to work properly, but the extension ID can change and it will not work in Firefox.

addon.self.browser

Value "chrome" | "firefox"
Nullable No

The browser Scratch Addons is running on.

addon.self.disabled

Type Boolean
Nullable No

Whether the addon is currently disabled or not.
Useful for returning early in event listeners for addons that support dynamicDisable.

addon.self.enabledLate

Type Boolean
Nullable No

Whether the running userscript was injected dynamically in response to the user enabling the addon.
Can only be true if the addon supports dynamicEnable.
If the addon was enabled when the page loaded, then was disabled and reenabled, this will still be false. In that case, the reenabled event will fire.

Methods

getEnabledAddons

Parameter Type Required Description
tag String No The tag for filtering.
Return value Promise<string[]>

Gets a list of addon IDs enabled, optionally filtered using a tag.

Events

disabled

Fires when the addon gets disabled.
Requires the dynamicDisable manifest property.

reenabled

Fires when the addon gets reenabled, after being disabled.