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

| Category               | APIs                                                                                                                                                                                                                                                                                                                                                                                                                                                           |
| ---------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| Debugging              | [`console`](https://developer.mozilla.org/en-US/docs/Web/API/Console)                                                                                                                                                                                                                                                                                                                                                                                          |
| Network                | [`fetch`](https://developer.mozilla.org/en-US/docs/Web/API/fetch), [`Request`](https://developer.mozilla.org/en-US/docs/Web/API/Request), [`Response`](https://developer.mozilla.org/en-US/docs/Web/API/Response), [`Headers`](https://developer.mozilla.org/en-US/docs/Web/API/Headers), [`AbortController`](https://developer.mozilla.org/en-US/docs/Web/API/AbortController), [`AbortSignal`](https://developer.mozilla.org/en-US/docs/Web/API/AbortSignal) |
| URLs                   | [`URL`](https://developer.mozilla.org/en-US/docs/Web/API/URL), [`URLSearchParams`](https://developer.mozilla.org/en-US/docs/Web/API/URLSearchParams), [`URLPattern`](https://developer.mozilla.org/en-US/docs/Web/API/URLPattern), [`open`](https://developer.mozilla.org/en-US/docs/Web/API/Window/open), [`window.open`](https://developer.mozilla.org/en-US/docs/Web/API/Window/open)                                                                       |
| Storage and cookies    | [`localStorage`](https://developer.mozilla.org/en-US/docs/Web/API/Window/localStorage), [`cookieStore`](https://developer.mozilla.org/en-US/docs/Web/API/CookieStore), [`document.cookie`](https://developer.mozilla.org/en-US/docs/Web/API/Document/cookie)                                                                                                                                                                                                   |
| Crypto                 | [`crypto`](https://developer.mozilla.org/en-US/docs/Web/API/Crypto)                                                                                                                                                                                                                                                                                                                                                                                            |
| Encoding and data      | [`TextEncoder`](https://developer.mozilla.org/en-US/docs/Web/API/TextEncoder), [`TextDecoder`](https://developer.mozilla.org/en-US/docs/Web/API/TextDecoder), [`btoa`](https://developer.mozilla.org/en-US/docs/Web/API/btoa), [`atob`](https://developer.mozilla.org/en-US/docs/Web/API/atob), [`Uint8Array`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Uint8Array)                                                    |
| Data formats           | [`Blob`](https://developer.mozilla.org/en-US/docs/Web/API/Blob), [`JSON`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON)                                                                                                                                                                                                                                                                                               |
| Structured clone       | [`structuredClone`](https://developer.mozilla.org/en-US/docs/Web/API/structuredClone)                                                                                                                                                                                                                                                                                                                                                                          |
| Environment and events | [`navigator`](https://developer.mozilla.org/en-US/docs/Web/API/Navigator), [`Event`](https://developer.mozilla.org/en-US/docs/Web/API/Event), [`EventTarget`](https://developer.mozilla.org/en-US/docs/Web/API/EventTarget)                                                                                                                                                                                                                                    |
| Eval                   | [`eval`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/eval)                                                                                                                                                                                                                                                                                                                                                                |

## `Azot`

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

| Property      | Description                                                                                                          |
| ------------- | -------------------------------------------------------------------------------------------------------------------- |
| `Azot.signal` | `AbortSignal` of the operation running your code. Throws outside an operation. See [Cancellation](https://azot.mov/docs/extensions/api.md#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;
```

## Open external links

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);
```
