3

I want to achieve that when a user clicks on a hyperlink inside a TChromium browser page, the new page opens in his default browser.

Domus
  • 1,263
  • 7
  • 23
  • 3
    In the `OnBeforeBrowse` event check if the `navType` equals to `NAVTYPE_LINKCLICKED` and if so, return True to the `Result` parameter (which will cancel the request for Chromium) and call e.g. `ShellExecute` passing the `request.Url`. – TLama Sep 02 '14 at 15:28
  • 2
    That is brilliant. Why didn't you post this as answer? Too unworthy for you? :) – Domus Sep 02 '14 at 15:56

2 Answers2

4

In the OnBeforeBrowse event check if the navType parameter equals to NAVTYPE_LINKCLICKED and if so, return True to the Result parameter (which will cancel the request for Chromium) and call e.g. ShellExecute passing the request.Url value to open the link in the user's default browser:

uses
  ShellAPI, ceflib;

procedure TForm1.Chromium1BeforeBrowse(Sender: TObject;
  const browser: ICefBrowser; const frame: ICefFrame; const request: ICefRequest;
  navType: TCefHandlerNavtype; isRedirect: boolean; out Result: Boolean);
begin
  if navType = NAVTYPE_LINKCLICKED then
  begin
    Result := True;
    ShellExecuteW(0, nil, PWideChar(request.Url), nil, nil, SW_SHOWNORMAL);
  end;
end;
TLama
  • 75,147
  • 17
  • 214
  • 392
  • 1
    mabybe add an "else Result := False" ? – Domus Sep 03 '14 at 11:40
  • 1
    Better might be `Result := False` at the beginning of the method, but it's not necessary since False is the [`initial result`](https://code.google.com/p/delphichromiumembedded/source/browse/trunk/src/cefvcl.pas#644). – TLama Sep 03 '14 at 11:51
  • 2
    You're right of course, but a nilled out parameter is more of an implied situation, with Delphi. :) – Domus Sep 03 '14 at 12:05
4

In CEF3, navType = NAVTYPE_LINKCLICKED is no longer possible in the OnBeforeBrowse event, as in TLama's answer. Instead, I discovered how to detect this using the TransitionType property...

procedure TfrmEditor.BrowserBeforeBrowse(Sender: TObject;
  const browser: ICefBrowser; const frame: ICefFrame;
  const request: ICefRequest; isRedirect: Boolean; out Result: Boolean);
begin
  case Request.TransitionType of
    TT_LINK: begin
      // User clicked on link, launch URL...
      ShellExecuteW(0, nil, PWideChar(Request.Url), nil, nil, SW_SHOWNORMAL);
      Result:= True;
    end;
    TT_EXPLICIT: begin
      // Source is some other "explicit" navigation action such as creating a new
      // browser or using the LoadURL function. This is also the default value
      // for navigations where the actual type is unknown. Do nothing.
    end;
  end;
end;
Community
  • 1
  • 1
Jerry Dodge
  • 26,858
  • 31
  • 155
  • 327