-
Notifications
You must be signed in to change notification settings - Fork 0
/
iframe.sandbox.participant.worker.html
152 lines (152 loc) · 5.26 KB
/
iframe.sandbox.participant.worker.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
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
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Participant worker (IFrame)</title>
<meta content="width=device-width, initial-scale=1.0" name="viewport">
<meta content="yes" name="mobile-web-app-capable">
<script>
'use strict'
let _parent = null;
let _worker = null;
let _source = null;
let _addedScripts = {};
let _interfaceSettings;
let _iframeId = window.location.search.substring(1);
let _messageIndex = null;
function a(){
function addScript(url){
if(url in _addedScripts){
return _addedScripts[url];
}
let script = document.createElement('script');
_addedScripts[url] = new Promise(resolve => script.onload = resolve);
script.src = url;
document.head.appendChild(script);
return _addedScripts[url];
}
function isIframe(){
try{
return window.self !== window.top;
}catch(e){
return true;
}
}
let scriptLoaded_promise = addScript(decodeURI(location.hash.substring(1)));
if(isIframe()){
let breakBeforeFirstMessage;
function respond(event, message=null, systemMessage=false){
_parent.postMessage({type: 'Response', data: {event: event, source: _source, payload: {message: message, systemMessage: systemMessage, messageIndex: _messageIndex}}}, '*');
}
function messageEventToWorker(messageEvent, targetOrigin){
_messageIndex = messageEvent.data.index;
if(messageEvent.data.systemMessage){
systemMessages[messageEvent.data.message]();
}else{
if(breakBeforeFirstMessage){
breakBeforeFirstMessage = false;
debugger
}
_worker.postMessage(messageEvent.data, targetOrigin);
}
}
function blockNetwork(){
window.stop();
window.requestAnimationFrame(blockNetwork);
}
let systemMessages = {
'Kill': ()=>{
if(_worker instanceof Worker){
_worker.terminate();
}else if(_worker && !_interfaceSettings.keepOpenAfterTermination){
_worker.close();
}
respond('Response', 'Dead', true);
}
};
window.onmessage = messageEvent => {
if(_parent === null){
_parent = messageEvent.source;
_source = {
participant: messageEvent.data.participant,
name: messageEvent.data.name,
iframeId: _iframeId,
matchIndex: parseInt(_iframeId.match(/(\d+)/g)[0])
}
}
scriptLoaded_promise.then(() => {
if(messageEvent.data.url.startsWith('!')){
/** URL:s that begins with a exclamation mark (!) is reserved for interfaces thats opens as a window instead as a Worker. */
let workerData = messageEvent.data.workerData;
let created = false;
window.onmessage = messageEvent => {
if(messageEvent.source === _parent){
messageEventToWorker(messageEvent, '*');
}else{
if(created){
respond('Response', messageEvent.data);
}else{
created = true;
_interfaceSettings = messageEvent.data ?? {};
_worker = messageEvent.source;
if(breakBeforeFirstMessage){
breakBeforeFirstMessage = false;
debugger
}
_worker.postMessage(workerData, '*');
respond('Worker-Created');
}
}
}
let interfaceUrl = messageEvent.data.url.substr(1);
breakBeforeFirstMessage = interfaceUrl[0] === '?';
if(breakBeforeFirstMessage){
interfaceUrl = interfaceUrl.substr(1);
}
if(!window.open(interfaceUrl)){
console.error('Could not open interface. Was popup is blocked?', interfaceUrl);
}
}else if(messageEvent.data.url.startsWith('?')){
/** URL:s that begins with a question mark (?) is reserved for plain javascript that can be debugged. */
window.onmessage = messageEvent => messageEventToWorker(messageEvent);
let url = messageEvent.data.url.substr(1);
breakBeforeFirstMessage = url[0] === '?';
if(breakBeforeFirstMessage){
url = url.substr(1);
}
ArenaHelper.CreateWorkerFromRemoteURL(url, {system: [messageEvent.data.includeScripts.urls.seedrandom]}, messageEvent.data.workerData.settings.general.seed+'@'+_iframeId).then(worker=>{
_worker = worker;
_worker.onmessage = m=>{
respond('Response', m.data);
}
messageEventToWorker(messageEvent);
respond('Worker-Created');
});
}else{
window.onmessage = messageEvent => messageEventToWorker(messageEvent);
ArenaHelper.CreateWorkerFromRemoteURL('ParticipantWorker.js').then(worker=>{
_worker = worker;
_worker.onmessage = m=>{
if(m.data){
respond(m.data.type, m.data.response);
}else{
if(!messageEvent.data.workerData.settings.general.advanced.allowRemoteExecution){
blockNetwork();
}
respond('Worker-Created');
}
}
messageEventToWorker(messageEvent);
});
}
});
}
}else{
window.location.replace(window.location.href.replace('\/(?:.(?!\/))+$', '/'));
}
window.parent.postMessage({type: 'Participant-Worker-Initiated', data: _iframeId}, '*');
}
</script>
</head>
<body onload="a()"></body>
</html>