Using the Web Extension
Relevant source files
The following files were used as context for generating this wiki page:
The React Grab web extension is a developer tool designed to inspect and copy React components directly from web pages. It operates entirely locally within the user's browser, injecting code to enable element selection and copying the corresponding HTML/JSX to the clipboard without sending any data to external servers. The extension is a core part of the react-grab project, providing a browser-integrated way to use the library's context-grabbing capabilities.
The primary goal is to improve developer workflow by providing precise context (component stacks, file paths, line numbers) to AI coding agents, making them faster and more accurate for UI-related tasks. The extension is designed with privacy in mind, collecting no personal data or browsing history.
Sources: packages/website/app/privacy/page.tsx:40-62, packages/react-grab/README.md:10-12, CONTRIBUTING.md:25-25
Architecture
The web extension is built on Chrome's Manifest V3 platform and consists of three main parts: a manifest file, a service worker, and a content script.
- Manifest (
manifest.json): Defines the extension's metadata, permissions, and entry points for the service worker and content script. - Service Worker (
service-worker.ts): A background script that listens for browser events, such as the user clicking the extension's toolbar icon. - Content Script (
react-grab.ts): A script injected into web pages that interacts with the DOM and communicates with the service worker. It is responsible for initializing the corereact-grabfunctionality on the page.
Sources: packages/web-extension/src/manifest.json, packages/web-extension/src/background/service-worker.ts, packages/web-extension/src/content/react-grab.ts
The following diagram illustrates the high-level architecture of the web extension.
Permissions
The extension requests a minimal set of permissions required for its core functionality. These are declared in manifest.json and are used solely for enabling element selection and storing user preferences locally.
| Permission | Description |
|---|---|
activeTab | Allows interaction with the currently active tab, necessary for using keyboard shortcuts to grab elements. |
storage | Used to store user preferences locally on the device, such as whether the extension is enabled. |
<all_urls> | Required to inject the element selection functionality into any webpage the user visits. |
Sources: packages/web-extension/src/manifest.json:21-24, packages/website/app/privacy/page.tsx:64-85
The manifest.json file specifies these permissions:
{
"permissions": [
"activeTab",
"storage"
],
"host_permissions": [
"<all_urls>"
]
}
Sources: packages/web-extension/src/manifest.json:21-27
Functionality and Data Flow
The extension's primary function is to toggle the react-grab element selector on the current page. This process is initiated by the user and involves communication between the service worker and the content script.
Activation Flow
- The user clicks the React Grab icon in the browser's toolbar.
- The
chrome.action.onClickedevent is triggered in the service worker. - The service worker sends a message with the type
toggle-activationto the content script running in the active tab. - The content script's message listener receives the message and calls the
init()function from the corereact-grablibrary, which activates or deactivates the element selection UI.
This sequence diagram shows the interaction between the user, service worker, and content script.
Implementation Details
The service worker's logic is concise, focused only on relaying the user's action to the content script.
// packages/web-extension/src/background/service-worker.ts
chrome.action.onClicked.addListener(async (tab) => {
if (!tab.id) return;
chrome.tabs.sendMessage(tab.id, {
type: "toggle-activation",
});
});
Sources: packages/web-extension/src/background/service-worker.ts:1-6
The content script listens for this message and initializes the core library.
// packages/web-extension/src/content/react-grab.ts
import { init } from "react-grab/core";
const grabber = init();
chrome.runtime.onMessage.addListener((message) => {
if (message.type === "toggle-activation") {
grabber.toggle();
}
});
Sources: packages/web-extension/src/content/react-grab.ts:1-8
Privacy and Data Handling
A key principle of the web extension is privacy. All processing happens locally on the user's device.
- The extension does not collect, store, or transmit any personal data, browsing history, or information about visited websites.
- It does not use any third-party analytics, tracking, or advertising services.
- When an element is selected, the HTML/JSX is copied to the user's local clipboard. No data is sent to external servers.
The project is open source, allowing users to verify these privacy claims by reviewing the code.
Sources: packages/website/app/privacy/page.tsx:43-62, packages/website/app/privacy/page.tsx:95-104