@startside/swa 0.0.0-stage → 1.0.1
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.
- package/LICENSE +81 -0
- package/README.md +94 -2
- package/bin/swa.mjs +33 -0
- package/package.json +30 -4
- package/swa/1.0/agents.md +638 -0
- package/swa/1.0/quickstart.md +145 -0
- package/swa/1.0/runtime.js +4 -0
- package/swa/1.0/spec.md +2125 -0
- package/swa/1.0/swa.d.ts +361 -0
package/swa/1.0/swa.d.ts
ADDED
|
@@ -0,0 +1,361 @@
|
|
|
1
|
+
// SWA 1.0, the Startside Widget API: its types, for a widget file's scripts (docs/swa-spec.md, section 7, its Web IDL).
|
|
2
|
+
// Served at /swa/1.0/swa.d.ts. In an editor, next to a widget's code:
|
|
3
|
+
//
|
|
4
|
+
// /// <reference path="./swa.d.ts" />
|
|
5
|
+
//
|
|
6
|
+
// or in a jsconfig.json / tsconfig.json, "files": ["swa.d.ts"]. The global `widget` is then typed, with completion.
|
|
7
|
+
// scripts/check/swa-api.mjs checks that the runtime's `widget` has every member named here, and nothing else public, and
|
|
8
|
+
// that the readonly attributes are: assigning one throws in strict code, and what they give is frozen.
|
|
9
|
+
|
|
10
|
+
export {};
|
|
11
|
+
|
|
12
|
+
declare global {
|
|
13
|
+
/** The widget the file's code runs in (SWA 1.0, 7.1). */
|
|
14
|
+
const widget: SWA.Widget;
|
|
15
|
+
|
|
16
|
+
/** Every event the host fires at a widget (SWA 1.0, 7.6.1). */
|
|
17
|
+
class WidgetEvent<D = unknown> extends Event {
|
|
18
|
+
constructor(type: string, init?: EventInit & { detail?: D });
|
|
19
|
+
/** The event's data, as the table of 7.6.1 says: `fullsize`'s new value, `audiostate`'s state… */
|
|
20
|
+
readonly detail: D;
|
|
21
|
+
/** A load's (`load`, `refresh`): aborted when the next load starts or the widget goes. Null on other events. */
|
|
22
|
+
readonly signal: AbortSignal | null;
|
|
23
|
+
/** Extends a load (`load`, `refresh`) until `promise` settles; a rejection is reported in the widget. */
|
|
24
|
+
waitUntil(promise: Promise<unknown>): void;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
namespace SWA {
|
|
28
|
+
/** The events a widget listens for (SWA 1.0, 7.6.1), each with its `detail`. */
|
|
29
|
+
interface WidgetEventMap {
|
|
30
|
+
load: WidgetEvent<null>;
|
|
31
|
+
loadcomplete: WidgetEvent<null>;
|
|
32
|
+
refresh: WidgetEvent<null>;
|
|
33
|
+
resize: WidgetEvent<null>;
|
|
34
|
+
updatebody: WidgetEvent<null>;
|
|
35
|
+
updatepreferences: WidgetEvent<null>;
|
|
36
|
+
hideedit: WidgetEvent<null>;
|
|
37
|
+
/** Cancelable: canceling skips marking `readItems`' items read. */
|
|
38
|
+
readall: WidgetEvent<null>;
|
|
39
|
+
audiostate: WidgetEvent<AudioState>;
|
|
40
|
+
audioresume: WidgetEvent<null>;
|
|
41
|
+
videostate: WidgetEvent<VideoState | null>;
|
|
42
|
+
fullsize: WidgetEvent<boolean>;
|
|
43
|
+
fullscreen: WidgetEvent<boolean>;
|
|
44
|
+
/** Cancelable: canceling skips the refresh that follows. */
|
|
45
|
+
settingschange: WidgetEvent<{ names: string[] }>;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
interface Widget extends EventTarget {
|
|
49
|
+
// 7.2 Identity and environment
|
|
50
|
+
readonly id: string;
|
|
51
|
+
/** The title shown: the person's name for the widget if they renamed it (then `settingschange` with `title`), else its own (7.3). */
|
|
52
|
+
readonly title: string;
|
|
53
|
+
/** The widget file's address. */
|
|
54
|
+
readonly uwaUrl: string;
|
|
55
|
+
/** The person's language in UWA's form, `en_US`: `lang.replace('_', '-')` is a language tag for `Intl`. */
|
|
56
|
+
readonly lang: string;
|
|
57
|
+
/** The person's region, `us`. */
|
|
58
|
+
readonly locale: string;
|
|
59
|
+
/** The file's metadata (`<meta name content>`), by name. */
|
|
60
|
+
readonly metas: Readonly<Record<string, string>>;
|
|
61
|
+
/** The settings the file declares, frozen. */
|
|
62
|
+
readonly preferences: readonly WidgetSetting[];
|
|
63
|
+
/** Frozen. */
|
|
64
|
+
readonly environment: Readonly<WidgetEnvironment>;
|
|
65
|
+
/** The widget's content: set from `load` on, in every environment (SWA 1.0, 7.3.1). */
|
|
66
|
+
readonly body: HTMLElement;
|
|
67
|
+
log(...data: unknown[]): void;
|
|
68
|
+
|
|
69
|
+
// 7.3 Content and title
|
|
70
|
+
setBody(content: string | Node): void;
|
|
71
|
+
addBody(content: string | Node): void;
|
|
72
|
+
createElement<K extends keyof HTMLElementTagNameMap>(tagName: K, attributes?: Record<string, unknown>): HTMLElementTagNameMap[K];
|
|
73
|
+
createElement(tagName: string, attributes?: Record<string, unknown>): HTMLElement;
|
|
74
|
+
setTitle(title: string): void;
|
|
75
|
+
getTitle(): string;
|
|
76
|
+
setStyle(css: string): void;
|
|
77
|
+
|
|
78
|
+
// 7.4 Appearance
|
|
79
|
+
setIcon(url: string | null): void;
|
|
80
|
+
setChromeColor(color: string | null, options?: { background?: string | null }): void;
|
|
81
|
+
|
|
82
|
+
// 7.5 Settings
|
|
83
|
+
readonly settings: WidgetSettings;
|
|
84
|
+
getPreference(name: string): WidgetSetting | null;
|
|
85
|
+
setMaxValue(name: string, max: number): void;
|
|
86
|
+
openPreferences(): void;
|
|
87
|
+
withPreferencesLink(text: string): HTMLSpanElement;
|
|
88
|
+
|
|
89
|
+
// 7.6 Events and lifecycle
|
|
90
|
+
addEventListener<K extends keyof WidgetEventMap>(
|
|
91
|
+
type: K,
|
|
92
|
+
listener: (this: Widget, event: WidgetEventMap[K]) => unknown,
|
|
93
|
+
options?: boolean | AddEventListenerOptions,
|
|
94
|
+
): void;
|
|
95
|
+
addEventListener(type: string, listener: EventListenerOrEventListenerObject | null, options?: boolean | AddEventListenerOptions): void;
|
|
96
|
+
removeEventListener<K extends keyof WidgetEventMap>(
|
|
97
|
+
type: K,
|
|
98
|
+
listener: (this: Widget, event: WidgetEventMap[K]) => unknown,
|
|
99
|
+
options?: boolean | EventListenerOptions,
|
|
100
|
+
): void;
|
|
101
|
+
removeEventListener(type: string, listener: EventListenerOrEventListenerObject | null, options?: boolean | EventListenerOptions): void;
|
|
102
|
+
/** A refresh the widget asks for itself, as when its settings apply. */
|
|
103
|
+
refresh(): void;
|
|
104
|
+
setAutoRefresh(minutes: number): void;
|
|
105
|
+
setPeriodical(name: string, callback: () => void, delay: number, now?: boolean): void;
|
|
106
|
+
clearPeriodical(name: string): void;
|
|
107
|
+
|
|
108
|
+
// 7.7 Lists and grips
|
|
109
|
+
countGrip(box: Element, itemSelector: string, options?: CountGripOptions): void;
|
|
110
|
+
heightGrip(box: Element, scroller: Element, options?: HeightGripOptions): void;
|
|
111
|
+
|
|
112
|
+
// 7.8 Memory and stored data
|
|
113
|
+
readonly storage: WidgetStorage;
|
|
114
|
+
|
|
115
|
+
// 8 Data access
|
|
116
|
+
/** The web's fetch, through the host's proxy: GET only, the body as text; an HTTP error a Response, not a rejection. */
|
|
117
|
+
fetch(input: string | URL | Request, init?: RequestInit): Promise<Response>;
|
|
118
|
+
/** RSS, Atom or JSON Feed as a JSON Feed 1.1, kept by the host: a kept copy answers at once. */
|
|
119
|
+
fetchFeed(url: string, options?: FetchFeedOptions): Promise<ContentFeed>;
|
|
120
|
+
|
|
121
|
+
// 7.10 Read state and counts
|
|
122
|
+
readItems(box: Element, itemSelector: string, options?: { key?: (item: Element) => string }): void;
|
|
123
|
+
isRead(key: string): boolean;
|
|
124
|
+
setRead(keys: string | string[], read?: boolean): void;
|
|
125
|
+
setUnreadCount(count: number): void;
|
|
126
|
+
setSearchResultCount(count: number): void;
|
|
127
|
+
|
|
128
|
+
// 7.11 The reader (activation-gated)
|
|
129
|
+
openURL(url: string): void;
|
|
130
|
+
openItems(items: ReaderItem[], index: number, options?: OpenItemsOptions): void;
|
|
131
|
+
|
|
132
|
+
// 7.12 Sound, video, pictures
|
|
133
|
+
/** Frozen: a new object at each change (`audiostate`). */
|
|
134
|
+
readonly audioState: Readonly<AudioState>;
|
|
135
|
+
playAudio(options: PlayAudioOptions): unknown;
|
|
136
|
+
pauseAudio(): unknown;
|
|
137
|
+
seekAudio(seconds: number): unknown;
|
|
138
|
+
audioStarted(stop: () => void, controls?: AudioControls): void;
|
|
139
|
+
audioStopped(): void;
|
|
140
|
+
audioInfo(info?: AudioInfo): void;
|
|
141
|
+
readonly videoState: Readonly<VideoState> | null;
|
|
142
|
+
playVideo(video: VideoSpec): unknown;
|
|
143
|
+
openPhoto(photo: PhotoSpec): unknown;
|
|
144
|
+
|
|
145
|
+
// 7.13 The page
|
|
146
|
+
shareUrl(): string | null;
|
|
147
|
+
share(data?: { text?: string; url?: string }): unknown;
|
|
148
|
+
/** Absent where no widget can be added (a preview, a widget on its own page): `if (widget.addWidget)`. */
|
|
149
|
+
addWidget?(spec: AddWidgetSpec): unknown;
|
|
150
|
+
dragWidget(element: Element, spec: AddWidgetSpec | (() => AddWidgetSpec)): boolean;
|
|
151
|
+
readonly canFullSize: boolean;
|
|
152
|
+
readonly fullSize: boolean;
|
|
153
|
+
openFull(): void;
|
|
154
|
+
closeFull(): void;
|
|
155
|
+
/** Full screen: the browser's, or the widget alone over the window where the browser has none (7.13). */
|
|
156
|
+
readonly fullScreen: boolean;
|
|
157
|
+
setFullScreen(on: boolean): void;
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
/** Settings typed by the control that declares them (7.5): a checkbox's a boolean, a range's or a number's a number. */
|
|
161
|
+
interface WidgetSettings {
|
|
162
|
+
/** The setting's value; `undefined` for a name the form doesn't declare. `title`: the widget's title (7.3, 7.5). */
|
|
163
|
+
get(name: string): string | number | boolean | null | undefined;
|
|
164
|
+
/**
|
|
165
|
+
* Saved without a refresh; a checkbox's takes a boolean, a range's or a number's a number, any other's a string
|
|
166
|
+
* (null or undefined: empty). A wrong type, or an undeclared name, throws a TypeError. A host may limit a value's
|
|
167
|
+
* length (Startside: 20,000 characters): one over it isn't kept, and the widget's console says so (7.5).
|
|
168
|
+
* `set('title', name)`: the person's name for the widget, typed in it ('' for its own; 7.5).
|
|
169
|
+
*/
|
|
170
|
+
set(name: string, value: string | number | boolean | null | undefined): void;
|
|
171
|
+
reset(name: string): void;
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
/** What a widget keeps (7.8): JSON values, copied in and out. */
|
|
175
|
+
interface WidgetStorage {
|
|
176
|
+
/** 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. */
|
|
177
|
+
readonly memory: WidgetStorageArea;
|
|
178
|
+
/** Across reloads, shared by every widget of the same file: scores. */
|
|
179
|
+
readonly local: WidgetStorageArea;
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
interface WidgetStorageArea {
|
|
183
|
+
get<T = unknown>(key: string): Promise<T | undefined>;
|
|
184
|
+
set(key: string, value: unknown): Promise<void>;
|
|
185
|
+
delete(key: string): Promise<void>;
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
interface WidgetEnvironment {
|
|
189
|
+
readonly name: string;
|
|
190
|
+
readonly standalone: boolean;
|
|
191
|
+
/** false: an SWA widget never runs inline (SWA 1.0, 10.2). */
|
|
192
|
+
readonly inline: boolean;
|
|
193
|
+
/** The page's style, or "" standalone. */
|
|
194
|
+
readonly style: string;
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
/** A setting as the file declares it: frozen, as `preferences` and `getPreference` give it. */
|
|
198
|
+
interface WidgetSetting {
|
|
199
|
+
readonly name: string;
|
|
200
|
+
readonly type: 'text' | 'password' | 'hidden' | 'boolean' | 'list' | 'range' | 'number' | 'url';
|
|
201
|
+
readonly label?: string;
|
|
202
|
+
readonly defaultValue?: string;
|
|
203
|
+
readonly options?: readonly { readonly value: string; readonly label?: string; readonly feed?: string }[];
|
|
204
|
+
readonly min?: number;
|
|
205
|
+
readonly max?: number;
|
|
206
|
+
readonly step?: number;
|
|
207
|
+
readonly hint?: string;
|
|
208
|
+
readonly when?: string;
|
|
209
|
+
readonly commit?: 'ok';
|
|
210
|
+
readonly shareable?: boolean;
|
|
211
|
+
readonly load?: string;
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
interface CountGripOptions {
|
|
215
|
+
/** Adds the items not shown yet. */
|
|
216
|
+
onStart?: () => void;
|
|
217
|
+
onDone?: (count: number) => void;
|
|
218
|
+
min?: number;
|
|
219
|
+
/** The setting holding the count: a double-click restores its default. */
|
|
220
|
+
setting?: string;
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
interface HeightGripOptions {
|
|
224
|
+
height?: number;
|
|
225
|
+
min?: number;
|
|
226
|
+
/** A selector: the list's items. */
|
|
227
|
+
items?: string;
|
|
228
|
+
onDone?: (pixels: number) => void;
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
interface FetchFeedOptions {
|
|
232
|
+
signal?: AbortSignal | null;
|
|
233
|
+
/** Skip the kept copy (a refresh the person asked for). */
|
|
234
|
+
fresh?: boolean;
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
/** A JSON Feed 1.1 document with the host's extensions (8.5). Its schema is SWA's Issue 11. */
|
|
238
|
+
interface ContentFeed {
|
|
239
|
+
version: string;
|
|
240
|
+
title?: string;
|
|
241
|
+
home_page_url?: string;
|
|
242
|
+
feed_url?: string;
|
|
243
|
+
items: ContentItem[];
|
|
244
|
+
_myvibes?: Record<string, unknown>;
|
|
245
|
+
[extension: `_${string}`]: unknown;
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
interface ContentItem {
|
|
249
|
+
id: string;
|
|
250
|
+
url?: string;
|
|
251
|
+
external_url?: string;
|
|
252
|
+
title?: string;
|
|
253
|
+
summary?: string;
|
|
254
|
+
content_html?: string;
|
|
255
|
+
content_text?: string;
|
|
256
|
+
image?: string;
|
|
257
|
+
date_published?: string;
|
|
258
|
+
date_modified?: string;
|
|
259
|
+
authors?: { name?: string; url?: string; avatar?: string }[];
|
|
260
|
+
tags?: string[];
|
|
261
|
+
attachments?: { url: string; mime_type: string; title?: string; size_in_bytes?: number; duration_in_seconds?: number }[];
|
|
262
|
+
_myvibes?: Record<string, unknown>;
|
|
263
|
+
[extension: `_${string}`]: unknown;
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
interface ReaderItem {
|
|
267
|
+
url: string;
|
|
268
|
+
/** Where its headline points, when not `url`. */
|
|
269
|
+
link?: string;
|
|
270
|
+
title?: string;
|
|
271
|
+
/** HTML, the reader's text. */
|
|
272
|
+
content?: string;
|
|
273
|
+
/** Its first view. */
|
|
274
|
+
prefer?: 'feed' | 'page' | 'reader';
|
|
275
|
+
/** An RFC 3339 date, in the reader's list. */
|
|
276
|
+
date?: string;
|
|
277
|
+
/** A short excerpt, in the reader's list. */
|
|
278
|
+
summary?: string;
|
|
279
|
+
author?: string;
|
|
280
|
+
image?: string;
|
|
281
|
+
read?: boolean;
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
interface OpenItemsOptions {
|
|
285
|
+
prefer?: 'feed' | 'page' | 'reader';
|
|
286
|
+
/** Sites that allow being framed. */
|
|
287
|
+
frameable?: string[];
|
|
288
|
+
onOpen?: (index: number) => void;
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
interface PlayAudioOptions {
|
|
292
|
+
/** https */
|
|
293
|
+
url: string;
|
|
294
|
+
/** Played without a Referer, through the host's relay. */
|
|
295
|
+
relay?: boolean;
|
|
296
|
+
live?: boolean;
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
interface AudioState {
|
|
300
|
+
url: string | null;
|
|
301
|
+
playing: boolean;
|
|
302
|
+
loading: boolean;
|
|
303
|
+
error: null | 'blocked' | 'failed';
|
|
304
|
+
/** Seconds, for a file. */
|
|
305
|
+
time?: number | null;
|
|
306
|
+
duration?: number | null;
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
interface AudioControls {
|
|
310
|
+
resume?: () => void;
|
|
311
|
+
/** 0 to 1. */
|
|
312
|
+
setVolume?: (volume: number) => void;
|
|
313
|
+
next?: () => void;
|
|
314
|
+
previous?: () => void;
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
interface AudioInfo {
|
|
318
|
+
source?: string;
|
|
319
|
+
title?: string;
|
|
320
|
+
artist?: string;
|
|
321
|
+
artwork?: string;
|
|
322
|
+
artworkType?: string;
|
|
323
|
+
artworkSizes?: string;
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
interface VideoSpec {
|
|
327
|
+
provider: 'youtube' | 'vimeo' | 'arte';
|
|
328
|
+
id: string;
|
|
329
|
+
title?: string;
|
|
330
|
+
/** A YouTube playlist. */
|
|
331
|
+
list?: string;
|
|
332
|
+
/** An ARTE language. */
|
|
333
|
+
lang?: string;
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
interface VideoState {
|
|
337
|
+
provider: string;
|
|
338
|
+
id: string;
|
|
339
|
+
list?: string;
|
|
340
|
+
playing: boolean;
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
interface PhotoSpec {
|
|
344
|
+
src: string;
|
|
345
|
+
alt?: string;
|
|
346
|
+
title?: string;
|
|
347
|
+
preview?: string;
|
|
348
|
+
width?: number;
|
|
349
|
+
height?: number;
|
|
350
|
+
credit?: string;
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
interface AddWidgetSpec {
|
|
354
|
+
/** A file of the host's widget directory. */
|
|
355
|
+
file: string;
|
|
356
|
+
title?: string;
|
|
357
|
+
/** At most 12, 200 characters each. */
|
|
358
|
+
values?: Record<string, string>;
|
|
359
|
+
}
|
|
360
|
+
}
|
|
361
|
+
}
|