Search in sources :

Example 1 with HtmlUnitContextFactory

use of com.gargoylesoftware.htmlunit.javascript.HtmlUnitContextFactory in project htmlunit by HtmlUnit.

the class HTMLObjectElement method setClassid.

/**
 * Sets the {@code classid} attribute.
 * @param classid the {@code classid} attribute
 */
@JsxSetter(IE)
public void setClassid(final String classid) {
    getDomNodeOrDie().setAttribute("classid", classid);
    if (classid.indexOf(':') != -1 && getBrowserVersion().hasFeature(HTML_OBJECT_CLASSID)) {
        final WebClient webClient = getWindow().getWebWindow().getWebClient();
        final Map<String, String> map = webClient.getActiveXObjectMap();
        if (map != null) {
            final String xClassString = map.get(classid);
            if (xClassString != null) {
                try {
                    final Class<?> xClass = Class.forName(xClassString);
                    final Object object = xClass.newInstance();
                    boolean contextCreated = false;
                    if (Context.getCurrentContext() == null) {
                        new HtmlUnitContextFactory(webClient).enterContext();
                        contextCreated = true;
                    }
                    wrappedActiveX_ = Context.toObject(object, getParentScope());
                    if (contextCreated) {
                        Context.exit();
                    }
                } catch (final Exception e) {
                    throw Context.reportRuntimeError("ActiveXObject Error: failed instantiating class " + xClassString + " because " + e.getMessage() + ".");
                }
                return;
            }
        }
        if (webClient.getOptions().isActiveXNative() && System.getProperty("os.name").contains("Windows")) {
            try {
                wrappedActiveX_ = new ActiveXObjectImpl(classid);
                wrappedActiveX_.setParentScope(getParentScope());
            } catch (final Exception e) {
                Context.throwAsScriptRuntimeEx(e);
            }
        }
    }
}
Also used : HtmlUnitContextFactory(com.gargoylesoftware.htmlunit.javascript.HtmlUnitContextFactory) ScriptableObject(net.sourceforge.htmlunit.corejs.javascript.ScriptableObject) HtmlObject(com.gargoylesoftware.htmlunit.html.HtmlObject) NativeJavaObject(net.sourceforge.htmlunit.corejs.javascript.NativeJavaObject) ActiveXObjectImpl(com.gargoylesoftware.htmlunit.javascript.host.ActiveXObjectImpl) WebClient(com.gargoylesoftware.htmlunit.WebClient) JsxSetter(com.gargoylesoftware.htmlunit.javascript.configuration.JsxSetter)

Example 2 with HtmlUnitContextFactory

use of com.gargoylesoftware.htmlunit.javascript.HtmlUnitContextFactory in project htmlunit by HtmlUnit.

the class NamedAttrNodeMapImpl method fireEvent.

/**
 * <span style="color:red">INTERNAL API - SUBJECT TO CHANGE AT ANY TIME - USE AT YOUR OWN RISK.</span><br>
 *
 * Fires the event on the element. Nothing is done if JavaScript is disabled.
 * @param event the event to fire
 * @return the execution result, or {@code null} if nothing is executed
 */
public ScriptResult fireEvent(final Event event) {
    final WebClient client = getPage().getWebClient();
    if (!client.isJavaScriptEnabled()) {
        return null;
    }
    if (!handles(event)) {
        return null;
    }
    if (LOG.isDebugEnabled()) {
        LOG.debug("Firing " + event);
    }
    final EventTarget jsElt = getScriptableObject();
    final HtmlUnitContextFactory cf = ((JavaScriptEngine) client.getJavaScriptEngine()).getContextFactory();
    final ScriptResult result = cf.callSecured(cx -> jsElt.fireEvent(event), getHtmlPageOrNull());
    if (event.isAborted(result)) {
        preventDefault();
    }
    return result;
}
Also used : ScriptResult(com.gargoylesoftware.htmlunit.ScriptResult) HtmlUnitContextFactory(com.gargoylesoftware.htmlunit.javascript.HtmlUnitContextFactory) WebClient(com.gargoylesoftware.htmlunit.WebClient) EventTarget(com.gargoylesoftware.htmlunit.javascript.host.event.EventTarget) AbstractJavaScriptEngine(com.gargoylesoftware.htmlunit.javascript.AbstractJavaScriptEngine) JavaScriptEngine(com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine)

Example 3 with HtmlUnitContextFactory

use of com.gargoylesoftware.htmlunit.javascript.HtmlUnitContextFactory in project htmlunit by HtmlUnit.

the class WebClientUtils method attachVisualDebugger.

/**
 * Attaches a visual (GUI) debugger to the specified client.
 * @param client the client to which the visual debugger is to be attached
 * @see <a href="http://www.mozilla.org/rhino/debugger.html">Mozilla Rhino Debugger Documentation</a>
 */
public static void attachVisualDebugger(final WebClient client) {
    final HtmlUnitContextFactory cf = ((JavaScriptEngine) client.getJavaScriptEngine()).getContextFactory();
    final Main main = Main.mainEmbedded(cf, (ScopeProvider) null, "HtmlUnit JavaScript Debugger");
    main.getDebugFrame().setExtendedState(Frame.MAXIMIZED_BOTH);
    final SourceProvider sourceProvider = script -> {
        String sourceName = script.getSourceName();
        if (sourceName.endsWith("(eval)") || sourceName.endsWith("(Function)")) {
            // script is result of eval call. Rhino already knows the source and we don't
            return null;
        }
        if (sourceName.startsWith("script in ")) {
            sourceName = StringUtils.substringBetween(sourceName, "script in ", " from");
            for (final WebWindow ww : client.getWebWindows()) {
                final WebResponse wr = ww.getEnclosedPage().getWebResponse();
                if (sourceName.equals(wr.getWebRequest().getUrl().toString())) {
                    return wr.getContentAsString();
                }
            }
        }
        return null;
    };
    main.setSourceProvider(sourceProvider);
}
Also used : Main(net.sourceforge.htmlunit.corejs.javascript.tools.debugger.Main) Frame(java.awt.Frame) HtmlUnitContextFactory(com.gargoylesoftware.htmlunit.javascript.HtmlUnitContextFactory) ScopeProvider(net.sourceforge.htmlunit.corejs.javascript.tools.debugger.ScopeProvider) WebWindow(com.gargoylesoftware.htmlunit.WebWindow) WebClient(com.gargoylesoftware.htmlunit.WebClient) SourceProvider(net.sourceforge.htmlunit.corejs.javascript.tools.debugger.SourceProvider) JavaScriptEngine(com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine) StringUtils(org.apache.commons.lang3.StringUtils) WebResponse(com.gargoylesoftware.htmlunit.WebResponse) WebResponse(com.gargoylesoftware.htmlunit.WebResponse) HtmlUnitContextFactory(com.gargoylesoftware.htmlunit.javascript.HtmlUnitContextFactory) SourceProvider(net.sourceforge.htmlunit.corejs.javascript.tools.debugger.SourceProvider) Main(net.sourceforge.htmlunit.corejs.javascript.tools.debugger.Main) JavaScriptEngine(com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine) WebWindow(com.gargoylesoftware.htmlunit.WebWindow)

Example 4 with HtmlUnitContextFactory

use of com.gargoylesoftware.htmlunit.javascript.HtmlUnitContextFactory in project htmlunit by HtmlUnit.

the class HtmlPage method executeEventHandlersIfNeeded.

/**
 * Looks for and executes any appropriate event handlers. Looks for body and frame tags.
 * @param eventType either {@link Event#TYPE_LOAD}, {@link Event#TYPE_UNLOAD}, or {@link Event#TYPE_BEFORE_UNLOAD}
 * @return {@code true} if user accepted <tt>onbeforeunload</tt> (not relevant to other events)
 */
private boolean executeEventHandlersIfNeeded(final String eventType) {
    // If JavaScript isn't enabled, there's nothing for us to do.
    if (!getWebClient().isJavaScriptEnabled()) {
        return true;
    }
    // Execute the specified event on the document element.
    final WebWindow window = getEnclosingWindow();
    if (window.getScriptableObject() instanceof Window) {
        final Event event;
        if (eventType.equals(Event.TYPE_BEFORE_UNLOAD)) {
            event = new BeforeUnloadEvent(this, eventType);
        } else {
            event = new Event(this, eventType);
        }
        // here so it could be used with HtmlPage.
        if (LOG.isDebugEnabled()) {
            LOG.debug("Firing " + event);
        }
        final EventTarget jsNode;
        if (Event.TYPE_DOM_DOCUMENT_LOADED.equals(eventType)) {
            jsNode = this.getScriptableObject();
        } else {
            // The load/beforeunload/unload events target Document but paths Window only (tested in Chrome/FF)
            jsNode = window.getScriptableObject();
        }
        final HtmlUnitContextFactory cf = ((JavaScriptEngine) getWebClient().getJavaScriptEngine()).getContextFactory();
        cf.callSecured(cx -> jsNode.fireEvent(event), this);
        if (!isOnbeforeunloadAccepted(this, event)) {
            return false;
        }
    }
    // If this page was loaded in a frame, execute the version of the event specified on the frame tag.
    if (window instanceof FrameWindow) {
        final FrameWindow fw = (FrameWindow) window;
        final BaseFrameElement frame = fw.getFrameElement();
        // if part of a document fragment, then the load event is not triggered
        if (Event.TYPE_LOAD.equals(eventType) && frame.getParentNode() instanceof DomDocumentFragment) {
            return true;
        }
        if (frame.hasEventHandlers("on" + eventType)) {
            if (LOG.isDebugEnabled()) {
                LOG.debug("Executing on" + eventType + " handler for " + frame);
            }
            if (window.getScriptableObject() instanceof Window) {
                final Event event;
                if (Event.TYPE_BEFORE_UNLOAD.equals(eventType)) {
                    event = new BeforeUnloadEvent(frame, eventType);
                } else {
                    // ff does not trigger the onload event in this case
                    if (PageDenied.BY_CONTENT_SECURIRY_POLICY == fw.getPageDenied() && hasFeature(JS_EVENT_LOAD_SUPPRESSED_BY_CONTENT_SECURIRY_POLICY)) {
                        return true;
                    }
                    event = new Event(frame, eventType);
                }
                // This fires the "load" event for the <frame> element which, like all non-window
                // load events, propagates up to Document but not Window.  The "load" event for
                // <frameset> on the other hand, like that of <body>, is handled above where it is
                // fired against Document and directed to Window.
                frame.fireEvent(event);
                if (!isOnbeforeunloadAccepted((HtmlPage) frame.getPage(), event)) {
                    return false;
                }
            }
        }
    }
    return true;
}
Also used : WebWindow(com.gargoylesoftware.htmlunit.WebWindow) Window(com.gargoylesoftware.htmlunit.javascript.host.Window) TopLevelWindow(com.gargoylesoftware.htmlunit.TopLevelWindow) HtmlUnitContextFactory(com.gargoylesoftware.htmlunit.javascript.HtmlUnitContextFactory) Event(com.gargoylesoftware.htmlunit.javascript.host.event.Event) BeforeUnloadEvent(com.gargoylesoftware.htmlunit.javascript.host.event.BeforeUnloadEvent) BeforeUnloadEvent(com.gargoylesoftware.htmlunit.javascript.host.event.BeforeUnloadEvent) EventTarget(com.gargoylesoftware.htmlunit.javascript.host.event.EventTarget) WebWindow(com.gargoylesoftware.htmlunit.WebWindow) AbstractJavaScriptEngine(com.gargoylesoftware.htmlunit.javascript.AbstractJavaScriptEngine) JavaScriptEngine(com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine)

Example 5 with HtmlUnitContextFactory

use of com.gargoylesoftware.htmlunit.javascript.HtmlUnitContextFactory in project htmlunit by HtmlUnit.

the class NodeList method forEach.

/**
 * Calls the {@code callback} given in parameter once for each value pair in the list, in insertion order.
 * @param callback function to execute for each element
 */
@JsxFunction({ CHROME, EDGE, FF, FF_ESR })
public void forEach(final Object callback) {
    final List<DomNode> nodes = getElements();
    final WebClient client = getWindow().getWebWindow().getWebClient();
    final HtmlUnitContextFactory cf = ((JavaScriptEngine) client.getJavaScriptEngine()).getContextFactory();
    final ContextAction<Object> contextAction = cx -> {
        final Function function = (Function) callback;
        final Scriptable scope = getParentScope();
        for (int i = 0; i < nodes.size(); i++) {
            function.call(cx, scope, NodeList.this, new Object[] { nodes.get(i).getScriptableObject(), i, NodeList.this });
        }
        return null;
    };
    cf.call(contextAction);
}
Also used : Scriptable(net.sourceforge.htmlunit.corejs.javascript.Scriptable) JsxSymbol(com.gargoylesoftware.htmlunit.javascript.configuration.JsxSymbol) HtmlUnitContextFactory(com.gargoylesoftware.htmlunit.javascript.HtmlUnitContextFactory) ScriptableObject(net.sourceforge.htmlunit.corejs.javascript.ScriptableObject) DomNode(com.gargoylesoftware.htmlunit.html.DomNode) HtmlUnitScriptable(com.gargoylesoftware.htmlunit.javascript.HtmlUnitScriptable) CHROME(com.gargoylesoftware.htmlunit.javascript.configuration.SupportedBrowser.CHROME) FF(com.gargoylesoftware.htmlunit.javascript.configuration.SupportedBrowser.FF) EDGE(com.gargoylesoftware.htmlunit.javascript.configuration.SupportedBrowser.EDGE) ES6Iterator(net.sourceforge.htmlunit.corejs.javascript.ES6Iterator) JsxFunction(com.gargoylesoftware.htmlunit.javascript.configuration.JsxFunction) JsxClass(com.gargoylesoftware.htmlunit.javascript.configuration.JsxClass) List(java.util.List) NativeArrayIterator(net.sourceforge.htmlunit.corejs.javascript.NativeArrayIterator) ContextAction(net.sourceforge.htmlunit.corejs.javascript.ContextAction) JsxConstructor(com.gargoylesoftware.htmlunit.javascript.configuration.JsxConstructor) FF_ESR(com.gargoylesoftware.htmlunit.javascript.configuration.SupportedBrowser.FF_ESR) WebClient(com.gargoylesoftware.htmlunit.WebClient) JavaScriptEngine(com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine) Function(net.sourceforge.htmlunit.corejs.javascript.Function) JsxFunction(com.gargoylesoftware.htmlunit.javascript.configuration.JsxFunction) Function(net.sourceforge.htmlunit.corejs.javascript.Function) DomNode(com.gargoylesoftware.htmlunit.html.DomNode) HtmlUnitContextFactory(com.gargoylesoftware.htmlunit.javascript.HtmlUnitContextFactory) ScriptableObject(net.sourceforge.htmlunit.corejs.javascript.ScriptableObject) Scriptable(net.sourceforge.htmlunit.corejs.javascript.Scriptable) HtmlUnitScriptable(com.gargoylesoftware.htmlunit.javascript.HtmlUnitScriptable) WebClient(com.gargoylesoftware.htmlunit.WebClient) JavaScriptEngine(com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine) JsxFunction(com.gargoylesoftware.htmlunit.javascript.configuration.JsxFunction)

Aggregations

HtmlUnitContextFactory (com.gargoylesoftware.htmlunit.javascript.HtmlUnitContextFactory)5 WebClient (com.gargoylesoftware.htmlunit.WebClient)4 JavaScriptEngine (com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine)4 WebWindow (com.gargoylesoftware.htmlunit.WebWindow)2 AbstractJavaScriptEngine (com.gargoylesoftware.htmlunit.javascript.AbstractJavaScriptEngine)2 EventTarget (com.gargoylesoftware.htmlunit.javascript.host.event.EventTarget)2 ScriptableObject (net.sourceforge.htmlunit.corejs.javascript.ScriptableObject)2 ScriptResult (com.gargoylesoftware.htmlunit.ScriptResult)1 TopLevelWindow (com.gargoylesoftware.htmlunit.TopLevelWindow)1 WebResponse (com.gargoylesoftware.htmlunit.WebResponse)1 DomNode (com.gargoylesoftware.htmlunit.html.DomNode)1 HtmlObject (com.gargoylesoftware.htmlunit.html.HtmlObject)1 HtmlUnitScriptable (com.gargoylesoftware.htmlunit.javascript.HtmlUnitScriptable)1 JsxClass (com.gargoylesoftware.htmlunit.javascript.configuration.JsxClass)1 JsxConstructor (com.gargoylesoftware.htmlunit.javascript.configuration.JsxConstructor)1 JsxFunction (com.gargoylesoftware.htmlunit.javascript.configuration.JsxFunction)1 JsxSetter (com.gargoylesoftware.htmlunit.javascript.configuration.JsxSetter)1 JsxSymbol (com.gargoylesoftware.htmlunit.javascript.configuration.JsxSymbol)1 CHROME (com.gargoylesoftware.htmlunit.javascript.configuration.SupportedBrowser.CHROME)1 EDGE (com.gargoylesoftware.htmlunit.javascript.configuration.SupportedBrowser.EDGE)1