Java Code Examples for net.sourceforge.htmlunit.corejs.javascript.Context#getCurrentContext()

The following examples show how to use net.sourceforge.htmlunit.corejs.javascript.Context#getCurrentContext() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: XMLHttpRequest.java    From htmlunit with Apache License 2.0 6 votes vote down vote up
/**
 * Invokes the onerror handler if one has been set.
 * @param context the context within which the onerror handler is to be invoked;
 *                if {@code null}, the current thread's context is used.
 */
private void processError(Context context) {
    final Function onError = getOnerror();
    if (onError != null) {
        final Scriptable scope = onError.getParentScope();
        final JavaScriptEngine jsEngine = (JavaScriptEngine) containingPage_.getWebClient().getJavaScriptEngine();

        final Object[] params = {new ProgressEvent(this, Event.TYPE_ERROR)};

        if (LOG.isDebugEnabled()) {
            LOG.debug("Calling onerror handler");
        }
        jsEngine.callFunction(containingPage_, onError, this, scope, params);
        if (LOG.isDebugEnabled()) {
            if (context == null) {
                context = Context.getCurrentContext();
            }
            LOG.debug("onerror handler: " + context.decompileFunction(onError, 4));
            LOG.debug("Calling onerror handler done.");
        }
    }
}
 
Example 2
Source File: NodeList.java    From htmlunit with Apache License 2.0 6 votes vote down vote up
/**
 * Returns an {@link Iterator} allowing to go through all key/value pairs contained in this object.
 * @return an {@link Iterator}
 */
@JsxFunction({CHROME, FF, FF68, FF60})
public Iterator entries() {
    final List<DomNode> elements = getElements();
    final Context context = Context.getCurrentContext();
    final Scriptable scope = getParentScope();

    final List<Scriptable> list = new ArrayList<>();
    for (int i = 0; i < elements.size(); i++) {
        final Object[] array = new Object[] {i, elements.get(i).getScriptableObject()};
        list.add(context.newArray(scope, array));
    }
    final Iterator object = new Iterator(ITERATOR_NAME, list.iterator());
    object.setParentScope(scope);
    object.setPrototype(ITERATOR_PROTOTYPE_);
    return object;
}
 
Example 3
Source File: XMLHTTPRequest.java    From htmlunit with Apache License 2.0 6 votes vote down vote up
/**
 * Sets the state as specified and invokes the state change handler if one has been set.
 * @param state the new state
 * @param context the context within which the state change handler is to be invoked;
 *     if {@code null}, the current thread's context is used
 */
private void setState(final int state, Context context) {
    state_ = state;

    if (stateChangeHandler_ != null && !openedMultipleTimes_) {
        final Scriptable scope = stateChangeHandler_.getParentScope();
        final JavaScriptEngine jsEngine = (JavaScriptEngine) containingPage_.getWebClient().getJavaScriptEngine();

        if (LOG.isDebugEnabled()) {
            LOG.debug("Calling onreadystatechange handler for state " + state);
        }
        final Object[] params = ArrayUtils.EMPTY_OBJECT_ARRAY;

        jsEngine.callFunction(containingPage_, stateChangeHandler_, scope, this, params);
        if (LOG.isDebugEnabled()) {
            if (context == null) {
                context = Context.getCurrentContext();
            }
            LOG.debug("onreadystatechange handler: " + context.decompileFunction(stateChangeHandler_, 4));
            LOG.debug("Calling onreadystatechange handler for state " + state + ". Done.");
        }
    }
}
 
Example 4
Source File: XMLHttpRequest.java    From HtmlUnit-Android with Apache License 2.0 6 votes vote down vote up
/**
 * Invokes the onerror handler if one has been set.
 * @param context the context within which the onerror handler is to be invoked;
 *                if {@code null}, the current thread's context is used.
 */
private void processError(Context context) {
    if (errorHandler_ != null) {
        final Scriptable scope = errorHandler_.getParentScope();
        final JavaScriptEngine jsEngine = (JavaScriptEngine) containingPage_.getWebClient().getJavaScriptEngine();

        final Object[] params = new Event[] {new ProgressEvent(this, Event.TYPE_ERROR)};

        if (LOG.isDebugEnabled()) {
            LOG.debug("Calling onerror handler");
        }
        jsEngine.callFunction(containingPage_, errorHandler_, this, scope, params);
        if (LOG.isDebugEnabled()) {
            if (context == null) {
                context = Context.getCurrentContext();
            }
            LOG.debug("onerror handler: " + context.decompileFunction(errorHandler_, 4));
            LOG.debug("Calling onerror handler done.");
        }
    }
}
 
Example 5
Source File: NodeList.java    From HtmlUnit-Android with Apache License 2.0 6 votes vote down vote up
/**
 * Returns an {@link Iterator} allowing to go through all key/value pairs contained in this object.
 * @return an {@link Iterator}
 */
@JsxFunction({CHROME, FF52})
public Iterator entries() {
    final List<DomNode> elements = getElements();
    final Context context = Context.getCurrentContext();
    final Scriptable scope = getParentScope();

    final List<Scriptable> list = new ArrayList<>();
    for (int i = 0; i < elements.size(); i++) {
        final Object[] array = new Object[] {i, elements.get(i).getScriptableObject()};
        list.add(context.newArray(scope, array));
    }
    final Iterator object = new Iterator(ITERATOR_NAME, list.iterator());
    object.setParentScope(scope);
    setIteratorPrototype(object);
    return object;
}
 
Example 6
Source File: XMLHTTPRequest.java    From HtmlUnit-Android with Apache License 2.0 6 votes vote down vote up
/**
 * Sets the state as specified and invokes the state change handler if one has been set.
 * @param state the new state
 * @param context the context within which the state change handler is to be invoked;
 *     if {@code null}, the current thread's context is used
 */
private void setState(final int state, Context context) {
    state_ = state;

    if (stateChangeHandler_ != null && !openedMultipleTimes_) {
        final Scriptable scope = stateChangeHandler_.getParentScope();
        final JavaScriptEngine jsEngine = (JavaScriptEngine) containingPage_.getWebClient().getJavaScriptEngine();

        if (LOG.isDebugEnabled()) {
            LOG.debug("Calling onreadystatechange handler for state " + state);
        }
        final Object[] params = ArrayUtils.EMPTY_OBJECT_ARRAY;

        jsEngine.callFunction(containingPage_, stateChangeHandler_, scope, this, params);
        if (LOG.isDebugEnabled()) {
            if (context == null) {
                context = Context.getCurrentContext();
            }
            LOG.debug("onreadystatechange handler: " + context.decompileFunction(stateChangeHandler_, 4));
            LOG.debug("Calling onreadystatechange handler for state " + state + ". Done.");
        }
    }
}
 
Example 7
Source File: MouseEvent.java    From htmlunit with Apache License 2.0 5 votes vote down vote up
/**
 * Returns the mouse event currently firing, or {@code null} if no mouse event is being processed.
 * @return the mouse event currently firing
 */
@SuppressWarnings("unchecked")
public static MouseEvent getCurrentMouseEvent() {
    final Context context = Context.getCurrentContext();
    if (context != null) {
        final LinkedList<Event> events = (LinkedList<Event>) context.getThreadLocal(KEY_CURRENT_EVENT);
        if (events != null && !events.isEmpty() && events.getLast() instanceof MouseEvent) {
            return (MouseEvent) events.getLast();
        }
    }
    return null;
}
 
Example 8
Source File: Event.java    From htmlunit with Apache License 2.0 5 votes vote down vote up
/**
 * Called when the event starts being fired.
 */
@SuppressWarnings("unchecked")
public void startFire() {
    final Context context = Context.getCurrentContext();
    LinkedList<Event> events = (LinkedList<Event>) context.getThreadLocal(KEY_CURRENT_EVENT);
    if (events == null) {
        events = new LinkedList<>();
        context.putThreadLocal(KEY_CURRENT_EVENT, events);
    }
    events.add(this);
}
 
Example 9
Source File: MouseEvent.java    From HtmlUnit-Android with Apache License 2.0 5 votes vote down vote up
/**
 * Returns the mouse event currently firing, or {@code null} if no mouse event is being processed.
 * @return the mouse event currently firing
 */
@SuppressWarnings("unchecked")
public static MouseEvent getCurrentMouseEvent() {
    final Context context = Context.getCurrentContext();
    if (context != null) {
        final LinkedList<Event> events = (LinkedList<Event>) context.getThreadLocal(KEY_CURRENT_EVENT);
        if (events != null && !events.isEmpty() && events.getLast() instanceof MouseEvent) {
            return (MouseEvent) events.getLast();
        }
    }
    return null;
}
 
Example 10
Source File: Event.java    From HtmlUnit-Android with Apache License 2.0 5 votes vote down vote up
/**
 * Called when the event starts being fired.
 */
@SuppressWarnings("unchecked")
public void startFire() {
    final Context context = Context.getCurrentContext();
    LinkedList<Event> events = (LinkedList<Event>) context.getThreadLocal(KEY_CURRENT_EVENT);
    if (events == null) {
        events = new LinkedList<>();
        context.putThreadLocal(KEY_CURRENT_EVENT, events);
    }
    events.add(this);
}
 
Example 11
Source File: Window.java    From htmlunit with Apache License 2.0 4 votes vote down vote up
/**
 * Initializes this window.
 * @param webWindow the web window corresponding to this window
 */
public void initialize(final WebWindow webWindow) {
    webWindow_ = webWindow;
    webWindow_.setScriptableObject(this);

    windowProxy_ = new WindowProxy(webWindow_);

    final Page enclosedPage = webWindow.getEnclosedPage();
    if (enclosedPage instanceof XmlPage) {
        document_ = new XMLDocument();
    }
    else {
        document_ = new HTMLDocument();
    }
    document_.setParentScope(this);
    document_.setPrototype(getPrototype(document_.getClass()));
    document_.setWindow(this);

    if (enclosedPage instanceof SgmlPage) {
        final SgmlPage page = (SgmlPage) enclosedPage;
        document_.setDomNode(page);

        final DomHtmlAttributeChangeListenerImpl listener = new DomHtmlAttributeChangeListenerImpl();
        page.addDomChangeListener(listener);

        if (page.isHtmlPage()) {
            ((HtmlPage) page).addHtmlAttributeChangeListener(listener);
            ((HtmlPage) page).addAutoCloseable(this);
        }
    }

    documentProxy_ = new DocumentProxy(webWindow_);

    navigator_ = new Navigator();
    navigator_.setParentScope(this);
    navigator_.setPrototype(getPrototype(navigator_.getClass()));

    screen_ = new Screen();
    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);

    console_ = new Console();
    ((Console) console_).setWebWindow(webWindow_);
    console_.setParentScope(this);
    ((Console) console_).setPrototype(getPrototype(((SimpleScriptable) console_).getClass()));

    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();
        }
    }
}
 
Example 12
Source File: Window.java    From HtmlUnit-Android with Apache License 2.0 4 votes vote down vote up
/**
 * Initializes this window.
 * @param webWindow the web window corresponding to this window
 */
public void initialize(final WebWindow webWindow) {
    webWindow_ = webWindow;
    webWindow_.setScriptableObject(this);

    windowProxy_ = new WindowProxy(webWindow_);

    final Page enclosedPage = webWindow.getEnclosedPage();
    if (enclosedPage instanceof XmlPage) {
        document_ = new XMLDocument();
    }
    else {
        document_ = new HTMLDocument();
    }
    document_.setParentScope(this);
    document_.setPrototype(getPrototype(document_.getClass()));
    document_.setWindow(this);

    if (enclosedPage instanceof SgmlPage) {
        final SgmlPage page = (SgmlPage) enclosedPage;
        document_.setDomNode(page);

        final DomHtmlAttributeChangeListenerImpl listener = new DomHtmlAttributeChangeListenerImpl();
        page.addDomChangeListener(listener);

        if (page.isHtmlPage()) {
            ((HtmlPage) page).addHtmlAttributeChangeListener(listener);
            ((HtmlPage) page).addAutoCloseable(this);
        }
    }

    documentProxy_ = new DocumentProxy(webWindow_);

    navigator_ = new Navigator();
    navigator_.setParentScope(this);
    navigator_.setPrototype(getPrototype(navigator_.getClass()));

    screen_ = new Screen();
    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);

    console_ = new Console();
    ((Console) console_).setWebWindow(webWindow_);
    console_.setParentScope(this);
    ((Console) console_).setPrototype(getPrototype(((SimpleScriptable) console_).getClass()));

    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();
        }
    }
}