@zerogpu/browser-extension-sdk 0.2.2 → 0.2.3

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.
@@ -1,7 +1,7 @@
1
1
  'use strict';
2
2
 
3
- const SDK_VERSION = typeof "0.2.2+0a17606" === 'string' && "0.2.2+0a17606".length > 0
4
- ? "0.2.2+0a17606"
3
+ const SDK_VERSION = typeof "0.2.3+38ce04b" === 'string' && "0.2.3+38ce04b".length > 0
4
+ ? "0.2.3+38ce04b"
5
5
  : '0.0.0-dev';
6
6
 
7
7
  const ORCHESTRATOR_BASE_URL = typeof "https://devices.zerogpu.ai" === 'string' && "https://devices.zerogpu.ai".length > 0
@@ -1,5 +1,5 @@
1
- const SDK_VERSION = typeof "0.2.2+0a17606" === 'string' && "0.2.2+0a17606".length > 0
2
- ? "0.2.2+0a17606"
1
+ const SDK_VERSION = typeof "0.2.3+38ce04b" === 'string' && "0.2.3+38ce04b".length > 0
2
+ ? "0.2.3+38ce04b"
3
3
  : '0.0.0-dev';
4
4
 
5
5
  const ORCHESTRATOR_BASE_URL = typeof "https://devices.zerogpu.ai" === 'string' && "https://devices.zerogpu.ai".length > 0
@@ -4,8 +4,8 @@
4
4
  (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.ZeroGpuBrowserSdk = {}));
5
5
  })(this, (function (exports) { 'use strict';
6
6
 
7
- const SDK_VERSION = typeof "0.2.2+0a17606" === 'string' && "0.2.2+0a17606".length > 0
8
- ? "0.2.2+0a17606"
7
+ const SDK_VERSION = typeof "0.2.3+38ce04b" === 'string' && "0.2.3+38ce04b".length > 0
8
+ ? "0.2.3+38ce04b"
9
9
  : '0.0.0-dev';
10
10
 
11
11
  const ORCHESTRATOR_BASE_URL = typeof "https://devices.zerogpu.ai" === 'string' && "https://devices.zerogpu.ai".length > 0
@@ -55,8 +55,28 @@ This single command:
55
55
  - creates `offscreen.html`, `offscreen.js`, and `background.js` (it never overwrites
56
56
  files you already have),
57
57
  - adds the entries the SDK needs to your `manifest.json` (the `offscreen` and
58
- `storage` permissions, a content-security-policy entry, and the host permissions
59
- used to download models).
58
+ `storage` permissions, a content-security-policy entry, the host permissions
59
+ used to download models, and the `externally_connectable` entry that lets the
60
+ ZeroGPU-hosted settings page reach your extension).
61
+
62
+ ### Add the opt-out link
63
+
64
+ This is the one piece of markup you add yourself. Put it anywhere in an
65
+ extension page you already show users — your popup is the usual place:
66
+
67
+ ```html
68
+ <a data-zerogpu-settings>Resource sharing</a>
69
+ <script src="vendor/zerogpu/zerogpu-settings-link.js"></script>
70
+ ```
71
+
72
+ That is the whole integration, and it is identical in every extension. There is
73
+ no id, name, or URL to fill in: the script reads `chrome.runtime.id` and your
74
+ manifest's name at load time and builds the link itself, so it works in an
75
+ unpacked build and a published one without changes.
76
+
77
+ Style the anchor however you like — the script only sets `href`, `target` and
78
+ `rel`. Section 5 covers the other ways to expose the opt-out if a link in your
79
+ popup is not what you want.
60
80
 
61
81
  ## 3. Add your operator key
62
82
 
@@ -100,7 +120,162 @@ present and matches the installed SDK version.
100
120
 
101
121
  ---
102
122
 
103
- ## 5. Keep it up to date
123
+ ## 5. Let users opt out
124
+
125
+ Your users must be able to turn resource sharing off, and have it stay off. The
126
+ SDK ships this for you — `npx zerogpu init` wires it into the scaffolded
127
+ `background.js`, and if you merged the SDK into a service worker of your own,
128
+ `npx zerogpu doctor` checks the worker your manifest actually points at and
129
+ fails if the call is missing.
130
+
131
+ Enforcement is entirely **client-side**: while a user is opted out, the service
132
+ worker never creates the offscreen document, so nothing starts and nothing
133
+ connects. Nothing is deregistered server-side.
134
+
135
+ ### Where a user can change it
136
+
137
+ **1. The ZeroGPU-hosted settings page.** Give your users this link — it is the
138
+ one they can reach without opening your extension's UI:
139
+
140
+ ```text
141
+ https://zerogpu.ai/extension-settings?browser=chrome&extension_id=<your extension id>
142
+ ```
143
+
144
+ Build it in code with:
145
+
146
+ ```js
147
+ ZeroGpuBackground.buildZeroGpuSettingsUrl({ extensionId: chrome.runtime.id });
148
+ ```
149
+
150
+ This works because `npx zerogpu init` adds an `externally_connectable` entry to
151
+ your manifest, which lets that one page — and only that page — send the
152
+ extension three messages:
153
+
154
+ ```jsonc
155
+ "externally_connectable": { "matches": ["https://*.zerogpu.ai/*"] }
156
+ ```
157
+
158
+ No content script and no extra host permission is involved. The bridge accepts
159
+ only `zerogpu:ping`, `zerogpu:getConsent` and `zerogpu:setConsent`, validates the
160
+ sender's origin against an exact allowlist first, and can neither start
161
+ inference nor read device data.
162
+
163
+ **2. A plain link from your own UI.** The lightest option: paste your link
164
+ anywhere, in any page. No script, nothing vendored.
165
+
166
+ ```html
167
+ <a href="https://zerogpu.ai/extension-settings?browser=chrome&extension_id=YOUR_ID&app_name=Your%20App">
168
+ Resource sharing
169
+ </a>
170
+ ```
171
+
172
+ The `extension_id` is required — Chrome gives a web page no way to discover
173
+ which extensions are installed, so the page cannot work out who to message
174
+ without being told. Your published extension's id is permanent and public (it
175
+ is in your Chrome Web Store URL), so you write this link once.
176
+
177
+ One catch: an **unpacked** extension's id is derived from its folder path, so it
178
+ differs on every machine and differs from your store id. A hardcoded link works
179
+ in production and fails for anyone testing locally. Either use the drop-in
180
+ script below during development, or pin the id by adding your store listing's
181
+ public key to `manifest.json` as `"key"`, which makes the unpacked id match the
182
+ store id.
183
+
184
+ **3. The drop-in script.** Use this when you do not want to hand-manage a URL,
185
+ or when you need the link to work in unpacked builds too. Two lines, and they
186
+ are the same two lines in every extension:
187
+
188
+ ```html
189
+ <a data-zerogpu-settings>Resource sharing</a>
190
+ <script src="vendor/zerogpu/zerogpu-settings-link.js"></script>
191
+ ```
192
+
193
+ There is nothing to configure and no JavaScript to write. The script fills in
194
+ the `href` on load, taking the extension id from `chrome.runtime.id` and the
195
+ display name from your manifest, so it works unchanged in a popup, an options
196
+ page, or a settings screen of your own.
197
+
198
+ It works on any element, and keeps each one usable by keyboard:
199
+
200
+ | Element | What the script does |
201
+ | --- | --- |
202
+ | `<a>` | Sets `href`, `target` and `rel`, so it can be middle-clicked, copied, and opened with Enter like any link |
203
+ | `<button>` | Adds a click handler. Nothing else is needed: a button already fires click on Enter and Space |
204
+ | Anything else (`<span>`, `<div>`) | Adds a click handler, plus `tabindex="0"`, `role="button"` and Enter/Space activation, so it is reachable by keyboard rather than mouse-only |
205
+
206
+ A `role` or `tabindex` you set yourself is never overwritten. Off an extension
207
+ page, where there is no id to send, the script leaves your markup alone rather
208
+ than rendering a link that cannot work.
209
+
210
+ Optional per-element overrides: `data-zerogpu-app-name` to show a different
211
+ name, `data-zerogpu-base-url` to point at staging, `data-zerogpu-debug="true"`
212
+ to render the page's raw payload. If you render your UI dynamically, call
213
+ `ZeroGpuSettingsLink.applyZeroGpuSettingsLinks()` afterwards; it is idempotent,
214
+ so wiring the same element twice is a no-op.
215
+
216
+ **4. Your own toggle.** `init` also scaffolds an `options.html`/`options.js`
217
+ pair with a working toggle. Wire the same calls into your popup if you want the
218
+ control in your own UI rather than a link out:
219
+
220
+ ```js
221
+ // current state
222
+ chrome.runtime.sendMessage({ type: "zerogpu:getConsent" }, (res) => {
223
+ console.log(res.consent); // { optedIn, updatedAt, source }
224
+ });
225
+
226
+ // turn it off — the SDK stops immediately and stays off across restarts
227
+ chrome.runtime.sendMessage({ type: "zerogpu:setConsent", optedIn: false });
228
+ ```
229
+
230
+ Or directly, inside the service worker:
231
+
232
+ ```js
233
+ await ZeroGpuBackground.getZeroGpuConsent();
234
+ await ZeroGpuBackground.setZeroGpuConsent(false);
235
+ ```
236
+
237
+ `ZeroGpuBackground` is a **global**, not a module. `zerogpu-background.js` is
238
+ built as a classic script so your service worker does not have to become
239
+ `"type": "module"` — you get it from `importScripts("vendor/zerogpu/zerogpu-background.js")`
240
+ (the scaffolded `background.js` already does this), and there is nothing to
241
+ `import`. Only the TypeScript types are exported from the package:
242
+
243
+ ```ts
244
+ import type { ZeroGpuConsent } from "@zerogpu/browser-extension-sdk/extension/consent";
245
+ ```
246
+
247
+ `setZeroGpuConsent` **rejects** if the write does not land — for example when
248
+ the `storage` permission is missing. Treat a rejection as "the choice was not
249
+ saved" and tell the user, rather than showing the toggle as flipped. Reads are
250
+ the opposite: `getZeroGpuConsent` falls back to the opted-in default rather than
251
+ throwing, so a storage problem can never stop the SDK from starting.
252
+
253
+ ### The default
254
+
255
+ New installs are **opted in**, which is why adding this to an existing extension
256
+ changes nothing for your current users. If your own consent flow requires an
257
+ explicit yes first, write `false` on install and flip it when the user agrees:
258
+
259
+ ```js
260
+ chrome.runtime.onInstalled.addListener(async ({ reason }) => {
261
+ if (reason === "install") {
262
+ await ZeroGpuBackground.setZeroGpuConsent(false);
263
+ chrome.tabs.create({ url: "consent.html" });
264
+ }
265
+ });
266
+ ```
267
+
268
+ ### Verifying it
269
+
270
+ 1. Load the extension and confirm **Inspect views: offscreen.html** appears on
271
+ its `chrome://extensions` card.
272
+ 2. Open the settings link, or your options page, and turn it off.
273
+ 3. The offscreen view disappears — the SDK has stopped.
274
+ 4. Restart Chrome. It stays off.
275
+
276
+ ---
277
+
278
+ ## 6. Keep it up to date
104
279
 
105
280
  Because Manifest V3 extensions can't change their code at runtime, your users get a
106
281
  new SDK version when you **publish a new build of your extension**. The aim is to
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zerogpu/browser-extension-sdk",
3
- "version": "0.2.2",
3
+ "version": "0.2.3",
4
4
  "description": "ZeroGPU device-side SLM runtime SDK for browsers and Chrome (MV3) extensions",
5
5
  "type": "module",
6
6
  "main": "dist/zerogpu-browser-sdk.cjs",
@@ -24,6 +24,12 @@
24
24
  "./extension": {
25
25
  "types": "./dist/types/extension/offscreen.d.ts",
26
26
  "import": "./dist/extension/zerogpu-offscreen.js"
27
+ },
28
+ "./extension/background": {
29
+ "types": "./dist/types/extension/background.d.ts"
30
+ },
31
+ "./extension/consent": {
32
+ "types": "./dist/types/extension/consent.d.ts"
27
33
  }
28
34
  },
29
35
  "types": "dist/types/index.d.ts",