From c4577dc38d48de28256c29bc387ffc628f7c4ce9 Mon Sep 17 00:00:00 2001 From: Loren McIntyre Date: Mon, 15 Jun 2026 03:51:35 -0700 Subject: [PATCH] spec+plan: console-first surface, date-tag refs, signing roadmap - Build the interactive surface console-first: in-script config + console API (usLoader.load/loadEntry/list/hashIt, callable with args) + prompt-based menu; the on-page panel (overlays/notifications/buttons) becomes a deferred later phase. - ref convention @YYYY-MM[.vv] / @YYYY-MM-DD[.vv] date tags (any tag is a valid ref). - DEFAULT_OWNER runtime-overridable so forks prefer their own owner. - Roadmap: registry.meta.json with a signing pubkey; signed *.meta.js as gossip-able, verifiable update/version units (age/gpg). - Plan: Task 11 now ships the console API + menu; Task 12 (panel) marked later-phase. Co-Authored-By: Claude Opus 4.8 (1M context) --- .../2026-06-15-tampermonkey-script-loader.md | 60 +++++++++++++++---- ...06-15-tampermonkey-script-loader-design.md | 47 +++++++++++---- 2 files changed, 83 insertions(+), 24 deletions(-) diff --git a/docs/superpowers/plans/2026-06-15-tampermonkey-script-loader.md b/docs/superpowers/plans/2026-06-15-tampermonkey-script-loader.md index 5a284b9..c99c6dc 100644 --- a/docs/superpowers/plans/2026-06-15-tampermonkey-script-loader.md +++ b/docs/superpowers/plans/2026-06-15-tampermonkey-script-loader.md @@ -70,6 +70,7 @@ Pure-helper signatures (used consistently across tasks): // @grant GM_setValue // @grant GM_getValue // @grant GM_registerMenuCommand +// @grant unsafeWindow // @connect cdn.jsdelivr.net // @connect raw.githubusercontent.com // @downloadURL https://cdn.jsdelivr.net/gh/mcint/userscripts@main/tampermonkey/loader.user.js @@ -918,44 +919,79 @@ async function getRegistry() { } } +async function hashInput(input) { + const src = parseFreeform(input, { owner: DEFAULT_OWNER, ref: 'main' }); + const text = src.kind === 'snippet' ? src.snippet + : await gmFetchText(src.kind === 'url' ? src.url + : buildCdnUrl({ repo: src.repo, ref: src.ref || 'main', path: src.path })); + return computeSriToken(new TextEncoder().encode(text), 'sha384'); +} + function main() { getRegistry().then((entries) => { const state = loadState(); - const due = entriesToAutoLoad(entries, state, location.href); - for (const e of due) { + // 1. auto-load matching + enabled entries + for (const e of entriesToAutoLoad(entries, state, location.href)) { loadSource({ kind: 'ref', repo: e.repo, ref: e.ref || 'main', path: e.path, entry: e }) .catch((err) => console.error('[loader]', e.id, err)); } - registerMenu(entries); // Task 12 + // 2. console API (devtools, with args) + prompt-based menu — the minimal + // interactive surface; the on-page panel is a later phase (Task 12). + const api = { + list: () => entries.map((e) => ({ id: e.id, name: e.name, repo: e.repo, path: e.path })), + load: (input, opts) => loadSource(parseFreeform(input, { owner: DEFAULT_OWNER, ref: 'main' }), opts), + loadEntry: (id) => { + const e = entries.find((x) => x.id === id); + return e ? loadSource({ kind: 'ref', repo: e.repo, ref: e.ref || 'main', path: e.path, entry: e }) + : Promise.reject(new Error('no entry ' + id)); + }, + hashIt: (input) => hashInput(input), + }; + (typeof unsafeWindow !== 'undefined' ? unsafeWindow : window).usLoader = api; + GM_registerMenuCommand('usLoader: load (prompt)', () => { + const v = prompt('owner/repo/path@ref, URL#sri, or paste snippet'); + if (v) { api.load(v).catch((e) => alert('load failed: ' + e.message)); } + }); + GM_registerMenuCommand('usLoader: list in console', () => console.table(api.list())); + GM_registerMenuCommand('usLoader: hash input', async () => { + const v = prompt('owner/repo/path@ref or URL to hash'); + if (v) { prompt('SRI token (copy into registry.json "integrity"):', await api.hashIt(v)); } + }); }); } ``` -Note: `Date.now()` is fine in the browser; it is only referenced inside `main`/`getRegistry`, never at require time, so Node tests are unaffected. +Note: `Date.now()` and `unsafeWindow` are only referenced inside `main`/`getRegistry`, never at require time, so Node tests are unaffected. - [ ] **Step 4: Add to exports** — add `entriesToAutoLoad,`. - [ ] **Step 5: Run tests** — Run: `cd tampermonkey && node --test` Expected: PASS. -Note: `registerMenu` is defined in Task 12; until then, comment out its call to keep the script runnable if you smoke-test mid-stream. +- [ ] **Step 6: Manual smoke (console-first surface)** — install the loader, open any page, devtools console: `usLoader.list()` shows entries; `usLoader.loadEntry('nav-popups')` loads on a Wikipedia page; Tampermonkey menu shows the three `usLoader:` commands. This is the minimal interactive surface — no on-page panel yet. -- [ ] **Step 6: Commit** +- [ ] **Step 7: Commit** ```bash git add tampermonkey/loader.user.js tampermonkey/test/autoload.test.js -git commit -m "feat(tampermonkey): registry load (embedded/TTL) + auto-load orchestration" +git commit -m "feat(tampermonkey): registry load + auto-load + console API/menu (console-first)" ``` --- -### Task 12: Picker panel UI + menu command + hash helper +### Task 12 (LATER PHASE — defer): On-page panel UI (overlays/notifications/buttons) + +**Defer until after v0 ships and the console-first inclusion mechanics are proven** +(per the console-first decision). The minimal surface — console API + prompt menu — +ships in Task 11. This task upgrades that to a styled on-page panel; it's the +"overlays / notifications / buttons" phase, not v0. Build it only when you want the +in-page interactive surface; nothing else depends on it. **Files:** -- Modify: `tampermonkey/loader.user.js` +- Modify: `tampermonkey/loader.user.js` (add `openPicker`; add a menu item that calls it) Manual verification (DOM + GM_registerMenuCommand). -- [ ] **Step 1: Implement UI** — add above `main()`: +- [ ] **Step 1: Implement UI** — add above `main()`, and add a 4th menu item in `main` (`GM_registerMenuCommand('usLoader: open panel', () => openPicker(entries))`): ```javascript function el(tag, attrs, children) { @@ -1211,7 +1247,7 @@ git commit -m "docs(tampermonkey): document embedded-registry distribution build ## Self-Review **Spec coverage:** -- §1 layout → Tasks 1, 8, 13, 14. §2 loader header/invocation/config statics → Tasks 1, 11. §3 registry schema → Tasks 6, 8. §4 load+execute/integrity/hash helper → Tasks 5, 9, 12. §5 persistence (+recents cap) → Task 10. §6 static/dynamic → Tasks 11, 14, 15. §7 self-hosting headers → Tasks 1 (header), 14 (meta.js). §8 refs → Task 13. §9 scope: v0 = Tasks 1–13; v0.9 = Tasks 14–15; deferred items not scheduled (correct). +- §1 layout → Tasks 1, 8, 13, 14. §2 loader header/invocation/config statics → Tasks 1, 11. §3 registry schema (+ date-tag refs) → Tasks 6, 8. §4 load+execute/integrity/hash helper → Tasks 5, 9, 11 (console `hashIt`). §5 persistence (+recents cap) → Task 10. §6 static/dynamic → Tasks 11, 14, 15. §7 self-hosting headers → Tasks 1 (header), 14 (meta.js). §8 refs → Task 13. §9 **console-first** scope: v0 minimal = Tasks 1–11 + 13 (console API + prompt menu, no panel); on-page panel = Task 12 (later phase, deferred); v0.9 = Tasks 14–15; signing/registry-meta roadmap not scheduled (correct). - Storage-size limiting (§5 future) intentionally unscheduled — `pushRecent` cap is the only bound in v0. - SRI *enforcement default* deferred: `REQUIRE_SRI=false` + per-load checkbox (Tasks 1, 9, 12) — matches spec. @@ -1219,4 +1255,4 @@ git commit -m "docs(tampermonkey): document embedded-registry distribution build **Type consistency:** `loadSource(source, opts)`, `gmFetchText`, `buildCdnUrl`, `entryCdnUrl`, `normalizeIntegrity`, `computeSriToken`, `verifyIntegrity`, `parseRegistry`, `validateEntry`, `matchUrl`, `emptyState/applyEnabled/isEnabled/pushRecent`, `entriesToAutoLoad`, `getRegistry`, `openPicker/registerMenu` — names consistent across tasks. `EMBEDDED_REGISTRY_JSON` (build output) vs `EMBEDDED_REGISTRY` (in-loader const) used consistently. -**Note:** Tasks 9 and 12 are browser-manual; if the executor is fully headless, mark them blocked for a human pass and proceed with unit-tested tasks. +**Note:** The browser-manual steps are Task 9 (smoke), Task 11 Step 6 (console surface), and Task 12 (panel, deferred). If the executor is fully headless, mark those for a human pass and proceed with the unit-tested tasks (1–8, 10, 14, plus the pure parts of 11). diff --git a/docs/superpowers/specs/2026-06-15-tampermonkey-script-loader-design.md b/docs/superpowers/specs/2026-06-15-tampermonkey-script-loader-design.md index 5857dab..032c9fd 100644 --- a/docs/superpowers/specs/2026-06-15-tampermonkey-script-loader-design.md +++ b/docs/superpowers/specs/2026-06-15-tampermonkey-script-loader-design.md @@ -72,7 +72,8 @@ scripts first. - `const REQUIRE_SRI = false;` — when `true`, the loader refuses to run anything without a verified integrity hash (the global counterpart to the per-entry checkbox in §4). -- `const DEFAULT_OWNER = 'mcint';` +- `const DEFAULT_OWNER = 'mcint';` — also overridable at runtime (GM storage / UI) + so a fork can prefer its own owner without editing source. - `const REGISTRY_URL = '…registry.json@main';`, `const REGISTRY_TTL_MS = …;` ## 3. Registry schema (`registry.json`) @@ -97,7 +98,12 @@ conventions** so an entry maps cleanly onto a `==UserScript==` header and a ``` The CDN URL is derived: `https://cdn.jsdelivr.net/gh/@/`. -`@` defaults to `main` (floats); pin a commit SHA for immutability. +`@` defaults to `main` (floats); pin a commit SHA for immutability. Besides +`@main` and SHAs, the **date-tag convention** `@YYYY-MM[.vv]` / `@YYYY-MM-DD[.vv]` +(git tags) gives human, sortable update points for consumers (`vv` = same-day +minor). Any tag string is a valid `ref` — jsDelivr resolves it; no loader change. +Caveat: git tags are mutable/floating and can't be validated cheaply in-flight — +integrity for a pinned tag still comes from the SRI token, not the tag itself. ## 4. Load + execute @@ -168,16 +174,33 @@ mw `suggest diff-bump` roadmap. ## 9. Scope -**v0 (core):** loader.user.js with self-hosting header; `registry.json` + embedded -copy; GM_xhr fetch + execute; SRI-ready integrity (token + fragment formats, helper, -`REQUIRE_SRI` static + per-entry checkbox); per-domain auto-load; picker panel; -freeform URL **and** snippet; persistence; `refs/` docs. - -**v0.9 (still in scope):** `build.sh`, static install index, generated `*.meta.js`. - -**Deferred (YAGNI for now):** SRI *enforcement by default*; GitHub-API auto-discovery -of scripts; the cross-repo "suggest diff-bump for wiki common.js / Tampermonkey" -tooling (lives in the mw roadmap); storage-size limiting (§5). +Interactive surface is built **console-first**, simplest → richest: +1. **in-script config** (the `const` block) + **per-domain auto-load**, +2. a **console API** (`usLoader.load(input, opts)`, `.loadEntry(id)`, `.list()`, + `.hashIt()`) callable from devtools, with args, plus prompt-based GM menu items, +3. *later* an **on-page panel** (overlays / notifications / buttons) — the + styled interactive REPL. + +**v0 (minimal core):** loader.user.js with self-hosting header; `registry.json`; +GM_xhr fetch + execute; SRI-ready integrity (token format, helper, `REQUIRE_SRI` +static + per-load flag); per-domain auto-load; freeform URL **and** snippet via the +**console API + prompt menu**; persistence; `refs/` docs. + +**v0.9 (still in scope):** `build.sh`, embedded registry, static install index, +generated `*.meta.js`. + +**Later phase:** the on-page panel UI (overlays/notifications/buttons) — prototype +the *inclusion* mechanics through the console first. + +**Roadmap (post-v0.9):** +- **`registry.meta.json`** — registry-level metadata carrying a **signing public + key**; cheaper than a per-script merkle tree, embeddable as one reference. +- **Signed `*.meta.js` as gossip-able update units** — each `*.meta.js` doubles as + the version/update report; sign it (age / gpg) so a relayed/"gossiped" update + notice is verifiable against the registry pubkey. Needs dev signing keys + (generate; cache/store the *public* half in-source, never the private). +- SRI *enforcement by default*; GitHub-API auto-discovery; the cross-repo + "suggest diff-bump" tooling (mw roadmap); storage-size limiting (§5). ## 10. Risks / open questions -- knotmirror 0.0.1