use of net.sourceforge.htmlunit.corejs.javascript.Context in project htmlunit by HtmlUnit.
the class HTMLObjectElement method createAppletMethodAndProperties.
private void createAppletMethodAndProperties() throws Exception {
final HtmlObject appletNode = (HtmlObject) getDomNodeOrDie();
final Applet applet = appletNode.getApplet();
if (applet == null) {
return;
}
// Rhino should provide the possibility to declare delegate for Functions as it does for properties!!!
for (final Method method : applet.getClass().getMethods()) {
final Function f = new BaseFunction() {
@Override
public Object call(final Context cx, final Scriptable scope, final Scriptable thisObj, final Object[] args) {
final Object[] realArgs = new Object[method.getParameterTypes().length];
for (int i = 0; i < realArgs.length; i++) {
final Object arg;
if (i > args.length) {
arg = null;
} else {
arg = Context.jsToJava(args[i], method.getParameterTypes()[i]);
}
realArgs[i] = arg;
}
try {
return method.invoke(applet, realArgs);
} catch (final Exception e) {
throw Context.throwAsScriptRuntimeEx(e);
}
}
};
ScriptableObject.defineProperty(this, method.getName(), f, ScriptableObject.READONLY);
}
}
use of net.sourceforge.htmlunit.corejs.javascript.Context in project htmlunit by HtmlUnit.
the class HTMLAppletElement method createAppletMethodAndProperties.
private void createAppletMethodAndProperties() throws Exception {
final HtmlApplet appletNode = (HtmlApplet) getDomNodeOrDie();
final Applet applet = appletNode.getApplet();
if (applet == null) {
return;
}
// Rhino should provide the possibility to declare delegate for Functions as it does for properties!!!
for (final Method method : applet.getClass().getMethods()) {
final Function f = new BaseFunction() {
@Override
public Object call(final Context cx, final Scriptable scope, final Scriptable thisObj, final Object[] args) {
final Object[] realArgs = new Object[method.getParameterTypes().length];
for (int i = 0; i < realArgs.length; i++) {
final Object arg;
if (i > args.length) {
arg = null;
} else {
arg = Context.jsToJava(args[i], method.getParameterTypes()[i]);
}
realArgs[i] = arg;
}
try {
return method.invoke(applet, realArgs);
} catch (final Exception e) {
throw Context.throwAsScriptRuntimeEx(e);
}
}
};
ScriptableObject.defineProperty(this, method.getName(), f, ScriptableObject.READONLY);
}
}
use of net.sourceforge.htmlunit.corejs.javascript.Context in project htmlunit by HtmlUnit.
the class HTMLCollectionFrames method initialize.
/**
* Initializes this window.
* @param webWindow the web window corresponding to this window
* @param pageToEnclose the page that will become the enclosing page
*/
public void initialize(final WebWindow webWindow, final Page pageToEnclose) {
webWindow_ = webWindow;
webWindow_.setScriptableObject(this);
windowProxy_ = new WindowProxy(webWindow_);
if (pageToEnclose instanceof XmlPage) {
document_ = new XMLDocument();
} else {
document_ = new HTMLDocument();
}
document_.setParentScope(this);
document_.setPrototype(getPrototype(document_.getClass()));
document_.setWindow(this);
if (pageToEnclose instanceof SgmlPage) {
final SgmlPage page = (SgmlPage) pageToEnclose;
document_.setDomNode(page);
if (page.isHtmlPage()) {
final HtmlPage htmlPage = (HtmlPage) page;
htmlPage.addAutoCloseable(this);
}
}
documentProxy_ = new DocumentProxy(webWindow_);
navigator_ = new Navigator();
navigator_.setParentScope(this);
navigator_.setPrototype(getPrototype(navigator_.getClass()));
screen_ = new Screen(getWebWindow().getScreen());
screen_.setParentScope(this);
screen_.setPrototype(getPrototype(screen_.getClass()));
history_ = new History();
history_.setParentScope(this);
history_.setPrototype(getPrototype(history_.getClass()));
location_ = new Location();
location_.setParentScope(this);
location_.setPrototype(getPrototype(location_.getClass()));
location_.initialize(this, pageToEnclose);
final Console console = new Console();
console.setWebWindow(webWindow_);
console.setParentScope(this);
console.setPrototype(getPrototype(console.getClass()));
console_ = console;
applicationCache_ = new ApplicationCache();
applicationCache_.setParentScope(this);
applicationCache_.setPrototype(getPrototype(applicationCache_.getClass()));
// like a JS new Object()
final Context ctx = Context.getCurrentContext();
controllers_ = ctx.newObject(this);
if (webWindow_ instanceof TopLevelWindow) {
final WebWindow opener = ((TopLevelWindow) webWindow_).getOpener();
if (opener != null) {
opener_ = opener.getScriptableObject();
}
}
}
use of net.sourceforge.htmlunit.corejs.javascript.Context in project htmlunit by HtmlUnit.
the class WorkerJob method postMessage.
/**
* Posts a message to the {@link Worker} in the page's context.
* @param message the message
*/
@JsxFunction
public void postMessage(final Object message) {
final MessageEvent event = new MessageEvent();
event.initMessageEvent(Event.TYPE_MESSAGE, false, false, message, origin_, "", owningWindow_, Undefined.instance);
event.setParentScope(owningWindow_);
event.setPrototype(owningWindow_.getPrototype(event.getClass()));
if (LOG.isDebugEnabled()) {
LOG.debug("[DedicatedWorker] postMessage: {}" + message);
}
final JavaScriptEngine jsEngine = (JavaScriptEngine) owningWindow_.getWebWindow().getWebClient().getJavaScriptEngine();
final ContextAction<Object> action = cx -> {
worker_.getEventListenersContainer().executeCapturingListeners(event, null);
final Object[] args = { event };
worker_.getEventListenersContainer().executeBubblingListeners(event, args);
return null;
};
final ContextFactory cf = jsEngine.getContextFactory();
final JavaScriptJob job = new WorkerJob(cf, action, "postMessage: " + Context.toString(message));
final HtmlPage page = (HtmlPage) owningWindow_.getDocument().getPage();
owningWindow_.getWebWindow().getJobManager().addJob(job, page);
}
use of net.sourceforge.htmlunit.corejs.javascript.Context in project htmlunit by HtmlUnit.
the class ProxyAutoConfig method evaluate.
/**
* Evaluates the <tt>FindProxyForURL</tt> method of the specified content.
* @param content the JavaScript content
* @param url the URL to be retrieved
* @return semicolon-separated result
*/
public static String evaluate(final String content, final URL url) {
try (Context cx = Context.enter()) {
final ProxyAutoConfig config = new ProxyAutoConfig();
final Scriptable scope = cx.initSafeStandardObjects();
config.defineMethod("isPlainHostName", scope);
config.defineMethod("dnsDomainIs", scope);
config.defineMethod("localHostOrDomainIs", scope);
config.defineMethod("isResolvable", scope);
config.defineMethod("isInNet", scope);
config.defineMethod("dnsResolve", scope);
config.defineMethod("myIpAddress", scope);
config.defineMethod("dnsDomainLevels", scope);
config.defineMethod("shExpMatch", scope);
config.defineMethod("weekdayRange", scope);
config.defineMethod("dateRange", scope);
config.defineMethod("timeRange", scope);
cx.evaluateString(scope, "var ProxyConfig = function() {}; ProxyConfig.bindings = {}", "<init>", 1, null);
cx.evaluateString(scope, content, "<Proxy Auto-Config>", 1, null);
final Object[] functionArgs = { url.toExternalForm(), url.getHost() };
final Object fObj = scope.get("FindProxyForURL", scope);
final NativeFunction f = (NativeFunction) fObj;
final Object result = f.call(cx, scope, scope, functionArgs);
return Context.toString(result);
}
}
Aggregations