DOM2Code is a Visual Studio Code extension that allows developers to select HTML elements from a rendered webpage and inject the corresponding HTML markup into their active editor. This creates a seamless bridge between visual UI elements and their code representation.
- Static HTML Selection: Choose from a gallery of pre-defined HTML components
- Localhost URL Integration: View and select elements from your running localhost application
- Chrome Extension Integration: Select elements directly in Chrome using the companion extension
- HTML Formatting Options: Inject HTML with your preferred formatting style (pretty, minified, or raw)
- Visual Selection: Intuitively point and click to select the elements you want
You can install this extension from the VS Code marketplace:
- Open VS Code
- Go to Extensions (Ctrl+Shift+X)
- Search for "DOM2Code"
- Click Install
For the best experience, install the companion Chrome extension:
- Open the command palette (Ctrl+Shift+P)
- Type "DOM2Code: Install Chrome Extension"
- Follow the instructions to install the Chrome extension
- Once installed, click the extension icon in Chrome to connect to VS Code
- Open the command palette (Ctrl+Shift+P)
- Type "DOM2Code: Open Element Selector"
- Click on any element in the gallery to inject its HTML into your editor
- Start your web application locally (e.g., on http://localhost:3000)
- Open the command palette (Ctrl+Shift+P)
- Type "DOM2Code: Open With Localhost URL"
- Enter your localhost URL when prompted
- Click "Enable Selection" in the toolbar
- Click on any element in the rendered page to inject its HTML into your editor
- Make sure both the VS Code extension and Chrome extension are installed
- Open VS Code with a file where you want to inject HTML
- Open Chrome and navigate to the webpage you want to extract elements from
- Click the DOM2Code extension icon in Chrome
- Click "Connect to VS Code" to establish a connection
- Click "Enable Selection Mode"
- Hover over elements on the page to see them highlighted
- Click an element to send its HTML directly to VS Code
- Alternatively, right-click on any element and select "Send to VS Code"
This extension contributes the following settings:
dom2code.defaultLocalhostUrl: Default localhost URL to open (default: "http://localhost:3000")dom2code.htmlFormatting: HTML formatting style for injected code (options: "pretty", "minified", "raw")dom2code.selectionHighlightColor: Color used for highlighting selected elements (default: "#007bff")dom2code.portMappings: Port mappings for localhost connectionsdom2code.serverPort: Port for Chrome extension WebSocket communication (default: 54321)
- Some websites block iframe access for security reasons, which may prevent the localhost viewer from working properly
- Complex HTML structures may not always format perfectly in the injected code
- Current workflow requires multiple clicks between browser and editor - we're working on streamlining this process
- Success confirmations for element selection and code injection could be more visible and informative
- Chrome extension interface could benefit from improved visual feedback and more intuitive controls
If you see the error "Cannot access iframe content", this is likely due to the website's Content Security Policy (CSP) blocking iframe access. Try using the Chrome Extension method instead, or modify your local application's CSP settings.
If the Chrome extension cannot connect to VS Code:
- Make sure VS Code is running with the DOM2Code extension installed
- Check that the WebSocket server is started (visible in the status bar)
- Verify both extensions are using the same port (default: 54321)
- Temporarily disable any firewalls or security software that might block WebSocket connections
If the injected HTML doesn't look right, try changing the HTML formatting option in the extension settings.
Contributions are welcome! Please feel free to submit a Pull Request.
- Fork the repository
- Create your feature branch:
git checkout -b my-new-feature - Commit your changes:
git commit -am 'Add some feature' - Push to the branch:
git push origin my-new-feature - Submit a pull request
This project is licensed under the MIT License - see the LICENSE file for details.
Major update with Chrome extension integration:
- Added companion Chrome extension for direct HTML selection
- WebSocket server for real-time communication
- Improved element selection and highlighting
- Better HTML formatting options
Initial release of DOM2Code with basic functionality:
- Static HTML selection
- Localhost URL integration
- HTML formatting options