I'm creating a map editor for my game, and I added feature which is adding script to map (textarea), then code writed by person which is editing a map is saved to variable, and then when this person save map, this variable is saved to JSON file as variable in Map object.
When map is loading in the game, this code is transforming into Function object and then it's launching.
My question is: Is there option to check is this string valid JavaScript, and if not alert("Error" + error) or something like that?
// Editor is only for developers, not for users, so I don't need to check it on server-side for safety
You could do the following :
try {
eval("function f () {"
+ "user code here"
+ "}");
} catch (e) {
console.log(e+"");
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With