如何在默认浏览器中打开Chrome浏览器链接?

时间:2014-09-02 15:10:06

标签: delphi chromium-embedded tchromium

我想实现这一点,当用户点击TChromium浏览器页面内的超链接时,新页面将在其默认浏览器中打开。

2 个答案:

答案 0 :(得分:4)

OnBeforeBrowse事件中检查navType参数是否等于NAVTYPE_LINKCLICKED,如果是,则返回True到Result参数(这将取消Chromium的请求)并呼吁例如ShellExecute传递request.Url值以打开用户默认浏览器中的链接:

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;

答案 1 :(得分:2)

在CEF3中,navType = NAVTYPE_LINKCLICKED事件中不再可能OnBeforeBrowse,就像在TLama的答案中一样。相反,我发现了如何使用TransitionType属性...

来检测这一点
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;