javascript - 进行评估的可能方法
问题描述
我知道的是eval
,Function
和setTimeout
。尽管setImmediate
reference 没有提到可以使用字符串参数调用它,但我认为它的工作方式与setTimeout
这方面相同。
从浏览器中的字符串评估代码的可能方法是什么(包括非标准方法)?
解决方案
在浏览器上,我唯一知道的是:
eval
- 构造
Function
函数 setTimeout
及相关(setInterval
,非标准setImmediate
)- 创建脚本元素,设置其文本内容,并将其附加到文档(通过 DOM 方法,或使用
document.write
或类似方法) - 在链接等上使用
javascript:
伪协议(然后人为地点击它们或邀请用户这样做)- 书签是这个的一个特例
- DOM0 事件处理程序(然后人为触发它们或邀请用户这样做)(很好的一个GOTO 0)
居住:
eval("console.log('eval');");
(0,eval)("console.log('indirect eval');");
new Function("console.log('Function constructor');")();
setTimeout("console.log('setTimeout and such');", 0);
var script = document.createElement("script");
script.textContent = "console.log('script element');";
document.body.appendChild(script);
var link = document.createElement("a");
link.href = "javascript:console.log('javascript: pseudo-protocol');";
document.body.appendChild(link);
link.click();
var div = document.createElement("div");
div.setAttribute("onclick", "console.log('DOM0 event handler');");
document.body.appendChild(div);
div.click();
/* Or to be long-winded
div.dispatchEvent(new MouseEvent("click", {
view: window,
bubbles: true,
cancelable: true
}));
*/
推荐阅读
- python-3.x - Jupyter 和 Terminal 有不同的 Conda 包
- c - 无限提示循环问题
- google-compute-engine - 如何更新计算引擎实例上部署的存储桶?
- javascript - 没有从连接的 js 中捕获提交,在哪里挖掘?
- javascript - 在没有类的情况下使用 componentWillMount
- python-3.x - 如何将浮点数添加到类中的金额
- node.js - @package 和 node_modules 中的 package 之间的关系是什么
- conda - 更新 conda 时出现 AssertionError
- javascript - Watchers 是否与 Vue 中的 Global Mixins 一起使用?
- security - Ledger Nano S 的 24 字助记种子短语可在 Trezor 上使用?