User Script API Reference
Section titled “User Script API Reference”- API level: 1 (
StarX.apiLevel) - Language: Rhino ES6 (
Context.VERSION_ES6), optimization level -1 (interpreted only, no bytecode generation) - Each script gets its own global scope, with
StarX,console,setTimeout,setInterval,clearTimeoutandclearIntervalinstalled
Contents
Section titled “Contents”- Global objects
- Common methods
- Timers
- StarX.storage
- StarX.http
- StarX.clipboard
- StarX.ai
- StarX.tiku
- StarX.answer
- StarX.question
- StarX.hook
- StarX.page
- Event bus
- Error handling
- Versioning
Global objects
Section titled “Global objects”StarX.apiLevel
Section titled “StarX.apiLevel”Integer, currently 1.
StarX.version
Section titled “StarX.version”Module version string, for example 1.5.4.
StarX.script
Section titled “StarX.script”Metadata of this script, available after load:
{ id: 'my_script', name: 'My script', namespace: 'local', version: '1.0.0', description: 'description', author: 'you', scopes: ['exam', 'work'], matches: [], runAt: 'document-end', env: 'native'}StarX.device
Section titled “StarX.device”{ sdkInt: 36, packageName: 'com.chaoxing.mobile', processName: 'com.chaoxing.mobile', isMainProcess: true, appVersionName: '7.0.4', appVersionCode: 700004, moduleVersion: '1.5.4', hookApiAvailable: true, onMainThread: true}Common methods
Section titled “Common methods”| Method | Description |
|---|---|
StarX.now() | Millisecond timestamp |
StarX.random() | Random number in [0, 1) |
StarX.sleep(ms) | Blocks the script thread, capped at 120000 ms |
StarX.toast(msg) | Shows a toast (subject to the module toast switch) |
StarX.dump(value) | Converts any value to a string / JSON text for debugging |
StarX.log(...) | Writes a log line, same as StarX.console.log |
StarX.console provides log / info / warn / error / debug, all written to scripts/logs/<id>.log and logcat.
Timers
Section titled “Timers”var id = StarX.setTimeout(fn, ms);var id = StarX.setInterval(fn, ms);StarX.clearTimeout(id);StarX.clearInterval(id);Timers run on the script’s own executor thread and are cleared when the script is disabled or reloaded.
StarX.storage
Section titled “StarX.storage”Per-script key/value storage persisted at scripts/data/<id>.json.
| Method | Description |
|---|---|
get(key, def) | Reads a string, returns def when absent |
set(key, value) | Stores a value (non-strings are stringified) |
getJson(key, def) | Reads and JSON.parses |
setJson(key, value) | JSON.stringify then store |
remove(key) | Removes one key |
keys() | Array of all keys |
clear() | Clears everything |
StarX.http
Section titled “StarX.http”var r = StarX.http.request({ url: 'https://example.com/api', method: 'POST', // default GET body: 'a=1&b=2', // string; JSON.stringify objects yourself headers: { 'X-Token': 'x' }, timeout: 15000 // milliseconds, 1000..60000});// success: r = { ok: true, status: 200, body: '...', headers: { ... } }// failure: r = { ok: false, status: 0, body: '', error: '...' }- Shorthands:
StarX.http.get(url, headers)andStarX.http.post(url, body, headers). - Default
User-AgentisMozilla/5.0 (Linux; Android <release>; StarX/<module version>). - No host cookies are attached; pass what you need in
headers.
StarX.clipboard
Section titled “StarX.clipboard”| Method | Description |
|---|---|
get() | Reads clipboard text |
set(text) | Writes clipboard text |
StarX.ai
Section titled “StarX.ai”| Method | Returns |
|---|---|
isConfigured() | Boolean, whether the module has a usable AI configuration |
ask(question, type, options) | String or null |
askImage(base64, mime) | String or null |
ocr(base64, mime) | String or null |
typeuses the question type codes, default-1.optionsmay be a string, array or object (serialized to JSON before reaching the model).- Without an AI configuration everything returns
nulland nothing throws.
StarX.tiku
Section titled “StarX.tiku”StarX.tiku.query(question, type, options) returns a string or null. Queries the third-party question bank.
StarX.answer
Section titled “StarX.answer”StarX.answer.query(question, type, options, images) returns { answer, source } or null.
- Uses the same pipeline as the module itself (question bank first, AI as fallback).
imagesis an array of base64 strings and may be omitted.- Internally times out after 20 seconds and blocks the calling thread; calling it inside a
questioncallback slows that question down.
StarX.question
Section titled “StarX.question”| Method | Description |
|---|---|
last() | Payload of the most recent question event, may be null |
publish(payload) | Manually broadcasts a question to all global scripts |
answer(value) | Answers inside a question callback, returns true / false |
answer normalization: strings are used as-is; numbers and booleans are stringified; arrays whose elements are all single characters join into AC, otherwise they join with commas.
StarX.hook
Section titled “StarX.hook”Requires framework API 102 or newer; check with StarX.hook.available().
if (StarX.hook.available()) { var id = StarX.hook.method('com.example.Foo', 'bar', { arity: 1, // parameter count, -1 for any before: function (ctx) { ctx.args[0] = 'replaced'; // change arguments // ctx.skip = true; // skip the original method // ctx.result = 'fake'; // used with skip as the return value }, after: function (ctx) { return ctx.result + '!'; // returning non-undefined replaces the result } }); StarX.hook.unhook(id);}Interceptor context:
{ method: 'bar', class: 'com.example.Foo', args: [ ... ], this: <instance>, result: <only during after>}- Returns a
handleIdstring;nullwhen the class or method is missing, or when framework API < 102. - Both callbacks must return synchronously and run on the thread where the interception happens.
- All registered hooks are removed when the script is disposed, reloaded or disabled.
Also available: StarX.hook.classExists(name) and StarX.hook.unhookAll().
StarX.page
Section titled “StarX.page”| Method | Description |
|---|---|
list() | Array of tracked WebView URLs |
inject(js) | Injects JS into live WebViews, returns how many were injected |
Event bus
Section titled “Event bus”StarX.on(name, fn); // subscribe, returns the callbackStarX.off(name, fn); // unsubscribe; without fn it clears the eventStarX.emit(name, payload); // broadcast to other scripts (not back to yourself)Callback signature: fn(payload, eventName, fromId).
Framework events, their payloads and timing are documented in the User Script Guide section 6.
Error handling
Section titled “Error handling”- Every failing API call returns
null/falseinstead of throwing into the script. - An exception in an event callback logs
event <name> handler failed: <msg>and other callbacks keep running. - An exception during script load marks that script as failed and shows the error on its card.
Versioning
Section titled “Versioning”StarX.apiLevellets scripts probe capabilities; the level only increases.- New APIs do not change existing semantics; breaking changes bump
apiLevel.