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.

Back and Forward

Hitman
Posts: 73
Joined: Sat May 20, 2017 11:08 am

Back and Forward

Post by Hitman »

Hi guys,
I use the following for "LoadingStateChange"

Code: Select all

  btnPrevious.Enabled := canGoBack; { enable this to activate the button }
  btnForward.Enabled := canGoForward; { enable this to activate the button }
But,
how should i do when i need to keep my "Previous" button disabled before the browser has load anything?
What is the procedure of chromium for that, is it "OnBeforeBrowse" or something else?

I have "Previous", "Forward" and "Refresh" buttons and i would like to be able to control those buttons accordingly.

Thanks!
User avatar
salvadordf
Posts: 4067
Joined: Thu Feb 02, 2017 12:24 pm
Location: Spain
Contact:

Re: Back and Forward

Post by salvadordf »

The TChromium.OnLoadingStateChange event is triggered multiple times when you load a web page. Take a look at the MiniBrowser demo to see an example.

All the demos disable all navigation controls until the browser is initialized and then they enable the controls and load the homepage which triggers TChromium.OnLoadingStateChange.
Hitman
Posts: 73
Joined: Sat May 20, 2017 11:08 am

Re: Back and Forward

Post by Hitman »

Hi Salvador,
could you please check this?
Here is the code behind "LoadingStateChange" on MiniBrowser demo

Code: Select all

procedure TMiniBrowserFrm.Chromium1LoadingStateChange(Sender: TObject;
  const browser: ICefBrowser; isLoading, canGoBack, canGoForward: Boolean);
begin
  if not(Chromium1.IsSameBrowser(browser)) or FClosing then exit;

  // This event is executed in a CEF thread and this can cause problems when
  // you change the 'Enabled' and 'Visible' properties from VCL components.
  // It's recommended to change the 'Enabled' and 'Visible' properties
  // in the main application thread and not in a CEF thread.
  // It's much safer to use PostMessage to send a message to the main form with
  // all this information and update those properties in the procedure handling
  // that message.

  BackBtn.Enabled    := canGoBack;
  ForwardBtn.Enabled := canGoForward;

  if isLoading then
    begin
      StatusBar1.Panels[0].Text := 'Loading...';
      ReloadBtn.Enabled         := False;
      StopBtn.Enabled           := True;
    end
   else
    begin
      StatusBar1.Panels[0].Text := 'Finished';
      ReloadBtn.Enabled         := True;
      StopBtn.Enabled           := False;
    end;
end;
And here is code behind "LoadingStateChange" on my browser

Code: Select all

{ - <<< - [ handling navigation events... ] - >>> - }
procedure TfrmMain.chromiumMainLoadingStateChange(Sender: TObject;
  const browser: ICefBrowser; isLoading, canGoBack, canGoForward: Boolean);
begin
  if not(chromiumMain.IsSameBrowser(browser)) then
    Exit;

  btnBack.Enabled := canGoBack; { enable this to activate the button }
  btnForward.Enabled := canGoForward; { enable this to activate the button }

  if isLoading then
  begin
    statusbarMain.Panels[0].text := 'Loading...';
    btnRefresh.Enabled := False; { enable this to activate the button }
  end
  else
  begin
    statusbarMain.Panels[0].text := 'Finished';
    btnRefresh.Enabled := True; { enable this to activate the button }
  end;
end;
I see no difference but still,
when the browser is executed, the "Previous/Back" button is usable and it takes me to a blank white page with no content in it (named as "about:blank") in both my browser and also MiniBrowser.
Any ideas?

What i need to do is to prevent going back if nothing has been navigated.

Thanks!
User avatar
salvadordf
Posts: 4067
Joined: Thu Feb 02, 2017 12:24 pm
Location: Spain
Contact:

Re: Back and Forward

Post by salvadordf »

TChromium.DefaultUrl is 'about:blank' by default and it's the first URL loaded by the browser as soon as it's created. In your case, you load your homepage after 'about:blank' so the browser can go back from your homepage to 'about:blank'.

To avoid this, set TChromium.DefaultUrl to the URL of your homepage before calling TChromium.CreateBrowser.
The FullScreenBrowser demo has an example about this.
Hitman
Posts: 73
Joined: Sat May 20, 2017 11:08 am

Re: Back and Forward

Post by Hitman »

Hi Salvador,
honestly,
i am a bit lost here :(
This is what we have in the full screen browser as default url,

Code: Select all

procedure TMainForm.FormShow(Sender: TObject);
begin
  Chromium1.DefaultUrl := 'https://www.google.com';
My point here is,
the blank page which is shown.
I simply do not understand why browser have to show a blank page if it is set to LoadUrl ('www.darkside.org'); and why should it go back to that blank page.
The idea is to get rid of that blank page.

Please keep in mind,
i am not setting the URL by typing it into the code, the URL is set on "FORMSHOW" event automatically and read from an xml file.


Thanks!
User avatar
salvadordf
Posts: 4067
Joined: Thu Feb 02, 2017 12:24 pm
Location: Spain
Contact:

Re: Back and Forward

Post by salvadordf »

TChromium.DefaultURL is the first web page loaded when the browser is created. If you call TChromium.LoadURL after the browser's creation then it will be the second web page loaded.

That list of URLs visited by the browser could go like this if you visit several URLs :

Code: Select all

DefaultURL (1st URL) <-> LoadURL (2nd URL) <-> LoadURL (3rd URL) <-> LoadURL (4th URL) <-> ...
The back a forward buttons will load the previous or next URL in that list and they will go back to the TChromium.DefaultURL because it was the first URL to be visited.

TChromium.DefaultURL exists because the internal CEF3 function to create and initialize a browser has a URL parameter to load a web page as soon as it's created.

Many CEF3 wrappers set that URL parameter to 'about:blank' but if your app knows that it has to load a certain URL then set TChromium.DefaultURL accordingly.
User avatar
salvadordf
Posts: 4067
Joined: Thu Feb 02, 2017 12:24 pm
Location: Spain
Contact:

Re: Back and Forward

Post by salvadordf »

I forgot...

If you set TChromium.DefaultURL you won't need to call TChromium.LoadURL to load the homepage when the browser is created because it's already loaded.
Hitman
Posts: 73
Joined: Sat May 20, 2017 11:08 am

Re: Back and Forward

Post by Hitman »

Sorry,
yes but how do you exactly set an automatic URL which you are supposed to read from an xml file?
I have used your OSR sample where i have the following,
Here is "OnShow"

Code: Select all

  { loading the user interface (default url) }
  chromiumMain.LoadURL(frmRequired.leditUserInterface.text);
And this is what you have in the OSR demo which i have used as it was by adding necessary parameters,

Code: Select all

{ - <<< - [ browser create events... ] - >>> - }
procedure TfrmMain.BrowserCreatedMsg(var aMessage: TMessage);
begin
  Caption := 'My Browser';
  chromiumMain.SendFocusEvent(False);
  chromiumMain.CustomHeaderName := 'Cache-Control';
  chromiumMain.CustomHeaderValue := 'no-cache';
  chromiumMain.LoadURL(frmRequired.leditUserInterface.text);
end;
If i change the "LoadUrl" to "DefaultUrl" it loads nothing. It gives only empty white page.

These 2 are the only pieces of code where i load the URL.
I am loading the same URL also when user is pressing "Home" button.

So,
how do i get rid of the blank page?

Thanks!
User avatar
salvadordf
Posts: 4067
Joined: Thu Feb 02, 2017 12:24 pm
Location: Spain
Contact:

Re: Back and Forward

Post by salvadordf »

TChromium.DefaultURL is just a property that is used inside TChromium.CreateBrowser. Your URL will be loaded asynchronously during the CreateBrowser procedure.

The TForm.OnShow event should be like this :

Code: Select all

procedure TForm1.FormShow(Sender: TObject);
begin
  if chromiumMain.Initialized then
    begin
      chromiumMain.WasHidden(False);   // This is necessary if your app was hidden and it's shown again
      chromiumMain.SendFocusEvent(True);  // This is necessary if your app was hidden and it's shown again
    end
   else
    begin
      // opaque white background color
      chromiumMain.Options.BackgroundColor := CefColorSetARGB($FF, $FF, $FF, $FF);
      chromiumMain.CustomHeaderName := 'Cache-Control';
      chromiumMain.CustomHeaderValue := 'no-cache';
      chromiumMain.DefaultURL := frmRequired.leditUserInterface.text;
      
      if chromiumMain.CreateBrowser(nil, '') then
        chromiumMain.InitializeDragAndDrop(Panel1)
       else
        Timer1.Enabled := True;
    end;
end;
The BrowserCreatedMsg procedure shouldn't load your homepage because it will be loaded inside CreateBrowser.
Hitman
Posts: 73
Joined: Sat May 20, 2017 11:08 am

Re: Back and Forward

Post by Hitman »

Okay,
should i delete this?
"BrowserCreatedMsg"
It was with the OSR browser demo.


Many thanks!!
Post Reply