@duckduckgo/content-scope-scripts
    Preparing search index...

    Class AndroidAdsjsMessagingConfig

    Android shared messaging configuration for addWebMessageListener API. This class should be constructed once and then shared between features.

    The following example shows all the fields that are required to be passed in:

    const config = new AndroidAdsjsMessagingConfig({
    // a value that native has injected into the script
    messageSecret: 'abc',

    // the object name that will be used for addWebMessageListener
    objectName: "androidAdsjs",

    // the global object where methods will be registered
    target: globalThis
    });

    The native Android code should use addWebMessageListener to listen for messages:

    WebViewCompat.addWebMessageListener(
    webView,
    "androidAdsjs",
    Set.of("*"),
    new WebMessageListener() {
    @Override
    public void onPostMessage(WebView view, WebMessageCompat message, Uri sourceOrigin, boolean isMainFrame, JavaScriptReplyProxy replyProxy) {
    // Handle the message here
    String data = message.getData();
    // Process the message and send response via replyProxy.postMessage()
    }
    }
    );

    The JavaScript side uses postMessage() to send messages, which the native side receives through the WebMessageListener. Responses from the native side are delivered through addEventListener on the captured handler.

    Index

    Constructors

    • Parameters

      • params: { debug: boolean; objectName: string; target: Record<string, any> }
        • debug: boolean
        • objectName: string

          the object name for addWebMessageListener

        • target: Record<string, any>

      Returns AndroidAdsjsMessagingConfig

    Properties

    _capturedHandler:
        | null
        | {
            addEventListener: (
                type: string,
                listener: (event: MessageEvent) => void,
            ) => void;
            postMessage: (message: string) => void;
        }
    debug: boolean
    objectName: string
    target: Record<string, any>

    Methods

    • Parameters

      • fn: (...args: any[]) => any
      • Optionalcontext: string = 'none'

      Returns any

    • Send an initial ping message to the platform to establish communication. This is a fire-and-forget notification that signals the JavaScript side is ready. Only sends in top context (not in frames) and if the messaging interface is available.

      Parameters

      Returns boolean

      true if ping was sent, false if in frame or interface not ready