-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsandbox.html
100 lines (88 loc) · 3.49 KB
/
sandbox.html
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
<html>
<script>
console.log("Sandbox loaded. Time: " + new Date().getTime());
var innerHTMLCache = {}; //html,hash
function applyDiff(oldHtml, diff) {
let result = '';
let lastIndex = 0;
diff.forEach(({ start, oldContent, newContent }) => {
// Append unchanged content
result += oldHtml.slice(lastIndex, start);
// Replace with new content
result += newContent;
// Update the last processed index
lastIndex = start + oldContent.length;
});
// Append remaining unchanged content
result += oldHtml.slice(lastIndex);
return result;
}
function simpleHash(input) {
let hash = 0;
for (let i = 0; i < input.length; i++) {
const char = input.charCodeAt(i);
hash = (hash << 5) - hash + char;
hash |= 0;
}
return hash;
}
window.addEventListener('message', async function (event) {
//console.log("Got action. Time: " + new Date().getTime());
const startTime = new Date().getTime();
const toSandboxDuration = startTime - event.data.sentAt;
let html = innerHTMLCache.html;
if (event.data.html) {
html = event.data.html;
innerHTMLCache = {html: html, hash: event.data.hash};
//console.log("Using new HTML. Hash: " + event.data.hash);
} else if (event.data.diff) {
const existingHash = innerHTMLCache.hash;
//console.log("Existing hash: " + existingHash + " - Received hash: " + event.data.hash);
if (existingHash !== event.data.hash) {
try {
html = applyDiff(html, event.data.diff);
innerHTMLCache = {html: html, hash: event.data.hash};
//console.log("Using diff");
} catch (e) {
console.error(e);
}
//console.log("received hash: " + simpleHash(html));
} else {
//console.log("Hashes match. Using cache");
}
}
const parser = new DOMParser();
const doc = parser.parseFromString(html, 'text/html');
//console.log("Parsed HTML. Time: " + new Date().getTime());
const code = event.data.code
//replace document. with doc.
let codeWithDoc = code.replace(/document\./g, 'doc.');
//replace [[action]] with action
if (event.data.action) {
//console.log("Replacing [[action]] with " + event.data.action);
codeWithDoc = codeWithDoc.replace(/\[\[action\]\]/g, event.data.action);
} else {
//console.log("No action to replace");
codeWithDoc = codeWithDoc.replace(/\[\[action\]\]/g, '');
}
let evalResult;
try {
evalResult = eval(codeWithDoc);
} catch (e) {
evalResult = e;
}
const endTime = new Date().getTime();
//console.log("Executed code. Duration: " + (endTime - startTime) + "ms - Result: " + JSON.stringify(evalResult));
const execResult = {
debug: {
evalDuration: endTime - startTime,
toSandboxDuration: toSandboxDuration
},
result: evalResult,
voqal_resp_id: event.data.voqal_resp_id
}
//console.log("Executed code. Time: " + new Date().getTime());
event.source.window.postMessage(execResult, event.origin);
});
</script>
</html>