Developer Tools

coater
Posts: 81
Joined: Sat Sep 29, 2018 1:51 pm

Re: Developer Tools

Post by coater » Thu Mar 26, 2020 4:49 am

I couldn't understand fromIndex and toIndex. I think they are the char index of the search results, but it seems wrong.

shobits1
Posts: 11
Joined: Wed Mar 04, 2020 9:16 pm

Re: Developer Tools

Post by shobits1 » Thu Mar 26, 2020 12:46 pm

ok, I think I find way to get the nodes, try this:

Code: Select all

Send({ id: 1, method: "DOM.enable" });

Send({ id: 2, method: 'DOM.getDocument', params: { depth: -1, pierce: true} }); //if you don't do getDocument you'll get nodeId = 0 when you call getSearchResults

performSearch = Send({ id: 2, method: 'DOM.performSearch', 
                              params: { query: '-webkit-input-placeholder', includeUserAgentShadowDOM : true}  });

SearchResults =  Send({ id: 3, method: "DOM.getSearchResults", 
                               params: {searchId: performSearch.result.searchId, fromIndex: 0, toIndex: performSearch.result.resultCount} });

nodeIds will be at : searchResults.result.nodeIds

coater
Posts: 81
Joined: Sat Sep 29, 2018 1:51 pm

Re: Developer Tools

Post by coater » Fri Mar 27, 2020 3:22 am

:lol: Yes,it is !
Thank you very much!
would you like to share your send function? Thank you very much!

shobits1
Posts: 11
Joined: Wed Mar 04, 2020 9:16 pm

Re: Developer Tools

Post by shobits1 » Fri Mar 27, 2020 4:38 pm

I use this to simplify things,, but keep in mind I'm not expert in js:

Code: Select all

'use strict';

var CDP;
if (!CDP)
  CDP = {};
 
(function() {
  let ws;
  
  CDP.GetDebuggerUrl = async function(host, port){
    let sUrl = 'http://' + host + ':' + port + '/json/list';
    let response = await fetch( sUrl );
    let json = await response.json();
    
    if ( !Array.isArray(json) )
      return '';
    
    let i;
    for (i = 0; i < json.length; i++){
      if( (typeof(json[0].url) != 'string') || json[i].url == '' )
        continue;
      
      if( json[i].url.includes('devtools://') )
        continue; // skip devtools tab
      
      return json[i].webSocketDebuggerUrl;
    }
    
    return '';
  }
  
  CDP.wait_ws_connection = function(ws, timeout){
    return new Promise(function (resolve, reject){
      let isConnected = (ws.readyState == 1);
      
      if (timeout <= 0)
        resolve(isConnected);
           
      let loop500 = setInterval(function (){
        timeout = timeout - 500;
        isConnected = (ws.readyState == 1);

        if( (isConnected == true) || (timeout <= 0 ) ){
          clearInterval(loop500);
          resolve(isConnected);
        }
      }, 500);
    });  
  }
  
  CDP.Attach = async function(host, port){
    if( (typeof(this.ws) == 'object') && (this.ws instanceof WebSocket) ){
      if( this.ws.readyState == 1 )
        return true;  // return if ws is connected
    }
    
    this.ws = null;
    let sUrl = await this.GetDebuggerUrl(host, port);
    if ( sUrl != '' ){
      try{
        this.ws = new WebSocket(sUrl);
        return await CDP.wait_ws_connection(this.ws, 2000);
      }catch (e){
        return false;
      }      
    }
  }
  
  CDP.CheckConnected  = function(){
    if( (typeof(this.ws) == 'object') && (this.ws instanceof WebSocket) ){
      if( this.ws.readyState == 1 )
        return true;
      else
        return false;
    }else
      return false;    
  }
  
  
  CDP.Send = async function(JMsg){
    let Msg = JSON.stringify(JMsg);  
    let ws = this.ws;
    return new Promise( function(resolve){
      ws.addEventListener('message', function (text) {         
        //console.log('Message from server ', text.data);
        let jsonData = JSON.parse(text.data);
        if (JMsg.id == jsonData.id){
          this.removeEventListener('message', this);
          resolve(jsonData);
        }
      });
      ws.send(Msg);
    });
  }
})();
Copy and paste it on console then:

Code: Select all

// to connect 
connected = await CDP.Attach('127.0.0.1', 9222); // 9222 is the port

await CDP.Send({ id: 1, method: "DOM.enable" });

await CDP.Send({ id: 2, method: 'DOM.getDocument', 
                        params: { depth: -1, pierce: true} });

performSearch = await CDP.Send({ id: 2, method: 'DOM.performSearch', 
                                        params: { query: '-webkit-input-placeholder', includeUserAgentShadowDOM : true}
                              });
// .
// .
// .
// have fun

coater
Posts: 81
Joined: Sat Sep 29, 2018 1:51 pm

Re: Developer Tools

Post by coater » Sat Mar 28, 2020 4:06 am

Thank you very much! :)

shobits1
Posts: 11
Joined: Wed Mar 04, 2020 9:16 pm

Re: Developer Tools

Post by shobits1 » Sat Mar 28, 2020 1:01 pm

you welcome :)

BTW,, keep in mind that the code doesn't display messages when event triggered. he only return the result of the method called.

Post Reply