我正在尝试覆盖 MSHTML 实例上的计时器方法(setTimeout
、和) clearTimeout
,因此我可以跟踪已添加到/的函数并阻止脚本退出,直到这些函数被调用或删除。setInterval
clearInterval
setTimeout
setInterval
在包装器函数中调用原始文件时,出现Permission denied错误:
setTimeout
setTimeout
var window = new ActiveXObject('htmlfile').parentWindow.window;
var _setTimeout = window.setTimeout;
var timeouts = {};
setTimeout = function (code, ms, lang) {
//script fails here - Permission denied
var id = _setTimeout(code, ms, lang || 'javascript');
timeouts[id] = true;
return id;
};
setTimeout(function () {
window.alert('Timed function');
}, 1000);
var testTimeouts = function () {
var i;
for (i in timeouts) {
if (timeouts[i]) {return false;}
}
};
while (!testTimeouts()) {
WScript.Sleep(100);
}
window.alert('At end of script');
setTimeout
我可以在它被覆盖之前
将代码传递给原始代码:
window.setTimeout(function () {
window.alert('Timed function');
}, 1000);
WScript.Sleep(2500);
window.alert('At end of script');
保留原始上下文(如似乎已经消失的答案中所建议的那样)不起作用:
window._setTimeout = window.setTimeout;
var timeouts = {};
window.setTimeout = function (code, ms, lang) {
var id = window._setTimeout(code, ms, lang || 'javascript');
timeouts[id] = true;
return id;
};
window.setTimeout(function () {
window.alert('Timed function');
}, 1000);
它在调用window._setTimeout
with时失败Object doesn't support this property or method
。
我应该注意我正在运行 IE8、WinXP SP3、JScript 5.8。