-
Notifications
You must be signed in to change notification settings - Fork 0
/
embed-console.js
129 lines (111 loc) · 3.59 KB
/
embed-console.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
(function (EMPTY_OBJECT, queuedEmbeds, doc) {
embedYJSConsole = function (details, opt_fnCode) {
var script = doc.getElementsByClassName('yourjs-console-embed')[0];
if (!script || script.nodeName.toUpperCase() !== 'SCRIPT') {
throw new Error('No "script.yourjs-console-embed" was found.');
}
script.removeAttribute('class');
queue(script, details, opt_fnCode);
};
function queue(script, details, opt_fnCode) {
queuedEmbeds.push([script, details, opt_fnCode]);
if (/^(complete|loaded)$/.test(doc.readyState)) {
runQueue();
}
}
function has(obj, key) {
return EMPTY_OBJECT.hasOwnProperty.call(obj, key);
}
function forEachProp(value, callback) {
value = Object(value);
for (var k in value) {
if (has(value, k)) {
callback(value[k], k, value);
}
}
}
function extend(target, source) {
forEachProp(source, function (value, key) {
target[key] = value;
});
return target;
}
function runQueue() {
queuedEmbeds.forEach(function (args) {
var script = args[0],
details = args[1],
opt_fnCode = details.code || args[2];
var div = document.createElement('div');
extend(
div.style,
extend(
{
boxShadow: '0 0 5px 0 #000',
borderRadius: '10px 10px 0 0',
position: 'relative',
overflow: 'hidden',
height: '500px',
width: '100%'
},
details.style
)
);
var iframe = document.createElement('iframe');
extend(extend(iframe, { frameBorder: 0, src: 'about:blank' }).style, {
position: 'absolute',
top: 0,
left: 0,
height: '100%',
width: '100%'
});
var urlHasParams, url = 'http://yourjs.' + (details.isLocal ? 'local' : 'com') + '/console/';
forEachProp(details.params, function (value, key) {
url += (urlHasParams ? '&' : '?') + escape(key) + '=' + escape(value);
urlHasParams = 1;
});
if (opt_fnCode) {
opt_fnCode = (opt_fnCode + '').replace(/function.*?\{([^]*)\}/g, '$1');
var indentation = opt_fnCode.match(/^[ \xA0\t]+(?=\S)/mg).reduce(function (min, line) {
return Math.min(min, line.length);
}, Infinity);
indentation = isFinite(indentation) ? indentation : 0;
opt_fnCode = opt_fnCode.replace(
new RegExp('^[\\xA0 ]{' + indentation + '}|^\t{' + Math.floor(indentation / (details.tabSize || 4)) + '}', 'mg'),
''
);
var interval = setInterval(function () {
var frmWin = iframe.contentWindow,
frmDoc = frmWin.document;
if (frmWin && /^(loaded|complete)$/i.test(frmDoc.readyState)) {
postURL(frmDoc, url, { code: opt_fnCode });
clearInterval(interval);
}
}, 50);
}
else {
iframe.src = url;
}
div.appendChild(iframe);
script.parentNode.insertBefore(div, script);
});
queuedEmbeds = [];
}
function postURL(doc, url, opt_data) {
var body = doc.body,
form = extend(doc.createElement("FORM"), { method: 'POST', action: url });
form.style.display = "none";
forEachProp(opt_data, function (value, key) {
form.appendChild(extend(doc.createElement("TEXTAREA"), {
name: key,
value: 'object' === typeof value ? JSON.stringify(value) : value
}));
});
body.appendChild(form);
form.submit();
body.removeChild(form);
return form;
}
doc.addEventListener("DOMContentLoaded", function () {
runQueue();
});
})({}, [], document);