禁止在Delphi TWebbrowser控件中弹出“您确定要离开此页面" [英] Suppress the “are you sure you want to leave this page” popup in the Delphi TWebbrowser control

查看:77
本文介绍了禁止在Delphi TWebbrowser控件中弹出“您确定要离开此页面"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Delphi应用程序,它使用 TWebbrowser 组件自动导航到我们拥有的另一个Web应用程序.

I have an Delphi application that uses TWebbrowser component to automate navigation to another web application we have.

我的问题是,有时IE会显示臭名昭著的确定要离开此页面"消息,并且发生这种情况时,除非用户单击离开此页面"按钮,否则我的应用程序无法导航到其他页面.不幸的是,我无法编辑网站代码来删除此警告.

My problem is that sometimes IE shows the infamous 'Are you sure you want to leave this page' message and when this happens, my app can't navigate to another pages unless an user clicks on 'Leave this page' button. I can't edit the website code to remove this warning, unfortunately.

此消息困扰了我的应用数周,而我再也找不到合适的解决方案了.我要做的是让后台进程在显示此窗口时手动发送击键,但这不是一个好的解决方案,因为在我的应用程序运行时没有人可以使用计算机.

This message is plaguing my app for weeks, and I could not reach to a proper solution anymore. What I did is to keep a background process do manually send a keystroke when this window is show, but this is not a good solution because nobody can use the computer while my app is working.

在下面的主题中,我看到了C#的可能解决方案,但我需要使用Delphi代码.

I saw possible solution for C# in the topic below but I need a Delphi code instead.

任何帮助都非常感谢.

谢谢:)

推荐答案

如果网页处理您链接的答案在页面使用 addEventListener(" beforeonload,handler) attachEvent(" onbeforeunload,handler)的情况下不起作用.我认为,如果不求助于低级的Windows钩子,就没有可靠的方法.

This message is shown by the underlying MSHTML engine if the web page handles window.onbeforeunload event. Usually, it's there for a reason, to let the user know his/her input hasn't been saved or submitted yet. The prompt suppression script from the answer you linked doesn't work for cases when the page uses addEventListener("beforeonload", handler) or attachEvent("onbeforeunload", handler). I don't think there's a reliable way of doing this, without resorting to low-level Windows hooks.

[UPDATE] 以下脚本(查找注入此脚本")是一种黑客,它通过主动抑制 onbeforeunload 事件的页面自身处理程序setInterval .它应该可以在99%的情况下正常工作,但是在浏览之前,页面仍然有一个空白可以覆盖 onbeforeonload .

[UPDATE] The following script (look for "Inject this script") is a hack which aggressively suppresses the page's own handlers for onbeforeunload event, via setInterval. It should work in 99% of cases, but it still leaves a gap for the page to override onbeforeonload right before navigating away.

<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <title></title>
    <script type="text/javascript">
        window.attachEvent("onbeforeunload", function (ev) {
            window.event.returnValue = "onbeforeunload via window.attachEvent()";
        });

        window.addEventListener("beforeunload", function (ev) {
            window.event.returnValue = "onbeforeunload via window.addEventListener()";
        });

        window.onbeforeunload = function (ev) {
            window.event.returnValue = "onbeforeunload via window.onbeforeunload";
        };
    </script>

    <script type="text/javascript">
        //
        // Inject this script
        //
        (function () {
            var onbeforeunloadHandler = function (ev) {
                if (ev) {
                    if (ev.stopPropagation)
                        ev.stopPropagation();
                    if (ev.stopImmediatePropagation)
                        ev.stopImmediatePropagation();
                    ev.returnValue = undefined;
                }
                window.event.returnValue = undefined;
            }

            var handler = null;
            var intervalHandler = function () {
                if (handler)
                    window.detachEvent("onbeforeunload", handler);

                // window.attachEvent works best
                handler = window.attachEvent("onbeforeunload", onbeforeunloadHandler);
                // handler = window.addEventListener("beforeunload", onbeforeunloadHandler);
                // handler = window.onload = onbeforeunloadHandler;
            };

            window.setInterval(intervalHandler, 500);
            intervalHandler();
        })();
    </script>

</head>
<body>
    <a href="http://www.example.com">Go away</a>
</body>
</html>

要使用Delphi注入此脚本,您可能需要使用低级的 WebBrowser/MSHTML COM接口,例如 IWebBrowser2 IHTMLDocument2 IHTMLScriptElement ,其方式与链接答案中的操作非常相似.经过更多的努力,也可以仅使用 IDispatch :: GetIDsOfNames IDispatch :: Invoke 进行后期绑定来完成此操作.如果您要确切的Delphi代码,我没有.

To inject this script with Delp you'd probably need to resort to low-level WebBrowser/MSHTML COM interfaces, like IWebBrowser2, IHTMLDocument2, IHTMLScriptElement, in a very similar way it's done in the linked answer. With some more efforts, the same can also be done via late binding, using IDispatch::GetIDsOfNames and IDispatch::Invoke only. If you're asking for exact Delphi code, I don't have one.

这篇关于禁止在Delphi TWebbrowser控件中弹出“您确定要离开此页面"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆