Injectable Scripts
Pre-built script strings for WebView injection. Import from @snapfill/core or @snapfill/core/injectable.
snapfillScript
const snapfillScript: stringCombined injectable containing form detection, cart detection, and value capture. Inject this single string into a WebView for full autofill support.
When evaluated, the script:
- Scans the DOM for form fields and posts a
formDetectedmessage - Scans for shopping cart data and posts a
cartDetectedmessage - Attaches listeners to detected fields and posts
valuesCapturedon change - Watches for DOM mutations and re-scans automatically
// Inject once after page load
webview.evaluateJavaScript(snapfillScript);The script is idempotent — injecting it multiple times has no effect.
buildFillScript(mappings)
function buildFillScript(mappings: AutofillMappings): stringBuilds a fill script string for WebView injection. The returned string fills form fields using the field map created by snapfillScript.
Parameters:
mappings— Object mapping field types to values
Returns: A JavaScript string to evaluate in the WebView
const script = buildFillScript({
firstName: 'Jane',
lastName: 'Doe',
email: 'jane@example.com',
});
webview.evaluateJavaScript(script);The fill script posts a formFillComplete message with the result.
WARNING
snapfillScript must be injected first — the fill script depends on the field map it creates.
fillScriptTemplate
const fillScriptTemplate: stringRaw fill script with a __SNAPFILL_MAPPINGS__ placeholder. Used internally by buildFillScript and by native libraries (Android/iOS) that perform their own JSON serialization.
// Native usage (Kotlin/Swift)
val script = fillScriptTemplate.replace("__SNAPFILL_MAPPINGS__", jsonMappings)
webView.evaluateJavascript(script, null)Individual Scripts
You can also inject detection scripts individually:
formDetectorScript
const formDetectorScript: stringOnly form detection — no cart detection or value capture. Posts formDetected messages.
cartDetectorScript
const cartDetectorScript: stringOnly cart detection. Posts cartDetected messages.
valueCaptureScript
const valueCaptureScript: stringOnly value capture. Requires formDetectorScript to be injected first. Posts valuesCaptured messages.