Disclosure Statement: This site contains affiliate links, which means that I may receive a commission if you make a purchase using these links. As an eBay Partner, I earn from qualifying purchases.

Developer Tools

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

Re: Developer Tools

Post by coater »

I couldn't understand fromIndex and toIndex. I think they are the char index of the search results, but it seems wrong.
shobits1
Posts: 25
Joined: Wed Mar 04, 2020 9:16 pm

Re: Developer Tools

Post by shobits1 »

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: 135
Joined: Sat Sep 29, 2018 1:51 pm

Re: Developer Tools

Post by coater »

:lol: Yes,it is !
Thank you very much!
would you like to share your send function? Thank you very much!
shobits1
Posts: 25
Joined: Wed Mar 04, 2020 9:16 pm

Re: Developer Tools

Post by shobits1 »

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: 135
Joined: Sat Sep 29, 2018 1:51 pm

Re: Developer Tools

Post by coater »

Thank you very much! :)
shobits1
Posts: 25
Joined: Wed Mar 04, 2020 9:16 pm

Re: Developer Tools

Post by shobits1 »

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