问题
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.
回答1:
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;
回答2:
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;
来源:https://stackoverflow.com/questions/25626707/how-make-a-click-on-a-chromium-browser-link-open-in-the-default-browser