Skip to content
View .md

Globals ​

Azot extensions run with a set of familiar Web APIs.

If you already know these APIs from the browser, most of the behavior and usage is the same. When in doubt, follow the MDN docs.

CategoryAPIs
Debuggingconsole
Networkfetch, Request, Response, Headers, AbortController, AbortSignal
URLsURL, URLSearchParams, URLPattern, open, window.open
Storage and cookieslocalStorage, cookieStore, document.cookie
Cryptocrypto
Encoding and dataTextEncoder, TextDecoder, btoa, atob, Uint8Array
Data formatsBlob, JSON
Structured clonestructuredClone
Environment and eventsnavigator, Event, EventTarget
Evaleval

Azot ​

The Azot global holds the host API of the current extension.

PropertyDescription
Azot.signalAbortSignal of the operation running your code. Throws outside an operation. See Cancellation.

Console and debugging ​

Use the standard console helpers while developing.

ts
console.log("hello");
console.warn("be careful");
console.error("something went wrong");

Network ​

Use fetch with the same Request / Response model as in the browser.

Under the hood, Azot already adds headers like User-Agent and similar to impersonate Chrome, so you only need to override them if you have a specific requirement.

ts
const controller = new AbortController();
const response = await fetch("https://example.com/api", {
  headers: new Headers({ accept: "application/json" }),
  signal: controller.signal,
});
const data = await response.json();

URLs ​

Parse URLs or match them with patterns.

ts
const url = "https://example.com/movie/123456/watch";
const pattern = new URLPattern({ pathname: "/:mediaType/:id/watch" });
const match = pattern.exec(url);
const mediaType = match?.pathname.groups.mediaType;
const id = match?.pathname.groups.id;

Use window.open() or global open() to open a URL in the default browser.

ts
window.open("https://example.com/pricing", "_blank");
open("https://example.com/help", "_blank");

Storage and cookies ​

Use localStorage for simple key-value storage, and cookieStore for HTTP cookies. cookieStore is automatically populated from requests, so you can read cookies without setting them manually.

ts
const token = localStorage.getItem("token");
if (!token) {
  throw new Error("User is not authorized");
}

const sessionId = await cookieStore.get("sessionId");
if (!sessionId) {
  throw new Error("Missing auth cookie");
}

You can also read cookies from document.cookie:

ts
const cookies = document.cookie;

Crypto ​

Use Web Crypto APIs for hashing and random bytes.

ts
const data = new TextEncoder().encode("hello");
const hash = await crypto.subtle.digest("SHA-256", data);
const bytes = new Uint8Array(hash);

Encoding and data ​

Encoding helpers work the same as in the browser.

ts
const raw = new TextEncoder().encode("hello");
const text = new TextDecoder().decode(raw);
const base64 = btoa(text);
const original = atob(base64);

There's also Base64 and HEX helpers in Uint8Array.

ts
const bytes = Uint8Array.fromHex("48656c6c6f");
const hex = bytes.toHex();
const base64 = bytes.toBase64();
const decoded = Uint8Array.fromBase64(base64);

https://tc39.es/proposal-arraybuffer-base64/

Structured clone ​

Clone complex data safely.

ts
const copy = structuredClone({ items: [1, 2, 3] });

Environment and events ​

Read environment info and use EventTarget utilities.

ts
const platform = navigator.platform;
const target = new EventTarget();
const event = new Event("ready");
target.dispatchEvent(event);

Eval ​

Evaluate code dynamically when needed.

ts
const add = eval("(a, b) => a + b");
const result = add(2, 3);

Released under the MIT License.