npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

element-prompt-builder-dev

v0.1.110

Published

React component library for inspecting DOM elements and generating AI prompts

Downloads

122

Readme

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 element-prompt-builder
# or
yarn add element-prompt-builder

Usage

Basic Example

import { ElementInspector } from 'element-prompt-builder';

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 'element-prompt-builder';

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 'element-prompt-builder';

// Get XPath for an element
const xpath = getXPathForElement(document.getElementById('myElement'));

// Get relevant attributes from an element
const attributes = getElementAttributes(document.getElementById('myElement'));

// Generate detailed context for an element
const context = generateElementContext(document.getElementById('myElement'), 0);

// Find the most specific element at a point
const element = getMostSpecificElementAtPoint(100, 200, '.exclude-me');

// Check if a point is within an element
const isInElement = isElementAtPoint(document.getElementById('myElement'), 100, 200);

// Get percentage offsets from a point to an element
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 'element-prompt-builder';

// Generate a prompt for an AI model
const elementPrompt = createElementsPrompt(
  [document.getElementById('myButton')], // Array of selected elements
  "Please change this button's background color to blue and make the text bold" // User's question or prompt
);

// Use this prompt with your preferred AI model
console.log(elementPrompt);

API Reference

ElementInspector

Main component that provides a UI for selecting and highlighting elements.

<ElementInspector
  initialIsActive={boolean} // Whether the inspector is active initially
  excludeSelector={string} // CSS selector for elements to exclude from selection
  maxElements={number} // Maximum number of elements that can be selected
  elementLabel={(element: HTMLElement) => ReactNode} // Custom label for selected elements
  selectorStyle={React.CSSProperties} // Custom styles for the selector
  highlighterStyle={React.CSSProperties} // Custom styles for the highlighter
/>

ElementSelector

Component that creates an overlay to select DOM elements.

<ElementSelector
  onElementHovered={(element: HTMLElement) => void} // Callback when an element is hovered
  onElementUnhovered={() => void} // Callback when an element is no longer hovered
  onElementSelected={(element: HTMLElement) => void} // Callback when an element is selected
  ignoreList={HTMLElement[]} // List of elements to ignore during selection
  excludeSelector={string} // CSS selector for elements to exclude
  className={string} // CSS class for the selector overlay
  style={React.CSSProperties} // Custom styles for the selector overlay
/>

ElementHighlighter

Component that highlights a DOM element with a border.

<ElementHighlighter
  element={HTMLElement} // The element to highlight
  className={string} // CSS class for the highlighter
  style={React.CSSProperties} // Custom styles for the highlighter
  updateRate={number} // Update rate in frames per second (0 to disable updates)
  borderColor={string} // Border color for the highlighter
  backgroundColor={string} // Background color for the highlighter
>
  {/* Optional content to render inside the highlighter */}
</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:

// Listen for prompt generation events
document.addEventListener('promptGenerated', (event) => {
  const { prompt, elements } = event.detail;
  console.log('Generated prompt:', prompt);
  console.log('Selected elements:', elements);
  
  // Handle the prompt in your application
  // For example, send it to an AI model API
});

Cross-Frame Communication

When the Element Inspector is used inside an iframe, it communicates with the parent window using the postMessage API:

// In the parent window
window.addEventListener('message', (event) => {
  // Check if the message is from the Element Inspector
  if (event.data.type === 'ELEMENT_INSPECTOR_PROMPT') {
    const { prompt, elements } = event.data.payload;
    console.log('Generated prompt:', prompt);
    console.log('Selected elements:', elements);
    
    // Handle the prompt in your parent application
  }
});

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