Java Code Examples for com.gargoylesoftware.htmlunit.WebWindow#getScriptableObject()

The following examples show how to use com.gargoylesoftware.htmlunit.WebWindow#getScriptableObject() . 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: JavaScriptEngine.java    From HtmlUnit-Android with Apache License 2.0 6 votes vote down vote up
/**
 * Handles an exception that occurred during execution of JavaScript code.
 * @param scriptException the exception
 * @param triggerOnError if true, this triggers the onerror handler
 */
protected void handleJavaScriptException(final ScriptException scriptException, final boolean triggerOnError) {
    // Trigger window.onerror, if it has been set.
    final HtmlPage page = scriptException.getPage();
    if (triggerOnError && page != null) {
        final WebWindow window = page.getEnclosingWindow();
        if (window != null) {
            final Window w = window.getScriptableObject();
            if (w != null) {
                try {
                    w.triggerOnError(scriptException);
                }
                catch (final Exception e) {
                    handleJavaScriptException(new ScriptException(page, e, null), false);
                }
            }
        }
    }
    getWebClient().getJavaScriptErrorListener().scriptException(page, scriptException);
    // Throw a Java exception if the user wants us to.
    if (getWebClient().getOptions().isThrowExceptionOnScriptError()) {
        throw scriptException;
    }
    // Log the error; ScriptException instances provide good debug info.
    LOG.info("Caught script exception", scriptException);
}
 
Example 2
Source File: Window.java    From htmlunit with Apache License 2.0 5 votes vote down vote up
/**
 * Returns the value of the {@code top} property.
 * @return the value of {@code top}
 */
@JsxGetter
public Object getTop() {
    if (top_ != NOT_FOUND) {
        return top_;
    }

    final WebWindow top = getWebWindow().getTopWindow();
    return top.getScriptableObject();
}
 
Example 3
Source File: Window.java    From htmlunit with Apache License 2.0 5 votes vote down vote up
@Override
protected Scriptable getScriptableForElement(final Object obj) {
    final WebWindow window;
    if (obj instanceof BaseFrameElement) {
        window = ((BaseFrameElement) obj).getEnclosedWindow();
    }
    else {
        window = ((FrameWindow) obj).getFrameElement().getEnclosedWindow();
    }

    return window.getScriptableObject();
}
 
Example 4
Source File: JavaScriptEngine.java    From htmlunit with Apache License 2.0 5 votes vote down vote up
/**
 * Handles an exception that occurred during execution of JavaScript code.
 * @param scriptException the exception
 * @param triggerOnError if true, this triggers the onerror handler
 */
protected void handleJavaScriptException(final ScriptException scriptException, final boolean triggerOnError) {
    // shutdown was already called
    final WebClient webClient = getWebClient();
    if (webClient == null) {
        if (LOG.isInfoEnabled()) {
            LOG.info("handleJavaScriptException('" + scriptException.getMessage()
                + "') called after the shutdown of the Javascript engine - exception ignored.");
        }

        return;
    }

    // Trigger window.onerror, if it has been set.
    final HtmlPage page = scriptException.getPage();
    if (triggerOnError && page != null) {
        final WebWindow window = page.getEnclosingWindow();
        if (window != null) {
            final Window w = window.getScriptableObject();
            if (w != null) {
                try {
                    w.triggerOnError(scriptException);
                }
                catch (final Exception e) {
                    handleJavaScriptException(new ScriptException(page, e, null), false);
                }
            }
        }
    }

    webClient.getJavaScriptErrorListener().scriptException(page, scriptException);
    // Throw a Java exception if the user wants us to.
    if (webClient.getOptions().isThrowExceptionOnScriptError()) {
        throw scriptException;
    }
}
 
Example 5
Source File: Window.java    From HtmlUnit-Android with Apache License 2.0 5 votes vote down vote up
/**
 * Returns the value of the {@code top} property.
 * @return the value of {@code top}
 */
@JsxGetter
public Object getTop() {
    if (top_ != NOT_FOUND) {
        return top_;
    }

    final WebWindow top = getWebWindow().getTopWindow();
    return top.getScriptableObject();
}
 
Example 6
Source File: Window.java    From HtmlUnit-Android with Apache License 2.0 5 votes vote down vote up
@Override
protected Scriptable getScriptableForElement(final Object obj) {
    final WebWindow window;
    if (obj instanceof BaseFrameElement) {
        window = ((BaseFrameElement) obj).getEnclosedWindow();
    }
    else {
        window = ((FrameWindow) obj).getFrameElement().getEnclosedWindow();
    }

    return window.getScriptableObject();
}
 
Example 7
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 8
Source File: Window.java    From htmlunit with Apache License 2.0 4 votes vote down vote up
/**
 * Returns the value of the {@code parent} property.
 * @return the value of the {@code parent} property
 */
@JsxGetter
public ScriptableObject getParent() {
    final WebWindow parent = getWebWindow().getParentWindow();
    return parent.getScriptableObject();
}
 
Example 9
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();
        }
    }
}
 
Example 10
Source File: Window.java    From HtmlUnit-Android with Apache License 2.0 4 votes vote down vote up
/**
 * Returns the value of the {@code parent} property.
 * @return the value of the {@code parent} property
 */
@JsxGetter
public ScriptableObject getParent() {
    final WebWindow parent = getWebWindow().getParentWindow();
    return parent.getScriptableObject();
}
 
Example 11
Source File: Window.java    From htmlunit with Apache License 2.0 2 votes vote down vote up
/**
 * Returns the proxy for the specified window.
 * @param w the window whose proxy is to be returned
 * @return the proxy for the specified window
 */
public static WindowProxy getProxy(final WebWindow w) {
    return ((Window) w.getScriptableObject()).windowProxy_;
}
 
Example 12
Source File: Window.java    From HtmlUnit-Android with Apache License 2.0 2 votes vote down vote up
/**
 * Returns the proxy for the specified window.
 * @param w the window whose proxy is to be returned
 * @return the proxy for the specified window
 */
public static WindowProxy getProxy(final WebWindow w) {
    return ((Window) w.getScriptableObject()).windowProxy_;
}