Capture values of an input in Delphi (Tchromium)

Asked

Viewed 876 times

2

Good morning,

I’m using HTML, CSS and Javascript to set up a chat layout for a messaging system I’m developing. Until then, everything ok, only due to a limitation problem I found in Twebbrowser, I migrated to Tchromium, the same made it easier for me in several aspects, however, I’m doubtful how I can be getting the values of an input in my HTML code. With Twebbrowser I used the following code arqID := Browser.OleObject.Document.All.Tags('input').item('arqID').value;

Does anyone know the equivalent function I used in Twebbrowser (Oleobject) in Tchromium?

inserir a descrição da imagem aqui

@EDIT: I gave up trying to use this method to get the values, I was able to do what I wanted using the URL manipulation in the browser, and through a Chromium event that notices the URL change (Addresschange), I pull the current URL (Chromium1.Browser.MainFrame.Url;) and with the POS filter the values I want to take.

  • I already answered something similar, but there was no iteration with the author of the question: https://answall.com/questions/373648/como-pega-os-valores-dos-id-ao-click-do-bot%C3%a3o-html-no-tchromium-cef4delphi

1 answer

0

Create a js function that can be triggered by a click on a button or any other medium.

In Delphi you need to register an extension in the Browser. Ex.

type
  TCustomRenderProcessHandler = class(TCefRenderProcessHandlerOwn)
  protected
    procedure OnWebKitInitialized; override;
  end;

  TExtensao = class
    class procedure click(const data: string);
  end;

For class TExtensao note that we have a procedure called click, this will be the contact with the js function, we are creating nothing more or less than a System between Delphi and Browser.

Procedure click:

class procedure TExtensao.click(const data: string);
var
  vMensagem : ICefProcessMessage;
begin
  // Registra o Evento Click para a Extensão que criamos anteriormente
  vMensagem := TCefProcessMessageRef.New('click');
  vMensagem.ArgumentList.SetString(0, data);
  TCefv8ContextRef.Current.Browser.SendProcessMessage(PID_BROWSER, vMensagem);
end;

Now, we need to inform the system that on startup it captures the events of the Browser, then in the boot section add:

initialization 
...     
  // Inicializa virtualmente os Procedimentos externos do Navegador que no caso vai registrar o TExtension}
  CefRenderProcessHandler  := TCustomRenderProcessHandler.Create;
  CefBrowserProcessHandler := TCefBrowserProcessHandlerOwn.Create;

The component TChromium has an event called ProcessMessageReceived, here every time the Istener click is triggered in the Navigator informs TCustomRenderProcessHandler that we registered earlier that a new message has arrived.

You can filter events through the parameter message. something like:

if message.Name = 'click' then
  message.ArgumentList.GetString(0)

As his name says message.ArgumentList is a list of arguments sent by the Navigator, and at position 0 contains the result of the js function that was created to pick the values of the desired fields.

Edit.

Missing implementation of the app Registration:

{ TCustomRenderProcessHandler }
procedure TCustomRenderProcessHandler.OnWebKitInitialized;
begin
  // Registra uma Extensão chamada "app" para o evento "click"
  TCefRTTIExtension.Register('app', TExtensao);
end;
  • I left for the suggestion you gave me, however, I believe I am doing it wrong. In the following code if message.Name = 'click' then&#xA; message.ArgumentList.GetString(0) , instead of bringing the value, I specified a Showmessage, to see if it was being called, but nothing happens. Created the simple js function in html: <script type="text/javascript">&#xA; function GetArqID(ID) {&#xA; alert('ALERT: TESTE')&#xA; return ID;&#xA; }&#xA; </script> , giving this call <input type="button" name="arqID" value="Teste" onclick="GetArqID(30);">

  • this Alert inside the js was to check if the function was being called, until then, all right, the same problem is time to perform the functions inside the Delphi

  • now it’s enough to execute the action you want. Something like: Chromium.Browser.MainFrame.ExecuteJavaScript('id_do_botao.click();', 'about:blank', 0);

  • I did the following, in my own class where I own the Tchromium component, I implemented the code you gave me, I created the function I specified up there, I ran the debug, and the minute I click on the button where the JS call is made, nothing happens in Delphi, it is as if the Processmessagereceived event is not being called

  • you do not click on HTML, you run the click by Delphi... through the Tchromium function ExecuteJavaScript

  • ah, but run an action in HTML from Delphi I know, the question is what kind, I’m creating a chat system in which I use Tchromium with an HTML I created to be the chat, when I send a file, i Rewrite the Primary key of the specified file inside the HTML next to its name, passing the <a></a> tags, I want to click on the name of the file, which is rendered inside the Tchromium, execute an action in Delphi in which I can pull a value (Primary key of the file) for Delphi to download it, I will post some prints in my post above

  • posted the print, from here is the following, when clicking on the name there Chat.PNG, Delphi somehow detect the click on it and I pull the value of some particular location, before I did the following, through the </a> tags, I changed the URL by passing the ID, and through the property chrChat.Browser.Mainframe.Url, I was able to bring the value in the event of Onaddresschange, the problem is that the page is changing kkk, then it was the chat, in Twebbrowser, I use only <a href= "#"> not to change the page, changed an invisible input and took with Oleoject the input value

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.