I'm working on a software to verify the problem of porting it from D5 to XE5 . In D5 , TWebBrowser.BeforeNavigate2
was call each time the user click on the submit button of a displayed form. In XE5, it's not the case. I figured out it is because the URL for the submit contain http:/aDirectory/ExecToBeCall.exe
. If I add an extra / after: the event is fire.
Under D5 the URL is change for:
http ://localhost/aDirectory/ExecToBeCall.exe
(space added to break the link in the post)
That behavior of TWebBrowser
under D5 to fire anyway and change the URL is important for the software and I cannot change the HTML (about 2000 files) to contain 2. It allowed us to know if the submit was made inside Delphi or from a outside Browser. I tried other and newer events of TWebBrowser
and none are fire.
How can I be informed of a problematic URL, check it and change it into a localhost URL? A small and clean method would be preferable.
Thanks for your help and suggestions
TWebBrowser
is just a thin wrapper around Internet Explorer's ActiveX object, so it is IE itself, not TWebBrowser
, that is behaving differently.
http:/aDirectory/ExecToBeCall.exe
is actually a valid URL. Since the :
is not followed by //
, there is no authority portion in the URL, and thus no explicit hostname. localhost
is used as an implicit hostname, and the path is /aDirectory/ExecToBeCall.exe
. That is what the URL is being changed to in D5, which is correct behavior. Changing the URL to http://aDirectory/ExecToBeCall.exe
is incorrect, as that creates an authority portion of the URL and thus the hostname is explicitly set to aDirectory
and the path is set to /ExecToBeCall.exe
, which is not what you want.
Why the URL is not changing in XE5, I have no clue. Sounds like a bug in whatever version of IE is being used in that version of TWebBrowser
.
In any case, it is IE that is triggering the event, so if it is not triggering for a URL it does not like, there is nothing you can do about that, short of using the browser's DOM interfaces to handle the onsubmit
event of the HTML webform directly.
If you want to redirect unexpected url instead of navigating to it, you can start with TEmbeddedWB project or you can DIY by extending TWebBrowser
class with IDocHostUIHandler
, which has an interesting method TranslateURL
.
function TAdvWebBrowser.TranslateURL(const dwTranslate: DWORD; const pchURLIn: POLESTR; var ppchURLOut: POLESTR): HRESULT;
var
Url: string;
BufferSize: Integer;
begin
Url := PChar(pchURLIn);
if GetSafeUrlFor(Url) then
begin
ppchURLOut := CoTaskMemAlloc(BufferSize);
CopyMemory(ppchURLOut, PChar(Url), BufferSize);
// redirects to new location
Result := S_OK;
end
else
// no redirection
Result := S_FALSE;
end;
// You can change the function to add more complex redirection rules
function GetSafeUrlFor(var Url: string): Boolean;
begin
Result := Url.EndsWithText('.exe');
if Result then
Url := 'http://localhost/';
end;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.