@startside/swa 1.0.2 → 1.0.4

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,152 @@
1
+ # Running SWA widgets in your own page
2
+
3
+ How a host page runs an SWA 1.0 widget file in a sandboxed frame, with this package's `swa/1.0/frame.js` (the runtime
4
+ the frame runs) and `swa/1.0/frame.css` (its base styles). It is what Startside's dashboard does, reduced to what a
5
+ widget needs to run. The specification says what a host must do (section 6, the processing model, and 10.1, framed);
6
+ this page says how this runtime expects to be given a widget.
7
+
8
+ The messages below are this runtime's, release by release: a host serving `frame.js` from one release of the package
9
+ speaks that release's messages. Pin the version you tested.
10
+
11
+ `runtime.js` is not for a host: it is the runtime a widget file loads to run on its own page, and a host skips it (4.3).
12
+ RWA files (`.rwa.html`) need RWA's runtime: `@startside/rwa`, whose `rwa/1.0/host.md` says what differs.
13
+
14
+ ## The host's side as code: `host.js`
15
+
16
+ Since 1.0.4, `swa/1.0/host.js` does what this page describes for the file and the frame's document, with the code
17
+ Startside's dashboard runs for its own frames: a plain ES module for the browser, nothing to build. Take it from the
18
+ same release as `frame.js`.
19
+
20
+ ```js
21
+ import { accepts, buildSrcdoc, parseWidget, prefShown, prefValues } from 'https://cdn.jsdelivr.net/npm/@startside/swa@1.0.4/swa/1.0/host.js';
22
+ ```
23
+
24
+ - `parseWidget(text, url)`: the file's text, fetched from `url`, read (6.2) with the browser's `DOMParser`: `{ url,
25
+ format, title, icon, metas, preferences, actions, styles, stylesheets, scripts, body }`, the file's own runtime left
26
+ out. It throws for a file that isn't SWA.
27
+ - `prefValues(doc, saved)`: each setting's value as a string, the saved one, else its default; saved values of no
28
+ declared setting are kept.
29
+ - `prefShown(pref, values)`: whether a setting shows, by its `data-when` (5).
30
+ - `buildSrcdoc(doc, config, look)`: the frame's document, below. `config` is your part of the config (`instance`,
31
+ `values`, `lang`, `memory`, `stored`, `read`, `noAdd`); what it says of the file (`title`, `url`, `format`,
32
+ `preferences` with a password's default left out, `metas`, `actions`) is added from `doc`. `look`: `mode`, `light` or
33
+ `dark`; `vars`, the CSS variables for `:root` (`{ '--text': '#222' }`); `css`, `frame.css`'s text; `runtime`, `{ code
34
+ }` to write the runtime in or `{ src }` to load it by address (with `crossorigin`); `origin`, an origin that answers
35
+ with CORS, whose scripts the file names are loaded with `crossorigin` too.
36
+ - `accepts(event, frameWindow, instance)`: whether a message is from that frame, about that instance.
37
+
38
+ The frame itself, its sandbox and the messages stay yours, as below.
39
+
40
+ ## The frame
41
+
42
+ One frame per widget, sandboxed with `allow-scripts` and never `allow-same-origin`: the widget's origin is opaque, it
43
+ reaches neither your page, its storage, nor your cookies (10.1). Add `allow-forms`, and `allow-popups
44
+ allow-popups-to-escape-sandbox` for links the widget opens. Give it its document with `srcdoc`.
45
+
46
+ Never serve a widget from your own origin, or give its frame `allow-same-origin` with a document from your origin: its
47
+ scripts would be your page's.
48
+
49
+ ## The frame's document
50
+
51
+ Read the widget file first (section 6.2): its title, its metadata, its settings form, its styles and stylesheet links,
52
+ its scripts, its body. Then write, in this order:
53
+
54
+ ```html
55
+ <!doctype html>
56
+ <html lang="en" data-color-scheme="light">
57
+ <head>
58
+ <meta charset="utf-8">
59
+ <meta http-equiv="Content-Security-Policy" content="connect-src https:">
60
+ <base href="WIDGET_FILE_URL" target="_blank">
61
+ <style>@layer host, widget; @layer host { /* frame.css, and the variables below */ }</style>
62
+ <style>@layer widget { /* the file's own styles */ }</style>
63
+ <script>window.__UWA_CONFIG__ = { /* the config, below */ };</script>
64
+ <script>/* frame.js */</script>
65
+ </head>
66
+ <body>
67
+ <div id="moduleContent" class="moduleContent"><!-- the file's body --></div>
68
+ <script>__startside.attachBody();</script>
69
+ <!-- the file's scripts: those with a src first, in order, then its inline ones, in order -->
70
+ <script>__startside.start();</script>
71
+ </body>
72
+ </html>
73
+ ```
74
+
75
+ - The file's own runtime script (`/swa/1.0/runtime.js`, or `data-runtime="swa/1.0"`) is left out.
76
+ - **The runtime's text written in**, as above, is simplest. Its text never holds `<script`, `</script` or `<!--`. A JSON
77
+ config written into a script needs its `<` escaped (`<`).
78
+ - **Loaded by its address** instead (`<script src="…/frame.js" crossorigin="anonymous">`): the server must send
79
+ `Access-Control-Allow-Origin` (jsDelivr and unpkg do), and the tag needs `crossorigin`. Otherwise the browser hides
80
+ every error from the frame's scripts as "Script error.", the widget's own included, since the frame's origin is
81
+ opaque and every script is cross-origin to it. The same goes for the file's scripts loaded by address.
82
+ - `frame.css` uses these variables, each with a fallback: `--text`, `--muted`, `--link`, `--border`, `--font-body`. The
83
+ specification lists what a widget may read (4.7): `--text`, `--muted`, `--link`, `--link-visited`, `--bg`,
84
+ `--widget-bg`, `--border`, `--hover`, `--font-body`, `--accent`. Set the ones your page has on `:root`, in the host
85
+ layer.
86
+ - Light or dark: `data-color-scheme` on the root, `light` or `dark`, and `color-scheme` the same in the host layer
87
+ (4.7). A widget reads its mode from them.
88
+
89
+ ## The config
90
+
91
+ `window.__UWA_CONFIG__`, set before `frame.js` runs.
92
+
93
+ | Field | | What it is |
94
+ |---|---|---|
95
+ | `instance` | required | A string unique to this load of this widget. Every message carries it, both ways. A new one at each reload. |
96
+ | `title` | required | The file's title (4.4). |
97
+ | `url` | required | The widget file's absolute address: relative addresses in its requests resolve against it. |
98
+ | `format` | required | `"swa"`. |
99
+ | `preferences` | required | The settings form as read (5): `[{ name, type, label, defaultValue, options: [{ value, label }], min?, max?, step? }]`. A password's `defaultValue` is `""` (5.2). |
100
+ | `values` | required | The settings' saved values, by name, as strings. `{}` the first time. |
101
+ | `metas` | required | The file's metadata (4.5), by name. |
102
+ | `lang` | | The page's language, `en-US` by default. |
103
+ | `actions` | | The file's declared actions (7.13), `[]` if it has none. |
104
+ | `memory` | | What the widget remembered in its last load (`widget.remember`), by key. |
105
+ | `stored` | | What it stored (`widget.setStored`), by key: the frame has no storage of its own. |
106
+ | `read` | | The keys of the items it marked read. |
107
+ | `noAdd` | | `true`: `widget.addWidget` is absent. |
108
+
109
+ Startside's other fields (`dashboard`, `theme`, `mode`, `accent`, `neutral`, `moduleVars`, `faces`, `people`) are its own:
110
+ leave them out. Without `dashboard`, the frame's `fetch` goes to the network directly, never to your page.
111
+
112
+ ## Messages
113
+
114
+ Both ways, with `postMessage`, each an object with a `type` and `uwa`, the config's `instance`. The frame posts to
115
+ `parent` with the target `'*'` (its origin is opaque). Accept a message only when `event.source` is that frame's
116
+ `contentWindow` and `uwa` is its instance, and check each field's type and size before using it (10.1).
117
+
118
+ ### Requests: answer each one
119
+
120
+ A request carries a `reqId`. Its promise in the widget waits until you answer with the same `reqId`: a request left
121
+ unanswered leaves the widget waiting forever. Refuse what you don't offer.
122
+
123
+ | The frame sends | You answer |
124
+ |---|---|
125
+ | `{ type: 'request', reqId, kind, url }`: `widget.fetch` (`kind` `text`, or `json` when it asked with a JSON `Accept`), `widget.fetchFeed` (`jsonfeed`), `widget.fetchImage` (`image`) | `{ type: 'response', reqId, ok: true, data }` or `{ …, ok: false, error }`. `text`, `json`: the body as a string. `jsonfeed`: the feed as JSON Feed 1.1 (8.3). `image`: `{ type, bytes }`, an `ArrayBuffer`, or the address itself as a string. An error `"HTTP 404"` becomes a response with that status in the widget; any other, a failed fetch. Another `kind`: refuse it. |
126
+ | `{ type: 'files', reqId, op, … }`: `widget.storage.files` (7.8) | `{ type: 'filesResult', reqId, ok: false, error: { name: 'NotSupportedError', message } }` unless you keep files. |
127
+ | `{ type: 'saveFile', reqId, … }`: `widget.saveFile` (7.13) | `{ type: 'saveFileResult', reqId, ok: false, error: 'NotSupportedError' }` unless you offer it. |
128
+ | `{ type: 'connectGoogle', reqId, … }`: Startside's own | `{ type: 'dashboardResponse', reqId, ok: false, error }`. |
129
+
130
+ The proxy behind `request` is yours: a widget's requests go to any site, so it must refuse local and private network
131
+ addresses at every redirect, and limit sizes and time (12).
132
+
133
+ ### What the frame tells you: use what you need
134
+
135
+ - `height` `{ height }`: the document's height in CSS pixels, as it changes. Size the frame to it.
136
+ - `setTitle` `{ title }`, `setIcon` `{ url }`, `setUnreadCount` `{ count }`, `loading` `{ on }`.
137
+ - `setValue` `{ name, value }`, `deleteValue` `{ name }`: save the setting; give it back in `values` at the next load.
138
+ - `remember` `{ key, value }`, `setStored` `{ key, value }`, `setRead` `{ keys, read }`: keep them for the next load's
139
+ `memory`, `stored` and `read`.
140
+ - `openURL` `{ url }`: open it, in a new tab if you have nothing better. Only after a click in the widget (6.7).
141
+ - `openPreferences`: show its settings form; once saved, send `prefsSaved`.
142
+
143
+ Any other type can be ignored: the feature it serves then does nothing (2: a host may implement part of section 7).
144
+
145
+ ### What you can send
146
+
147
+ - `{ type: 'prefsSaved', uwa, values }`: the settings changed; the widget applies them (6.5).
148
+ - `{ type: 'refresh', uwa }`: the refresh button (6.6).
149
+ - `{ type: 'resize', uwa }`: the frame's width changed.
150
+ - `{ type: 'focus', uwa }`: the keyboard comes into the widget (6.8).
151
+ - `{ type: 'tabShown', uwa, shown }`: your page hides or shows the widget without unloading it; it reads as hidden
152
+ meanwhile.
@@ -124,8 +124,10 @@ What it shows, in order:
124
124
  | SHOULD | Follow its width with a `ResizeObserver` on its own element when its layout depends on it | 7.3.2 |
125
125
  | SHOULD | Meet WCAG 2.2 AA: keyboard alone, names on controls, 4.5:1 text, `prefers-reduced-motion` | 14 |
126
126
  | 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 |
127
+ | MAY | Read an API that wants the person's own key: `<input type="password" name="key" data-key-for="https://api.example">` in the settings, then `widget.fetch` to that origin. The host keeps the key and adds `Authorization: Bearer`; the widget never sees it (`widget.settings.get('key')` is `''`), and a 401 or 403 means no key yet: say to add one in the settings | 5.3, 8.2 |
127
128
  | 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 |
128
129
  | 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 |
130
+ | MAY | Take a file dragged from the computer onto it: `<meta name="acceptFiles" content=".gpx">` (the extensions it takes), then its own `dragover` (`preventDefault()`) and `drop` listeners; a file it doesn't take, left uncancelled, is refused, never opened. A file input (`<input type="file" accept=".gpx">`) beside it, for the keyboard and a phone | 4.5, 10.1 |
129
131
  | 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 |
130
132
 
131
133
  ## Checklist