@startside/swa 1.0.0 → 1.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,711 @@
1
+ > **For an agent writing SWA widgets.** This text is published at https://startside.app/swa/1.0/agents.md, kept short enough
2
+ > to read whole (a web fetcher cuts long pages): *SWA in 60 lines* (a complete widget, the rules as a table, a
3
+ > checklist), the tools for authors, the TypeScript types, and an index of the full specification, whose sections you
4
+ > read as you need them, at https://startside.app/swa/1.0/ (Markdown: https://startside.app/swa/1.0/spec.md). Examples 6 and 7 (a game, a small
5
+ > application) are in its Appendix A. To try a widget, open its file in a browser; `#mode=dark&width=300` after its
6
+ > address opens it dark and narrow.
7
+
8
+ ---
9
+
10
+ # SWA in 60 lines
11
+
12
+ **A quickstart for the Startside Widget API (SWA) 1.0.** The full specification, [swa-spec.md](https://startside.app/swa/1.0/spec.md), is the
13
+ reference; this page is what you need to write a widget that conforms to it: one complete widget, the rules as a
14
+ table, and a checklist.
15
+
16
+ ## What a widget is
17
+
18
+ One HTML file, named `<name>.swa.html`: its metadata in the head, its settings as a hidden form, its markup in the
19
+ body, its code in a script, all talking to one global `widget`. A Startside page runs it in a sandboxed frame; opened
20
+ in a browser, it runs on its own page, with a menu, light and dark, and a width you can drag. Nothing to build or
21
+ install.
22
+
23
+ ## A complete widget
24
+
25
+ The latest headlines of a feed, chosen in its settings:
26
+
27
+ ```html
28
+ <!doctype html>
29
+ <html lang="en">
30
+ <head>
31
+ <script src="https://startside.app/swa/1.0/runtime.js"></script>
32
+ <title>Headlines</title>
33
+ <meta name="description" content="The latest headlines of a feed." />
34
+ <meta name="apiVersion" content="1.0" />
35
+ <meta name="category" content="News" />
36
+ <style>
37
+ .news ul { list-style: none; margin: 0; padding: 0; }
38
+ .news li { padding: 3px 0; border-bottom: 1px solid light-dark(#e5e7eb, #333a42); }
39
+ .news a { color: var(--link, #1e5bb8); }
40
+ .news .uwa-read a { color: var(--muted, #6b7280); }
41
+ .news [hidden] { display: none; }
42
+ </style>
43
+ </head>
44
+ <body>
45
+ <form id="preferences" hidden>
46
+ <label>Feed <input type="url" name="feed" value="https://daringfireball.net/feeds/main" /></label>
47
+ <label>Items <input type="range" name="count" min="1" max="20" value="5" /></label>
48
+ </form>
49
+ <div class="news">
50
+ <p class="state waiting">Loading…</p>
51
+ <p class="state failed" role="alert" hidden>Could not read the feed.</p>
52
+ <ul hidden></ul>
53
+ <template><li><a class="ext-link"></a></li></template>
54
+ </div>
55
+ <script>
56
+ const News = {
57
+ show(state) {
58
+ for (const el of widget.body.querySelectorAll('.state')) el.hidden = !el.classList.contains(state);
59
+ },
60
+ draw(feed) {
61
+ const list = widget.body.querySelector('ul');
62
+ const row = widget.body.querySelector('template').content.firstElementChild;
63
+ const items = feed.items.slice(0, widget.settings.get('count'));
64
+ list.replaceChildren(...items.map((item) => {
65
+ const li = row.cloneNode(true);
66
+ const a = li.querySelector('a');
67
+ a.href = item.url;
68
+ a.textContent = item.title || item.url; // text, never markup
69
+ return li;
70
+ }));
71
+ list.hidden = false;
72
+ News.show('none');
73
+ },
74
+ load(event, fresh) {
75
+ const url = String(widget.settings.get('feed'));
76
+ return widget.fetchFeed(url, { signal: event.signal, fresh }).then(News.draw, (error) => {
77
+ if (error.name !== 'AbortError' && !widget.body.querySelector('li')) News.show('failed');
78
+ });
79
+ },
80
+ };
81
+ widget.addEventListener('load', (event) => {
82
+ const list = widget.body.querySelector('ul');
83
+ list.addEventListener('click', (e) => {
84
+ const a = e.target.closest('a');
85
+ if (a) (e.preventDefault(), widget.openURL(a.href));
86
+ });
87
+ widget.readItems(list, 'li');
88
+ event.waitUntil(News.load(event, false));
89
+ });
90
+ widget.addEventListener('refresh', (event) => event.waitUntil(News.load(event, true)));
91
+ </script>
92
+ </body>
93
+ </html>
94
+ ```
95
+
96
+ What it shows, in order:
97
+
98
+ 1. **The runtime** in the head, by its full address, so the file also runs opened from disk.
99
+ 2. **Metadata**: a title and a description; `category` places it in a catalogue.
100
+ 3. **Settings as a hidden form**: a control per setting, its `name` the setting's, its `value` the default, its
101
+ label the setting's label. `widget.settings.get()` gives each typed: a range a number, a checkbox a boolean.
102
+ 4. **Markup in the file**: every state (loading, failed, the list) is there from the start, shown or hidden with
103
+ `hidden`; a `template` gives each row.
104
+ 5. **Code in one object**, wired once in `load`: one listener on the list for every row, the read state from
105
+ `readItems`. `load` and `refresh` give `waitUntil` their promise, and pass on the `signal` that cancels a request
106
+ when the next load starts.
107
+ 6. **Text set with `textContent`**, never fetched text as markup.
108
+ 7. **Colors from the host's properties**, each with a fallback, and `light-dark()` for both modes.
109
+
110
+ ## The rules
111
+
112
+ | | Rule | Section |
113
+ |---|---|---|
114
+ | MUST | Name the file `<name>.swa.html`, begin it with `<!doctype html>` | 4.1, 4.2 |
115
+ | MUST | Load the runtime once, in the head: `https://startside.app/swa/1.0/runtime.js` (where startside.app can't be loaded, the npm package on a CDN, `https://cdn.jsdelivr.net/npm/@startside/swa@1/swa/1.0/runtime.js`; or inlined: `<script data-runtime="swa/1.0">`) | 4.3 |
116
+ | MUST | Declare settings in `<form id="preferences" hidden>`, one named control each | 5 |
117
+ | MUST NOT | Use `localStorage`, `sessionStorage`, IndexedDB or cookies: `widget.storage` or a hidden setting | 4.8, 7.8 |
118
+ | MUST NOT | Use UWA's API (`onLoad`, `getValue`, `UWA.Data`): events, `widget.settings`, `widget.fetch`, `widget.fetchFeed` | 7, 8, 11 |
119
+ | MUST NOT | Style or script the page around the widget on its own page | 10.3 |
120
+ | MUST | Work framed: no inline mode, no access to the page | 10.1, 10.2 |
121
+ | MUST NOT | Use `alert()`, `confirm()` or `prompt()` (off in a frame): ask in the widget's own box | 10.1 |
122
+ | SHOULD | Write the markup in the file and update it in place; `setBody` only for content made whole at each draw | 4.9, 7.3.1 |
123
+ | SHOULD NOT | Put fetched or typed text into markup (`innerHTML`, a `setBody` string) | 7.3 |
124
+ | SHOULD | Keep a game in progress or the person's data in a hidden setting, read defensively, and under the host's limit if it grows (Startside: 20,000 characters a setting) | 7.5, 7.8 |
125
+ | SHOULD | Let the host set the width; the height is the content's; never `100vh` | 7.3.2 |
126
+ | SHOULD | While it loads, show a placeholder at the size of what's coming (a skeleton of its rows, its picture's box), not a one-line "Loading…": the widget doesn't change size as its content arrives | 7.3.1 |
127
+ | SHOULD | Make a board, a grid or a list moved through with the arrow keys one Tab stop, the keys heard on the widget's own element; a list of plain links, as above, keeps its links in the Tab order | 7.3.1, 14 |
128
+ | SHOULD | Mark where the keyboard lands when the host moves the focus into the widget, when that isn't its first control: `autofocus` on a game's board (`<div class="board" tabindex="0" autofocus>`). Call `preventDefault()` on the Escape and arrow keys it uses itself: left uncancelled, a host may use Escape to leave the widget and an arrow to go to the next | 6.8 |
129
+ | SHOULD | Announce results and errors in a `role="status"` element present from the start, nothing as it first draws; keep the focus through updates | 14 |
130
+ | SHOULD | Write colors with `light-dark()`, other dark-mode changes under `:root[data-color-scheme="dark"]`, not `prefers-color-scheme` | 4.7 |
131
+ | SHOULD | Scope every rule under the widget's root class (`.news li`); give each host property a fallback, `var(--text, #222)`; avoid the host's class names (`loading`, `spinner`) | 4.7 |
132
+ | SHOULD | Do nothing at rest: an animation that draws the eye plays a few times; timers only for what changes on screen | 7.3.1 |
133
+ | SHOULD | Follow its width with a `ResizeObserver` on its own element when its layout depends on it | 7.3.2 |
134
+ | SHOULD | Meet WCAG 2.2 AA: keyboard alone, names on controls, 4.5:1 text, `prefers-reduced-motion` | 14 |
135
+ | MUST | Show only pictures it may show (public domain, or a licence or permission that allows it), their required credits kept; a picture opened large carries its own: `widget.openPhoto({ src, alt, credit })` | 7.12 |
136
+ | MAY | Declare actions of its own for its options menu, in the file: `<menu id="actions" hidden>` with one `<button type="button" name="reset">Reset</button>` each, five at most (`disabled` or `hidden` as it starts, `data-confirm="…?"` for a question the host asks first). Answer the `action` event (`event.detail.id`), and change their state as the widget's does with `widget.setActionStates({ reset: { disabled: false } })` | 7.13 |
137
+ | MAY | Hand the person a file it makes, from one of those actions only: `data-save=".cook"` on its button, then `widget.saveFile({ name, type, data }, { action: event })` in its `action` listener | 7.13 |
138
+ | MAY | Carry its icon in the file: `<link rel="icon" href="data:image/svg+xml,…">`, kept when the file is shared by itself (best: an SVG drawn for 16 pixels, or a 64 × 64 PNG) | 4.6 |
139
+
140
+ ## Checklist
141
+
142
+ - [ ] `npx @startside/swa validate name.swa.html` reports no error (Appendix F): it reads the file without running it.
143
+ - [ ] It opens in a browser, from disk and from a server, with no error in the widget or the console.
144
+ - [ ] Each setting changed in the menu (▾) applies, and Refresh keeps what it should.
145
+ - [ ] It reads well light and dark, narrow (300 pixels) and wide, maximized: open it as
146
+ `name.swa.html#mode=dark&width=300`, then light and wider.
147
+ - [ ] Everything works with the keyboard alone; the focus is visible and never lost. A game plays with its keys once the
148
+ focus is on it, and the keys it doesn't use are left uncancelled.
149
+ - [ ] Saved data from an older version, or edited by hand, doesn't break it.
150
+ - [ ] Nothing moves or runs when nobody uses it.
151
+
152
+ ## Next
153
+
154
+ - The types, for an editor: `https://startside.app/swa/1.0/swa.d.ts` (Appendix F).
155
+ - The tools: the npm package `@startside/swa`, its `validate` and `inline` commands (Appendix F).
156
+ - More examples: a blog (Example 4), a game (Example 6), a to-do list with an action of its own in the options menu
157
+ (Example 7), in Appendix A of the specification.
158
+
159
+ ---
160
+
161
+ *Published at https://startside.app/swa/1.0/quickstart.html (Markdown: https://startside.app/swa/1.0/quickstart.md). ©
162
+ 2026 François Hodierne, shared under the [Creative Commons Attribution 4.0 International](https://creativecommons.org/licenses/by/4.0/)
163
+ licence (CC BY 4.0).*
164
+
165
+
166
+ ---
167
+
168
+ ## Tools for authors
169
+
170
+ *This appendix is non-normative.*
171
+
172
+ - **This specification, published.** `https://startside.app/swa/1.0/`, and as Markdown, `spec.md` beside it; *SWA in
173
+ 60 lines* at `quickstart.html` (`quickstart.md`); for an agent, `agents.md`, short enough to read whole: the
174
+ quickstart, these tools, the types and an index of this specification's sections.
175
+ - **Types.** SWA 1.0's TypeScript types are served beside the runtime, at `https://startside.app/swa/1.0/swa.d.ts`:
176
+ saved next to a widget's scripts, with `/// <reference path="swa.d.ts" />` (or a `jsconfig.json` naming it),
177
+ `widget` is typed in an editor, its events with their `detail`, and what the host freezes read-only. The npm package
178
+ `@startside/swa` carries them too: installed, `"types": ["@startside/swa"]` in a `tsconfig.json` or `jsconfig.json`'s
179
+ `compilerOptions`.
180
+ - **The npm package `@startside/swa`.** The runtime (`swa/1.0/runtime.js`, the text startside.app serves) and the types
181
+ (`swa/1.0/swa.d.ts`), under the MIT licence; and, since 1.0.1, this specification, the quickstart and the guide for
182
+ agents as published here (`swa/1.0/spec.md`, `quickstart.md`, `agents.md`), under CC BY 4.0, as they were at the
183
+ package's release: for an author or an agent that can reach npm or a CDN but not this site
184
+ (`https://cdn.jsdelivr.net/npm/@startside/swa@1/swa/1.0/agents.md`). Served by jsDelivr and unpkg (4.3), installed
185
+ with `npm install @startside/swa`. Since 1.0.1 too, a command, for Node 20 or later, with nothing else to install:
186
+ `validate` and `inline`, below.
187
+ - **Checking a file.** `npx @startside/swa validate my-widget.swa.html` reads a widget file without running it, with no
188
+ network: the file's name and document, its runtime tag, metadata and settings form, and its scripts against this
189
+ API, UWA's members and globals named with what replaces them. Each finding is `file:line:col: error|warning:
190
+ message`, citing its section; `--json` gives them as a list of `{ file, line, col, level, message }`. It exits 1 on
191
+ an error. Run it after each edit: a file it passes can still fail as it runs, which only trying it shows.
192
+ - **Trying a widget.** A widget file opened in a browser, from disk or any server, runs on its own page with the
193
+ runtime its head loads (4.3): its options menu (▾) shows its title, color and settings, applied as they change, and
194
+ its `fetch` and `fetchFeed` go through Startside's proxies, which answer any origin. Errors show in the widget and
195
+ in the browser's console. `widget.html#mode=dark&width=300` opens it dark and 300 pixels wide (10.3), for a test or
196
+ a screenshot, rather than reaching into the page's own menu and switches, which may change.
197
+ - **A copy with the runtime inlined** (4.3), for a place that loads no script from another site: the file with its
198
+ runtime tag replaced by `<script data-runtime="swa/1.0">` holding the text of `https://startside.app/swa/1.0/runtime.js`,
199
+ fetched as the copy is made; nothing else changed. Make it again after the runtime changes, and keep the file that
200
+ loads the runtime by its address as the widget's own. `npx @startside/swa inline my-widget.swa.html` writes that
201
+ copy, `my-widget-inline.swa.html` (`-o` names another, never the file itself), with the package's runtime, its
202
+ `data-host` kept, then checks it; `npx @startside/swa@latest` gets the latest release's runtime.
203
+
204
+ ---
205
+
206
+ ## The types (`https://startside.app/swa/1.0/swa.d.ts`)
207
+
208
+ ```ts
209
+ // SWA 1.0, the Startside Widget API: its types, for a widget file's scripts (docs/swa-spec.md, section 7, its Web IDL).
210
+ // Served at /swa/1.0/swa.d.ts. In an editor, next to a widget's code:
211
+ //
212
+ // /// <reference path="./swa.d.ts" />
213
+ //
214
+ // or in a jsconfig.json / tsconfig.json, "files": ["swa.d.ts"]. The global `widget` is then typed, with completion.
215
+ // scripts/check/swa-api.mjs checks that the runtime's `widget` has every member named here, and nothing else public, and
216
+ // that the readonly attributes are: assigning one throws in strict code, and what they give is frozen.
217
+
218
+ export {};
219
+
220
+ declare global {
221
+ /** The widget the file's code runs in (SWA 1.0, 7.1). */
222
+ const widget: SWA.Widget;
223
+
224
+ /** Every event the host fires at a widget (SWA 1.0, 7.6.1). */
225
+ class WidgetEvent<D = unknown> extends Event {
226
+ constructor(type: string, init?: EventInit & { detail?: D });
227
+ /** The event's data, as the table of 7.6.1 says: `fullsize`'s new value, `audiostate`'s state… */
228
+ readonly detail: D;
229
+ /** A load's (`load`, `refresh`): aborted when the next load starts or the widget goes. Null on other events. */
230
+ readonly signal: AbortSignal | null;
231
+ /** Extends a load (`load`, `refresh`) until `promise` settles; a rejection is reported in the widget. */
232
+ waitUntil(promise: Promise<unknown>): void;
233
+ }
234
+
235
+ namespace SWA {
236
+ /** The events a widget listens for (SWA 1.0, 7.6.1), each with its `detail`. */
237
+ interface WidgetEventMap {
238
+ load: WidgetEvent<null>;
239
+ loadcomplete: WidgetEvent<null>;
240
+ refresh: WidgetEvent<null>;
241
+ resize: WidgetEvent<null>;
242
+ updatebody: WidgetEvent<null>;
243
+ updatepreferences: WidgetEvent<null>;
244
+ hideedit: WidgetEvent<null>;
245
+ /** Cancelable: canceling skips marking `readItems`' items read. */
246
+ readall: WidgetEvent<null>;
247
+ audiostate: WidgetEvent<AudioState>;
248
+ audioresume: WidgetEvent<null>;
249
+ videostate: WidgetEvent<VideoState | null>;
250
+ fullsize: WidgetEvent<boolean>;
251
+ fullscreen: WidgetEvent<boolean>;
252
+ /** Cancelable: canceling skips the refresh that follows. */
253
+ settingschange: WidgetEvent<{ names: string[] }>;
254
+ /** One of the widget's own actions (declared in `<menu id="actions">`, 7.13) chosen in its options menu. */
255
+ action: WidgetEvent<{ id: string }>;
256
+ }
257
+
258
+ interface Widget extends EventTarget {
259
+ // 7.2 Identity and environment
260
+ readonly id: string;
261
+ /** The title shown: the person's name for the widget if they renamed it (then `settingschange` with `title`), else its own (7.3). */
262
+ readonly title: string;
263
+ /** The widget file's address. */
264
+ readonly uwaUrl: string;
265
+ /** The person's language in UWA's form, `en_US`: `lang.replace('_', '-')` is a language tag for `Intl`. */
266
+ readonly lang: string;
267
+ /** The person's region, `us`. */
268
+ readonly locale: string;
269
+ /** The file's metadata (`<meta name content>`), by name. */
270
+ readonly metas: Readonly<Record<string, string>>;
271
+ /** The settings the file declares, frozen. */
272
+ readonly preferences: readonly WidgetSetting[];
273
+ /** Frozen. */
274
+ readonly environment: Readonly<WidgetEnvironment>;
275
+ /** The widget's content: set from `load` on, in every environment (SWA 1.0, 7.3.1). */
276
+ readonly body: HTMLElement;
277
+ log(...data: unknown[]): void;
278
+
279
+ // 7.3 Content and title
280
+ setBody(content: string | Node): void;
281
+ addBody(content: string | Node): void;
282
+ createElement<K extends keyof HTMLElementTagNameMap>(tagName: K, attributes?: Record<string, unknown>): HTMLElementTagNameMap[K];
283
+ createElement(tagName: string, attributes?: Record<string, unknown>): HTMLElement;
284
+ setTitle(title: string): void;
285
+ getTitle(): string;
286
+ setStyle(css: string): void;
287
+
288
+ // 7.4 Appearance
289
+ setIcon(url: string | null): void;
290
+ setChromeColor(color: string | null, options?: { background?: string | null }): void;
291
+
292
+ // 7.5 Settings
293
+ readonly settings: WidgetSettings;
294
+ getPreference(name: string): WidgetSetting | null;
295
+ setMaxValue(name: string, max: number): void;
296
+ openPreferences(): void;
297
+ withPreferencesLink(text: string): HTMLSpanElement;
298
+
299
+ // 7.6 Events and lifecycle
300
+ addEventListener<K extends keyof WidgetEventMap>(
301
+ type: K,
302
+ listener: (this: Widget, event: WidgetEventMap[K]) => unknown,
303
+ options?: boolean | AddEventListenerOptions,
304
+ ): void;
305
+ addEventListener(type: string, listener: EventListenerOrEventListenerObject | null, options?: boolean | AddEventListenerOptions): void;
306
+ removeEventListener<K extends keyof WidgetEventMap>(
307
+ type: K,
308
+ listener: (this: Widget, event: WidgetEventMap[K]) => unknown,
309
+ options?: boolean | EventListenerOptions,
310
+ ): void;
311
+ removeEventListener(type: string, listener: EventListenerOrEventListenerObject | null, options?: boolean | EventListenerOptions): void;
312
+ /** A refresh the widget asks for itself, as when its settings apply. */
313
+ refresh(): void;
314
+ setAutoRefresh(minutes: number): void;
315
+ setPeriodical(name: string, callback: () => void, delay: number, now?: boolean): void;
316
+ clearPeriodical(name: string): void;
317
+
318
+ // 7.7 Lists and grips
319
+ /** `box` an HTML element: the grip sets its style. */
320
+ countGrip(box: HTMLElement, itemSelector: string, options?: CountGripOptions): void;
321
+ heightGrip(box: HTMLElement, scroller: HTMLElement, options?: HeightGripOptions): void;
322
+
323
+ // 7.8 Memory and stored data
324
+ readonly storage: WidgetStorage;
325
+
326
+ // 8 Data access
327
+ /** The web's fetch, through the host's proxy: GET only, the body as text; an HTTP error a Response, not a rejection. */
328
+ fetch(input: string | URL | Request, init?: RequestInit): Promise<Response>;
329
+ /** RSS, Atom or JSON Feed as a JSON Feed 1.1, kept by the host: a kept copy answers at once. */
330
+ fetchFeed(url: string, options?: FetchFeedOptions): Promise<ContentFeed>;
331
+
332
+ // 7.10 Read state and counts
333
+ readItems(box: Element, itemSelector: string, options?: { key?: (item: Element) => string }): void;
334
+ isRead(key: string): boolean;
335
+ setRead(keys: string | string[], read?: boolean): void;
336
+ setUnreadCount(count: number): void;
337
+ setSearchResultCount(count: number): void;
338
+
339
+ // 7.11 The reader (activation-gated)
340
+ openURL(url: string): void;
341
+ openItems(items: ReaderItem[], index: number, options?: OpenItemsOptions): void;
342
+
343
+ // 7.12 Sound, video, pictures
344
+ /** Frozen: a new object at each change (`audiostate`). */
345
+ readonly audioState: Readonly<AudioState>;
346
+ playAudio(options: PlayAudioOptions): unknown;
347
+ pauseAudio(): unknown;
348
+ seekAudio(seconds: number): unknown;
349
+ audioStarted(stop: () => void, controls?: AudioControls): void;
350
+ audioStopped(): void;
351
+ audioInfo(info?: AudioInfo): void;
352
+ readonly videoState: Readonly<VideoState> | null;
353
+ playVideo(video: VideoSpec): unknown;
354
+ openPhoto(photo: PhotoSpec): unknown;
355
+
356
+ // 7.13 The page
357
+ shareUrl(): string | null;
358
+ share(data?: { text?: string; url?: string }): unknown;
359
+ /** Absent where no widget can be added (a preview, a widget on its own page): `if (widget.addWidget)`. */
360
+ addWidget?(spec: AddWidgetSpec): unknown;
361
+ /** `spec`, or a function giving it as the drag starts, given the drag's event; nothing given, the drag doesn't start. */
362
+ dragWidget(element: Element, spec: AddWidgetSpec | ((event: DragEvent) => AddWidgetSpec | null | undefined)): boolean;
363
+ readonly canFullSize: boolean;
364
+ readonly fullSize: boolean;
365
+ openFull(): void;
366
+ closeFull(): void;
367
+ /** Full screen: the browser's, or the widget alone over the window where the browser has none (7.13). */
368
+ readonly fullScreen: boolean;
369
+ setFullScreen(on: boolean): void;
370
+ /**
371
+ * Changes the state of actions the file declares (`<menu id="actions">`, 7.13), by id: greyed, hidden, another
372
+ * label. An id the file doesn't declare throws a TypeError. States last while the widget runs.
373
+ */
374
+ setActionStates(states: Record<string, WidgetActionState>): void;
375
+ /**
376
+ * A file the widget made, for the person to keep (7.13): the host checks its name, its extension and its type,
377
+ * and delivers it. `options.action`: the `action` event of a choice of an action whose declaration carries
378
+ * `data-save`; one file per choice, within 30 seconds. Resolves once the host handed the file to the browser
379
+ * (not "saved"); rejects with a DOMException: NotAllowedError (no such choice, or already used), NotSupportedError
380
+ * (a name, an extension or a type the action or the host doesn't take), QuotaExceededError (over 8 MiB),
381
+ * AbortError (the person cancelled). Without `options.action`: NotAllowedError.
382
+ */
383
+ saveFile(file: WidgetSaveFile, options?: { action?: WidgetEvent<{ id: string }> }): Promise<void>;
384
+ }
385
+
386
+ /** A file a widget saves (7.13). */
387
+ interface WidgetSaveFile {
388
+ /** Its name, with its extension: one the action declares (`data-save`). The host cleans it. */
389
+ name: string;
390
+ /** Its type, one its extension takes ('' for the usual one): "text/plain;charset=utf-8". */
391
+ type: string;
392
+ /** Its content, 8 MiB at most (a string's as UTF-8); copied as the call is made. */
393
+ data: string | Blob | ArrayBuffer;
394
+ }
395
+
396
+ /** Settings typed by the control that declares them (7.5): a checkbox's a boolean, a range's or a number's a number. */
397
+ interface WidgetSettings {
398
+ /** The setting's value; `undefined` for a name the form doesn't declare. `title`: the widget's title (7.3, 7.5). */
399
+ get(name: string): string | number | boolean | null | undefined;
400
+ /**
401
+ * Saved without a refresh; a checkbox's takes a boolean, a range's or a number's a number, any other's a string
402
+ * (null or undefined: empty). A wrong type, or an undeclared name, throws a TypeError. A host may limit a value's
403
+ * length (Startside: 20,000 characters): one over it isn't kept, and the widget's console says so (7.5).
404
+ * `set('title', name)`: the person's name for the widget, typed in it ('' for its own; 7.5).
405
+ */
406
+ set(name: string, value: string | number | boolean | null | undefined): void;
407
+ reset(name: string): void;
408
+ }
409
+
410
+ /** What a widget keeps (7.8): JSON values, copied in and out. */
411
+ interface WidgetStorage {
412
+ /** Across rebuilds (a tab switch) and, unless the person turned it off, reloads: a cache, which the host may drop. A game in progress is a hidden setting (`settings.set`), not memory. */
413
+ readonly memory: WidgetStorageArea;
414
+ /** Across reloads, shared by every widget of the same file: scores. */
415
+ readonly local: WidgetStorageArea;
416
+ }
417
+
418
+ interface WidgetStorageArea {
419
+ get<T = unknown>(key: string): Promise<T | undefined>;
420
+ set(key: string, value: unknown): Promise<void>;
421
+ delete(key: string): Promise<void>;
422
+ }
423
+
424
+ interface WidgetEnvironment {
425
+ readonly name: string;
426
+ readonly standalone: boolean;
427
+ /** false: an SWA widget never runs inline (SWA 1.0, 10.2). */
428
+ readonly inline: boolean;
429
+ /** The page's style, or "" standalone. */
430
+ readonly style: string;
431
+ }
432
+
433
+ /** A setting as the file declares it: frozen, as `preferences` and `getPreference` give it. */
434
+ interface WidgetSetting {
435
+ readonly name: string;
436
+ readonly type: 'text' | 'password' | 'hidden' | 'boolean' | 'list' | 'range' | 'number' | 'url';
437
+ readonly label?: string;
438
+ readonly defaultValue?: string;
439
+ readonly options?: readonly { readonly value: string; readonly label?: string; readonly feed?: string }[];
440
+ readonly min?: number;
441
+ readonly max?: number;
442
+ readonly step?: number;
443
+ readonly hint?: string;
444
+ readonly when?: string;
445
+ readonly commit?: 'ok';
446
+ readonly shareable?: boolean;
447
+ readonly load?: string;
448
+ }
449
+
450
+ interface CountGripOptions {
451
+ /** Adds the items not shown yet. */
452
+ onStart?: () => void;
453
+ onDone?: (count: number) => void;
454
+ min?: number;
455
+ /** The setting holding the count: a double-click restores its default. */
456
+ setting?: string;
457
+ }
458
+
459
+ interface HeightGripOptions {
460
+ height?: number;
461
+ min?: number;
462
+ /** A selector: the list's items. */
463
+ items?: string;
464
+ onDone?: (pixels: number) => void;
465
+ }
466
+
467
+ interface FetchFeedOptions {
468
+ signal?: AbortSignal | null;
469
+ /** Skip the kept copy (a refresh the person asked for). */
470
+ fresh?: boolean;
471
+ }
472
+
473
+ /** A JSON Feed 1.1 document with the host's extensions (8.5). Its schema is SWA's Issue 11. */
474
+ interface ContentFeed {
475
+ version: string;
476
+ title?: string;
477
+ home_page_url?: string;
478
+ feed_url?: string;
479
+ items: ContentItem[];
480
+ _myvibes?: Record<string, unknown>;
481
+ [extension: `_${string}`]: unknown;
482
+ }
483
+
484
+ interface ContentItem {
485
+ id: string;
486
+ url?: string;
487
+ external_url?: string;
488
+ title?: string;
489
+ summary?: string;
490
+ content_html?: string;
491
+ content_text?: string;
492
+ image?: string;
493
+ date_published?: string;
494
+ date_modified?: string;
495
+ authors?: { name?: string; url?: string; avatar?: string }[];
496
+ tags?: string[];
497
+ attachments?: { url: string; mime_type: string; title?: string; size_in_bytes?: number; duration_in_seconds?: number }[];
498
+ _myvibes?: Record<string, unknown>;
499
+ [extension: `_${string}`]: unknown;
500
+ }
501
+
502
+ interface ReaderItem {
503
+ url: string;
504
+ /** Where its headline points, when not `url`. */
505
+ link?: string;
506
+ title?: string;
507
+ /** HTML, the reader's text. */
508
+ content?: string;
509
+ /** Its first view. */
510
+ prefer?: 'feed' | 'page' | 'reader';
511
+ /** An RFC 3339 date, in the reader's list. */
512
+ date?: string;
513
+ /** A short excerpt, in the reader's list. */
514
+ summary?: string;
515
+ author?: string;
516
+ image?: string;
517
+ read?: boolean;
518
+ }
519
+
520
+ interface OpenItemsOptions {
521
+ prefer?: 'feed' | 'page' | 'reader';
522
+ /** Sites that allow being framed. */
523
+ frameable?: string[];
524
+ onOpen?: (index: number) => void;
525
+ }
526
+
527
+ interface PlayAudioOptions {
528
+ /** https */
529
+ url: string;
530
+ /** Played without a Referer, through the host's relay. */
531
+ relay?: boolean;
532
+ live?: boolean;
533
+ }
534
+
535
+ interface AudioState {
536
+ url: string | null;
537
+ playing: boolean;
538
+ loading: boolean;
539
+ error: null | 'blocked' | 'failed';
540
+ /** Seconds, for a file. */
541
+ time?: number | null;
542
+ duration?: number | null;
543
+ }
544
+
545
+ interface AudioControls {
546
+ resume?: () => void;
547
+ /** 0 to 1. */
548
+ setVolume?: (volume: number) => void;
549
+ next?: () => void;
550
+ previous?: () => void;
551
+ }
552
+
553
+ interface AudioInfo {
554
+ source?: string;
555
+ title?: string;
556
+ artist?: string;
557
+ artwork?: string;
558
+ artworkType?: string;
559
+ artworkSizes?: string;
560
+ }
561
+
562
+ interface VideoSpec {
563
+ provider: 'youtube' | 'vimeo' | 'arte';
564
+ id: string;
565
+ title?: string;
566
+ /** A YouTube playlist. */
567
+ list?: string;
568
+ /** An ARTE language. */
569
+ lang?: string;
570
+ }
571
+
572
+ interface VideoState {
573
+ provider: string;
574
+ id: string;
575
+ list?: string;
576
+ playing: boolean;
577
+ }
578
+
579
+ interface PhotoSpec {
580
+ src: string;
581
+ alt?: string;
582
+ title?: string;
583
+ preview?: string;
584
+ /** A larger picture of the same, asked only when the person zooms in. */
585
+ zoom?: string;
586
+ width?: number;
587
+ height?: number;
588
+ /** What the picture's licence asks to go with it. A plain string is its credit text. */
589
+ credit?: PhotoCredit | string;
590
+ }
591
+
592
+ /**
593
+ * A picture's credit, in schema.org's ImageObject names (https://schema.org/ImageObject), every member optional.
594
+ * Addresses are `https`.
595
+ */
596
+ interface PhotoCredit {
597
+ /** The work's title. */
598
+ name?: string;
599
+ /** Who made it. */
600
+ creator?: string;
601
+ /** The credit line: who keeps or gives the picture (a museum, an agency). */
602
+ creditText?: string;
603
+ /** Who the licence says to name, as it asks ("Photo: …", "© …"). */
604
+ copyrightNotice?: string;
605
+ /** The licence's address. */
606
+ license?: string;
607
+ /** The picture's page at its source. */
608
+ url?: string;
609
+ }
610
+
611
+ /**
612
+ * What a running widget may change of an action its file declares (7.13). A field left out stays as it was; `null`
613
+ * puts it back to what the file declares.
614
+ */
615
+ interface WidgetActionState {
616
+ /** Greyed: nothing to act on. */
617
+ disabled?: boolean | null;
618
+ /** Not in the menu. */
619
+ hidden?: boolean | null;
620
+ /** Another label, 1 to 40 characters: a word that depends on its state, or a translation. */
621
+ label?: string | null;
622
+ }
623
+
624
+ interface AddWidgetSpec {
625
+ /** A file of the host's widget directory. */
626
+ file: string;
627
+ title?: string;
628
+ /** At most 12, 200 characters each. */
629
+ values?: Record<string, string>;
630
+ }
631
+ }
632
+ }
633
+ ```
634
+
635
+ ---
636
+
637
+ ## The specification, by section
638
+
639
+ The full text: https://startside.app/swa/1.0/ (Markdown: https://startside.app/swa/1.0/spec.md).
640
+
641
+ - [Abstract](https://startside.app/swa/1.0/#abstract)
642
+ - [Status of This Document](https://startside.app/swa/1.0/#status-of-this-document)
643
+ - [1. Introduction](https://startside.app/swa/1.0/#1-introduction)
644
+ - [2. Conformance](https://startside.app/swa/1.0/#2-conformance)
645
+ - [3. Terminology](https://startside.app/swa/1.0/#3-terminology)
646
+ - [4. Widget files](https://startside.app/swa/1.0/#4-widget-files)
647
+ - [4.1 File names and keys](https://startside.app/swa/1.0/#41-file-names-and-keys)
648
+ - [4.2 Document requirements](https://startside.app/swa/1.0/#42-document-requirements)
649
+ - [4.3 The runtime script](https://startside.app/swa/1.0/#43-the-runtime-script)
650
+ - [4.4 The title](https://startside.app/swa/1.0/#44-the-title)
651
+ - [4.5 Metadata](https://startside.app/swa/1.0/#45-metadata)
652
+ - [4.6 Icon and thumbnail](https://startside.app/swa/1.0/#46-icon-and-thumbnail)
653
+ - [4.7 Styles](https://startside.app/swa/1.0/#47-styles)
654
+ - [4.8 Scripts](https://startside.app/swa/1.0/#48-scripts)
655
+ - [4.9 Initial content](https://startside.app/swa/1.0/#49-initial-content)
656
+ - [5. The settings form](https://startside.app/swa/1.0/#5-the-settings-form)
657
+ - [5.1 The `preferences` form](https://startside.app/swa/1.0/#51-the-preferences-form)
658
+ - [5.2 Controls](https://startside.app/swa/1.0/#52-controls)
659
+ - [5.3 Attributes](https://startside.app/swa/1.0/#53-attributes)
660
+ - [6. Processing model](https://startside.app/swa/1.0/#6-processing-model)
661
+ - [6.1 Widget records](https://startside.app/swa/1.0/#61-widget-records)
662
+ - [6.2 Reading a widget file](https://startside.app/swa/1.0/#62-reading-a-widget-file)
663
+ - [6.3 Extracting the settings](https://startside.app/swa/1.0/#63-extracting-the-settings)
664
+ - [6.4 Resolving the declared feed](https://startside.app/swa/1.0/#64-resolving-the-declared-feed)
665
+ - [6.5 Applying a setting](https://startside.app/swa/1.0/#65-applying-a-setting)
666
+ - [6.6 Starting and refreshing](https://startside.app/swa/1.0/#66-starting-and-refreshing)
667
+ - [6.7 Activation-gated operations](https://startside.app/swa/1.0/#67-activation-gated-operations)
668
+ - [6.8 The keyboard coming into a widget](https://startside.app/swa/1.0/#68-the-keyboard-coming-into-a-widget)
669
+ - [7. The `Widget` interface](https://startside.app/swa/1.0/#7-the-widget-interface)
670
+ - [7.1 The `widget` object](https://startside.app/swa/1.0/#71-the-widget-object)
671
+ - [7.2 Identity and environment](https://startside.app/swa/1.0/#72-identity-and-environment)
672
+ - [7.3 Content and title](https://startside.app/swa/1.0/#73-content-and-title)
673
+ - [7.4 Appearance](https://startside.app/swa/1.0/#74-appearance)
674
+ - [7.5 Settings](https://startside.app/swa/1.0/#75-settings)
675
+ - [7.6 Events and lifecycle](https://startside.app/swa/1.0/#76-events-and-lifecycle)
676
+ - [7.7 Lists and grips](https://startside.app/swa/1.0/#77-lists-and-grips)
677
+ - [7.8 Memory and stored data](https://startside.app/swa/1.0/#78-memory-and-stored-data)
678
+ - [7.9 Kept feeds](https://startside.app/swa/1.0/#79-kept-feeds)
679
+ - [7.10 Read state and counts](https://startside.app/swa/1.0/#710-read-state-and-counts)
680
+ - [7.11 The reader](https://startside.app/swa/1.0/#711-the-reader)
681
+ - [7.12 Sound, video and pictures](https://startside.app/swa/1.0/#712-sound-video-and-pictures)
682
+ - [7.13 The page](https://startside.app/swa/1.0/#713-the-page)
683
+ - [7.14 Absent members](https://startside.app/swa/1.0/#714-absent-members)
684
+ - [7.15 Error reports](https://startside.app/swa/1.0/#715-error-reports)
685
+ - [8. Data access](https://startside.app/swa/1.0/#8-data-access)
686
+ - [8.1 Fetching](https://startside.app/swa/1.0/#81-fetching)
687
+ - [8.2 `widget.fetch`](https://startside.app/swa/1.0/#82-widgetfetch)
688
+ - [8.3 `widget.fetchFeed`](https://startside.app/swa/1.0/#83-widgetfetchfeed)
689
+ - [8.4 Freshness](https://startside.app/swa/1.0/#84-freshness)
690
+ - [8.5 Content feeds](https://startside.app/swa/1.0/#85-content-feeds)
691
+ - [9. Built-in objects](https://startside.app/swa/1.0/#9-built-in-objects)
692
+ - [10. Execution environments](https://startside.app/swa/1.0/#10-execution-environments)
693
+ - [10.1 Framed](https://startside.app/swa/1.0/#101-framed)
694
+ - [10.2 No inline mode](https://startside.app/swa/1.0/#102-no-inline-mode)
695
+ - [10.3 Standalone](https://startside.app/swa/1.0/#103-standalone)
696
+ - [10.4 Trust](https://startside.app/swa/1.0/#104-trust)
697
+ - [11. Compatibility with UWA 1.2](https://startside.app/swa/1.0/#11-compatibility-with-uwa-12)
698
+ - [11.1 Files and preferences](https://startside.app/swa/1.0/#111-files-and-preferences)
699
+ - [11.2 UWA's extensions of built-in objects](https://startside.app/swa/1.0/#112-uwas-extensions-of-built-in-objects)
700
+ - [11.3 UWA's callbacks](https://startside.app/swa/1.0/#113-uwas-callbacks)
701
+ - [12. Security Considerations](https://startside.app/swa/1.0/#12-security-considerations)
702
+ - [13. Privacy Considerations](https://startside.app/swa/1.0/#13-privacy-considerations)
703
+ - [14. Accessibility Considerations](https://startside.app/swa/1.0/#14-accessibility-considerations)
704
+ - [Appendix A. Examples](https://startside.app/swa/1.0/#appendix-a-examples)
705
+ - [Appendix B. Changes from UWA 1.2.4](https://startside.app/swa/1.0/#appendix-b-changes-from-uwa-124)
706
+ - [Appendix C. Implementation status and features at risk](https://startside.app/swa/1.0/#appendix-c-implementation-status-and-features-at-risk)
707
+ - [Appendix D. IDL Index](https://startside.app/swa/1.0/#appendix-d-idl-index)
708
+ - [Appendix E. Open issues](https://startside.app/swa/1.0/#appendix-e-open-issues)
709
+ - [Appendix F. Tools for authors](https://startside.app/swa/1.0/#appendix-f-tools-for-authors)
710
+ - [Normative references](https://startside.app/swa/1.0/#normative-references)
711
+ - [Informative references](https://startside.app/swa/1.0/#informative-references)