Skip to content

Commit b294d4c

Browse files
save file
1 parent 337f293 commit b294d4c

File tree

1 file changed

+244
-0
lines changed

1 file changed

+244
-0
lines changed

utils/editors/js-console/html/output-console/output-console.html

Lines changed: 244 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,7 @@
259259
if(params.async){
260260
js = '(async()=>{\n'+js+'\n})()';
261261
}
262+
262263
var result = await win.eval(js);
263264
resolve(result);
264265

@@ -269,6 +270,249 @@
269270
}//iframe
270271

271272

273+
obj.run.iframe2 = async function(js,params={}){
274+
console.log('run.iframe2');
275+
var resolve,promise=new Promise(res=>resolve=res);
276+
277+
if(iframe){
278+
iframe.remove();
279+
}
280+
281+
if(params.async){
282+
js = '(async()=>{\n'+js+'\n})()';
283+
}
284+
285+
286+
287+
/*
288+
var html = `
289+
<script>
290+
291+
window.addEventListener('message',({data})=>{
292+
console.log('message',data);
293+
var {js} = data;
294+
console.log(js);
295+
window.eval(js);
296+
297+
});
298+
299+
</scr`+`ipt>
300+
`;
301+
302+
303+
var html = `
304+
305+
<script type="module">
306+
window.addEventListener("message", async (event) => {
307+
try {
308+
// Wrap the code in a module so dynamic import works
309+
const blob = new Blob([event.data.js], { type: "text/javascript" });
310+
const url = URL.createObjectURL(blob);
311+
await import(url);
312+
URL.revokeObjectURL(url);
313+
} catch (e) {
314+
console.error("Module error:", e);
315+
}
316+
});
317+
</scr`+`ipt>
318+
319+
320+
`;
321+
322+
323+
324+
325+
var html = `
326+
327+
<script>
328+
329+
${js}
330+
331+
</scr`+`ipt>
332+
`;
333+
334+
335+
var html = `
336+
337+
<!DOCTYPE html>
338+
339+
<html>
340+
<head>
341+
342+
<script type=module>
343+
344+
// Catch *everything* the iframe might throw
345+
window.addEventListener("error", (e) => {
346+
console.log("iframe error:", e.message);
347+
});
348+
349+
window.addEventListener("unhandledrejection", (e) => {
350+
console.log("iframe unhandledrejection:", e.reason);
351+
});
352+
353+
354+
window.addEventListener('message',async({data})=>{
355+
356+
var {js} = data;
357+
try{
358+
var blob = new Blob([js],{type:'text/javascript'});
359+
var url = window.URL.createObjectURL(blob);
360+
await import(url);
361+
}
362+
catch(err){
363+
console.error('import failed',err);
364+
}
365+
366+
});
367+
368+
</scr`+`ipt>
369+
370+
</head>
371+
372+
<body>
373+
</body>
374+
</html>
375+
376+
`;
377+
378+
379+
var html = `
380+
381+
<script>
382+
383+
window.addEventListener('message',async({data})=>{
384+
385+
var {js} = data;
386+
window.eval(js);
387+
388+
});
389+
390+
</scr`+`ipt>
391+
392+
`;
393+
394+
395+
396+
397+
*/
398+
399+
400+
401+
iframe = document.createElement('iframe');
402+
obj.iframe = iframe;
403+
document.body.append(iframe)
404+
//cur.root.append(iframe);
405+
406+
iframe.style.display = 'none';
407+
iframe.sandbox = 'allow-scripts allow-same-origin';
408+
iframe.onload = onload;
409+
410+
//var blob = new Blob([html],{type:'text/html'});
411+
//var url = window.URL.createObjectURL(blob);
412+
//iframe.src = url;
413+
414+
iframe.srcdoc = '';
415+
416+
return promise;
417+
418+
419+
async function onload(){
420+
421+
var win = iframe.contentWindow;
422+
423+
win.focus();
424+
win.console.log = log;
425+
win.console.clear = clear;
426+
win.console.error = error;
427+
win.console.write = write;
428+
429+
if(!chk.persist){
430+
clear();
431+
}
432+
433+
var result = await win.eval(js);
434+
resolve(result);
435+
436+
//iframe.remove();
437+
438+
}//onload
439+
440+
}//iframe
441+
442+
443+
444+
445+
obj.run.iframe3 = function(js){
446+
447+
448+
// Create the iframe
449+
const iframe = document.createElement("iframe");
450+
document.body.appendChild(iframe);
451+
452+
// HTML that will run inside the iframe
453+
const html = `
454+
<!doctype html>
455+
<html>
456+
<body>
457+
<script type="module">
458+
console.log("iframe module script loaded");
459+
460+
// Catch *everything* the iframe might throw
461+
window.addEventListener("error", (e) => {
462+
console.log("iframe error:", e.message);
463+
});
464+
465+
window.addEventListener("unhandledrejection", (e) => {
466+
console.log("iframe unhandledrejection:", e.reason);
467+
});
468+
469+
window.addEventListener("message", async (event) => {
470+
if (event.data?.type !== "run") return;
471+
472+
console.log("iframe got message, code:", event.data.code);
473+
474+
try {
475+
// Turn the incoming code into a module
476+
const blob = new Blob(
477+
[event.data.code],
478+
{ type: "text/javascript" }
479+
);
480+
const url = URL.createObjectURL(blob);
481+
482+
console.log("importing user module:", url);
483+
484+
await import(url);
485+
486+
console.log("user module finished");
487+
URL.revokeObjectURL(url);
488+
} catch (err) {
489+
console.log("import failed:", err);
490+
}
491+
});
492+
</scr`+`ipt>
493+
</body>
494+
</html>
495+
`;
496+
497+
// Load the iframe from a Blob URL
498+
const blob = new Blob([html], { type: "text/html" });
499+
iframe.src = URL.createObjectURL(blob);
500+
501+
// When iframe is ready, send some code
502+
iframe.addEventListener("load", () => {
503+
console.log("iframe loaded, posting message");
504+
505+
iframe.contentWindow.postMessage({
506+
type: "run",
507+
code: js
508+
}, "*");
509+
});
510+
511+
512+
}//iframe3
513+
514+
515+
272516
obj.kill = function(){return kill()}
273517

274518
function kill(){

0 commit comments

Comments
 (0)