TrashScript interpreter.
The primary purpose of TrashScript is to execute scripts in environments where eval
and new Function
are restricted, such as in browser extensions.
TrashScript was originally designed for modules in the filecxx browser extension such as Third-Party Query Interfaces which can run custom scripts.
While not all JavaScript syntax has been implemented, the most commonly used syntax is available.
TrashScript does not provide any asynchronous interfaces and does not recommend using any callback-based asynchronous execution.
Instead of setTimeout, the trashscript_lib.js offers a sleep function.
Additionally, all asynchronous functions bound with TrashScript.bind
will be converted to synchronous execution.
var test = async function(val)
{
return new Promise(function(resolve){
setTimeout(function(){
resolve(val)
},1000);
});
}
TrashScript.bind("test",test); //bind the test function
var ret = await test(111);
console.log(ret)
var ret = test(111); //no await
console.log(ret)
var count = 0;
var func = function()
{
setTimeout(function(){
if(count < 5){
alert(count++);
func();
}
},1000)
}
func();
for(i=0;i<5;++i){
sleep(1000);
alert(i);
}
TrashScript.config = {
max_exec_limit:10000000 //default
}
var executor = TrashScript("return 123",function(e)
{
if(e.status === "error"){
console.log(e);
}else if(e.status === "complete"){
console.log(e.result); //123
}
});
executor.variables({global_var1:123}); //add global variables
executor.exec();
TrashScript.bind("alert",function(arg){
window.alert(arg);
});
TrashScript.bind({
console:console,
test:function(){},
print:console.log,
alert:alert,
JSON:JSON
});
var executor = TrashScript("function aaa(){alert(this)};return aaa()",function(e){...});
executor.exec(window); //'this' is window
var executor = TrashScript("function aaa(){alert(this)};return aaa()",function(e){...});
executor.exec({}); //'this' is an empty object