最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How can I execute code that a user inputs into my ACE editor on my page - Stack Overflow

programmeradmin2浏览0评论

I am using ACE Editor as my text editor on my page and the user will type in it code.

  1. I am looking to execute code that has been entered by the user on or in the browser if possible. How do I get the input from the editor and utilize the Browsers V8 JavaScript compiler with it?

  2. I am then going to try to run it on a Node.js but first I have to learn Node :).

I am using ACE Editor as my text editor on my page and the user will type in it code.

  1. I am looking to execute code that has been entered by the user on or in the browser if possible. How do I get the input from the editor and utilize the Browsers V8 JavaScript compiler with it?

  2. I am then going to try to run it on a Node.js but first I have to learn Node :).

Share Improve this question edited Mar 27, 2014 at 21:41 tckmn 59.3k27 gold badges117 silver badges156 bronze badges asked Mar 27, 2014 at 21:39 inoabrianinoabrian 3,8001 gold badge21 silver badges27 bronze badges 1
  • have you managed to get this work ? curious to know how you did this ? – Jayavel Commented Feb 28, 2019 at 11:28
Add a comment  | 

2 Answers 2

Reset to default 18

It's relatively simple to grab some user entered code and run it, with JavaScript. Essentially, you'll grab the code from ACE:

var code = editor.getValue();

Then use javascript to run it. At the simplest level you can just do:

eval(code);

However, you probably don't want to use eval(). Instead you might do something like:

// grabbed from https://stackoverflow.com/questions/6432984/adding-script-element-to-the-dom-and-have-the-javascript-run
var script = document.createElement('script');
try {
  script.appendChild(document.createTextNode(code));
  document.body.appendChild(script);
} catch (e) {
  script.text = code;
  document.body.appendChild(script);
}

This will work. However, it will still cause some problems, as then, the user code could affect your environment. In this scenario, it may not be terrible from a security perspective (unless the user can share their code), but it would be confusing. For that reason, you'll want to sandbox your code.

This answer explains sandboxing client side javascript, using window.postMessage, you could send javascript to the sandboxed iframe and have it be evaluated there.

If you wish to bring this server-side and execute Node code, you'll need to do sandboxing differently. On a server, sandboxing is much more of a concern, as the user gets to do a lot more with Javascript and could maliciously interact with your server. Luckily, there are a few sandboxes for Node that should help.

Getting code is the easy part just do code = editor.getValue()
Simply utilizing V8 compiler is easy too, create iframe and do

try {
    var result = iframeWindow.eval(code)
} catch(e) {
    // report error...
}

but this won't be very useful since it will be very easy to create infinite loops and break the page.
You can have a look at https://github.com/jsbin/jsbin/blob/master/public/js/runner/loop-protect.js#L7 to resolve loop problem.

发布评论

评论列表(0)

  1. 暂无评论