Lynx

invokeCDPFromSDK

Sends a CDP Method to DevTool and asynchronously receives the result.

This can be used simultaneously with the DevTool desktop application without interfering with each other.

Syntax

iOS

LynxBaseInspectorOwner.h
- (void)invokeCDPFromSDK:(NSString *)msg withCallback:(CDPResultCallback)callback;

Android

LynxBaseInspectorOwner.java
void invokeCDPFromSDK(final String cdpMsg, final CDPResultCallback callback);

Parameters

  • msg / cdpMsg: The message to send, a JSON string formatted as a CDP Method.
  • callback: A callback that conforms to the CDPResultCallback definition, used to receive the result.

Request Data Format

A JSON-formatted string.

FieldTypeDescription
idnumberMessage ID
methodstringMethod name, please refer to CDP
paramsobjectMethod parameters, please refer to CDP

For example, the request data for DOM.getDocument is as follows:

{
  "id": 1,
  "method": "DOM.getDocument",
  "params": {}
}

Usage Example

Implementation Steps

  1. Implement CDPResultCallback.
  2. Get the LynxBaseInspectorOwner instance.
  3. Call the invokeCDPFromSDK method to send the message.

Code Example

The following example code demonstrates the complete process of calling the DOM.getDocument CDP Method after the LynxView has successfully loaded.

iOS

ViewController.m
#import <Lynx/LynxView.h>
#import <Lynx/LynxBaseInspectorOwner.h>

@interface LynxViewLifecycleClient : NSObject <LynxViewLifecycle>
@property(nonatomic, weak) LynxView* lynxView;
@end

@implementation LynxViewLifecycleClient

- (void)lynxView:(LynxView *)view didLoadFinishedWithUrl:(NSString *)url {
  [self invokeCDPMethod];
}

- (void)invokeCDPMethod {
  __strong LynxView* view = self.lynxView;
  id<LynxBaseInspectorOwner> owner = [view baseInspectorOwner];
  NSString *method = @"{\"id\":1,\"method\":\"DOM.getDocument\",\"params\":{}}";
  [owner invokeCDPFromSDK:method withCallback:^(NSString* result){
           NSLog(@"CDPResultCallback: %@", result);
         }];
}

@end

@implementation ViewController {
  LynxViewLifecycleClient *_client;
}

- (void)viewDidLoad {
  [super viewDidLoad];

  // ...

  _client = [LynxViewLifecycleClient new];
  _client.lynxView = lynxView;
  [lynxView addLifecycleClient:_client];
}

@end

Android

MainActivity.java
import android.util.Log;

import com.lynx.devtoolwrapper.CDPResultCallback;
import com.lynx.devtoolwrapper.LynxBaseInspectorOwner;
import com.lynx.tasm.LynxView;
import com.lynx.tasm.LynxViewClient;

public class MainActivity extends Activity {

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // ...

    lynxView.addLynxViewClient(new LynxViewClient() {
      @Override
      public void onLoadSuccess() {
        invokeCDPMethod(lynxView);
      }
    });
  }

  private void invokeCDPMethod(LynxView lynxView) {
    LynxBaseInspectorOwner owner = lynxView.getBaseInspectorOwner();
    if (owner == null) {
      return;
    }
    String method = "{\"id\":1,\"method\":\"DOM.getDocument\",\"params\":{}}";
    owner.invokeCDPFromSDK(method, new CDPResultCallback() {
      @Override
      public void onResult(String result) {
        Log.i("MainActivity", "CDPResultCallback: " + result);
      }
    });
  }
}

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.