Overview
ClawScript is PhoneClaw’s JavaScript execution engine, powered by Mozilla Rhino. It enables natural language automation by converting voice commands into executable JavaScript code that controls Android device functions.Architecture
JavaScript Engine: Mozilla Rhino
PhoneClaw uses Rhino 1.7.13, a pure Java implementation of JavaScript that runs directly on Android:build.gradle.kts
Why Rhino? Rhino provides a sandboxed JavaScript environment that’s perfect for Android automation - it runs entirely in the JVM without requiring V8 or other native engines.
Script Execution Flow
- Voice Input → User speaks a command (“Click the login button”)
- AI Translation → OpenRouter converts command to JavaScript
- Code Extraction → Extract JavaScript from AI response
- Rhino Execution → Execute in sandboxed environment
- Android Actions → Trigger device automation via AndroidJSInterface
Core Implementation
Setting Up the Rhino Context
FromMainActivity.kt:4169-4186:
JavaScript Bridge Interface
PhoneClaw exposes Android functionality through theAndroid object:
Available Functions
Magic Functions
These use vision AI to interact with UI elements:magicClicker(description)
magicClicker(description)
Finds and clicks UI elements using natural language descriptions.Uses Moondream API for vision-based element detection.
magicScraper(description)
magicScraper(description)
Extracts text content from the screen using vision AI.Returns extracted text as a string.
Accessibility Functions
Direct UI interaction via Android Accessibility Service:App Control
System Settings
Code Extraction
AI responses are automatically cleaned to extract pure JavaScript:Error Handling
All script execution includes automatic error catching:Performance Optimization
Optimization Level
Rhino’s optimization level is set to -1 (interpreted mode) for Android compatibility. Higher optimization levels require bytecode generation which isn’t reliable on Android.
Async Execution
Scripts run on a background dispatcher to prevent UI blocking:Example Scripts
Simple Click Automation
Data Scraping
Conditional Logic
Limitations
Next Steps
Accessibility Service
Learn how ClawScript interacts with Android UI
Vision Targeting
Understand vision-based UI detection