如何执行eval()代码并获取所有参数
How to execute eval() code and get all the params
问题
我允许用户在文本代码编辑器中编写一些代码,例如:
var Player = function(){
this.x = 0;
this.y = 0;
this.width = 32;
this.height = 64;
}
Player.prototype.run = function(){
this.x++;
}
我想执行这段代码(任何javascript)使用eval()
?把它存到一个变量里,这样我就可以遍历它,创建html实体。伪代码:
loop through varables // loop through the entities that the user created.
print varable_name // print the name of varable ("Player")
print varable.width // Print the varable entitiy ("32");
示例代码:for (var name in this) {
variables[name] = name;
variables[name]=this[name]
}
function maskedEval(scr){
var mask = {};
for (p in this)
mask[p] = undefined;
// execute script in private context
(new Function( "with(this) { " + scr + "}")).call(mask);
}
是否有办法动态地做到这一点?还是有更好的方法?我希望你能理解我在做什么。希望有人能帮助我。
谢谢,)
这是一种处理代码的好方法,将其放入临时散列JavaScript文件中;
演示:http://so.ghostsofthesun.com/user_965921/
index . php
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title></title>
<style>
.floatClear {
clear: both;
}
#userScript, #result {
float: left;
font-family: monospace;
box-sizing: border-box;
padding: 20px 15px;
width: 500px;
min-height: 300px;
background-color: #333;
color: #eee;
border-radius: 10px;
border: 5px solid orange;
box-shadow: 0px 3px 5px #444;
outline: none;
}
#result {
margin-left: 20px;
}
#submitCode {
margin: 20px;
}
</style>
<script src="http://code.jquery.com/jquery-1.10.0.min.js"></script>
<script>
$(document).ready(function() {
$('#submitCode').click(function() {
var input = {'code' : JSON.stringify($('#userScript').val().trim())};
$.post('process_code.php', input, function(data) {
$('#result').html('YES');
data = jQuery.parseJSON(data);
js_file_path = data.js_file_path;
$('script[data^=tmp_js]').remove();
$('head').append('<script data="tmp_js" src="' + js_file_path + '"><'/script>');
});
});
});
</script>
</head>
<body>
<textarea id="userScript">var test = 21; console.log(test);</textarea>
<div id="result">Result will be here</div>
<div class="floatClear"></div>
<button id="submitCode">Execute code!</button> (The left panel is editable)
</body>
</html>
process_code.php
<?php
if (isset($_POST['code'])) {
$code = json_decode($_POST['code']);
// Create new JS file
$js_file = 'tmp_'.md5(microtime()).'.js';
$handle = fopen($js_file, 'w') or die('can''t open file');
fwrite($handle, $code);
fclose($handle);
$full_path = 'http://localhost:8888/'.$js_file;
echo json_encode(array('js_file_path' => $full_path));
}
else {
echo json_encode(array('error' => 'Code wasn''t recieved correctly.'));
}
// Do someting to remove the created JS-file after a certain time (cache/buffer empty)
?>
. .和替换;
$full_path = 'http://so.ghostsofthesun.com/user_965921/'.$js_file;
. .(或者使用$_
超全局变量自动检测)
当然,您可以直接将代码重新路由到脚本标签中,但由于我手头有这个,我想我将立即分享全部内容:)
现代浏览器会注意到添加的脚本标签,并立即执行它。IE可能不能很好地处理这个问题;你可能需要在不同的平台上进行测试。否则,你可以在互联网上搜索在脚本嵌入后调用JavaScript的方法。左栏输入的另一个例子;
$('#result').css({
'font-size' : '60px',
'color' : 'blue',
'background' : '#aaa'
});
额外的注意;
你可能想使用$.getScript()
(正如我刚刚发现的)而不是重新绑定动态脚本标签;
$.post('process_code.php', input, function(data) {
$('#result').html('YES');
data = jQuery.parseJSON(data);
js_file_path = data.js_file_path;
$.getScript(js_file_path);
//$('script[data^=tmp_js]').remove();
//$('head').append('<script data="tmp_js" src="' + js_file_path + '"><'/script>');
});
相关文章:
- ROR:如何在不重新加载浏览器的情况下从控制器获取参数
- 如何在reactrouterv2.+中从URL获取参数
- 如何获取参数的值
- 从从字符串创建的函数中获取参数列表
- jquery从url中获取参数也会在控制台日志中留下url
- 从Javascript中的URL获取参数
- IFrame 从不同域上的父 url 获取参数
- 如何使用React Router在Route Handler上获取参数
- Angularjs-如何从URL获取参数
- 从外部函数获取参数名称
- PHP从带有url的变量中获取参数
- 是否可以在 JavaScript 中按函数名称获取参数
- JS .replace(str, str):如何获取参数 1 中的变量并全局工作
- 从页面网址获取参数
- 如何在 router.use 中定义参数时获取参数
- 如何从 JavaScript 中的 JSON 对象获取参数的名称
- 我将一个函数(本身有一个参数)作为参数传递给另一个函数以及如何获取参数
- 如何获取参数对象的子集?(关于具有可变大小的参数)
- Javascript对象通信并从其他对象获取参数
- Websocket 从查询中获取参数