Created
March 4, 2009 08:47
-
-
Save subtleGradient/73772 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
| /* | |
| SubtleSandbox | |
| Copyright 2009 Thomas Aylott (subtleGradient.com) | |
| MIT License | |
| */ | |
| var SubtleSandbox = (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>\ | |
| var MSIE/*@cc_on =1@*/;\ | |
| window.Sandboxed = function(object){ var Sandboxed = this.eval('parent.Sandbox.sandboxes[\""+name+"\"]'); Sandboxed.registerScript.call(Sandboxed, object); Sandboxed = null; };\ | |
| window.Sandboxed({ 'eval': MSIE?this:{eval:function(s){return eval(s)}} });\ | |
| <\/script>" | |
| ); | |
| thisSandbox.window.document.close(); | |
| return thisSandbox.window; | |
| }; | |
| function loadScript(url, win){ | |
| // console.log('loadInto'); | |
| win = win || window; | |
| var script = win.document.createElement('script'); | |
| script.src = url; | |
| win.document.body.appendChild(script); | |
| }; | |
| return Sandbox; | |
| })(); | |
| /* INFO: | |
| * loadScript is asynchronous | |
| you can't be sure yours loadScripts have fired | |
| unless you write defensive code in your evals or | |
| use `Sandboxed()` in your scripts to register them your sandbox | |
| The original goal of this thing was to make it easy to create a custom slickspeed. | |
| Load each jslib into its own iframe with its own tests. Then let the tests inform the main js runner that it's ready to go. | |
| */ | |
| /* USAGE: | |
| var mySandbox = new SubtleSandbox('mySandbox'); | |
| mySandbox.loadScript('url_to_file.js'); | |
| mySandbox.eval('doSomething();'); | |
| // In `url_to_file.js` | |
| Sandboxed({ | |
| 'test1': function(){} | |
| }); | |
| */ | |
| /* TODO: | |
| TODO: flesh out the registerScript functionality | |
| TODO: allow some sort of delayedEval that fires once the registerScript has loaded and fires a callback? | |
| TODO: integrate jslib Events? | |
| */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment