0

我会简短地说:

我想枚举对象内部的所有可用函数和对象,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";
        }
    }
}
4

1 回答 1

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>
于 2012-05-23T17:04:35.227 回答