-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMutationObserver.js
More file actions
288 lines (243 loc) · 8.55 KB
/
MutationObserver.js
File metadata and controls
288 lines (243 loc) · 8.55 KB
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
(function () {
"use strict";
function sendMessageWorker(el, messageHandler, message) {
return new Promise((resolve, reject) => {
if (!messageHandler) {
reject();
}
messageHandler.postMessage(message);
messageHandler.addEventListener('message', m => {
resolve(m.data);
});
});
}
function sendMessageNoWorker(el, messageHandler, message) {
const core = el.getAttribute("using");
if (window[core]) {
try {
return messageHandler({ data: message });
} catch (error) {
console.log(error.message);
}
}
}
function sendMessage(el, messageHandler, message) {
return el.getAttribute("use-worker") == "" ? sendMessageWorker(el, messageHandler, message) : sendMessageNoWorker(el, messageHandler, message);
}
function properties(el, messageHandler, props) {
const message = {
event: "get_properties",
properties: props
};
return sendMessage(el, messageHandler, message);
}
async function convertWithCanvas(el, messageHandler, format, blob) {
const res = blob;
const props = await properties(el, messageHandler, ["width", "height"]);
if (el.getAttribute("noCleanupOnExit") == null) {
const message = {
event: "destroy"
};
sendMessage(el, messageHandler, message);
}
const canvas = new OffscreenCanvas(props.width, props.height);
const data = new Uint8ClampedArray(await blob.arrayBuffer());
const ctx = canvas.getContext('2d');
let imgData;
switch (format) {
case "rgba":
imgData = new ImageData(data, props.width, props.height);
ctx.putImageData(imgData, 0, 0);
return await canvas.convertToBlob();
case "rgb":
imgData = new ImageData(props.width, props.height);
const dest = imgData.data;
const n = 4 * props.width * props.height;
let s = 0, d = 0;
while (d < n) {
dest[d++] = data[s++];
dest[d++] = data[s++];
dest[d++] = data[s++];
dest[d++] = 255; // skip alpha byte
}
ctx.putImageData(imgData, 0, 0);
return await canvas.convertToBlob();
}
}
async function dataURLToSrc(el, messageHandler, blob, out) {
if (!blob) return;
if (out == "rgb" || out == "rgba") {
blob = await convertWithCanvas(el, messageHandler, out, blob);
}
return URL.createObjectURL(blob);
}
async function processMessages(el, res, messageHandler, out) {
function clear_text(text) {
return text
.replaceAll("[37m", '')
.replaceAll("[0m", '');
}
const print = el.getAttribute("print");
if (res.print) {
if (print) {
print.value += clear_text(res.print) + "\n";
} else {
console.log(res.print);
}
}
const printerr = el.getAttribute("printerr");
if (res.printErr) {
if (printerr) {
printerr.value += clear_text(res.printErr) + "\n";
} else {
console.log(res.printErr);
}
}
if ("exit_code" in res) {
if (res.blob) {
const src = await dataURLToSrc(el, messageHandler, res.blob, out);
if (el instanceof HTMLImageElement || el instanceof HTMLPictureElement)
el.setAttribute("srcset", src);
else if (el instanceof HTMLAudioElement|| el instanceof HTMLVideoElement)
el.setAttribute("src", src);
}
}
}
function launchNoWorker(el, core, out, script, message) {
function addLoadEvent(script, func) {
var oldonload = script.onload;
if (typeof script.onload != 'function') {
script.onload = func;
} else {
script.onload = function () {
if (oldonload) {
oldonload();
}
func();
};
}
}
async function init() {
const elt = this;
const messageHandlerNoWorker = await window[core]();
const res = await messageHandlerNoWorker({ data: message });
processMessages(el, res, messageHandlerNoWorker, out);
}
const scripts = document.querySelectorAll(`script[src$="${script}"]`);
const bind_init = init.bind(el);
if (scripts.length > 0) {
const coreInit = window[core];
if (coreInit) {
init();
} else {
addLoadEvent(scripts[0], bind_init);
}
} else {
const script_elt = document.createElement('script');
script_elt.src = script;
addLoadEvent(script_elt, bind_init);
document.head.appendChild(script_elt);
this.script = script_elt;
}
}
function launchWorker(el, script, out, message) {
const worker = new Worker(script);
if (worker) {
worker.postMessage(message);
worker.addEventListener('message', m => {
processMessages(el, m.data, worker, out);
});
}
}
async function test_url(directories, url) {
const filter_directories = directories.filter(x => x != "");
for (const directory of filter_directories) {
if (await urlExist(directory + url)) {
return directory + url;
}
return "";
}
return url;
}
function addScriptDirectoryAndExtIfNeeded(scriptDirectory, url, ext) {
try {
const parsed_url = new URL(url);
if (parsed_url.protocol === 'blob:') {
return url;
} else if (parsed_url.protocol === 'http:' || parsed_url.protocol === 'https:') {
return test_url([], url + ext);
}
return test_url(scriptDirectory.split(';'), url + ext);
} catch (e) {
return test_url(scriptDirectory.split(';'), url + ext);
}
}
async function decode(el, el_using, el_with) {
let js = null;
let wasmBinaryFile = null;
let dynamicLibraries = [];
const scriptDirectory = el.getAttribute("script-directory") ? el.getAttribute("script-directory") : "";
js = await addScriptDirectoryAndExtIfNeeded(scriptDirectory, el_using, ".js");
wasmBinaryFile = await addScriptDirectoryAndExtIfNeeded(scriptDirectory, el_using, ".wasm");
const all_using = await Promise.all(el_with.split(';').map(x => addScriptDirectoryAndExtIfNeeded(scriptDirectory, x, ".wasm")));
dynamicLibraries = dynamicLibraries.concat(all_using);
// Set source
let src = el.getAttribute("src");
if (el instanceof HTMLCanvasElement){
src = el.getAttribute("data-url"); // canvas is using a special attribute since its not standard
el.setAttribute("id", "canvas"); //FIXME : This is only for the demo
}
// Set output format
let out = el.getAttribute("out");
if (!out) {
if (el instanceof HTMLImageElement || el instanceof HTMLPictureElement)
out = "rgb";
else if (el instanceof HTMLAudioElement)
out = "wav";
else if (el instanceof HTMLVideoElement)
out = "mp4";
}
const message = {
event: "init",
module: {
dynamicLibraries: dynamicLibraries,
noInitialRun: true,
noExitRuntime: true,
canvas: el instanceof HTMLCanvasElement ? el : null
},
wasmBinaryFile: wasmBinaryFile,
src: src,
dst: el instanceof HTMLCanvasElement ? null : "out." + out,
useWebcodec: false,
showStats: el.getAttribute("stats"),
showGraph: el.getAttribute("graph"),
showReport: el.getAttribute("report"),
showLogs: el.getAttribute("logs"),
get: el.out == "rgb" || el.out == "rgba" ? ["width", "height"] : [],
print: el.getAttribute("print"),
printErr: el.getAttribute("printErr"),
noCleanupOnExit: el.getAttribute("noCleanupOnExit") || el.out == "rgb" || el.out == "rgba",
loop: el instanceof HTMLCanvasElement ? el.getAttribute("noloop") == "" ? false : true : null,
width: el.getAttribute("width"),
height: el.getAttribute("height"),
vbench: el instanceof HTMLCanvasElement ? el.getAttribute("vbench") : null
};
if (!js) {
console.log("Warning! no accessor is used on the universal, using a usual tag instead...");
return;
}
try {
el instanceof HTMLCanvasElement ? launchNoWorker(el, el_using, out, js, message): launchWorker(el, js, out, message);
} catch (e) {
return;
}
}
new MutationObserver(mutations => mutations.forEach(mutation => mutation.addedNodes.forEach(el => {
if (el instanceof HTMLImageElement || el instanceof HTMLPictureElement)
decode(el, "solver_with_sdl_1", "libjxl_1");
else if(el instanceof HTMLAudioElement )
decode(el, "solver_with_sdl_1", "liba52_1");
else if( el instanceof HTMLCanvasElement)
decode(el, "solver_with_sdl_1", "ogg_1;vorbis_1;theora_1");
}))).observe(document.documentElement, { subtree: true, childList: true });
}());