javascript - 从 TextArea 运行 JavaScript

标签 javascript node.js forms algorithm


我必须在 Node.js 中创建一个应用程序来执行算法(在 js 中)并用它们的结果创建图表。
我的问题是我真的不知道如何在我的 textarea 中执行 JavaScript,我没有找到可以在 node.js 中运行 javascript 代码的插件。

谢谢

最佳答案

您可以在大多数 JS 方言中使用 JSON.eval("some string of javascript"),它将运行其中的代码。在此处查看更多信息:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/eval

很明显,这适用于学校,但作为一般做法,不推荐使用 JSON.eval(),因为它是运行某人想要的任何代码的一个很好的攻击向量。但是,无论您将其插入何处,它都可以在 Node JS 或浏览器的客户端上运行。只要它是用于学习练习,它可能就是您所需要的,但不要去找工作并到处使用 .eval() 。

这是我在 developer.mozilla.org 上链接到的那个页面的警告的复制粘贴

Don't use eval needlessly! eval() is a dangerous function, which executes the code it's passed with the privileges of the caller. If you run eval() with a string that could be affected by a malicious party, you may end up running malicious code on the user's machine with the permissions of your webpage / extension. More importantly, third party code can see the scope in which eval() was invoked, which can lead to possible attacks in ways to which the similar Function is not susceptible.

关于javascript - 从 TextArea 运行 JavaScript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42543524/

相关文章:

javascript - 我正在尝试在 JS 中使用可选链接,但它显示错误

javascript - 当我尝试在 Visual Studio Code 的终端中使用 Node.js 时,收到 “document is not defined” 错误消息

node.js - 没有代码签名就无法构建 Electron 应用程序

javascript - 提交后重置我的表格

jquery - 跨浏览器文件上传输入黑客

javascript - 如何在dialogflow中的计划时间自动触发意图

函数指针的 Javascript module.export 结构

javascript - Ember.js 需要有关简单 Todo 示例的集合处理程序的帮助

node.js - Express 在处理第一个 API 时不向第二个 API 提供服务

forms - 如何在 MVC Razor 表单中显示计算字段?