Element Prompt Builder
A React component library for inspecting DOM elements and generating structured prompts for AI models based on selected elements.
Features
- Element Selection: Select DOM elements by clicking on them
- Element Highlighting: Highlight selected elements with visible borders
- Element Context: Generate detailed context information about selected elements
- AI Prompt Generation: Create structured prompts for AI models with element context
- Event System: Cross-frame communication and DOM events for integration
Installation
npm install react-element-prompt-inspector
yarn add react-element-prompt-inspector
Usage
Basic Example
import { ElementInspector } from 'react-element-prompt-inspector';
function App() {
return (
<div>
<h1>My Web App</h1>
<ElementInspector
initialIsActive={false}
excludeSelector=".no-inspect"
/>
</div>
);
}
Individual Components
You can also use the individual components for more control:
import { useState } from 'react';
import {
ElementSelector,
ElementHighlighter
} from 'react-element-prompt-inspector';
function CustomInspector() {
const [hoveredElement, setHoveredElement] = useState<HTMLElement | null>(null);
return (
<>
<ElementSelector
onElementHovered={setHoveredElement}
onElementUnhovered={() => setHoveredElement(null)}
onElementSelected={(element) => console.log('Selected:', element)}
/>
{hoveredElement && (
<ElementHighlighter
element={hoveredElement}
borderColor="rgba(255, 0, 0, 0.8)"
backgroundColor="rgba(255, 0, 0, 0.2)"
/>
)}
</>
);
}
Using Utility Functions
import {
getXPathForElement,
getElementAttributes,
generateElementContext,
getMostSpecificElementAtPoint,
isElementAtPoint,
getOffsetsFromPointToElement,
createElementsPrompt
} from 'react-element-prompt-inspector';
const xpath = getXPathForElement(document.getElementById('myElement'));
const attributes = getElementAttributes(document.getElementById('myElement'));
const context = generateElementContext(document.getElementById('myElement'), 0);
const element = getMostSpecificElementAtPoint(100, 200, '.exclude-me');
const isInElement = isElementAtPoint(document.getElementById('myElement'), 100, 200);
const offsets = getOffsetsFromPointToElement(document.getElementById('myElement'), 100, 200);
AI Prompt Generation
The package includes utilities for generating AI prompts from selected elements:
import {
createElementsPrompt
} from 'react-element-prompt-inspector';
const elementPrompt = createElementsPrompt(
[document.getElementById('myButton')],
"Please change this button's background color to blue and make the text bold"
);
console.log(elementPrompt);
API Reference
ElementInspector
Main component that provides a UI for selecting and highlighting elements.
<ElementInspector
initialIsActive={boolean}
excludeSelector={string}
maxElements={number}
elementLabel={(element: HTMLElement) => ReactNode}
selectorStyle={React.CSSProperties}
highlighterStyle={React.CSSProperties}
/>
ElementSelector
Component that creates an overlay to select DOM elements.
<ElementSelector
onElementHovered={(element: HTMLElement) => void}
onElementUnhovered={() => void}
onElementSelected={(element: HTMLElement) => void}
ignoreList={HTMLElement[]}
excludeSelector={string}
className={string}
style={React.CSSProperties}
/>
ElementHighlighter
Component that highlights a DOM element with a border.
<ElementHighlighter
element={HTMLElement}
className={string}
style={React.CSSProperties}
updateRate={number}
borderColor={string}
backgroundColor={string}
>
{}
</ElementHighlighter>
Event System
The Element Inspector component provides an event system to handle generated prompts in your application.
Browser Events
When a prompt is generated, the component dispatches a custom DOM event that you can listen for:
document.addEventListener('promptGenerated', (event) => {
const { prompt, elements } = event.detail;
console.log('Generated prompt:', prompt);
console.log('Selected elements:', elements);
});
Cross-Frame Communication
When the Element Inspector is used inside an iframe, it communicates with the parent window using the postMessage API:
window.addEventListener('message', (event) => {
if (event.data.type === 'ELEMENT_INSPECTOR_PROMPT') {
const { prompt, elements } = event.data.payload;
console.log('Generated prompt:', prompt);
console.log('Selected elements:', elements);
}
});
The elements array in the message payload contains serialized information about each selected DOM element, including:
tagName: The HTML tag name
id: The element's ID attribute
className: The element's class names
textContent: The text content of the element
attributes: An array of the element's attributes (name-value pairs)
This allows you to process the prompt and element information even across different browsing contexts.
License
MIT