# Installing the form

> **In short.** For websites and web games: load `https://desk.juncodesk.app/widget/junco.js` and call
> `Junco.init({ game: "jk_...", server: "https://desk.juncodesk.app" })` once. It draws its own panel in a closed
> shadow root (about 8 KB gzipped, no dependencies), works in any framework, and only answers on
> the web addresses listed on the product in Junco. Phone apps, engines and desktop software use
> the hosted page or the API: [Apps, engines and desktop](/docs/platforms).

Every product has a **public key** (`jk_...`) on its page in Junco; it's safe to ship in client
code.

## The quickest way: ask your coding AI

Copy this into Claude Code, Cursor, Copilot or whichever coding AI you use, inside your project.
The product's **Install** page in Junco gives you the same prompt with your key, web addresses and
sign-in details already filled in; here they're placeholders.

```prompt
Add the Junco support form to this project. Junco is the support desk I use: users report bugs, suggest features or ask for help, and I answer in Junco.

Read the install guide first: https://desk.juncodesk.app/docs/install.md (plain Markdown).

Details for this project:
- Product: your product
- Public key: jk_YOUR_PUBLIC_KEY (safe to put in client code)
- Junco server: https://desk.juncodesk.app
- Web addresses allowed to use it: none yet (ask me which address the site runs on; I'll add it in Junco)

Do this:
1. Load https://desk.juncodesk.app/widget/junco.js and call Junco.init({ game: "jk_YOUR_PUBLIC_KEY", server: "https://desk.juncodesk.app", ... }) once when the app starts. It draws its own panel inside a shadow root, so our CSS can't break it.
2. Find the app's existing help, menu or settings area and add a "Support" (or "Help") item there that calls Junco.open(). Then pass button: false to hide Junco's own round button. If there's no sensible place, leave Junco's button on.
3. Pass context: () => ({ ... }) with what's useful when something goes wrong: the app's version, the current screen or page, and device details. Never include passwords, tokens, emails or other personal details.
4. If the app draws to a canvas, pass screenshot: () => a Blob of it (JPEG, at most 1280 px wide, under 2 MB).
5. If the project sets a Content-Security-Policy, allow https://desk.juncodesk.app in script-src and connect-src.
6. Users don't need to sign in: Junco keeps a random id in their browser. Don't add any login for it.
7. If the app shows a badge for notifications, pass onUnread: (n) => … to show replies the user hasn't read.

When it's done, run the app, open the form and send a bug report saying "Junco test". Tell me what you changed and anything I need to do myself.
```

The rest of this page is what it reads, if you'd rather do it yourself.

## Plain HTML

```html
<script src="https://desk.juncodesk.app/widget/junco.js"></script>
<script>
  Junco.init({
    game: "jk_...",                 // the product's public key
    server: "https://desk.juncodesk.app",
    context: () => ({ version: "1.4.2", page: location.pathname }),
  });
</script>
```

Put it before `</body>`, or anywhere with `defer` on the first script. A round **Support** button
appears in the bottom right corner.

## Before it works: allow your web addresses

On the product's page in Junco, list every web address the form runs on: the scheme, host and
port, with no path (`https://example.com`, `https://play.example.com`). `www.` is a different
address. For development, `http://localhost:5173` (with your port) and `http://127.0.0.1:…` work.
Up to 20. A page whose address isn't listed gets "This page isn't allowed to use this game's
support".

## In a framework

The widget isn't an npm package: load it at runtime, in the browser only, once. This helper works
in every framework below, server-side rendering included:

```js
// lib/junco.js
let loading;
export function startJunco(options) {
  if (typeof window === "undefined") return Promise.resolve(null);
  loading ??= new Promise((resolve, reject) => {
    const s = document.createElement("script");
    s.src = `${options.server}/widget/junco.js`;
    s.async = true;
    s.onload = () => resolve(window.Junco.init(options));
    s.onerror = () => reject(new Error("Couldn't load Junco"));
    document.head.append(s);
  });
  return loading;
}
```

**Options are read once.** Junco ignores a second `init`, so pass functions that read the current
state (`context: () => ({ screen: currentScreen })`), not values captured at start-up.

### React (Vite, Create React App)

```jsx
import { useEffect } from "react";
import { startJunco } from "./lib/junco";

export default function App() {
  useEffect(() => {
    startJunco({
      game: "jk_...",
      server: "https://desk.juncodesk.app",
      context: () => ({ version: import.meta.env.VITE_APP_VERSION, page: location.pathname }),
    });
  }, []);
  return <Routes />;
}
```

React's Strict Mode runs effects twice in development; `startJunco` and `Junco.init` both only
start once, so that's fine.

### Next.js (App Router)

```jsx
// app/junco.jsx
"use client";
import { useEffect } from "react";
import { usePathname } from "next/navigation";
import { startJunco } from "@/lib/junco";

let page = "/";
export default function Junco() {
  page = usePathname();
  useEffect(() => {
    startJunco({ game: "jk_...", server: "https://desk.juncodesk.app", context: () => ({ page, version: process.env.NEXT_PUBLIC_VERSION }) });
  }, []);
  return null;
}
```

Render `<Junco />` once in `app/layout.jsx`, inside `<body>`. With the Pages Router, do the same
in `pages/_app.jsx`. If you set a Content Security Policy in `next.config.js` or middleware, add
`https://desk.juncodesk.app` to `script-src` and `connect-src`.

### Vue and Nuxt

```js
// Vue: main.js, after app.mount("#app")
import { startJunco } from "./lib/junco";
startJunco({ game: "jk_...", server: "https://desk.juncodesk.app", context: () => ({ page: router.currentRoute.value.fullPath }) });
```

```js
// Nuxt: plugins/junco.client.js (".client" runs it in the browser only)
import { startJunco } from "~/lib/junco";
export default defineNuxtPlugin((nuxtApp) => {
  const router = useRouter();
  startJunco({ game: "jk_...", server: "https://desk.juncodesk.app", context: () => ({ page: router.currentRoute.value.fullPath }) });
});
```

### Svelte and SvelteKit

```svelte
<!-- src/routes/+layout.svelte (SvelteKit) or App.svelte -->
<script>
  import { onMount } from "svelte";
  import { page } from "$app/state";
  import { startJunco } from "$lib/junco";
  onMount(() => startJunco({ game: "jk_...", server: "https://desk.juncodesk.app", context: () => ({ page: page.url.pathname }) }));
</script>
<!-- the rest of your layout as it was -->
```

(`onMount` only runs in the browser. In Svelte 4 use `$app/stores` and `get(page)`.)

### Angular

```ts
// app.component.ts
import { AfterViewInit, Component } from "@angular/core";
import { Router } from "@angular/router";
import { startJunco } from "./junco";

@Component({ selector: "app-root", templateUrl: "./app.component.html" })
export class AppComponent implements AfterViewInit {
  constructor(private router: Router) {}
  ngAfterViewInit() {
    startJunco({ game: "jk_...", server: "https://desk.juncodesk.app", context: () => ({ page: this.router.url }) });
  }
}
```

With server-side rendering, the `typeof window` check in the helper keeps it in the browser.

### Web games (canvas, WebGL, Phaser, PixiJS, three.js, Babylon.js)

Games get the most from the extra callbacks: a screenshot, the save and recent log lines.

```js
Junco.init({
  game: "jk_...",
  server: "https://desk.juncodesk.app",
  button: false,                                   // open it from the game's own menu
  context: () => ({ version: GAME_VERSION, scene: game.scene.key, level: state.level, fps: Math.round(game.loop.actualFps) }),
  screenshot: () => canvasShot(game.canvas),
  save: () => JSON.stringify(state.save()),
  log: () => logBuffer.join("\n"),
  handle: () => state.playerName,
  onUnread: (n) => menu.setBadge("support", n),
});
pauseMenu.on("support", () => Junco.open());
pauseMenu.on("report-bug", () => Junco.open({ type: "bug" }));

function canvasShot(canvas) {
  return new Promise((resolve) => {
    const scale = Math.min(1, 1280 / canvas.width);
    const out = document.createElement("canvas");
    out.width = Math.round(canvas.width * scale);
    out.height = Math.round(canvas.height * scale);
    out.getContext("2d").drawImage(canvas, 0, 0, out.width, out.height);
    out.toBlob(resolve, "image/jpeg", 0.8);
  });
}
```

- **WebGL canvases** read back blank unless made with `preserveDrawingBuffer: true` (three.js:
  `new WebGLRenderer({ preserveDrawingBuffer: true })`; Phaser: `render: { preserveDrawingBuffer:
  true }`), or unless you render a frame just before reading. Without it you get a black image.
- The screenshot is taken when the panel opens, before it covers the game.
- Typing in the panel never reaches the game: keys, clicks and scrolls stay inside it, so
  shortcuts don't fire while someone writes a report.
- Unity and Godot **web exports** are web pages: add the script to the export's HTML template and
  call `Junco.open()` from the game through JavaScript interop (`Application.ExternalEval` is
  gone; in Unity use a `.jslib` plugin; in Godot 4, `JavaScriptBridge.eval("Junco.open()")`).

## Opening it from your own menu

```js
Junco.init({ game: "jk_...", server: "https://desk.juncodesk.app", button: false });
helpButton.addEventListener("click", () => Junco.open());                 // the three choices
bugButton.addEventListener("click", () => Junco.open({ type: "bug" }));   // straight to one: bug, idea, help
```

Pass `onUnread` to put the number of unread replies on your own button.

## Signed-in users

By default Junco recognises a user by a random id kept in their browser (`localStorage`), per
product. If your users sign in, let your server vouch for them, and their tickets follow their
account to any device: [Signed user tokens](/docs/user-tokens).

## Content Security Policy

If your site sets one, allow `https://desk.juncodesk.app` in `script-src` and `connect-src`. Screenshots are sent
as uploads, so nothing else is needed.

If your policy can't load scripts from another origin, you may copy `junco.js` into your own site
and serve it yourself (it still calls `https://desk.juncodesk.app`, so `connect-src` still needs it). You then
miss updates until you copy it again; `Junco.version` says which you have.

## Phone apps, desktop games and anything else

- **Junco's hosted page.** Turn on "Junco's support page" on the product's page and link to it
  (`https://desk.juncodesk.app/s/jk_...`) from your menu, store listing or community.
- **The API.** Build your own form in Unity, Godot, Swift, Kotlin or anything that speaks HTTP.

Both, with complete code: [Apps, engines and desktop](/docs/platforms).

## Check it works

Open the form, send a bug report saying "Junco test", and look at the product's page in Junco: it
says **Connected** when the first ticket arrives. If it doesn't:

- **"This page isn't allowed to use this game's support"**: add the page's exact address on the
  product's page.
- **Nothing appears**: look in the browser console for a Content Security Policy error, or a 404
  for `junco.js` (check the server address).
- **"This game needs a player token"**: the product only takes signed-in users and no token was
  sent.

All the messages: [Troubleshooting](/docs/troubleshooting). Every option: [Widget reference](/docs/widget).
