Firefox在这个简单的枚举上抛出安全错误1000

时间:2012-05-23 16:39:31

标签: javascript firefox cross-browser enumeration

我要简短:

我想枚举window对象内的所有可用函数和对象,以创建某种对象反射代码。它在每个浏览器中运行得很好但在Firefox中运行良好。 这是我的伪循环代码:

var all_names=[];
for (var i in window)
{
    //if it's NOT an object
    all_name.push(i.toString());
    //if it's an object        
    enum up to 3 more levels in child objects.
}

我不想在Firefox中使用可用的API,例如getOwnPropertyNames 所以我该怎么做?是否有更好的Javascript枚举解决方案(当然是跨浏览器)

<小时/> 以下是一些更多技术信息:

确切的Firefox错误:

[20:48:04.539] uncaught exception: [Exception... "Security error" code: "1000" nsresult: "0x805303e8 (NS_ERROR_DOM_SECURITY_ERR)" location: "http://localhost/test/common.js Line: 53"]

精确的枚举循环代码:

function reflectAsString() {
    try{
        var m1 = "";
        var m2 = "";
        var m3 = "";
        for (var i in window) {
            if(window[i] && window[i]!= null && window[i] != "globalStorage")
                {
                m1 += i;
                first_instance = window[i];
                if(typeof first_instance == "object")
                    {
                    for(var j in first_instance)
                    {
                        if(first_instance[j] && first_instance[j]!= null)
                            {
                            m2 += j;
                            second_instance = first_instance[j];
                            if(typeof second_instance == "object")
                                {
                                for (var k in second_instance)
                                {
                                    if(second_instance[k] && second_instance[k]!= null)
                                        {
                                        m3 += k;
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
        return hex_md5(hex_md5(m1)+hex_md5(m2)+hex_md5(m3));
    } catch(e) {
        try {
            var strToHash = Object.getOwnPropertyNames(window).filter(function(property) {
                return typeof window[property] == 'function';
            });
            return hex_md5(strToHash.toString());
        } catch(e2) {
            return "undef";
        }
    }
}

1 个答案:

答案 0 :(得分:1)

通过在我感兴趣的页面上打开iframe,并将iframe窗口中的全局变量与父窗口中的全局变量进行比较,我做了类似的事情。 如果有任何错误,try块将返回带有错误标志的属性名称。

iframe src的HTML:

<html lang= "en">
<head>
<meta charset= "utf-8">
<title>Get Globals</title>
<style>
p{border:none;font-size:1.25em;font-weight:600;}
h2{color:navy;border-top:3px ridge navy;margin:1ex 0;}
span{margin:0 1em;}
</style>
<script>
navigator.sayswho= (function(){
    var N= navigator.appName, ua= navigator.userAgent, tem,
    ie= navigator.IEmod,
    M= ua.match(/(opera|chrome|safari|firefox|msie)\/?\s*([\d\.]+)/i);
    if(M && (tem= ua.match(/version\/([\.\d]+)/i))!= null) M[2]= tem[1];
    M= M? [M[1], M[2]]:[N, navigator.appVersion, '-?'];
    if(ie && ie!= M[1]) M[2]= 'ie mode:'+navigator.IEmod;
    return M.join(' ');
})();
window.onload= function(){
    if(this!= top){
        var G={
            getInterface:1, InstallTrigger:1
        },
        B= [], C= [], k= ['HTML elements:'], d= navigator.sayswho || '',
        t= document.getElementsByTagName('p');
        for(var p in this) G[p]= 1;
        for(var key in top){
            try{
                if(!(key in G)){
                    if(top[key].nodeType== 1) k.push(key);
                    else B.push(key);
                }
                else C.push(key);
            }
            catch(er){
                B.push('error with '+key);
            }
        }
        if(k.length>1) B.push(k.join(' '));
        if(d) document.getElementsByTagName('span')[0].innerHTML+= d;
        t[0].innerHTML= B.join('<br>');
        t[1].innerHTML= C.sort().join(', ');
    }
}
</script>
</head>
<body>
<h1>Globals<span>in</span></h1>
<h2>New globals defined in the top window</h2>
<p></p>
<h2>Common window properties</h2>
<p></p>
</body>
</html>