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.
If you find these projects useful please consider becoming a sponsor with Patreon, GitHub or Liberapay.

RetrieveText does not fire ChromiumTextResultAvailable

Post Reply
PioPio
Posts: 42
Joined: Sun Nov 05, 2017 10:25 pm

RetrieveText does not fire ChromiumTextResultAvailable

Post by PioPio »

Hello,

I have another problem in retrieving the text of a website.

My application pulls some links from a database, one link at a time, then it runs LoadUrl, next it should retrieve the webpage text and finally move to the next link. The following is the source:

this is the main method where the current link is retrieved from a db and the url is given to Chromium

Code: Select all

function TMainForm.Scan: Boolean;
var a:string;
begin
  try
    Result := True;
    DB.StatisticsOpen;
    DB.StatisticsFirst;
    while not DB.StatisticEOF do
    begin
      a:=DB.FindLink;//retrieve link from database table
      LoadUrl(a);//open link
      DB.StatisticsNext;//next link in database table
    end;
  except
  end;
end;
This is the method that opens the link and should fire ChromiumTextResultAvailable

Code: Select all

function TMainForm.LoadUrl(const a: string): Boolean;
begin
  Chromium.LoadURL('http://'+a);
  Chromium.RetrieveText;//this does not trigger ChromiumTextResultAvailable
  Result:=True;
end;
I also tried and implemented this code in LoadEnd but still no ChromiumTextResultAvailable is triggered.

Code: Select all

procedure TMainForm.ChromiumLoadEnd(Sender: TObject; const browser: ICefBrowser;
  const frame: ICefFrame; httpStatusCode: Integer);
begin
  if frame = nil then
    exit;

  if httpStatusCode = 200 then
  begin
    Chromium.RetrieveText;//this method is not fired
  end;
end;

the application retrieves all the links correctly, fires the event LoadUrl and RetrieveText but ChromiumTextResultAvailable is not fired.

What am I doing wrong ?

Many thanks
Alberto
User avatar
salvadordf
Posts: 4564
Joined: Thu Feb 02, 2017 12:24 pm
Location: Spain
Contact:

Re: RetrieveText does not fire ChromiumTextResultAvailable

Post by salvadordf »

Hi,

CEF uses multiple threads and processes, and many functions in TChromium are asynchronous to improve UI responsiveness.

TChromium.LoadURL is asynchronous and you have to wait for the TChromium.OnLoadEnd event to know when the document is loaded.

However, one document can have several frames and each of them will trigger the TChromium.OnLoadEnd event so you need to check frame.IsMain to know when the main frame is fully loaded.

You also need to know that all TChromium events are called from a different thread and in some cases, even a different process.

To play it safe, you should send a customized message to the main form when this event is triggered and let the function associated to that message in the main form call the TChromium.RetrieveText function.

I forgot to tell you that the MiniBrowser demo has a new context menu option that uses the TChromium.RetrieveText function to copy the displayed text to the clipboard.
Post Reply