Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import jdk.nashorn.api.scripting.JSObject;
- import javax.script.Bindings;
- import javax.script.ScriptContext;
- import javax.script.ScriptEngine;
- import javax.script.ScriptEngineManager;
- import java.util.function.Consumer;
- class Scratch {
- public static void main(String[] args) throws Exception {
- ScriptEngine engine = new ScriptEngineManager().getEngineByName("nashorn");
- Bindings engineScope = engine.getBindings(ScriptContext.ENGINE_SCOPE);
- engineScope.put("window", engineScope);
- engineScope.put("self", engineScope);
- engineScope.put("parent", engineScope);
- engineScope.put("opener", engineScope);
- engineScope.put("top", engineScope);
- // http://mail.openjdk.java.net/pipermail/nashorn-dev/2013-December/002520.html
- // https://stackoverflow.com/a/33376041/622266
- engineScope.put("postMessage", (FakeWindow) (data, frameUrl) -> System.out.println("frame url:"+frameUrl));
- engineScope.put("logString", (Consumer<String>)(s) -> System.out.println("simple:"+s));
- engine.eval("window.logString('something')");
- engine.eval("window.postMessage({}, 'http://localhost');");
- }
- @FunctionalInterface
- interface FakeWindow {
- void postMessage(JSObject data, String frameUrl);
- }
- }
Add Comment
Please, Sign In to add comment