# ctx.page

The page your app opened on. Read a little of it, or take the whole DOM — you choose which in `manifest.json`.

card mode · app mode · page.text · page.selection · page

## Three levels of access

| Member | What it is | Needs |
| --- | --- | --- |
| `url` | The address when your app opened, as a string | — |
| `title` | The page title when your app opened | — |
| `lang` | The language the page says it’s in — its `<html lang>`, like `'ko'` or `'en-US'` — or `null` when it doesn’t say. Use it for the page (translate it, sum it up); your own words follow [ctx.locale](https://developers.fave1.com/docs/ctx-locale.md) instead | — |
| **read** | | |
| `text` | All visible text when your app opened — a snapshot, not live. Only with `page.text`: `page` alone doesn’t fill it, read `document` instead | page.text |
| `selection` | What the person highlighted — or, if nothing is now, what they had highlighted when they opened Fave1 | page.selection · page |
| **take the page** | | |
| `document` | The page’s real `document` — missing, with every helper below, when `page` is switched off | page |
| `query(sel)` | `querySelector` on that document | page |
| `queryAll(sel)` | `querySelectorAll` on that document, as an array | page |
| `on(event, cb)` | A listener on `document`, in the capture phase — it runs before the page’s own handlers. The shell removes it when your app closes. Returns a remover. | page |
| `layer(opts)` | A full-screen layer over the page for what you draw yourself | page |
| `isFave1(node)` | Is this Fave1’s own card, frame, top-layer box, or the app-layer element? Leave it alone. What you put inside `layer()` is still yours | page |
| `sinkFloaters()` | Pull fixed layers the page stacked above the card back under it. Fave1 puts them back when you close | page |
| `lowerFloater(el)` | Pull one element under the card. Fave1 puts it back when you close. The card, frames and your layer are left alone | page |
| `raiseFloaters()` | Put those layers back while your app stays open — a round ended, the app did not. Closing the app does this for you | page |
| `fetchImage(url)` | The bytes of one image on the page — even when its server won’t share them | page |

> **Ask for the narrowest one.** Only reading text? Declare `page.text`. People see “Reads the text on this page” instead of “Changes this page freely”, and review goes faster.

## Rewrite the page

With `page` declared, `document` is the host page’s own. No wrapper, no proxy — create, remove, restyle, animate, observe.

`src/index.ts`

```ts
const page = ctx.page;
if (!page.document) return;          // page access is off — see below

// Every image on the page, grayscale.
page.queryAll('img').forEach((img) => {
  img.style.filter = 'grayscale(1)';
});

// Plant something of your own.
const badge = page.document.createElement('div');
badge.textContent = 'Faved';
page.document.body.appendChild(badge);
```

Check `document` once and TypeScript knows the rest of `ctx.page` is there — keep it in a `const` as above, and the check holds inside your callbacks too. `query` and `queryAll` hand back `HTMLElement`s; name another type when you need one: `page.query<HTMLImageElement>('img.hero')`.

## Draw over the page

A game, a score bar, confetti — anything that covers the whole screen goes in `layer()`. Fave1 keeps it above everything the page has and below its own card, so the person can always reach Back, × and the sign-in buttons. You don’t pick a z-index; nothing you put inside can climb out of the layer. Late arrivals don’t matter either: the shell keeps its card, the sign-in frame and your layer in the browser’s top layer, so a cookie bar or chat bubble the page adds after you opened still sits underneath.

`src/index.ts`

```ts
const page = ctx.page;
if (!page.document) return;

const layer = page.layer();
const canvas = page.document.createElement('canvas');
canvas.style.cssText = 'position:absolute;inset:0;width:100%;height:100%;pointer-events:auto';
layer.append(canvas);
ctx.ui.corner('bottom-right');              // move the card out of the way

// Catch clicks anywhere on the page — but leave Fave1's own alone.
page.on('pointerdown', (e) => {
  if (page.isFave1(e.target)) return;
  e.preventDefault();
  fire(e.clientX, e.clientY);
});

// Round's over — confetti on top of the card for a moment.
page.layer({ over: 'card' });               // takes no clicks while it's up
// …then
page.layer();                               // back under the card
```

- **It lets clicks through** (clicks) — The layer itself takes no clicks. Give `pointer-events: auto` to what should catch them.
- **Look, don’t touch** (over the card) — With `{ over: 'card' }` it draws above the card but takes no clicks at all, so the card’s buttons keep working.
- **Same element every time** (one layer) — Call it as often as you like. Fave1 removes it when your app closes.

> **Draw your own UI inside a shadow root.** The layer is on the page, so the page’s CSS reaches what you put there — `* { text-transform: uppercase }` lands on your buttons. Give one element in the layer `attachShadow({ mode: 'open' })` and put your styles and DOM inside it.

> **Fields in your layer are on the page too.** The panel keeps its keys to itself; your layer doesn’t. Stop `keydown · keyup · keypress · beforeinput · input · compositionstart · compositionupdate · compositionend` at your shadow root so a site that jumps to its search box on any key stays quiet, and ignore Enter while `e.isComposing` — Korean and Japanese keyboards send it to finish a word.

> **Walking the page?** Skip what `isFave1()` flags and your own layer (`layer.contains(el)`). A cookie bar the page pins above the card is `sinkFloaters()` — Fave1 puts it back when you close. One element is `lowerFloater(el)`. When the round ends and the app stays open, `raiseFloaters()` puts them back yourself.

## Images the server won’t share

An image on the page shows up fine, but your code can’t read its pixels unless its server allows other sites to (CORS). Many don’t. `fetchImage()` gets the bytes anyway: it asks the image’s own server first, and if that’s refused, Fave1 fetches that one image and hands it back.

`src/index.ts`

```ts
const page = ctx.page;
if (!page.document) return;
const img = page.query<HTMLImageElement>('img.hero');
if (!img) return;

const blob = await page.fetchImage(img.currentSrc);   // Blob, or null
if (blob) {
  const bitmap = await createImageBitmap(blob);
  canvas.getContext('2d')!.drawImage(bitmap, 0, 0);
}

// Already tried fetch() yourself? Skip straight to Fave1:
await page.fetchImage(url, { direct: false, signal });
```

- **Only what anyone gets** (public) — Fave1 sends no cookies and no referrer. An image you have to be signed in to see, or one its site keeps to its own pages, stays `null`.
- **Nothing else** (images) — Images only — PNG, JPEG, GIF, WebP, AVIF, SVG — up to 12 MB. A page or a file comes back `null`.
- **Nothing kept** (kept) — Fave1 passes the image along and keeps neither the picture nor its address.

It resolves to `null` when it can’t — blocked, too big, too slow, not an image — and only rejects for something that isn’t an address (`invalid`). `data:` and `blob:` addresses are read as they are. Give it a `signal` to stop early.

## When the answer is no

People can switch `page` off for your app in Fave1 settings. Then `document` and every helper are missing and `ctx.can('page')` is false — check once, say so in the panel and offer the switch, instead of failing quietly. Review sends back an app that breaks here.

`src/index.ts`

```ts
const page = ctx.page;
if (!page.document) {
  ctx.ui.render([
    ctx.ui.empty('🔒', 'Page access is off for this app.'),
    ctx.ui.button('Turn it on in settings', { variant: 'ghost', onClick: () => ctx.perms.open() }),
  ]);
  return;
}
```

A page that locks Fave1 out altogether — a strict Content-Security-Policy, or Trusted Types — never gets this far. The launcher says *This page doesn’t support Fave1* and opens no apps there, so your code doesn’t run on it; you don’t need a branch for it. If the page refuses your app after it started (Trusted Types rejecting `innerHTML`), the panel shows *can’t run on this page* for you. And on a page that blocks Fave1’s sign-in frame, apps open as if nobody were signed in — `ctx.can('storage')` is false — so the signed-out branch you already have covers it.

## Put it back

Changed the page? Undo it in `onDestroy`. Leave it changed and people think the site broke — not your app.

The one exception is a change that *is* the job — a reading mode, a page you unlocked for copying. Keep it after the panel closes only if the panel said so, only on this page until it reloads, and give a way back the next time your app opens there. Keep a handle on `window` under a `Symbol.for('your-app-id')` key so the next launch finds what is still on.

Listeners you attach with `ctx.page.on` and your `layer()` are removed for you. Anything else you started — timers, `requestAnimationFrame` loops, a `MutationObserver` on `page.document` — is yours to stop in `onDestroy`.

## In app mode

Opened from My Fave1, your app runs on `<id>.fave1.app`, and `ctx.page` is that page. It is never a fave1.com page — accounts and settings stay out of reach. `fetchImage()` works there too, for pictures your app kept the address of. `lang` is `null` there, and `text` and `selection` describe your own page — the page is your own. Links, screen size and sign-in in app mode are in [App mode](https://developers.fave1.com/docs/app-mode.md).
