Created
March 4, 2009 07:24
-
-
Save subtleGradient/73756 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" | |
| "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> | |
| <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> | |
| <head> | |
| <meta http-equiv="Content-type" content="text/html; charset=utf-8" /> | |
| </head> | |
| <body onload="init();"> | |
| <script type="text/javascript" charset="utf-8"> | |
| var Sandbox = (function(){ | |
| // public | |
| function Sandbox(name){ | |
| this.window = makeIframe.call(this, name); | |
| }; | |
| Sandbox.prototype = { | |
| loadScript: function(url){ | |
| loadInto(url, this.window); | |
| }, | |
| registerScript: function(object){ | |
| console.log('registerScript'); | |
| if (object && object.eval) this.eval = object.eval.eval; | |
| } | |
| }; | |
| Sandbox.sandboxes = {}; | |
| // private | |
| function makeIframe(name){ | |
| console.log('makeIframe'); | |
| var thisSandbox = Sandbox.sandboxes[name] = this; | |
| var iframe = document.createElement("iframe"); | |
| iframe.style.display = "none"; | |
| document.body.appendChild(iframe); | |
| thisSandbox.iframe = iframe; | |
| thisSandbox.window = frames[frames.length - 1]; | |
| // write a script into the <iframe> and register | |
| thisSandbox.window.document.write( | |
| "<script>\ | |
| window.Sandboxed = parent.Sandbox.sandboxes['"+name+"'].registerScript;\ | |
| var MSIE/*@cc_on =1@*/;\ | |
| parent.Sandbox.sandboxes['"+name+"'].registerScript({ 'eval': MSIE?this:{eval:function(s){return eval(s)}} });\ | |
| <\/script>" | |
| ); | |
| thisSandbox.window.document.close(); | |
| return thisSandbox.window; | |
| }; | |
| function loadInto(url, win){ | |
| console.log('loadInto'); | |
| win = win || window; | |
| var script = win.document.createElement('script'); | |
| script.src = url; | |
| win.document.body.appendChild(script); | |
| }; | |
| return Sandbox; | |
| })(); | |
| function init(){ | |
| // var iframe = makeIframe('flarm'); | |
| // loadInto('iframe_scoped_tests.js', iframe); | |
| var mySandbox = new Sandbox('mySandbox'); | |
| mySandbox.loadScript('sandbox_tests.js'); | |
| console.log( eval('document.getElementsByTagName("*")').length ); | |
| console.log( mySandbox.eval('document.getElementsByTagName("*")').length ); | |
| console.log( mySandbox.eval('document.body.appendChild(document.createElement("div"));document.getElementsByTagName("*")').length ); | |
| var mySandbox1 = new Sandbox('mySandbox1'); | |
| mySandbox1.loadScript('sandbox_tests.js?123'); | |
| console.log( eval('document.getElementsByTagName("*")').length ); | |
| console.log( mySandbox1.eval('document.getElementsByTagName("*")').length ); | |
| }; | |
| </script> | |
| </body> | |
| </html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Sandboxed({ | |
| test1: function(){ | |
| return document.getElementsByTagName('*').length; | |
| } | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment