Skip to main content

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

String
required
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

String
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 Examples:
  • “Error: No screenshot”
  • “Error: Activity destroyed”
  • “Error: Operation cancelled”

How It Works

  1. Screenshot Capture: Takes a screenshot of the current screen
  2. Image Encoding: Converts screenshot to base64 JPEG format
  3. Vision AI Query: Sends image and question to OpenRouter’s vision model
  4. Text Extraction: AI analyzes the image and returns the requested information
  5. 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 through callStreamingAPIWithImage() which:
  1. Sends image as data URL in message content
  2. Uses system prompt to ensure concise responses
  3. Limits response to 150 tokens for efficiency
  4. Returns trimmed result string

Best Practices

Ask Specific Questions: The more specific your query, the more accurate the extraction.Good: “What is the battery percentage in the status bar?”Better: “battery percentage”
Check for Empty Results: Always validate the returned data before using it in automation logic.

Query Optimization

Efficient queries:
  • “battery percentage” → “75%”
  • “time in status bar” → “2:45 PM”
  • “notification count” → “5”
Less efficient:
  • “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

Timeout: Operations have a 30-second timeout. Complex queries may take longer.
  • 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:
This logs:
  • Input description
  • Output result
  • Timestamp
  • Device ID

Model Selection

Change the vision model in app settings:
Select via the Model button in the PhoneClaw app interface.

Advanced Usage

Combining with magicClicker

Data Validation Loop

Comparison with Other Methods

  • magicClicker() - Click UI elements using natural language
  • speakText() - Provide voice feedback with scraped data
  • delay() - 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)

See Also