【问题标题】:How make a click on a chromium browser link open in the default browser?如何在默认浏览器中打开铬浏览器链接?
【发布时间】:2014-09-02 15:10:05
【问题描述】:

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

【问题讨论】:

  • OnBeforeBrowse 事件中检查navType 是否等于NAVTYPE_LINKCLICKED,如果是,则向Result 参数返回True(这将取消对Chromium 的请求)并调用例如ShellExecute 传递 request.Url
  • 太棒了。你为什么不把这个作为答案发布?太不配你了? :)

标签: delphi chromium-embedded tchromium


【解决方案1】:

OnBeforeBrowse 事件中检查navType 参数是否等于NAVTYPE_LINKCLICKED,如果是,则对Result 参数返回True(这将取消对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;

【讨论】:

  • 可能添加一个“else Result := False”?
  • 在方法的开头最好是Result := False,但这不是必需的,因为False是initial result
  • 你当然是对的,但是对于 Delphi,一个 nilled out 参数更多的是一种隐含的情况。 :)
【解决方案2】:

在 CEF3 中,navType = NAVTYPE_LINKCLICKEDOnBeforeBrowse 事件中不再可能,就像 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;

【讨论】:

    猜你喜欢
    • 2012-08-11
    • 1970-01-01
    • 2012-07-18
    • 1970-01-01
    • 2018-05-02
    • 2011-07-26
    • 2018-10-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多