Add JSON support to javascript sandbox
This task use JSON-lib to add JSON support to javascript sandbox.
Add JSON support to javascript sandbox
- Create javascript sandbox with jsoup support
- Modify com.paesia.schema.script.Machine class as following
- Create javascript as following
Modify com.paesia.schema.script.Machine class
public class Machine { private Handler handler; public static void run(Machine env, String js, Map args) throws Exception { try { Context cx = Context.enter(); cx.setClassShutter(new ClassShutter() { public boolean visibleToScripts(String className) { ........... if (className.startsWith("com.paesia.schema.script.safe.")) return true; if ("org.jsoup.helper.HttpConnection$Response".equals(className)) return true; if ("net.sf.json.JSONArray".equals(className)) return true; if ("net.sf.json.JSONObject".equals(className)) return true; return false; } }); ........... } catch (Exception e) { throw e; } finally { Context.exit(); } } public JSONArray newJSONArray() { return new JSONArray(); } public JSONObject newJSONObject() { return new JSONObject(); } ........... }javascript
function main(env, args) { var links = args.get('links'); try { var json = env.newJsoup().connect(env.newURL('https://www.odesk.com/api/profiles/v1/search/jobs.json?page=0;50&sort=date_posted;D')).execute().body(); var root = env.newJSONObject().fromObject(json); var jobs = root.getJSONObject('jobs').getJSONArray('job'); for (var i = 0; i < jobs.size(); i++) { var job = jobs.getJSONObject(i); var desc = job.getString('op_description'); var title = job.getString('op_title'); var url = 'https://www.odesk.com/jobs/' + job.getString('ciphertext'); var item = env.newHashMap(); item.put('title', title); item.put('desc', desc); item.put('url', url); links.add(item); } } catch (e) { env.error(e); } }
No comments:
Post a Comment