# Widget reference

> **In short.** `https://desk.juncodesk.app/widget/junco.js` defines one global, `Junco`, with `init(options)`,
> `open({ type })`, `close()` and `version`. Only `game` is required (pass `server` too unless the
> page is on Junco's own address). Every other option is a function Junco calls when it needs the
> value; each may return a value, `null` or a promise, and may fail without breaking anything.

## `Junco.init(options)`

Starts the widget. Call it once per page load; later calls are ignored and return `Junco`. If the
page's `<body>` doesn't exist yet, it waits for `DOMContentLoaded`. Throws only if `game` is
missing.

| Option | Type | Default | What it's for |
|---|---|---|---|
| `game` | `string` | required | The product's public key, `jk_...`. |
| `server` | `string` | the page's own origin | Junco's address: `https://desk.juncodesk.app`. Always pass it on your own site. |
| `context` | `() => object` | none | What's useful when something goes wrong: version, screen, device, settings. Called each time the panel opens. Up to 16 KB as JSON. No personal details. |
| `screenshot` | `() => Blob` | none | An image of the screen (PNG, JPEG or WebP, under 2 MB). Called when the panel opens, before it covers the page. The user chooses whether to attach it. |
| `save` | `() => Blob \| string` | none | The user's save or state (under 1 MB). A string is sent as JSON text. Asked for only if the user ticks "Save and log". |
| `log` | `() => string` | none | Recent log lines (UTF-8 text, under 512 KB). With "Save and log". |
| `playerToken` | `() => string` | none | A signed token from your server for a signed-in user ([Signed user tokens](/docs/user-tokens)). Called before every request; return `null` for users who aren't signed in. |
| `handle` | `() => string` | none | The user's display name (up to 40 characters), shown to your team. A signed token's `name` wins over it. |
| `button` | `boolean` | `true` | `false` hides Junco's round button; open the panel from your own menu instead. |
| `onUnread` | `(count: number) => void` | none | Called when the number of tickets with replies the user hasn't read changes, for a badge on your own button. |

"Function" options may be `async`. If one throws or rejects, Junco carries on without that value.

## `Junco.open(options?)`

Opens the panel. With no argument it shows the three choices (**Report a bug**, **Suggest a
feature**, **Get help**) and the user's earlier tickets. `Junco.open({ type: "bug" })` goes
straight to one form; `type` is `"bug"`, `"idea"` or `"help"`. Returns a promise, or `undefined`
before `init`.

## `Junco.close()`

Closes the panel. The user can also press **Esc** or the close button; focus goes back to where it
was.

## `Junco.version`

The widget's version, such as `"0.1.6"`.

## What users see

- **Home**: the three choices and their tickets, newest activity first, with a dot on those with
  unread replies.
- **A form**: a text box (up to 5,000 characters), a **Screenshot** tick (if `screenshot` gave
  one) and **Save and log** (if `save` or `log` is set), and Send.
- **The thread**: their messages, replies from "AI" and "the team" (never staff names, never
  internal notes), the ticket's status in words ("Being looked at", "With the developer", "Being
  worked on", "Fixed: coming in the next update", "Fixed and live"), typing dots while the AI writes, and a reply box.
- **The footer**: a **Privacy** link to the notice for your product's users, and **Delete
  everything I've sent**, which deletes all their tickets, messages and files after a confirm.

## How it behaves

- **Isolated.** It adds one element, `<junco-support>`, to the end of `<body>`, with a closed
  shadow root: your CSS can't change it and its CSS can't change your page. It sits above
  everything (`z-index: 2147483000`), fixed to the bottom right.
- **Quiet.** Keys, clicks, touches and scrolls inside it don't reach your page, so game controls
  and keyboard shortcuts don't fire while someone types.
- **No cookies.** Requests go to `https://desk.juncodesk.app` with `credentials: "omit"`.
- **Who the user is.** With a `playerToken`, the token. Otherwise a random id (32 URL-safe
  characters) kept in `localStorage` under `junco:player:<public key>`. If storage is blocked
  (some private modes), each visit gets a new id and earlier tickets aren't shown.
- **Updates.** It checks for unread replies when it starts, every minute, and when the tab comes
  back into view; every 10 seconds while a thread is open; every 1.5 seconds while the AI is
  writing (for up to 90 seconds). It does nothing while the tab is hidden.
- **Phones.** The panel is laid out for small screens first (a sheet from the bottom) and stays
  above the on-screen keyboard.
- **Size.** About 21 KB, 8 KB gzipped, no dependencies. Served with a 5-minute cache.
- **Accessible.** Keyboard only works throughout; it's labelled for screen readers and respects
  reduced motion.

## Example with everything

```js
Junco.init({
  game: "jk_...",
  server: "https://desk.juncodesk.app",
  button: false,
  context: () => ({ version: APP_VERSION, screen: router.current, device: navigator.userAgent, language: navigator.language }),
  screenshot: () => captureCanvasAsJpeg(),
  save: () => JSON.stringify(store.exportSave()),
  log: () => logger.recent(200).join("\n"),
  playerToken: () => fetch("/api/junco-token").then((r) => (r.ok ? r.text() : null)),
  handle: () => session.user?.displayName,
  onUnread: (n) => (supportBadge.textContent = n ? String(n) : ""),
});
document.querySelector("#support").addEventListener("click", () => Junco.open());
```

TypeScript declarations: [For coding AIs](/docs/for-ai-agents), section 7.
