Java Code Examples for com.gargoylesoftware.htmlunit.javascript.AbstractJavaScriptEngine#isScriptRunning()

The following examples show how to use com.gargoylesoftware.htmlunit.javascript.AbstractJavaScriptEngine#isScriptRunning() . 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: HtmlInput.java    From htmlunit with Apache License 2.0 6 votes vote down vote up
/**
 * Executes the onchange script code for this element if this is appropriate.
 * This means that the element must have an onchange script, script must be enabled
 * and the change in the element must not have been triggered by a script.
 *
 * @param htmlElement the element that contains the onchange attribute
 * @return the page that occupies this window after this method completes (may or
 *         may not be the same as the original page)
 */
static Page executeOnChangeHandlerIfAppropriate(final HtmlElement htmlElement) {
    final SgmlPage page = htmlElement.getPage();

    final AbstractJavaScriptEngine<?> engine = htmlElement.getPage().getWebClient().getJavaScriptEngine();
    if (engine.isScriptRunning()) {
        return page;
    }
    final ScriptResult scriptResult = htmlElement.fireEvent(Event.TYPE_CHANGE);

    if (page.getWebClient().containsWebWindow(page.getEnclosingWindow())) {
        // may be itself or a newly loaded one
        return page.getEnclosingWindow().getEnclosedPage();
    }

    if (scriptResult != null) {
        // current window doesn't exist anymore
        return page.getWebClient().getCurrentWindow().getEnclosedPage();
    }

    return page;
}
 
Example 2
Source File: HtmlInput.java    From HtmlUnit-Android with Apache License 2.0 6 votes vote down vote up
/**
 * Executes the onchange script code for this element if this is appropriate.
 * This means that the element must have an onchange script, script must be enabled
 * and the change in the element must not have been triggered by a script.
 *
 * @param htmlElement the element that contains the onchange attribute
 * @return the page that occupies this window after this method completes (may or
 *         may not be the same as the original page)
 */
static Page executeOnChangeHandlerIfAppropriate(final HtmlElement htmlElement) {
    final SgmlPage page = htmlElement.getPage();

    final AbstractJavaScriptEngine<?> engine = htmlElement.getPage().getWebClient().getJavaScriptEngine();
    if (engine.isScriptRunning()) {
        return page;
    }
    final ScriptResult scriptResult = htmlElement.fireEvent(Event.TYPE_CHANGE);

    if (page.getWebClient().containsWebWindow(page.getEnclosingWindow())) {
        // may be itself or a newly loaded one
        return page.getEnclosingWindow().getEnclosedPage();
    }

    if (scriptResult != null) {
        // current window doesn't exist anymore
        return scriptResult.getNewPage();
    }

    return page;
}
 
Example 3
Source File: BaseFrameElement.java    From htmlunit with Apache License 2.0 5 votes vote down vote up
/**
 * <span style="color:red">INTERNAL API - SUBJECT TO CHANGE AT ANY TIME - USE AT YOUR OWN RISK.</span><br>
 *
 * Called after the node for the {@code frame} or {@code iframe} has been added to the containing page.
 * The node needs to be added first to allow JavaScript in the frame to see the frame in the parent.
 * @throws FailingHttpStatusCodeException if the server returns a failing status code AND the property
 *      {@link com.gargoylesoftware.htmlunit.WebClientOptions#setThrowExceptionOnFailingStatusCode(boolean)} is
 *      set to true
 */

public void loadInnerPage() throws FailingHttpStatusCodeException {
    String source = getSrcAttribute();
    if (source.isEmpty() || StringUtils.startsWithIgnoreCase(source, WebClient.ABOUT_SCHEME)) {
        source = WebClient.ABOUT_BLANK;
    }

    loadInnerPageIfPossible(source);

    final Page enclosedPage = getEnclosedPage();
    if (enclosedPage != null && enclosedPage.isHtmlPage()) {
        final HtmlPage htmlPage = (HtmlPage) enclosedPage;

        final AbstractJavaScriptEngine<?> jsEngine = getPage().getWebClient().getJavaScriptEngine();
        if (jsEngine != null && jsEngine.isScriptRunning()) {
            final PostponedAction action = new PostponedAction(getPage()) {
                @Override
                public void execute() throws Exception {
                    htmlPage.setReadyState(READY_STATE_COMPLETE);
                }
            };
            jsEngine.addPostponedAction(action);
        }
        else {
            htmlPage.setReadyState(READY_STATE_COMPLETE);
        }
    }
}
 
Example 4
Source File: BaseFrameElement.java    From htmlunit with Apache License 2.0 5 votes vote down vote up
private void loadSrc() {
    loadSrcWhenAddedToPage_ = false;
    final String src = getSrcAttribute();

    final AbstractJavaScriptEngine<?> jsEngine = getPage().getWebClient().getJavaScriptEngine();
    // When src is set from a script, loading is postponed until script finishes
    // in fact this implementation is probably wrong: JavaScript URL should be
    // first evaluated and only loading, when any, should be postponed.
    if (jsEngine == null || !jsEngine.isScriptRunning()
            || src.startsWith(JavaScriptURLConnection.JAVASCRIPT_PREFIX)) {
        loadInnerPageIfPossible(src);
    }
    else {
        final Page pageInFrame = getEnclosedPage();
        final PostponedAction action = new PostponedAction(getPage()) {
            @Override
            public void execute() throws Exception {
                if (!src.isEmpty() && getSrcAttribute().equals(src)) {
                    loadInnerPage();
                }
            }

            @Override
            public boolean isStillAlive() {
                // skip if page in frame has already been changed
                return super.isStillAlive() && pageInFrame == getEnclosedPage();
            }
        };
        jsEngine.addPostponedAction(action);
    }
}
 
Example 5
Source File: BaseFrameElement.java    From HtmlUnit-Android with Apache License 2.0 5 votes vote down vote up
/**
 * <span style="color:red">INTERNAL API - SUBJECT TO CHANGE AT ANY TIME - USE AT YOUR OWN RISK.</span><br>
 *
 * Called after the node for the {@code frame} or {@code iframe} has been added to the containing page.
 * The node needs to be added first to allow JavaScript in the frame to see the frame in the parent.
 * @throws FailingHttpStatusCodeException if the server returns a failing status code AND the property
 *      {@link com.gargoylesoftware.htmlunit.WebClientOptions#setThrowExceptionOnFailingStatusCode(boolean)} is
 *      set to true
 */

public void loadInnerPage() throws FailingHttpStatusCodeException {
    String source = getSrcAttribute();
    if (source.isEmpty() || StringUtils.startsWithIgnoreCase(source, WebClient.ABOUT_SCHEME)) {
        source = WebClient.ABOUT_BLANK;
    }

    loadInnerPageIfPossible(source);

    final Page enclosedPage = getEnclosedPage();
    if (enclosedPage != null && enclosedPage.isHtmlPage()) {
        final HtmlPage htmlPage = (HtmlPage) enclosedPage;
        final AbstractJavaScriptEngine<?> jsEngine = getPage().getWebClient().getJavaScriptEngine();
        if (jsEngine.isScriptRunning()) {
            final PostponedAction action = new PostponedAction(getPage()) {
                @Override
                public void execute() throws Exception {
                    htmlPage.setReadyState(READY_STATE_COMPLETE);
                }
            };
            jsEngine.addPostponedAction(action);
        }
        else {
            htmlPage.setReadyState(READY_STATE_COMPLETE);
        }
    }
}
 
Example 6
Source File: BaseFrameElement.java    From HtmlUnit-Android with Apache License 2.0 5 votes vote down vote up
private void loadSrc() {
    loadSrcWhenAddedToPage_ = false;
    final String src = getSrcAttribute();

    final AbstractJavaScriptEngine<?> jsEngine = getPage().getWebClient().getJavaScriptEngine();
    // When src is set from a script, loading is postponed until script finishes
    // in fact this implementation is probably wrong: JavaScript URL should be
    // first evaluated and only loading, when any, should be postponed.
    if (!jsEngine.isScriptRunning() || src.startsWith(JavaScriptURLConnection.JAVASCRIPT_PREFIX)) {
        loadInnerPageIfPossible(src);
    }
    else {
        final Page pageInFrame = getEnclosedPage();
        final PostponedAction action = new PostponedAction(getPage()) {
            @Override
            public void execute() throws Exception {
                if (!src.isEmpty() && getSrcAttribute().equals(src)) {
                    loadInnerPage();
                }
            }

            @Override
            public boolean isStillAlive() {
                // skip if page in frame has already been changed
                return super.isStillAlive() && pageInFrame == getEnclosedPage();
            }
        };
        jsEngine.addPostponedAction(action);
    }
}