addon.self

Available in userscripts✔️
Available in popup scripts✔️
Required manifest permissionsNone

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

TypeString
NullableNo

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

addon.self.dir

TypeString
NullableNo

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.lib

TypeString
NullableNo

Path to the /libraries directory, without trailing slash.

addon.self.browser

Value"chrome" | "firefox"
NullableNo

The browser Scratch Addons is running on.

addon.self.disabled

TypeBoolean
NullableNo

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

addon.self.enabledLate

TypeBoolean
NullableNo

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

ParameterTypeRequiredDescription
tagStringNoThe tag for filtering.
Return valuePromise<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.


Last updated at 29 July 2022 by Norbiros on commit 28699b6. Improve this page.

Comments

Make sure to follow the code of conduct. You can see this comment section on GitHub Discussions, as well as editing and removing your comment.