allow using console interface within project

This commit is contained in:
Reimar 2025-10-11 20:05:48 +02:00
parent 9a2ad7f2c5
commit 2ecdfcebf0
3 changed files with 37 additions and 3 deletions

View File

@ -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);
}
}

View File

@ -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);
}
}

View File

@ -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;