Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Sep 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Javascript reflective reference

    If i am right than every object is under window.
    i wrote this code

    for(i in window)
    {
    document.write(i+"<br/>");
    }
    it returns me every defined object

    but there is some object which is created by constructor and they are not returned

    if("WebSocket" in window)

    how can i get these
    what else could be missing

  • #2
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,292
    Thanks
    10
    Thanked 583 Times in 564 Posts
    some folks say you can't.

    here is a wise-guy workaround, but it's more of a joke than a real tool.
    still, it might find a couple of surprises for you. window.dump? hehe
    or it may hang your browser.
    grab a sandwich check back later.


    yes, it's a brute-force object detector. i'd never seen one before. this is just a proof of concept. i want a scalable distributed version.
    Code:
    var mystery=window; //"hello world"  false [1,2,3]  
    var d1=+new Date;
    r=[""];for(var i2=97; i2< 123;i2++){r.push(String.fromCharCode(i2));}
    r[-1]="";
    var s=r.join("");
    var s0=0, s1=0, s2=0, s3=0, s4=0, s5=0, s6=0, s7=0, s8=0, n=27;
    window.p={};
    
     window.k6="";
     window.k4="";
     var k1, k2, k3, k5;
     c=[0,0,0,0,0,0,0,0,0,0];
    
    function runOne(){
     var i=0, x;
    for(s0=c[0];s0<n;++s0){
    for(s1=c[1];s1<n;s1++){ 
    for(s2=c[2];s2<n;s2++){
    for(s3=c[3];s3<n;s3++){ 
     if(i++>19){ c=[s0,s1?s1-1:0, s2?s2-1:0,s3?s3-1:0,0,0,0];  report(); return setTimeout(runOne, 750);  }
    for(s4=c[4];s4<n;s4++){ k4=r[s0]+r[s1]+r[s2]+r[s3]+r[s4];
    for(s5=c[5];s5<n;s5++){ k5=k4+r[s5];
    for(s6=c[6];s6<n;s6++){ k6=k5+r[s6];
      if( (x=mystery[k6])!==undefined ) { p[k6]=String(x); }
    }}}}}}}
    
     
     var d2= +new Date;
     console.log(document.title="done. found "+Object.keys(p).length +" properties in "+ (d2-d1)+"ms" );
    
    }//end runOne()
    
    function report(){
        document.body.innerHTML="<pre>"+JSON.stringify(p, null, "\t");
    }
    
    runOne()
    it misses anything with a number or uppercase letter. that slows it down even more...

    after about 30 seconds, it had found this when run on this page using firebug:
    Code:
      {
    	"c": "0,0,0,0,0,0,0,0,0,0",
    	"i": "16",
    	"n": "27",
    	"p": "[object Object]",
    	"r": ",a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z",
    	"s": "abcdefghijklmnopqrstuvwxyz",
    	"ltr": "left",
    	"top": "[object Window]",
    	"who": "function who(A) {\n    return openWindow(\"misc.php?\" + SESSIONURL + \"do=whoposted&t=\" + A, 230, 300);\n}",
    	"atob": "function atob() {\n    [native code]\n}",
    	"back": "function back() {\n    [native code]\n}",
    	"blur": "function blur() {\n    [native code]\n}",
    	"btoa": "function btoa() {\n    [native code]\n}",
    	"dump": "function dump() {\n    [native code]\n}",
    	"eval": "function eval() {\n    [native code]\n}",
    	"find": "function find() {\n    [native code]\n}",
    	"gapi": "[object Object]",
    	"home": "function home() {\n    [native code]\n}",
    	"java": "[Java Package \"java\"]",
    	"name": "",
    	"open": "function open() {\n    [native code]\n}",
    	"self": "[object Window]",
    	"stop": "function stop() {\n    [native code]\n}"
    }

    i wrote this just this weekend. this one is 7 chars, and the code got away from me, so it's a little sloppy. the 4 chars was fast and stable, but limited.

    real answer: i think firebug is easier.
    Last edited by rnd me; 09-06-2012 at 09:08 PM.
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/5/28) IE7:0.1, IE8:5.3, IE11:8.4, IE9:3.2, IE10:3.2, FF:18.2, CH:46, SF:7.9, NON-MOUSE:32%

  • #3
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,603
    Thanks
    0
    Thanked 645 Times in 635 Posts
    The for..in statement will never process anything flagged as not enumerable. This includes many of the predevined properties and methods in the built in objects (such as window).

    The latest version of JavaScript provides more direct access to the se types of settings so that you can now create your own properties and methods that will have that flag set and so be skipped over by for..in
    Last edited by felgall; 09-06-2012 at 10:28 PM.
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •