-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrunnable.js
64 lines (49 loc) · 1.54 KB
/
runnable.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
(function () {
var slice = Array.prototype.slice;
function post(win, type, el) {
var data = {
type: type,
text: el.innerText
};
win.postMessage(data, '*');
}
function build(el) {
var vm = document.createElement('iframe');
vm.onload = function () {
var win = vm.contentWindow;
slice
.call(el.querySelectorAll('.css'))
.map(post.bind(null, win, 'css'))
.join('');
slice
.call(el.querySelectorAll('.html'))
.map(post.bind(null, win, 'html'))
.join('');
slice
.call(el.querySelectorAll('.js'))
.map(post.bind(null, win, 'js'))
.join('');
};
vm.src = 'runnable.html';
el.appendChild(vm);
}
function append(event) {
var data = event.data;
var type = data && data.type;
var text = data && data.text;
switch (type) {
case 'css':
return document.body
.insertAdjacentHTML('beforeend', '<style>' + text + '</style>');
case 'html':
return document.body
.insertAdjacentHTML('beforeend', text);
case 'js':
return eval(text); //jshint ignore: line
}
}
window.addEventListener('message', append, false);
slice
.call(document.querySelectorAll('.runnable'))
.forEach(build);
}());