delphichromium-embeddedtchromium

How make a click on a chromium browser link open in the default browser?


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.


Solution

  • 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;