Search in sources :

Example 1 with Context

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);
    }
}
Also used : Context(net.sourceforge.htmlunit.corejs.javascript.Context) BaseFunction(net.sourceforge.htmlunit.corejs.javascript.BaseFunction) JsxFunction(com.gargoylesoftware.htmlunit.javascript.configuration.JsxFunction) Function(net.sourceforge.htmlunit.corejs.javascript.Function) BaseFunction(net.sourceforge.htmlunit.corejs.javascript.BaseFunction) Applet(java.applet.Applet) HtmlObject(com.gargoylesoftware.htmlunit.html.HtmlObject) ScriptableObject(net.sourceforge.htmlunit.corejs.javascript.ScriptableObject) HtmlObject(com.gargoylesoftware.htmlunit.html.HtmlObject) NativeJavaObject(net.sourceforge.htmlunit.corejs.javascript.NativeJavaObject) Method(java.lang.reflect.Method) Scriptable(net.sourceforge.htmlunit.corejs.javascript.Scriptable)

Example 2 with Context

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);
    }
}
Also used : HtmlApplet(com.gargoylesoftware.htmlunit.html.HtmlApplet) Context(net.sourceforge.htmlunit.corejs.javascript.Context) Function(net.sourceforge.htmlunit.corejs.javascript.Function) BaseFunction(net.sourceforge.htmlunit.corejs.javascript.BaseFunction) BaseFunction(net.sourceforge.htmlunit.corejs.javascript.BaseFunction) Applet(java.applet.Applet) HtmlApplet(com.gargoylesoftware.htmlunit.html.HtmlApplet) ScriptableObject(net.sourceforge.htmlunit.corejs.javascript.ScriptableObject) Method(java.lang.reflect.Method) Scriptable(net.sourceforge.htmlunit.corejs.javascript.Scriptable)

Example 3 with Context

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();
        }
    }
}
Also used : Context(net.sourceforge.htmlunit.corejs.javascript.Context) HtmlPage(com.gargoylesoftware.htmlunit.html.HtmlPage) HTMLDocument(com.gargoylesoftware.htmlunit.javascript.host.html.HTMLDocument) XMLDocument(com.gargoylesoftware.htmlunit.javascript.host.xml.XMLDocument) WebWindow(com.gargoylesoftware.htmlunit.WebWindow) SgmlPage(com.gargoylesoftware.htmlunit.SgmlPage) XmlPage(com.gargoylesoftware.htmlunit.xml.XmlPage) DocumentProxy(com.gargoylesoftware.htmlunit.javascript.host.html.DocumentProxy) TopLevelWindow(com.gargoylesoftware.htmlunit.TopLevelWindow)

Example 4 with Context

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);
}
Also used : AbstractJavaScriptConfiguration(com.gargoylesoftware.htmlunit.javascript.configuration.AbstractJavaScriptConfiguration) WindowOrWorkerGlobalScopeMixin(com.gargoylesoftware.htmlunit.javascript.host.WindowOrWorkerGlobalScopeMixin) URL(java.net.URL) Script(net.sourceforge.htmlunit.corejs.javascript.Script) HtmlUnitScriptable(com.gargoylesoftware.htmlunit.javascript.HtmlUnitScriptable) FF(com.gargoylesoftware.htmlunit.javascript.configuration.SupportedBrowser.FF) EDGE(com.gargoylesoftware.htmlunit.javascript.configuration.SupportedBrowser.EDGE) ClassConfiguration(com.gargoylesoftware.htmlunit.javascript.configuration.ClassConfiguration) BrowserVersion(com.gargoylesoftware.htmlunit.BrowserVersion) ContextAction(net.sourceforge.htmlunit.corejs.javascript.ContextAction) FF_ESR(com.gargoylesoftware.htmlunit.javascript.configuration.SupportedBrowser.FF_ESR) Event(com.gargoylesoftware.htmlunit.javascript.host.event.Event) JavaScriptEngine(com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine) MimeType(com.gargoylesoftware.htmlunit.util.MimeType) Context(net.sourceforge.htmlunit.corejs.javascript.Context) WebRequest(com.gargoylesoftware.htmlunit.WebRequest) Scriptable(net.sourceforge.htmlunit.corejs.javascript.Scriptable) JsxSetter(com.gargoylesoftware.htmlunit.javascript.configuration.JsxSetter) Window(com.gargoylesoftware.htmlunit.javascript.host.Window) CHROME(com.gargoylesoftware.htmlunit.javascript.configuration.SupportedBrowser.CHROME) IOException(java.io.IOException) JavaScriptJob(com.gargoylesoftware.htmlunit.javascript.background.JavaScriptJob) HtmlPage(com.gargoylesoftware.htmlunit.html.HtmlPage) WebResponse(com.gargoylesoftware.htmlunit.WebResponse) JsxGetter(com.gargoylesoftware.htmlunit.javascript.configuration.JsxGetter) JsxFunction(com.gargoylesoftware.htmlunit.javascript.configuration.JsxFunction) JsxClass(com.gargoylesoftware.htmlunit.javascript.configuration.JsxClass) List(java.util.List) ContextFactory(net.sourceforge.htmlunit.corejs.javascript.ContextFactory) IE(com.gargoylesoftware.htmlunit.javascript.configuration.SupportedBrowser.IE) WindowOrWorkerGlobalScope(com.gargoylesoftware.htmlunit.javascript.host.WindowOrWorkerGlobalScope) Log(org.apache.commons.logging.Log) WebClient(com.gargoylesoftware.htmlunit.WebClient) LogFactory(org.apache.commons.logging.LogFactory) Undefined(net.sourceforge.htmlunit.corejs.javascript.Undefined) Function(net.sourceforge.htmlunit.corejs.javascript.Function) JS_WORKER_IMPORT_SCRIPTS_ACCEPTS_ALL(com.gargoylesoftware.htmlunit.BrowserVersionFeatures.JS_WORKER_IMPORT_SCRIPTS_ACCEPTS_ALL) MessageEvent(com.gargoylesoftware.htmlunit.javascript.host.event.MessageEvent) EventTarget(com.gargoylesoftware.htmlunit.javascript.host.event.EventTarget) BasicJavaScriptJob(com.gargoylesoftware.htmlunit.javascript.background.BasicJavaScriptJob) ContextFactory(net.sourceforge.htmlunit.corejs.javascript.ContextFactory) JavaScriptJob(com.gargoylesoftware.htmlunit.javascript.background.JavaScriptJob) BasicJavaScriptJob(com.gargoylesoftware.htmlunit.javascript.background.BasicJavaScriptJob) HtmlPage(com.gargoylesoftware.htmlunit.html.HtmlPage) MessageEvent(com.gargoylesoftware.htmlunit.javascript.host.event.MessageEvent) JavaScriptEngine(com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine) JsxFunction(com.gargoylesoftware.htmlunit.javascript.configuration.JsxFunction)

Example 5 with Context

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);
    }
}
Also used : Context(net.sourceforge.htmlunit.corejs.javascript.Context) NativeFunction(net.sourceforge.htmlunit.corejs.javascript.NativeFunction) ScriptableObject(net.sourceforge.htmlunit.corejs.javascript.ScriptableObject) FunctionObject(net.sourceforge.htmlunit.corejs.javascript.FunctionObject) Scriptable(net.sourceforge.htmlunit.corejs.javascript.Scriptable)

Aggregations

Context (net.sourceforge.htmlunit.corejs.javascript.Context)50 Scriptable (net.sourceforge.htmlunit.corejs.javascript.Scriptable)40 ContextAction (net.sourceforge.htmlunit.corejs.javascript.ContextAction)39 ScriptableObject (net.sourceforge.htmlunit.corejs.javascript.ScriptableObject)20 Test (org.junit.jupiter.api.Test)15 ContextFactory (net.sourceforge.htmlunit.corejs.javascript.ContextFactory)11 Function (net.sourceforge.htmlunit.corejs.javascript.Function)6 WebClient (com.gargoylesoftware.htmlunit.WebClient)5 HtmlPage (com.gargoylesoftware.htmlunit.html.HtmlPage)5 JavaScriptEngine (com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine)5 Method (java.lang.reflect.Method)5 ArrayList (java.util.ArrayList)5 JsxFunction (com.gargoylesoftware.htmlunit.javascript.configuration.JsxFunction)4 Script (net.sourceforge.htmlunit.corejs.javascript.Script)4 JavaScriptJob (com.gargoylesoftware.htmlunit.javascript.background.JavaScriptJob)3 Window (com.gargoylesoftware.htmlunit.javascript.host.Window)3 BrowserVersion (com.gargoylesoftware.htmlunit.BrowserVersion)2 JS_WORKER_IMPORT_SCRIPTS_ACCEPTS_ALL (com.gargoylesoftware.htmlunit.BrowserVersionFeatures.JS_WORKER_IMPORT_SCRIPTS_ACCEPTS_ALL)2 WebRequest (com.gargoylesoftware.htmlunit.WebRequest)2 WebResponse (com.gargoylesoftware.htmlunit.WebResponse)2