Re: Developer Tools
Posted: 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.
The forum for BriskBard users and CEF4Delphi / WebView4Delphi / WebUI4Delphi / WebUI4CSharp developers
http://www.briskbard.com/forum/
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
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);
});
}
})();
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