@glazeen/mod-sdk 0.0.0-stage → 0.2.0
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 +21 -0
- package/README.md +221 -2
- package/bin/bearbar-mod.js +3 -0
- package/client/bb.css +58 -0
- package/client/bb.js +157 -0
- package/dist/bb-JW737zq0.d.ts +266 -0
- package/dist/cli.js +474 -0
- package/dist/global.d.ts +19 -0
- package/dist/global.js +0 -0
- package/dist/index.d.ts +33 -0
- package/dist/index.js +284 -0
- package/docs/api.md +238 -0
- package/docs/guide.md +200 -0
- package/docs/manifest.md +198 -0
- package/docs/runtime.md +150 -0
- package/docs/styling.md +118 -0
- package/package.json +66 -3
- package/schema/bearbar.mod.schema.json +276 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Antonio Scognamiglio
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,222 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @glazeen/mod-sdk
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Tools for writing widget mods for **bearbar**, the Windows dock:
|
|
4
|
+
|
|
5
|
+
- a `bearbar-mod` CLI that scaffolds a mod and validates it,
|
|
6
|
+
- TypeScript types for the `bb` API, including a config type derived from your settings,
|
|
7
|
+
- the `bearbar.mod.json` JSON Schema, for editor validation and hover docs,
|
|
8
|
+
- `validateManifest()`, for your own tooling.
|
|
9
|
+
|
|
10
|
+
A mod is a plain folder with a manifest and one or two HTML pages. It needs no build step, and
|
|
11
|
+
the dock injects the `bb` runtime into every page, so this package is optional.
|
|
12
|
+
|
|
13
|
+
## Documentation
|
|
14
|
+
|
|
15
|
+
| Doc | What |
|
|
16
|
+
| --------------------------------------- | ---------------------------------------------------------- |
|
|
17
|
+
| [Guide](docs/guide.md) | Writing your first mod, step by step |
|
|
18
|
+
| [Manifest reference](docs/manifest.md) | Every `bearbar.mod.json` field, setting type and rule |
|
|
19
|
+
| [`bb` API reference](docs/api.md) | Every property, method and event, with errors and limits |
|
|
20
|
+
| [Styling](docs/styling.md) | Theme variables, attributes, helper classes |
|
|
21
|
+
| [Sandbox and runtime](docs/runtime.md) | What the sandbox allows, permission checks, the protocol |
|
|
22
|
+
|
|
23
|
+
The docs ship in the package too, under `node_modules/@glazeen/mod-sdk/docs`.
|
|
24
|
+
|
|
25
|
+
## Quick start
|
|
26
|
+
|
|
27
|
+
```sh
|
|
28
|
+
npx @glazeen/mod-sdk init my-widget
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
This creates `my-widget/` with a manifest, a tile page and a script that has editor completion
|
|
32
|
+
for `bb`. Copy or link the folder into `%APPDATA%\bearbar\mods` (right-click the dock >
|
|
33
|
+
Add widget… > Mods folder), then add it from Add widget > Mods. Saving a file reloads the widget.
|
|
34
|
+
|
|
35
|
+
Install the package in a project that holds mods to get the types and the CLI there:
|
|
36
|
+
|
|
37
|
+
```sh
|
|
38
|
+
npm install --save-dev @glazeen/mod-sdk
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
Requires Node 20 or newer for the CLI and the validator. The types work with TypeScript 5.0+.
|
|
42
|
+
|
|
43
|
+
## CLI
|
|
44
|
+
|
|
45
|
+
```sh
|
|
46
|
+
bearbar-mod validate [dir...] # check one or more mods
|
|
47
|
+
bearbar-mod init [dir] [--name] # scaffold a mod (never overwrites files)
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
`validate` takes mod folders or folders of mods, and defaults to the current folder. It checks
|
|
51
|
+
each manifest, checks that the `tile` and `panel` pages exist, and warns about remote URLs in
|
|
52
|
+
the pages (the sandbox blocks them; use `bb.fetch` or copy the file into the mod).
|
|
53
|
+
|
|
54
|
+
```text
|
|
55
|
+
$ bearbar-mod validate mods
|
|
56
|
+
✓ clock mods/clock
|
|
57
|
+
✗ weather mods/weather
|
|
58
|
+
error settings.unit.default: "k" is not one of the option values
|
|
59
|
+
warning permisions: unknown property (did you mean "permissions"?)
|
|
60
|
+
|
|
61
|
+
2 mod(s) checked, 1 failed.
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
| Option | Effect |
|
|
65
|
+
| ---------- | -------------------------------------------------------------- |
|
|
66
|
+
| `--strict` | treat warnings as errors |
|
|
67
|
+
| `--json` | print the results as JSON, for other tools |
|
|
68
|
+
|
|
69
|
+
Exit codes: `0` all mods pass, `1` a mod failed or no mod was found, `2` usage error or a
|
|
70
|
+
folder that doesn't exist. Output is coloured on a terminal; set `NO_COLOR` to turn it off.
|
|
71
|
+
|
|
72
|
+
In CI:
|
|
73
|
+
|
|
74
|
+
```yaml
|
|
75
|
+
- run: npx @glazeen/mod-sdk validate --strict mods
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
## Editor completion for `bb`
|
|
79
|
+
|
|
80
|
+
In a plain-JS mod, reference the types at the top of your script. `// @ts-check` also turns on
|
|
81
|
+
type checking in VS Code:
|
|
82
|
+
|
|
83
|
+
```js
|
|
84
|
+
// @ts-check
|
|
85
|
+
/// <reference types="@glazeen/mod-sdk/global" />
|
|
86
|
+
|
|
87
|
+
const { size, config } = await bb.ready;
|
|
88
|
+
const off = bb.on("config", (cfg) => render(cfg)); // call off() to stop listening
|
|
89
|
+
const res = await bb.fetch("https://api.example.com/price"); // needs "net:api.example.com"
|
|
90
|
+
if (res.ok) render(res.json());
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
In TypeScript, add `"types": ["@glazeen/mod-sdk/global"]` to `compilerOptions`, or import the
|
|
94
|
+
types you need:
|
|
95
|
+
|
|
96
|
+
```ts
|
|
97
|
+
import type { Bb, ModManifest, SettingsSchema } from "@glazeen/mod-sdk";
|
|
98
|
+
```
|
|
99
|
+
|
|
100
|
+
## Typed config
|
|
101
|
+
|
|
102
|
+
`bb.config` is `Record<string, unknown>` by default. To type it from your settings, describe
|
|
103
|
+
the manifest in TypeScript with `defineManifest` and register the inferred type:
|
|
104
|
+
|
|
105
|
+
```ts
|
|
106
|
+
import { defineManifest, type InferConfig } from "@glazeen/mod-sdk";
|
|
107
|
+
|
|
108
|
+
export const manifest = defineManifest({
|
|
109
|
+
id: "weather",
|
|
110
|
+
name: "Weather",
|
|
111
|
+
version: "1.0.0",
|
|
112
|
+
tile: "tile.html",
|
|
113
|
+
settings: {
|
|
114
|
+
unit: {
|
|
115
|
+
type: "select",
|
|
116
|
+
label: "Unit",
|
|
117
|
+
default: "c",
|
|
118
|
+
options: [
|
|
119
|
+
{ value: "c", label: "Celsius" },
|
|
120
|
+
{ value: "f", label: "Fahrenheit" },
|
|
121
|
+
],
|
|
122
|
+
},
|
|
123
|
+
city: { type: "text", label: "City" },
|
|
124
|
+
},
|
|
125
|
+
});
|
|
126
|
+
|
|
127
|
+
declare module "@glazeen/mod-sdk" {
|
|
128
|
+
interface Register {
|
|
129
|
+
config: InferConfig<typeof manifest>; // { unit: "c" | "f"; city?: string }
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
const { config } = await bb.ready;
|
|
134
|
+
config.unit; // "c" | "f"
|
|
135
|
+
```
|
|
136
|
+
|
|
137
|
+
Fields with a `default` are always present; fields without one are optional until the user
|
|
138
|
+
sets them. Without the `Register` augmentation you can still type a single value with
|
|
139
|
+
`Bb<Config>`.
|
|
140
|
+
|
|
141
|
+
## Manifest schema
|
|
142
|
+
|
|
143
|
+
Point `bearbar.mod.json` at the schema to get validation and hover docs in your editor:
|
|
144
|
+
|
|
145
|
+
```json
|
|
146
|
+
{
|
|
147
|
+
"$schema": "https://unpkg.com/@glazeen/mod-sdk/schema/bearbar.mod.schema.json",
|
|
148
|
+
"id": "my-mod",
|
|
149
|
+
"name": "My mod",
|
|
150
|
+
"version": "1.0.0",
|
|
151
|
+
"tile": "tile.html"
|
|
152
|
+
}
|
|
153
|
+
```
|
|
154
|
+
|
|
155
|
+
The schema is also exported as `@glazeen/mod-sdk/schema.json`. It catches everything JSON
|
|
156
|
+
Schema can express; `validateManifest` and the CLI also check what it can't, such as a select
|
|
157
|
+
default that isn't one of the options, or `visibleWhen` pointing at a setting that doesn't exist.
|
|
158
|
+
|
|
159
|
+
## Validating a manifest
|
|
160
|
+
|
|
161
|
+
```ts
|
|
162
|
+
import { readFileSync } from "node:fs";
|
|
163
|
+
import { formatIssue, validateManifest } from "@glazeen/mod-sdk";
|
|
164
|
+
|
|
165
|
+
const result = validateManifest(JSON.parse(readFileSync("bearbar.mod.json", "utf8")));
|
|
166
|
+
for (const e of result.errors) console.error(formatIssue(e)); // "settings.unit.default: …"
|
|
167
|
+
for (const w of result.warnings) console.warn(formatIssue(w));
|
|
168
|
+
if (result.ok) console.log(result.manifest.name); // typed as ModManifest
|
|
169
|
+
```
|
|
170
|
+
|
|
171
|
+
Every issue is `{ path, message }`, with `path` like `"permissions[1]"` or
|
|
172
|
+
`"settings.unit.default"`. Errors make `ok` false; warnings (unknown keys, a version that isn't
|
|
173
|
+
semver, a default outside `min..max`) don't. Typos in key names come with a suggestion.
|
|
174
|
+
|
|
175
|
+
The validator is stricter than the dock, which loads what it can and fills in defaults.
|
|
176
|
+
|
|
177
|
+
## The `bb` contract
|
|
178
|
+
|
|
179
|
+
Behaviour the types can't express (details in the [API reference](docs/api.md)):
|
|
180
|
+
|
|
181
|
+
- `bb.fetch` resolves with `ok: false` instead of rejecting: `status` is `0` and `error` says
|
|
182
|
+
why when the request never got a response (missing permission, offline, timeout). It rejects
|
|
183
|
+
only when you pass a body that isn't a string.
|
|
184
|
+
- `bb.on` returns a function that removes the listener. A listener that throws doesn't stop
|
|
185
|
+
the others; the error is reported like an uncaught one.
|
|
186
|
+
- `bb.storage` is per widget instance and holds JSON values, 256 KB in total
|
|
187
|
+
(`STORAGE_QUOTA`). A write over the quota rejects.
|
|
188
|
+
- `bb.openUrl` rejects without the `open-url` permission. Other methods reject with an error
|
|
189
|
+
that starts with `bb: <method>:`.
|
|
190
|
+
- In the widget picker `bb.preview` is `true`: show sample data. `bb.fetch` resolves with
|
|
191
|
+
`ok: false` there.
|
|
192
|
+
|
|
193
|
+
`THEME_TOKENS` lists the CSS variables the dock sets on your page.
|
|
194
|
+
|
|
195
|
+
## API versions
|
|
196
|
+
|
|
197
|
+
`API_VERSION` is the mod API this package describes (currently `1`). A manifest can declare
|
|
198
|
+
the version it targets with `"apiVersion"`; a missing value means `1`.
|
|
199
|
+
|
|
200
|
+
- Additive changes (new `bb` methods, events, permissions or manifest fields) keep the same API
|
|
201
|
+
version and ship as minor releases of this package.
|
|
202
|
+
- Breaking changes bump `API_VERSION` and this package's major version (minor while it is `0.x`).
|
|
203
|
+
The dock will keep loading mods that target older API versions.
|
|
204
|
+
- Today the dock ignores `apiVersion`. Docks that understand it will warn about mods that target
|
|
205
|
+
a newer API than they support.
|
|
206
|
+
|
|
207
|
+
## Exports
|
|
208
|
+
|
|
209
|
+
| Path | What |
|
|
210
|
+
| ------------------------------ | ----------------------------------------------------------- |
|
|
211
|
+
| `@glazeen/mod-sdk` | types, `validateManifest`, `defineManifest`, constants |
|
|
212
|
+
| `@glazeen/mod-sdk/global` | declares the `bb` global (types only) |
|
|
213
|
+
| `@glazeen/mod-sdk/schema.json` | JSON Schema for `bearbar.mod.json` |
|
|
214
|
+
| `@glazeen/mod-sdk/client.js` | the `bb` runtime the dock injects (you don't load it) |
|
|
215
|
+
| `@glazeen/mod-sdk/client.css` | the base stylesheet the dock injects with it |
|
|
216
|
+
| `bearbar-mod` (bin) | the CLI |
|
|
217
|
+
|
|
218
|
+
The package is ESM only and has no dependencies.
|
|
219
|
+
|
|
220
|
+
## License
|
|
221
|
+
|
|
222
|
+
MIT
|
package/client/bb.css
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
/* Base stylesheet injected with bb.js into every mod page (bbmod://mods/_sdk/sdk.css). */
|
|
2
|
+
html,
|
|
3
|
+
body {
|
|
4
|
+
margin: 0;
|
|
5
|
+
height: 100%;
|
|
6
|
+
overflow: hidden;
|
|
7
|
+
background: transparent;
|
|
8
|
+
color: var(--bb-text, #fff);
|
|
9
|
+
font-family: var(--bb-font, "Segoe UI", system-ui, sans-serif);
|
|
10
|
+
font-size: var(--bb-fs-sm, 12px);
|
|
11
|
+
-webkit-user-select: none;
|
|
12
|
+
user-select: none;
|
|
13
|
+
cursor: default;
|
|
14
|
+
color-scheme: normal;
|
|
15
|
+
}
|
|
16
|
+
* {
|
|
17
|
+
box-sizing: border-box;
|
|
18
|
+
}
|
|
19
|
+
/* Tile helpers matching the built-in widgets. */
|
|
20
|
+
.bb-tile {
|
|
21
|
+
height: 100%;
|
|
22
|
+
display: flex;
|
|
23
|
+
align-items: center;
|
|
24
|
+
justify-content: center;
|
|
25
|
+
gap: 0.5em;
|
|
26
|
+
padding: 0 0.8em;
|
|
27
|
+
}
|
|
28
|
+
.bb-stack {
|
|
29
|
+
display: flex;
|
|
30
|
+
flex-direction: column;
|
|
31
|
+
line-height: 1.15;
|
|
32
|
+
}
|
|
33
|
+
.bb-label {
|
|
34
|
+
font-size: var(--bb-fs-xs, 10px);
|
|
35
|
+
font-weight: 700;
|
|
36
|
+
text-transform: uppercase;
|
|
37
|
+
letter-spacing: 0.03em;
|
|
38
|
+
color: var(--bb-text-dim);
|
|
39
|
+
}
|
|
40
|
+
.bb-value {
|
|
41
|
+
font-family: var(--bb-font-display, inherit);
|
|
42
|
+
font-size: var(--bb-fs-lg, 18px);
|
|
43
|
+
font-weight: 650;
|
|
44
|
+
font-variant-numeric: tabular-nums;
|
|
45
|
+
}
|
|
46
|
+
.bb-dim {
|
|
47
|
+
color: var(--bb-text-dim);
|
|
48
|
+
}
|
|
49
|
+
.bb-up {
|
|
50
|
+
color: var(--bb-green, #30d158);
|
|
51
|
+
}
|
|
52
|
+
.bb-down {
|
|
53
|
+
color: var(--bb-red, #ff453a);
|
|
54
|
+
}
|
|
55
|
+
button {
|
|
56
|
+
font: inherit;
|
|
57
|
+
color: inherit;
|
|
58
|
+
}
|
package/client/bb.js
ADDED
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The `bb` runtime. The dock injects it (with bb.css) into every mod page as
|
|
3
|
+
* bbmod://mods/_sdk/sdk.js; mods never load it themselves.
|
|
4
|
+
*
|
|
5
|
+
* Mods run in sandboxed iframes with an opaque origin and no network access;
|
|
6
|
+
* everything goes through postMessage to the dock, which checks the mod's
|
|
7
|
+
* permissions in the main process. Types: @glazeen/mod-sdk/global.
|
|
8
|
+
*/
|
|
9
|
+
(() => {
|
|
10
|
+
// biome-ignore lint/suspicious/noRedundantUseStrict: a classic <script>, not a module
|
|
11
|
+
"use strict";
|
|
12
|
+
const EVENTS = ["config", "size", "theme", "system"];
|
|
13
|
+
const pending = new Map();
|
|
14
|
+
const listeners = new Map(EVENTS.map((e) => [e, new Set()]));
|
|
15
|
+
let seq = 0;
|
|
16
|
+
let resolveReady;
|
|
17
|
+
const ready = new Promise((r) => {
|
|
18
|
+
resolveReady = r;
|
|
19
|
+
});
|
|
20
|
+
const state = { size: "m", config: {}, preview: false, surface: "tile" };
|
|
21
|
+
|
|
22
|
+
const post = (msg) => parent.postMessage(Object.assign({ bb: true }, msg), "*");
|
|
23
|
+
const call = (method, ...args) =>
|
|
24
|
+
new Promise((resolve, reject) => {
|
|
25
|
+
const id = ++seq;
|
|
26
|
+
pending.set(id, { resolve, reject, method });
|
|
27
|
+
post({ type: "call", id, method, args });
|
|
28
|
+
});
|
|
29
|
+
// One failing listener must not starve the others; report it like an uncaught error.
|
|
30
|
+
const emit = (event, value) => {
|
|
31
|
+
for (const fn of listeners.get(event)) {
|
|
32
|
+
try {
|
|
33
|
+
fn(value);
|
|
34
|
+
} catch (err) {
|
|
35
|
+
reportError(err);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
};
|
|
39
|
+
const reportError = (err) =>
|
|
40
|
+
setTimeout(() => {
|
|
41
|
+
throw err;
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
function applyTheme(t) {
|
|
45
|
+
if (!t) return;
|
|
46
|
+
const root = document.documentElement;
|
|
47
|
+
for (const [k, v] of Object.entries(t.tokens || {})) root.style.setProperty(k, v);
|
|
48
|
+
for (const [k, v] of Object.entries(t.attrs || {})) root.dataset[k] = v;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
addEventListener("message", (e) => {
|
|
52
|
+
if (e.source !== parent) return;
|
|
53
|
+
const m = e.data;
|
|
54
|
+
if (m?.bb !== true) return;
|
|
55
|
+
switch (m.type) {
|
|
56
|
+
case "init":
|
|
57
|
+
state.size = m.size;
|
|
58
|
+
state.config = m.config || {};
|
|
59
|
+
state.preview = !!m.preview;
|
|
60
|
+
state.surface = m.surface;
|
|
61
|
+
applyTheme(m.theme);
|
|
62
|
+
resolveReady(bb);
|
|
63
|
+
break;
|
|
64
|
+
case "theme":
|
|
65
|
+
applyTheme(m.theme);
|
|
66
|
+
emit("theme", m.theme);
|
|
67
|
+
break;
|
|
68
|
+
case "config":
|
|
69
|
+
state.config = m.config || {};
|
|
70
|
+
emit("config", state.config);
|
|
71
|
+
break;
|
|
72
|
+
case "size":
|
|
73
|
+
state.size = m.size;
|
|
74
|
+
emit("size", state.size);
|
|
75
|
+
break;
|
|
76
|
+
case "system":
|
|
77
|
+
emit("system", m.stats);
|
|
78
|
+
break;
|
|
79
|
+
case "reply": {
|
|
80
|
+
const p = pending.get(m.id);
|
|
81
|
+
if (!p) return;
|
|
82
|
+
pending.delete(m.id);
|
|
83
|
+
if (m.ok) p.resolve(m.value);
|
|
84
|
+
else p.reject(new Error(`bb: ${p.method}: ${m.error || "failed"}`));
|
|
85
|
+
break;
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
});
|
|
89
|
+
|
|
90
|
+
// Right-click belongs to the dock (widget menu), not to the page.
|
|
91
|
+
addEventListener("contextmenu", (e) => {
|
|
92
|
+
e.preventDefault();
|
|
93
|
+
post({ type: "contextmenu" });
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
function headersObject(h) {
|
|
97
|
+
if (h == null) return undefined;
|
|
98
|
+
const out = {};
|
|
99
|
+
if (typeof h[Symbol.iterator] === "function")
|
|
100
|
+
for (const [k, v] of h) out[String(k)] = String(v);
|
|
101
|
+
else if (typeof h.forEach === "function")
|
|
102
|
+
h.forEach((v, k) => {
|
|
103
|
+
out[String(k)] = String(v);
|
|
104
|
+
});
|
|
105
|
+
else for (const [k, v] of Object.entries(h)) out[k] = String(v);
|
|
106
|
+
return out;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
const bb = {
|
|
110
|
+
ready,
|
|
111
|
+
get size() {
|
|
112
|
+
return state.size;
|
|
113
|
+
},
|
|
114
|
+
get config() {
|
|
115
|
+
return state.config;
|
|
116
|
+
},
|
|
117
|
+
get preview() {
|
|
118
|
+
return state.preview;
|
|
119
|
+
},
|
|
120
|
+
get surface() {
|
|
121
|
+
return state.surface;
|
|
122
|
+
},
|
|
123
|
+
on(event, fn) {
|
|
124
|
+
const set = listeners.get(event);
|
|
125
|
+
if (!set) throw new TypeError(`bb.on: unknown event "${event}" (${EVENTS.join(", ")})`);
|
|
126
|
+
if (typeof fn !== "function") throw new TypeError("bb.on: listener must be a function");
|
|
127
|
+
if (event === "system" && set.size === 0) call("system.subscribe").catch(reportError);
|
|
128
|
+
set.add(fn);
|
|
129
|
+
return () => {
|
|
130
|
+
set.delete(fn);
|
|
131
|
+
};
|
|
132
|
+
},
|
|
133
|
+
async fetch(url, init = {}) {
|
|
134
|
+
if (init.body != null && typeof init.body !== "string")
|
|
135
|
+
throw new TypeError("bb.fetch: body must be a string (use JSON.stringify for JSON)");
|
|
136
|
+
const r = await call("fetch", {
|
|
137
|
+
url: String(url),
|
|
138
|
+
method: init.method,
|
|
139
|
+
headers: headersObject(init.headers),
|
|
140
|
+
body: init.body,
|
|
141
|
+
});
|
|
142
|
+
return Object.assign(r, { json: () => JSON.parse(r.text) });
|
|
143
|
+
},
|
|
144
|
+
storage: Object.freeze({
|
|
145
|
+
get: (key) => call("storage.get", String(key)),
|
|
146
|
+
set: (key, value) => call("storage.set", String(key), value).then(() => {}),
|
|
147
|
+
}),
|
|
148
|
+
panel: Object.freeze({
|
|
149
|
+
open: () => call("panel.open").then(() => {}),
|
|
150
|
+
close: () => call("panel.close").then(() => {}),
|
|
151
|
+
}),
|
|
152
|
+
openUrl: (url) => call("openUrl", String(url)).then(() => {}),
|
|
153
|
+
openSettings: () => call("settings.open").then(() => {}),
|
|
154
|
+
};
|
|
155
|
+
Object.defineProperty(window, "bb", { value: Object.freeze(bb), enumerable: true });
|
|
156
|
+
post({ type: "hello" });
|
|
157
|
+
})();
|