<div dir="ltr"><div>Hello.</div><div><br></div><div>I have a project to analyze how scripts on web pages interact with the browser window and document objects. I can only analyze them. I can&#39;t modify them. Most things are fairly obvious, but I have come up against a couple of items that I can&#39;t find answers for anywhere. If there is documentation that I have missed, I would be eternally grateful if someone could point me in the right direction. The project will be written in Java.<br></div><div><br></div><div>Given this evaluation:</div><div><br></div><div>Value value = context.eval(&quot;js&quot;, &quot;var w = 5;&quot;);</div><div><br></div><div>value will be of type undefined and value.hasMembers will return false;</div><div>Is there any way to bring the value of w back to Java?</div><div><br></div><div>This leads into the second question.</div><div><br></div><div>In a browser, the window object is actually a pseudonym for the global scope. Therefore</div><div><br></div><div>var w = 5;</div><div><br></div><div>can be functionally equivalent to</div><div><br></div><div>window.w = 5;</div><div><br></div><div>How does the javascript polyglot line things up between a weakly typed language like javascript and a strongly typed one like Java?</div><div><br></div><div>I can define a Window class in Java and give it a member variable w</div><div><br></div><div>class Window {</div><div>    public int w;</div><div>}</div><div><br></div><div>But then, because this is perfectly valid javascipt</div><div><br></div><div>window.w = &#39;hello&#39;;</div><div><br></div><div>w obviously can&#39;t be typed as an integer.<br></div><div><br></div><div>And how would I handle this case?</div><div><br></div><div>window.x = {</div><div>     number: 4,</div><div>    letter: &#39;6&#39;,</div><div>    string: &quot;hello&quot;,</div><div>    method: (() =&gt; a = b)<br></div><div>}</div><div><br></div><div>I could define x of type Object, but I would have no idea how to access the members. And obviously if I don&#39;t know what variables will be created beforehand, I can&#39;t declare them.<br></div><div><br></div><div>I have also run into nightmares like this</div><div><br></div><div>if (window.z === undefined) {</div><div>    window.z = [];</div><div>    window.z.a = { variableOne: &quot;something&quot;, variableTwo: &quot;something else&quot;};</div><div>    window.z.push({variableThree: &quot;another thing&quot;, variableFour: &quot;yet another thing&quot;});</div><div>}</div><div><br></div><div>There may be (I hope) a simple solution and I just haven&#39;t been able to find it. Any help is appreciated.<br></div><div><br></div><div><br></div></div>