@pen.dev/sdk
v0.1.0
Published
pen.dev SDK
Readme
@pen.dev/sdk
Captures a web page, or an element the user picks in it, from an Electron
WebContents as a pen.dev browser import. It is the Electron counterpart of
the pen.dev Chrome extension: the same in-page crawler, picker, and payload
format, with asset downloads and image work done through the host process and
the live page instead of an extension service worker.
The root entry exports the browser-import types. @pen.dev/sdk/electron is the
published, narrow surface over the internal @ha/electron-browser-import:
everything it and its dependencies need is bundled in, and Electron (>= 30) is
an optional peer dependency that only the electron entry needs.
Use
import { PenCapturer } from "@pen.dev/sdk/electron";
const capturer = new PenCapturer(view.webContents);
capturer.on("picker", (state) => {
// undefined: no session; { pick: undefined }: crosshair is up;
// { pick }: an element is selected (pick.element, on pick.path) and stays live.
});
capturer.on("action", (action) => {
// "import" on Enter, "screenshot" on Cmd/Ctrl+Enter, while a selection is live.
if (action === "import") {
void run();
}
});
capturer.startPicking(); // Escape in the page, or endPicking(), clears it.
async function run() {
// The picked element when there is one, the full page otherwise;
// `payload` is the string pen.dev accepts as a browser import.
const payload = await capturer.capture({
onProgress: (fraction) => progressBar.value = fraction,
});
}
capturer.dispose();capture() resolves to the self-contained payload. Failed downloads keep their
original URLs and produce an import warning on the pen.dev side.
select(selector) selects an element without the crosshair phase; the
selection is live afterwards exactly as after a pick. hover(selector) shows
the picker's hover highlight on an element, as the mouse does.
selectPathEntry(index) retargets a live selection along the picked element's
ancestor path.
Requirements
- The
WebContentsmust be attached to a visible view. Captures on hidden or detached contents never resolve, because the renderer does not paint. - Asset downloads go through
webContents.session.fetch, so they carry the page's cookies and are not subject to the page's CORS or CSP. - The page API is injected into the page's main world with
executeJavaScript. It installswindow.__pencil; nothing else is added to the page. screenshots: truealso captures canvases, videos, and cross-origin media the crawl cannot read. It attaches the Chrome DevTools Protocol debugger to theWebContentsfor the duration of the capture. It is off by default; without it those nodes import as placeholders.- Pass
onWarningto route warnings into the host's log; the default writes to the console.
Example
The pen-embed-demo repository next to this checkout embeds the pen.dev web
editor in Electron and uses this package for its browser window: element
picking, selector hover, breadcrumb navigation, and import onto the canvas
through the embed bridge.
Build
Built with tsdown like the CLI: @ha/electron-browser-import and everything under it
are inlined into dist, which is all the package ships.
./build-shared.sh
npm --prefix lib/browser-inject run build
npm --prefix lib/electron-browser-import run build
npm --prefix lib/sdk ci
npm --prefix lib/sdk run buildRelease
The SDK release workflow bumps the version, builds, creates an
sdk-v<version> GitHub release, and, when publish_npm is
checked, publishes to npm with npm publish --access public.
