在调用htmlfile窗口的原始setTimeout时“权限被拒绝”

时间:2012-11-11 13:05:26

标签: javascript mshtml wsh

我正在尝试覆盖MSHTML实例上的计时器方法(setTimeoutclearTimeoutsetIntervalclearInterval),因此我可以跟踪已添加到的函数setTimeout / setInterval并阻止脚本退出,直到调用或删除这些函数。

在包装器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._setTimeoutObject doesn't support this property or method的通话失败。

我应该注意到我正在运行IE8,WinXP SP3,JScript 5.8。

1 个答案:

答案 0 :(得分:1)

为什么不从存储的窗口中调用setTimeout

var setTimeout;
var timeouts = {};
(function() {
    var window = new ActiveXObject('htmlfile').parentWindow;
    setTimeout = function(code, delay, lang){
        var id = window.setTimeout(code, delay, lang || 'javascript');
        timeouts[id] = true;
        return id;
    };
})();