For AI agents: the complete documentation index is available at /next/llms.txt, the full documentation bundle is available at /next/llms-full.txt, and this page is available as Markdown at /next/api/lynx-devtool-native-api/cdp-event-listener.md.
Lynx
  • English
  • CDPEventListener

    A protocol (or interface) used to receive CDP Event messages sent by DevTool.

    Syntax

    iOS

    LynxBaseInspectorOwner.h
    @protocol CDPEventListener <NSObject>
    
    @required
    - (void)onEvent:(nonnull NSString *)event;
    
    @end

    Android

    CDPEventListener.java
    public interface CDPEventListener {
      void onEvent(String event);
    }

    Parameters

    • event: A JSON string containing the CDP Event content.

    Data Format

    FieldTypeDescription
    methodstringEvent name, please refer to CDP
    paramsobjectEvent parameters, please refer to CDP

    For example, the data for Page.screencastFrame is as follows:

    {
      "method": "Page.screencastFrame",
      "params": {
        "data": "...",
        "metadata": {
          "deviceHeight": 200.0,
          "deviceWidth": 100.0,
          "offsetTop": 0.0,
          "pageScaleFactor": 1.0,
          "scrollOffsetX": 0.0,
          "scrollOffsetY": 0.0
        }
      }
    }

    Usage Example

    Steps

    1. Implement CDPEventListener.
    2. Get the LynxBaseInspectorOwner instance.
    3. Call the addCDPEventListener method to add the message listener.

    Code Example

    The following example adds a CDP Event listener named TestListener and sends a Page.startScreencast message via invokeCDPFromSDK to enable screen capture functionality.

    As a result, the TestCDPEventListener will receive Page.screencastFrame message.

    iOS

    TestCDPEventListener.h
    #import <Lynx/LynxBaseInspectorOwner.h>
    
    @interface TestCDPEventListener : NSObject <CDPEventListener>
    
    @property(nonatomic, readonly, nonnull) NSString *name;
    
    - (instancetype)initWithName:(NSString * _Nonnull )name;
    
    @end
    TestCDPEventListener.m
    #import "TestCDPEventListener.h"
    
    @implementation TestCDPEventListener
    
    - (instancetype)initWithName:(NSString *)name {
      if (self = [super init]) {
        _name = name;
      }
      return self;
    }
    
    - (void)onEvent:(nonnull NSString *)event {
      NSLog(@"onEvent: %@", event);
    }
    
    @end
    ViewController.m
    #import <Lynx/LynxView.h>
    #import <Lynx/LynxBaseInspectorOwner.h>
    
    #import "TestCDPEventListener.h"
    
    @interface LynxViewLifecycleClient : NSObject <LynxViewLifecycle>
    @property(nonatomic, weak) LynxView* lynxView;
    @end
    
    @implementation LynxViewLifecycleClient {
      TestCDPEventListener* _listener;
    }
    
    - (void)lynxView:(LynxView *)view didLoadFinishedWithUrl:(NSString *)url {
      [self addCDPEventListener:@"TestListener"];
      __weak __typeof(self) weakSelf = self;
      dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(10 * NSEC_PER_SEC)),
                     dispatch_get_main_queue(), ^{
                       __strong __typeof(weakSelf) strongSelf = weakSelf;
                       [strongSelf removeCDPEventListener:@"TestListener"];
                     });
    }
    
    - (void)addCDPEventListener:(NSString *)name {
      __strong LynxView* view = self.lynxView;
      id<LynxBaseInspectorOwner> owner = [view baseInspectorOwner];
      _listener = [[TestCDPEventListener alloc] initWithName:name];
      [owner addCDPEventListener:name withListener:_listener];
      NSString *method = @"{\"id\":1,\"method\":\"Page.startScreencast\",\"params\":{\"format\":\"jpeg\",\"mode\":\"fullscreen\"}}";
      [owner invokeCDPFromSDK:method withCallback:^(NSString *result){
      }];
    }
    
    - (void)removeCDPEventListener:(NSString *)name {
      __strong LynxView* view = self.lynxView;
      id<LynxBaseInspectorOwner> owner = [view baseInspectorOwner];
      [owner removeCDPEventListener:name];
    }
    
    @end
    
    @implementation ViewController {
      LynxViewLifecycleClient *_client;
    }
    
    - (void)viewDidLoad {
      [super viewDidLoad];
    
      // ...
    
      _client = [LynxViewLifecycleClient new];
      _client.lynxView = lynxView;
      [lynxView addLifecycleClient:_client];
    }
    
    @end

    Android

    TestCDPEventListener.java
    import android.util.Log;
    
    import com.lynx.devtoolwrapper.CDPEventListener;
    
    public class TestCDPEventListener implements CDPEventListener {
      private final String name;
    
      public TestCDPEventListener(String name) {
        this.name = name;
      }
    
      public String getName() {
        return name;
      }
    
      @Override
      public void onEvent(String event) {
        Log.i("TestCDPEventListener", "onEvent: " + event);
      }
    }
    MainActivity.java
    import android.os.Handler;
    import android.os.Looper;
    
    import com.lynx.devtoolwrapper.LynxBaseInspectorOwner;
    import com.lynx.tasm.LynxView;
    import com.lynx.tasm.LynxViewClient;
    
    import java.lang.ref.WeakReference;
    
    public class MainActivity extends Activity {
      private TestCDPEventListener mTestCDPEventListener = null;
    
      @Override
      protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // ...
    
        lynxView.addLynxViewClient(new LynxViewClient() {
          @Override
          public void onLoadSuccess() {
            addCDPEventListener(lynxView, "TestListener");
          }
        });
      }
    
      private void addCDPEventListener(LynxView lynxView, String name) {
        LynxBaseInspectorOwner owner = lynxView.getBaseInspectorOwner();
        if (owner == null) {
          return;
        }
        mTestCDPEventListener = new TestCDPEventListener(name);
        owner.addCDPEventListener(name, mTestCDPEventListener);
    
        String method = "{\"id\":1,\"method\":\"Page.startScreencast\",\"params\":{\"format\":\"jpeg\",\"mode\":\"fullscreen\"}}";
        owner.invokeCDPFromSDK(method, result -> {
        });
    
        Handler mainHandler = new Handler(Looper.getMainLooper());
        WeakReference<LynxBaseInspectorOwner> weakOwner = new WeakReference<>(owner);
        mainHandler.postDelayed(() -> {
          LynxBaseInspectorOwner strongOwner = weakOwner.get();
          strongOwner.removeCDPEventListener(name);
        }, 10000);
      }
    }

    Compatibility

    LCD tables only load in the browser

    Except as otherwise noted, this work is licensed under a Creative Commons Attribution 4.0 International License, and code samples are licensed under the Apache License 2.0.