JavaScriptReplyProxy


@UiThread
abstract class JavaScriptReplyProxy


This class represents the JavaScript object injected by WebViewCompat#addWebMessageListener. An instance will be given by WebMessageListener#onPostMessage. The app can use postMessage to talk to the JavaScript context.

There is a 1:1 relationship between this object and the JavaScript object in a frame.

Summary

Public functions

abstract Unit
@RequiresFeature(name = WebViewFeature.JS_INJECTION_IN_FRAME_AND_WORLD, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
executeJavaScript(
    script: String,
    receiver: WebViewOutcomeReceiver<String!, JavaScriptExecutionException!>?
)

Executes JavaScript in the frame and world that sent this JavaScriptReplyProxy.

abstract Unit
@RequiresFeature(name = WebViewFeature.WEB_MESSAGE_ARRAY_BUFFER, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
postMessage(arrayBuffer: ByteArray)

Post a ArrayBuffer message to the injected JavaScript object which sent this JavaScriptReplyProxy.

abstract Unit
@RequiresFeature(name = WebViewFeature.WEB_MESSAGE_LISTENER, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
postMessage(message: String)

Post a String message to the injected JavaScript object which sent this JavaScriptReplyProxy.

Public functions

executeJavaScript

@RequiresFeature(name = WebViewFeature.JS_INJECTION_IN_FRAME_AND_WORLD, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
abstract fun executeJavaScript(
    script: String,
    receiver: WebViewOutcomeReceiver<String!, JavaScriptExecutionException!>?
): Unit

Executes JavaScript in the frame and world that sent this JavaScriptReplyProxy. The result of the JavaScript execution will be passed to the WebViewOutcomeReceiver callback.

This method should only be called if isFeatureSupported returns true for JS_INJECTION_IN_FRAME_AND_WORLD.

Parameters
receiver: WebViewOutcomeReceiver<String!, JavaScriptExecutionException!>?

The WebViewOutcomeReceiver to receive the result of the JavaScript execution.

script: String

The JavaScript to execute.

postMessage

Added in 1.8.0
@RequiresFeature(name = WebViewFeature.WEB_MESSAGE_ARRAY_BUFFER, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
abstract fun postMessage(arrayBuffer: ByteArray): Unit

Post a ArrayBuffer message to the injected JavaScript object which sent this JavaScriptReplyProxy. Be aware that large byte buffers can lead to out-of-memory crashes on low-end devices.

Parameters
arrayBuffer: ByteArray

The ArrayBuffer to send to the JavaScript context. An empty ArrayBuffer is supported.

postMessage

Added in 1.3.0
@RequiresFeature(name = WebViewFeature.WEB_MESSAGE_LISTENER, enforcement = "androidx.webkit.WebViewFeature#isFeatureSupported")
abstract fun postMessage(message: String): Unit

Post a String message to the injected JavaScript object which sent this JavaScriptReplyProxy.

Parameters
message: String

The String data to send to the JavaScript context.