CodingForums.com

CodingForums.com (http://www.codingforums.com/index.php)
-   JavaScript programming (http://www.codingforums.com/forumdisplay.php?f=2)
-   -   Javascript reflective reference (http://www.codingforums.com/showthread.php?t=272412)

wolfblood 09-06-2012 06:43 PM

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

rnd me 09-06-2012 08:59 PM

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.

felgall 09-06-2012 10:22 PM

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


All times are GMT +1. The time now is 06:03 AM.

Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.