1

Is there a way in JavaScript in browsers, to run an other script stored as string independently from the main JavaScript and also get the console output from that script in a way that it is not (just) printed to the console?

Basically I want something like this:

var script = 'console.log("Hello World");'

var consoleOut = script.runAsJS();
6

3 Answers 3

3

What environment?

In browser, you can use web workers.

In Node/Deno you can use VM or spawn separate process.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, 🙏 I added this to response
And I added the answer to the question
0

Maybe you could look into eval()

Hopefully this is what you mean by 'independent from the main javascript'. If you mean as in variables from the outer scope will not be defined in the string, eval() will not work for you. It is treated as part of the same program.

Edit: also see this answer on specifying the scope of eval()

If you want to store the console.log in a variable, try something like this:

var script = 'console.log("Hello World");'
var consoleOut = [];
script = script.replace(/console\.log/g,"consoleOut.push");
eval(script);

Edit: as suggested by comments, eval is often not the best in terms of security. A more efficient and secure option may be Function, however the docs mention that Function still suffers from security issues:

var script = 'console.log("Hello World");'
var consoleOut = [];
scriptFunction = new Function(script.replace(/console\.log/g,"consoleOut.push"));
scriptFunction();

10 Comments

in what way is eval independent?
Well I want that kind of independence and also I want to separately get the console outputs
Ah I see what you mean now
Since your script is stored as a string, what if you were to .replace all console.logs with a custom function that returns the value you want?
@user11914177 The use of eval() is not advised for most use cases. Often there is a better solution. If you can explain in more detail what you want to achieve we can help you with a better solution. See: developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…!
|
0

To run an other script stored as string you can use eval() as suggested by answer from @Leaf the Legend. And for script to run independently from the main JavaScript you can use IIFE.

So combining eval and IIFE you may get your desired result. Like (function() { eval(script) })();

Below is small snippet which may elaborate better.

// variable from global scope.
let a = 10;

function runAsJS(script) {
  (function() {
    eval(script)
  })();
}

let script = `
  let a = 10; 
  a += 10;
  console.log('a from eval script', a);`;

runAsJS(script);
console.log('a from global scope', a);

2 Comments

Well that just is Leaf the Legends answer
No, for eval yes, but I have suggest to use IIFE which can make difference and variable defined inside script will have limited scope to itself only. It will not affect global variables.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.