From b176bcb5cf2cfe1537377a33e8644a63be8a771b Mon Sep 17 00:00:00 2001 From: Mert Duzgun <46185450+mert-duzgun@users.noreply.github.com> Date: Fri, 27 Nov 2026 09:43:18 +0300 Subject: [PATCH 1/3] feat(grab): fire a knobs:grab event and send each grab to a target --- apps/web/content/docs/grab.md | 32 +++- apps/web/content/reference/grab.md | 39 +++++ packages/devknobs/CHANGELOG.md | 5 + packages/devknobs/README.md | 16 ++ packages/devknobs/src/global.ts | 4 +- packages/devknobs/src/grab/clipboard.ts | 17 +++ packages/devknobs/src/grab/index.ts | 4 +- packages/devknobs/src/grab/mode.ts | 10 +- packages/devknobs/src/grab/picks.ts | 10 ++ packages/devknobs/src/grab/send.ts | 77 ++++++++++ packages/devknobs/src/host.ts | 5 +- packages/devknobs/src/index.ts | 13 ++ packages/devknobs/test/grab-dwell.test.ts | 46 +++++- packages/devknobs/test/grab-send.test.ts | 170 ++++++++++++++++++++++ 14 files changed, 438 insertions(+), 10 deletions(-) create mode 100644 packages/devknobs/src/grab/send.ts create mode 100644 packages/devknobs/test/grab-send.test.ts diff --git a/apps/web/content/docs/grab.md b/apps/web/content/docs/grab.md index ecfecb7b..e74cbf5d 100644 --- a/apps/web/content/docs/grab.md +++ b/apps/web/content/docs/grab.md @@ -3,7 +3,7 @@ title: grab and inspect meta_title: Grab and inspect elements for a coding agent with knobs description: Grab any element on the page and copy its html, the components that rendered it and their source files for a coding agent, or inspect its box and css. order: 5 -updated: 2026-11-26 +updated: 2026-11-27 --- ## grab @@ -44,6 +44,36 @@ mount({ grabKey: 'alt+shift+g' }); `grab: false` leaves grab out. `grab(elements)` copies elements from code. +## send each grab + +every grab also fires a `knobs:grab` event on `window`, with what was copied +as its `detail`: + +```js +window.addEventListener('knobs:grab', (event) => { + console.log(event.detail.content, event.detail.entries, event.detail.note); +}); +``` + +`grabSend` sends each grab somewhere too, so nobody has to paste. a URL gets +a POST of `{ "type": "knobs.grab", "payload": ... }` as json; it has to be +the page's own origin, or http(s) on `localhost`, `127.0.0.1` or a +`.localhost` name. a function gets the payload: + +```js +mount({ grabSend: '/api/grab' }); +mount({ grabSend: (payload) => socket.send(JSON.stringify(payload)) }); +``` + +with the script tag, set it on the tag: + +```html + +``` + +the copy still happens. the toast says `Copied · Sent` once it went, and +`Send failed` when it did not. + ## coming from react-grab knobs has the same grab, the same line format and the same clipboard diff --git a/apps/web/content/reference/grab.md b/apps/web/content/reference/grab.md index b2cd23ce..64d3d31e 100644 --- a/apps/web/content/reference/grab.md +++ b/apps/web/content/reference/grab.md @@ -137,6 +137,45 @@ reads React's internals through [bippy](https://github.com/aidenybai/bippy), both by Aiden Bai and MIT, see [THIRD_PARTY_NOTICES.md](https://github.com/vinenastudio/knobs/blob/main/packages/devknobs/THIRD_PARTY_NOTICES.md). +## send each grab + +every grab, from the keys or from `grab()`, fires one `knobs:grab` event on +`window` once it is copied, whether the copy worked or not. its `detail` is +the payload: `content`, `entries` and `note` where there is one. it cannot be +canceled. + +```js +window.addEventListener('knobs:grab', (event) => { + console.log(event.detail.content, event.detail.entries, event.detail.note); +}); +``` + +the `grabSend` mount option sends each grab on as well, so nobody has to paste: + +- a URL gets a POST with `Content-Type: application/json` and + `credentials: "same-origin"`, its body `{ "type": "knobs.grab", "payload": ... }`. + only the page's own origin, a relative path included, and http(s) on + `localhost`, `127.0.0.1` or a `.localhost` name are taken; any other URL is + ignored with one warning in the console. +- a function is called with the payload, and may return a promise. + +```js +mount({ grabSend: '/api/grab' }); +mount({ grabSend: (payload) => socket.send(JSON.stringify(payload)) }); +``` + +the script tag build takes it from a `data-grab-send` attribute: + +```html + +``` + +a send never stops the copy. once it went, the toast says `Copied · Sent` +(`Sent` alone where the copy failed). where it fails, a thrown error, a +rejected promise, a network error or a status that is not 2xx, the toast says +`Send failed`. a page whose own mount defers to the extension hands its +`grabSend` to the extension's copy, as it does its keys. + ## migrating from react-grab knobs covers it: the same grab, the same line format, the same diff --git a/packages/devknobs/CHANGELOG.md b/packages/devknobs/CHANGELOG.md index 277e159f..0b18db3d 100644 --- a/packages/devknobs/CHANGELOG.md +++ b/packages/devknobs/CHANGELOG.md @@ -1,5 +1,10 @@ # changelog +## Unreleased + +- every grab fires a `knobs:grab` event on `window`, with what was copied as its `detail` +- `grabSend` sends each grab on as well: a URL on the page's origin or on localhost gets it as a json POST, a function gets the payload. the script tag takes it as `data-grab-send`. the copy still happens, and the toast says `Sent` or `Send failed` + ## 0.7.6 - grab gathers many elements: drag a box around them, or shift and a click. each shows its number diff --git a/packages/devknobs/README.md b/packages/devknobs/README.md index 7c6aedd2..6e6e0766 100644 --- a/packages/devknobs/README.md +++ b/packages/devknobs/README.md @@ -65,6 +65,22 @@ production. an icon from another origin that does not allow it (CORS) keeps its own picture. the dev tab icon switch in the panel's settings turns it off, or on for any host. +## send each grab + +every grab fires a `knobs:grab` event on `window`, the payload as its +`detail`. `grabSend` also POSTs it as json (`{ "type": "knobs.grab", payload }`) +to a URL on the page's origin or on localhost, or hands it to a function. the +copy still happens: + +```js +window.addEventListener("knobs:grab", (event) => console.log(event.detail.content)); +mount({ grabSend: "/api/grab" }); +``` + +```html + +``` + ## docs - [start](https://knobs.dev/docs/start): install @vinena/knobs and mount it in diff --git a/packages/devknobs/src/global.ts b/packages/devknobs/src/global.ts index e2f0f0be..be107d40 100644 --- a/packages/devknobs/src/global.ts +++ b/packages/devknobs/src/global.ts @@ -5,6 +5,8 @@ import { pin } from "./instance"; // Read as the script runs, the only time the document knows which one it is. const script = document.currentScript; bezelsBeside(script instanceof HTMLScriptElement ? script.src : ""); +// `