Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

TrashScript interpreter.

License

NotificationsYou must be signed in to change notification settings

filecxx/TrashScript

Repository files navigation

TrashScript interpreter.

The primary purpose of TrashScript is to execute scripts in environments whereeval andnew Function are restricted, such as in browser extensions.

TrashScript was originally designed for modules in thefilecxx browser extension such asThird-Party Query Interfaces which can run custom scripts.

While not all JavaScript syntax has been implemented, the most commonly used syntax is available.


Asynchronous to synchronous

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 withTrashScript.bind will be converted to synchronous execution.

Example function:

var test = async function(val){    return new Promise(function(resolve){        setTimeout(function(){            resolve(val)        },1000);    });}TrashScript.bind("test",test); //bind the test function

in javascript

var ret = await test(111);console.log(ret)

in trashscript

var ret = test(111); //no awaitconsole.log(ret)

in javascript

var count = 0;var func  = function(){    setTimeout(function(){        if(count < 5){            alert(count++);            func();        }    },1000)}func();

in trashscript

for(i=0;i<5;++i){    sleep(1000);    alert(i);}

Usage examples:

Config

TrashScript.config = {    max_exec_limit:10000000 //default}

Basic

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 variablesexecutor.exec();

Bind existing JavaScript functions or objects

TrashScript.bind("alert",function(arg){    window.alert(arg);});
TrashScript.bind({    console:console,    test:function(){},    print:console.log,    alert:alert,    JSON:JSON});

Context object for 'this'

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

[8]ページ先頭

©2009-2025 Movatter.jp