/
run.js
223 lines (197 loc) · 6.27 KB
/
run.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
/*global document window */
import { parse } from 'babylon';
import * as walk from 'babylon-walk';
import copy from 'copy-to-clipboard';
let container = null;
export const bindConsole = __console => {
const apply = ['log', 'warn', 'assert', 'debug', 'clear'];
apply.forEach(method => {
container.contentWindow.console[method] = (...args) => {
window.console[method].apply(window.console, args);
__console[method].apply(__console, args);
};
});
};
export function createContainer() {
container = document.createElement('iframe');
container.width = container.height = 1;
container.style.opacity = 0;
container.style.border = 0;
container.style.position = 'absolute';
container.style.top = '-100px';
container.setAttribute('name', '<proxy>');
document.body.appendChild(container);
setContainer(container);
}
export function setContainer(iframe) {
container = iframe;
const win = container.contentWindow;
const doc = container.contentDocument;
win.copy = copy;
win.$$ = s => Array.from(doc.querySelectorAll(s));
win.$ = s => doc.querySelector(s);
}
export default async function run(command) {
return new Promise(async resolve => {
const res = {
error: false,
command,
};
try {
// // trick from devtools
// // via https://chromium.googlesource.com/chromium/src.git/+/4fd348fdb9c0b3842829acdfb2b82c86dacd8e0a%5E%21/#F2
if (/^\s*\{/.test(command) && /\}\s*$/.test(command)) {
command = `(${command})`;
}
const { content, additionalCode } = preProcess(command);
// IMPORTANT: because we're going across iframe bridge here, the constructor
// of the response value is changed to Object, so even if we return an error
// or a promise, `value instanceof Error` will always be false.
// This is why across all the code, I actually use the `isa` pattern to get
// the original constructor from ({}).toString.call(value)
if (content.startsWith('(async () => ')) {
res.value = await container.contentWindow.eval(content);
} else {
res.value = container.contentWindow.eval(content);
}
// if there's no extra code (usually to block out a const), then let's
// go ahead and store the result in $_
if (!additionalCode) {
container.contentWindow.$_ = res.value;
}
if (additionalCode !== null) {
const doc = container.contentDocument;
const script = doc.createElement('script');
const blob = new Blob([additionalCode], {
type: 'application/javascript',
});
script.src = URL.createObjectURL(blob);
container.contentWindow.onerror = (message, file, line, col, error) => {
res.error = true;
res.value = error;
resolve(res);
};
script.onload = () => {
resolve(res);
container.contentWindow.onerror = () => {};
};
doc.documentElement.appendChild(script);
} else {
return resolve(res);
}
} catch (error) {
res.error = true;
res.value = error;
return resolve(res);
}
});
}
function preProcess(content) {
var wrapped = '(async () => {' + content + '})()';
var root = parse(wrapped, { ecmaVersion: 8 });
var body = root.program.body[0].expression.callee.body;
var changes = [];
var containsAwait = false;
var containsReturn = false;
const visitors = {
ClassDeclaration(node) {
if (node.parent === body)
changes.push({
text: node.id.name + '=',
start: node.start,
end: node.start,
});
},
FunctionDeclaration(node) {
changes.push({
text: node.id.name + '=',
start: node.start,
end: node.start,
});
return node;
},
AwaitExpression(node) {
containsAwait = true;
},
ReturnStatement(node) {
containsReturn = true;
},
VariableDeclaration(node) {
if (node.kind !== 'var' && node.parent !== body) return;
var onlyOneDeclaration = node.declarations.length === 1;
changes.push({
text: onlyOneDeclaration ? 'void' : 'void (',
start: node.start,
end: node.start + node.kind.length,
});
for (var declaration of node.declarations) {
if (!declaration.init) {
changes.push({
text: '(',
start: declaration.start,
end: declaration.start,
});
changes.push({
text: '=undefined)',
start: declaration.end,
end: declaration.end,
});
continue;
}
changes.push({
text: '(',
start: declaration.start,
end: declaration.start,
});
changes.push({
text: ')',
start: declaration.end,
end: declaration.end,
});
}
if (!onlyOneDeclaration) {
var last = node.declarations.peekLast();
changes.push({ text: ')', start: last.end, end: last.end });
}
},
};
walk.simple(body, visitors);
var last = body.body[body.body.length - 1];
if (last.type === 'ExpressionStatement') {
changes.push({
text: 'return window.$_ = (',
start: last.start,
end: last.start,
});
if (wrapped[last.end - 1] !== ';')
changes.push({ text: ')', start: last.end, end: last.end });
else changes.push({ text: ')', start: last.end - 1, end: last.end - 1 });
}
let additionalCode = null;
if (last.type === 'VariableDeclaration' && last.kind === 'const') {
additionalCode = `const ${last.declarations['0'].id.name} = $_`;
changes.push({
text: `const ${last.declarations['0'].id.name} = window.$_`,
start: last.start,
end: last.declarations['0'].id.end,
});
}
if (!containsAwait || containsReturn) {
if (additionalCode) {
const offset = 14; // length of `(async () => {`
content =
content.substr(0, last.declarations['0'].id.end - offset) +
' = window.$_' +
content.substr(last.declarations['0'].id.end - offset);
}
return { content, additionalCode };
}
while (changes.length) {
var change = changes.pop();
wrapped =
wrapped.substr(0, change.start) +
change.text +
wrapped.substr(change.end);
}
return { content: wrapped, additionalCode };
}