-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathsandie.js
324 lines (272 loc) · 9.47 KB
/
sandie.js
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
/*!
* Sandie
* github.com/premasagar/sandie
*
*//*
Load and isolate JavaScript variables, by injecting scripts into a temporary iframe element.
by Premasagar Rose
dharmafly.com
license
opensource.org/licenses/mit-license.php
v0.2
*/
var sandie = (function(window){
'use strict';
// DEPENDENCIES
/*
* getScript
* github.com/premasagar/mishmash/tree/master/getscript/
*
*/
function getScript(srcs, callback, options){
/**
* Load a script into a <script> element
* @param {String} src The source url for the script to load
* @param {Function} callback Called when the script has loaded
*/
function single(src, callback, options){
var
charset = options.charset,
keep = options.keep,
target = options.target,
document = target.document,
head = document.getElementsByTagName('head')[0],
script = document.createElement('script'),
loaded;
script.type = 'text/javascript'; // Needed for some gitchy browsers, outside of HTML5
script.charset = charset;
script.onload = script.onreadystatechange = function(){
var state = this.readyState;
if (!loaded && (!state || state === 'complete' || state === 'loaded')){
// Handle memory leak in IE
script.onload = script.onreadystatechange = null;
// Remove script element once loaded
if (!keep){
head.removeChild(script);
}
loaded = true;
callback.call(target);
}
};
// Async loading (extra hinting for compliant browsers)
script.async = true;
// Apply the src
script.src = src;
// And go...
head.appendChild(script);
}
// **
/**
* Load array of scripts into script elements.
*
* Note, there is only one callback function here, called after each is loaded
*
* @param {Array} srcs array of source files to load
* @param {Function} callback
*/
function multiple(srcs, callback, options){
var
length = srcs.length,
loaded = 0,
checkIfComplete, i;
// Check if all scripts have loaded
checkIfComplete = function(){
if (++loaded === length){
callback.call(options.target);
}
};
// Doesn't call callback until after all scripts have loaded
for (i = 0; i < length; i++){
single(srcs[i], checkIfComplete, options);
}
}
// **
var window = this,
method = (typeof srcs === 'string') ? single : multiple;
options = options || {};
if (!options.charset){
options.charset = 'utf-8';
}
if (!options.target){
options.target = window;
}
callback = callback || function(){};
return method.call(this, srcs, callback, options);
}
/* end getScript */
// end DEPENDENCIES
// **
var document = window.document;
// Check if an object is an array
function isArray(obj){
return Object.prototype.toString.call(obj) === "[object Array]" || obj.constructor === Array || obj instanceof Array;
}
// Get the body element of the host document
function hostBody(){
return document.getElementsByTagName('body')[0];
}
// Get an lookup hash of own properties from an object
function ownProps(obj){
var
lookup = {},
prop;
for (prop in obj){
// deliberately not using hasOwnProperty, as this is not implemented against the window object in some older browsers
lookup[prop] = true;
}
return lookup;
}
// Get a hash of key-value pairs from obj1 that are not in obj2
function varDiff(obj, lookup){
var
vars = {},
prop;
for (prop in obj){
if (!lookup[prop]){
vars[prop] = obj[prop];
}
}
return vars;
}
// **
function Sandie(){
this.init.apply(this, arguments);
}
Sandie.prototype = {
// whether iframe should auto-remove after first run
persist: false,
// initialise the sandbox
init: function(scripts, callback, persist){
var
self = this,
iframe = this.iframe = document.createElement('iframe'),
body = hostBody();
if (!body){
throw 'Sandie: no host DOM';
}
iframe.style.display = 'none';
body.appendChild(iframe);
return self.flush()
.load(scripts, callback, persist);
},
// Determine the properties of the iframe window
props: function(){
return ownProps(this.window());
},
// Determine which vars are new since the iframe was last created or flushed
vars: function(){
var self = this;
return varDiff(self.window(), self._initProps);
},
// Load scripts, execute functions & attach objects to the sandboxed window; callback any new vars on completion
load: function(scripts, callback, persist){
var self = this,
target = self.window(),
loaded = 0,
length, checkIfComplete, fnKey, i, script;
if (!scripts){
return self;
}
if (!target){
throw 'Sandie: already closed';
}
if (typeof persist === 'boolean'){
self.persist = persist;
}
if (!isArray(scripts)){
scripts = [scripts];
}
length = scripts.length;
callback = callback || function(){};
// Check if all scripts have loaded
checkIfComplete = function(){
if (++loaded === length){
callback.call(self, self.vars());
if (!self.persist){
self.close();
}
}
};
// Doesn't call callback until after all scripts have loaded
for (i = 0; i < length; i++){
script = scripts[i];
if (typeof script === 'string'){
// eval: experimental; this will be made more sophisticated in future
if (script.indexOf('<script>') === 0){
self.write({head:script});
checkIfComplete();
}
// load external scripts
else {
getScript(script, checkIfComplete, {target: target});
}
}
else if (typeof script === 'function'){
script.call(target);
checkIfComplete();
}
else if (typeof script === 'object'){
for (fnKey in script){
if (script.hasOwnProperty(fnKey)){
target[fnKey] = script[fnKey];
}
}
checkIfComplete();
}
}
return self;
},
// return the iframe's window
window: function(){
return this.iframe.contentWindow;
},
// return the iframe's document
document: function(){
return this.window().document;
},
// return the iframe's <head> element
head: function(){
return this.document().getElementsByTagName('head')[0];
},
// return the iframe's <body> element
body: function(){
return this.document().body;
},
html: function(options){
options = options || {};
return (options.doctype || '<!doctype html>') +
'<html>' +
'<head>' + (options.head || '') + '</head>' +
'<body>' + (options.body || '') + '</body>' +
'</html>';
},
// write out a new document
// optional argument {doctype:'<!doctype html>', head:'<script>var foo = "bar";</script>', body:'<div></div>'}
// TODO: Add an optional argument for an onload callback, for when an external script is added to the head
write: function(htmlOptions){
var self = this,
doc = self.document();
doc.open();
doc.write(self.html(htmlOptions));
doc.close();
return self;
},
// Keep the iframe element intact, but refresh its window & document
flush: function(htmlOptions){
var self = this;
self.write(htmlOptions); // create blank HTML document
self._initProps = self.props();
return self;
},
// remove the iframe element from the host page
close: function(){
var self = this;
hostBody().removeChild(self.iframe);
return self;
}
};
// wrapper function for creating a new sandbox
return function(script, props, callback){
return new Sandie(script, props, callback);
};
}(this));