Question

I want to find out if the TChromiumFMX browser component contains text that the user has selected, and if so, retrieve that text, without the user having to copy it to the clipboard (ctrl-c) first.


To improve on TLama's answer: If you're not using ShowMessage, the anonymous procedure will not always have completed before Button1Click is exited, hence often yielding no results (or too late). Therefore a Done := true as the last line of the procedure can be checked for to see if the value has been retrieved:

procedure TForm1.Button1Click(Sender: TObject);
var Done: boolean;
begin
  Done := false;
  Chromium1.Browser.GetFocusedFrame.VisitDomProc(
    procedure(const document: ICefDomDocument)
    begin
      SelectedText := document.SelectionAsText;
      Done := true
    end
  );
  while not Done do Application.ProcessMessages
end;
Was it helpful?

Solution

You will have to visit DOM, and as a gift you'll receive a reference to the current ICefDomDocument document interface. The ICefDomDocument interface then offers the SelectionAsText method, which returns the current selection as text (if any). In code you may write something like:

procedure TForm1.Button1Click(Sender: TObject);
begin
  Chromium1.Browser.GetFocusedFrame.VisitDomProc(
    procedure(const document: ICefDomDocument)
    begin
      ShowMessage(document.SelectionAsText);
    end
  );
end;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top