问题描述
我正在开发一种软件,以验证将其从 D5 移植到 XE5 的问题.在 D5 中,每次用户单击显示的表单的提交"按钮时,都会调用 TWebBrowser.BeforeNavigate2
.在XE5中并非如此.我发现这是因为提交的URL包含 http:/aDirectory/ExecToBeCall.exe
.如果在之后添加一个额外的/,则表示该事件为火.
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.
在D5下,URL更改为:
Under D5 the URL is change for:
http://localhost/aDirectory/ExecToBeCall.exe
(添加了空格以断开帖子中的链接)
http ://localhost/aDirectory/ExecToBeCall.exe
(space added to break the link in the post)
在 D5 下的 TWebBrowser
仍然可以触发并更改URL的行为对于该软件很重要,而我不能更改 HTML (关于2000个文件)包含2.它使我们知道提交是在Delphi内部还是从外部浏览器进行的.我尝试了 TWebBrowser
的其他新事件,但没有一个发生.
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.
如何通知我有问题的URL,将其检查并将其更改为本地URL?小型且清洁的方法将是更可取的.
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.
感谢您的帮助和建议
推荐答案
如果要重定向意外的URL而不是导航至该URL,可以从TEmbeddedWB项目开始,也可以通过扩展 TWebBrowser
进行DIY.带有 IDocHostUIHandler
的类,该类具有有趣的方法 TranslateURL
.
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;
这篇关于TWebBrowser和URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!