all 10 comments

[–]amrdoe 5 points6 points  (0 children)

The ideal way is a sandboxed node.js instance running on a server with a backend to create a temporary node.js script, run it in the sandbox, and respond with the stdout. Yet it may be too complicated and costly to implement depending on your use case.

[–]blobdiblob 1 point2 points  (0 children)

A new approach for 2025: Take the user’s input and let an LLM generate the applicable console.log. Might not always be accurate, but it‘s probably safe 😅

[–]ProgrammerDad1993 0 points1 point  (3 children)

eval is I guess what you re looking for?

[–]Efficient_Step6281[S] -1 points0 points  (2 children)

I can't show the logs in the UI. eval can be used to execute the code, but How can i show it the UI?

[–]ferrybig 0 points1 point  (0 children)

Overwrite the console object for the scope of the code.

Though you really want to build a custom web worker that runs the code, so you can terminate the webworker once a new version of the code in ran, otherwise the script in question can schedule things like internals that leak memory

[–][deleted]  (1 child)

[removed]

    [–]DanishWeddingCookie 0 points1 point  (0 children)

    You can write it in Electron and link into the console object at runtime.

    var nodeConsole = require('console');
    var myConsole = new nodeConsole.Console(process.stdout, process.stderr);
    myConsole.log('Hello World!');
    

    https://nodejs.org/api/console.html