/
sandbox.coffee
38 lines (29 loc) · 1.1 KB
/
sandbox.coffee
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
window.sandbox = (options = {}) ->
options = $.extend true, {}, {
html: '', css: '', js: ''
external: { js: {}, css: {} }
dialogs: true
onLog: (->)
}, options
{ js, html, css, external } = options
iframe = $('<iframe seamless sandbox="allow-scripts allow-forms allow-top-navigation allow-same-origin">').appendTo(options.el || 'body')[0]
doc = iframe.contentDocument || iframe.contentWindow.document
stopDialogs = "var dialogs = ['alert', 'prompt', 'confirm']; for (var i = 0; i < dialogs.length; i++) window[dialogs[i]] = function() {};"
scripts = [js]
unless options.dialogs
scripts = [stopDialogs].concat scripts
allScripts = ("(function() { #{script} })();" for script in scripts).join ''
externalJS = for src in external.js
"<script src='#{src}'></script>"
externalCSS = for src in external.css
"<link rel='stylesheet' type='text/css' href='#{src}' media='screen' />"
doc.open()
doc.write """
#{html}
#{externalJS.join ''}
#{externalCSS.join ''}
<script>#{allScripts}</script>
<style>#{css}</style>
"""
doc.close()
return iframe