如果網頁處理window.onbeforeunload事件,則此消息由基礎MSHTML引擎顯示。通常,這是出於某種原因,讓用戶知道他/她的輸入尚未保存或提交。當頁面使用addEventListener("beforeonload", handler)
或attachEvent("onbeforeunload", handler)
時,the answer you linked的提示抑制腳本不起作用。我不認爲有這樣做的可靠方法,而不訴諸低級別的Windows掛鉤。
[更新]下面的腳本(尋找「注入這個腳本」)是積極抑制頁面自身的處理程序onbeforeunload
事件一劈,通過setInterval
。它應該在99%的情況下工作,但它仍然留下一個空白,讓頁面在導航離開之前覆蓋onbeforeonload
。
<!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代碼,我沒有。
它更可能你可以TEmbeddedWB,該IShellDocView/InternetExplorer的改進版它有很多比TWebBrowser包裝更多的功能做到這一點。 –
爲什麼不能瀏覽的用戶點擊按鈕? –