不允许枚举跨原点对象-对于对象数组中的(i in x)

Not allowed to enumerate cross origin objects - for(i in x) in array of objects

本文关键字:对象 数组 in 枚举 于对象 不允许 原点      更新时间:2023-09-26

我不明白。我做了一个简单的递归函数来搜索我传递给它的任何对象。
除非我尝试给它一个窗口属性的数组(或对象)作为第一个参数,否则它会像我预期的那样工作。

window.search_object([window.API, window.Chat], /vote/);  //Error: Not allowed to enumerate cross origin objects
window.search_object({x:window.API, y:window.Chat}, /vote/);  //Error: Not allowed to enumerate cross origin objects

错误发生在for(var i in object)的线路上。

对象搜索功能:

window.search_object = function(object, search_string, max_recursion, search_value, history, path) {
    //This will be returned in the end
    var results = [];
    //Just ignore document object
    if(object == window.document)
      return [];
    //Only created in level 1 function, others get this passed by parameter
    if(history==null)
      history = {data:[]};
    //Simplest way to find the origin of found values
    if(path==null)
      path = [];
    //Convert sring to regext (I'm lazy)
    if(typeof search_string=="string") {
      search_string = new RegExp(search_string);
    }        
    //Some errors
    if(!(search_string instanceof window.RegExp)) {
      console.log([search_string, search_string instanceof window.RegExp])
      throw new Error("search_object() -> Parameter 2 (searched string) must be a string or RegExp object.");    
    }
    if(typeof object != "object") {
      throw new Error("search_object() -> Parameter 1 (object to search in) must be a Javascript object");    
    }
    //Loop
    for(var i in object) {   //<-------THIS IS THE ERROR LINE
        //Test if the property name matches
        if(search_string.test(i)) {
          //Add in results (no duplicate fallback here)
          results.push([object[i], path.concat(i)]);
        }
        //Recursion
        if(typeof object[i]=="object") {
          //Check if we did not parse this object already
          if(history.data.find(object[i])===false) {
            //Remember the object
            history.data.push(object[i]);
            //Add results from all sub-objects
            results = results.concat(window.search_object(object[i], search_string, null, null, history, path.concat(i)));
          }
        }
    }
    //console.log(results);
    return results;
}
Array.prototype.find = function(needle) {
    for(var i=0; i<this.length; i++) {
       if(this[i]==needle)
         return i;
    }
    return false;
} 

窗口对象具有循环引用。这将导致等待堆栈溢出。