Overview
magicScraper() is a vision-based data extraction function that reads information from your screen using natural language queries. It leverages OpenRouter’s vision models to analyze screenshots and return specific information.
Function Signature
Parameters
Natural language question or description of what data you want to extract from the screen.Examples:
- “What is the battery percentage?”
- “Read the notification count”
- “What time is displayed?”
- “Extract the email address shown”
- “What is the WiFi network name?”
Return Value
The extracted text or data from the screen. Returns an error message if extraction fails.Success Examples:
- “85%” (for battery query)
- “3:45 PM” (for time query)
- “MyWiFi-Network” (for WiFi name)
- “Error: No screenshot”
- “Error: Activity destroyed”
- “Error: Operation cancelled”
How It Works
- Screenshot Capture: Takes a screenshot of the current screen
- Image Encoding: Converts screenshot to base64 JPEG format
- Vision AI Query: Sends image and question to OpenRouter’s vision model
- Text Extraction: AI analyzes the image and returns the requested information
- Result Formatting: Cleans and formats the response for easy use
Code Examples
Basic Data Extraction
Screen Content Analysis
Conditional Automation
Form Data Extraction
Vision AI Integration
OpenRouter Streaming API
The function uses OpenRouter’s vision-capable models: Default Model: Selected via app settings (Gemini 2.0 Flash or Llama 4 Maverick) Request Format:Response Processing
The AI response is processed throughcallStreamingAPIWithImage() which:
- Sends image as data URL in message content
- Uses system prompt to ensure concise responses
- Limits response to 150 tokens for efficiency
- Returns trimmed result string
Best Practices
Query Optimization
Efficient queries:- “battery percentage” → “75%”
- “time in status bar” → “2:45 PM”
- “notification count” → “5”
- “Tell me everything about the battery” → Long response
- “What do you see on screen?” → Too broad
Error Handling
Common Error Messages
- “Error: Activity destroyed”: App is no longer active
- “Error: No screenshot”: Screenshot capture failed
- “Error: Operation cancelled”: Request was cancelled or timed out
- “Error: [message]”: General exception occurred
Performance Considerations
- Synchronous Operation: Uses
runBlocking- UI may freeze briefly - Image Compression: 85% JPEG quality balances size and clarity
- Token Limit: 150 max tokens keeps responses fast and focused
- Network Dependent: Requires active internet connection
Tracking and Analytics
Each scraping operation is tracked:- Input description
- Output result
- Timestamp
- Device ID
Model Selection
Change the vision model in app settings:Advanced Usage
Combining with magicClicker
Data Validation Loop
Comparison with Other Methods
| Feature | magicScraper | Traditional OCR | Accessibility |
|---|---|---|---|
| Setup | Zero config | Requires training | Needs service |
| Accuracy | High with context | Character-level | Element-dependent |
| Flexibility | Natural language | Fixed patterns | Limited to labels |
| Speed | 2-5 seconds | Sub-second | Instant |
Related Functions
magicClicker()- Click UI elements using natural languagespeakText()- Provide voice feedback with scraped datadelay()- Wait between scraping operations
Limitations
- Requires active internet connection
- 30-second timeout for complex queries
- May briefly freeze UI during execution
- Accuracy depends on screen clarity and query specificity
- Uses API credits/rate limits (check OpenRouter plan)