|
| 1 | +# Web Preview Feature |
| 2 | + |
| 3 | +The Web Preview feature in Roo Code allows developers to preview web applications directly within VSCode and select UI elements to provide context to the AI assistant. This feature is similar to Windsurf IDE's preview functionality. |
| 4 | + |
| 5 | +## Features |
| 6 | + |
| 7 | +- **Integrated Web Preview**: View web applications in a dedicated panel within VSCode |
| 8 | +- **Element Selection**: Click on UI elements to select them and extract their context |
| 9 | +- **Responsive Design Testing**: Switch between different device viewports |
| 10 | +- **Element Context Extraction**: Automatically extract HTML, CSS, position, and other metadata |
| 11 | +- **AI Integration**: Selected element context is automatically sent to the AI assistant |
| 12 | + |
| 13 | +## How to Use |
| 14 | + |
| 15 | +### Opening the Web Preview |
| 16 | + |
| 17 | +1. Open the Web Preview panel by: |
| 18 | + - Using the command palette: `Cmd/Ctrl + Shift + P` → "Roo Code: Open Web Preview" |
| 19 | + - Clicking on the Web Preview icon in the Roo Code sidebar |
| 20 | + |
| 21 | +### Loading a URL |
| 22 | + |
| 23 | +1. Enter the URL in the address bar at the top of the preview panel |
| 24 | +2. Click "Go" or press Enter to load the page |
| 25 | + |
| 26 | +### Selecting Elements |
| 27 | + |
| 28 | +1. Click the "🎯 Select Element" button to enable element selection mode |
| 29 | +2. Hover over elements in the preview to see them highlighted |
| 30 | +3. Click on an element to select it |
| 31 | +4. The element's context will be automatically sent to the AI chat |
| 32 | + |
| 33 | +### Device Simulation |
| 34 | + |
| 35 | +Use the device selector dropdown to switch between different viewport sizes: |
| 36 | + |
| 37 | +- Responsive (default) |
| 38 | +- iPhone SE (375x667) |
| 39 | +- iPhone 12/13 (390x844) |
| 40 | +- iPad (768x1024) |
| 41 | +- Desktop (1280x800) |
| 42 | +- Full HD (1920x1080) |
| 43 | + |
| 44 | +## Element Context |
| 45 | + |
| 46 | +When you select an element, the following information is extracted and sent to the AI: |
| 47 | + |
| 48 | +- **HTML**: The complete HTML of the selected element |
| 49 | +- **CSS**: All CSS rules that apply to the element |
| 50 | +- **Position**: X, Y coordinates and dimensions (width, height) |
| 51 | +- **Computed Styles**: Key style properties like display, position, colors, fonts |
| 52 | +- **Attributes**: All HTML attributes on the element |
| 53 | +- **Selectors**: Both CSS selector and XPath for the element |
| 54 | + |
| 55 | +## Example Use Cases |
| 56 | + |
| 57 | +1. **UI Debugging**: Select a misaligned element and ask the AI to fix the CSS |
| 58 | +2. **Component Analysis**: Select a component and ask the AI to explain how it works |
| 59 | +3. **Style Improvements**: Select an element and ask for design suggestions |
| 60 | +4. **Accessibility**: Select elements and ask for accessibility improvements |
| 61 | +5. **Code Generation**: Select a UI pattern and ask the AI to create similar components |
| 62 | + |
| 63 | +## Limitations |
| 64 | + |
| 65 | +- **Cross-Origin Restrictions**: Element inspection may not work on pages with strict CORS policies |
| 66 | +- **IFrame Content**: Cannot inspect elements inside cross-origin iframes |
| 67 | +- **Dynamic Content**: Some dynamically loaded content may not be immediately selectable |
| 68 | + |
| 69 | +## Technical Details |
| 70 | + |
| 71 | +The Web Preview feature consists of: |
| 72 | + |
| 73 | +1. **WebPreviewProvider**: Main provider class that manages the preview panel |
| 74 | +2. **Preview UI**: HTML/CSS/JS for the preview interface and controls |
| 75 | +3. **Element Inspector**: JavaScript injection for element selection and context extraction |
| 76 | +4. **Message Passing**: Communication between the preview, extension, and AI chat |
| 77 | + |
| 78 | +## API Reference |
| 79 | + |
| 80 | +### WebPreviewProvider |
| 81 | + |
| 82 | +```typescript |
| 83 | +class WebPreviewProvider { |
| 84 | + // Load a URL in the preview |
| 85 | + loadUrl(url: string): Promise<void> |
| 86 | + |
| 87 | + // Set viewport dimensions |
| 88 | + setViewport(width: number, height: number): Promise<void> |
| 89 | + |
| 90 | + // Get the last selected element context |
| 91 | + getSelectedElementContext(): ElementContext | undefined |
| 92 | +} |
| 93 | +``` |
| 94 | + |
| 95 | +### ElementContext Interface |
| 96 | + |
| 97 | +```typescript |
| 98 | +interface ElementContext { |
| 99 | + html: string |
| 100 | + css: string |
| 101 | + position: { |
| 102 | + x: number |
| 103 | + y: number |
| 104 | + width: number |
| 105 | + height: number |
| 106 | + } |
| 107 | + computedStyles?: Record<string, string> |
| 108 | + attributes?: Record<string, string> |
| 109 | + xpath?: string |
| 110 | + selector?: string |
| 111 | +} |
| 112 | +``` |
| 113 | + |
| 114 | +## Contributing |
| 115 | + |
| 116 | +To contribute to the Web Preview feature: |
| 117 | + |
| 118 | +1. The main code is in `src/core/webview/WebPreviewProvider.ts` |
| 119 | +2. Preview UI assets are in `src/core/webview/preview/` |
| 120 | +3. Tests are in `src/__tests__/WebPreviewProvider.spec.ts` |
| 121 | +4. Message types are defined in `src/shared/ExtensionMessage.ts` |
| 122 | + |
| 123 | +Please ensure all tests pass and add new tests for any new functionality. |
0 commit comments