From 2ecdfcebf0e9cd9499264c5b8452ec34ede7b50c Mon Sep 17 00:00:00 2001 From: Reimar Date: Sat, 11 Oct 2025 20:05:48 +0200 Subject: [PATCH] allow using console interface within project --- src/code_runner.js | 6 ++++-- src/playground_console.js | 25 ++++++++++++++++++++++++- style.css | 9 +++++++++ 3 files changed, 37 insertions(+), 3 deletions(-) diff --git a/src/code_runner.js b/src/code_runner.js index 8eb2fe1..2bcfb42 100644 --- a/src/code_runner.js +++ b/src/code_runner.js @@ -3,6 +3,8 @@ export class CodeRunner { this.console = console; this.codeStopper = codeStopper; this.isRunning = false; + + window.playgroundConsole = this.console; } setCode(code) { @@ -18,13 +20,13 @@ export class CodeRunner { // Use RNG to prevent caching const encodedText = encodeURIComponent( - this.code + `/*(tph): ${Math.random()}*/`, + `let console=playgroundConsole;${this.code}/*(tph): ${Math.random()}*/`, ); try { await import(`data:text/javascript;charset=utf-8,${encodedText}`); } catch (error) { - this.console.log(error); + this.console.error(error); } } diff --git a/src/playground_console.js b/src/playground_console.js index 5929708..06b4c42 100644 --- a/src/playground_console.js +++ b/src/playground_console.js @@ -4,6 +4,29 @@ export class PlaygroundConsole { } log(text) { - this.elem.textContent += `\n${text}\n`; + const el = document.createElement("span"); + el.textContent = `\n${text}\n`; + this.elem.appendChild(el); + } + + error(text) { + const el = document.createElement("span"); + el.className = "error"; + el.textContent = `\n${text}\n`; + this.elem.appendChild(el); + } + + debug(text) { + const el = document.createElement("span"); + el.className = "debug"; + el.textContent = `\n${text}\n`; + this.elem.appendChild(el); + } + + info(text) { + const el = document.createElement("span"); + el.className = "info"; + el.textContent = `\n${text}\n`; + this.elem.appendChild(el); } } diff --git a/style.css b/style.css index 4f4add7..2ba6b2d 100644 --- a/style.css +++ b/style.css @@ -121,6 +121,15 @@ div#buttons button { width: 100%; white-space: pre-wrap; } + +#console .error { + color: #D32F2F; +} + +#console .info, #console .debug { + color: #BDBDBD; +} + #console input { width: 100%; margin: 0;