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

The following examples show how to use com.gargoylesoftware.htmlunit.javascript.AbstractJavaScriptEngine#addPostponedAction() . 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: HtmlScript.java    From htmlunit with Apache License 2.0 6 votes vote down vote up
/**
 * If setting the <tt>src</tt> attribute, this method executes the new JavaScript if necessary
 * (behavior varies by browser version). {@inheritDoc}
 */
@Override
protected void setAttributeNS(final String namespaceURI, final String qualifiedName, final String attributeValue,
        final boolean notifyAttributeChangeListeners, final boolean notifyMutationObservers) {
    // special additional processing for the 'src'
    if (namespaceURI != null || !SRC_ATTRIBUTE.equals(qualifiedName)) {
        super.setAttributeNS(namespaceURI, qualifiedName, attributeValue, notifyAttributeChangeListeners,
                notifyMutationObservers);
        return;
    }

    final String oldValue = getAttributeNS(namespaceURI, qualifiedName);
    super.setAttributeNS(namespaceURI, qualifiedName, attributeValue, notifyAttributeChangeListeners,
            notifyMutationObservers);

    if (isAttachedToPage() && oldValue.isEmpty() && getFirstChild() == null) {
        final PostponedAction action = new PostponedAction(getPage()) {
            @Override
            public void execute() {
                executeScriptIfNeeded();
            }
        };
        final AbstractJavaScriptEngine<?> engine = getPage().getWebClient().getJavaScriptEngine();
        engine.addPostponedAction(action);
    }
}
 
Example 2
Source File: HtmlScript.java    From HtmlUnit-Android with Apache License 2.0 6 votes vote down vote up
/**
 * If setting the <tt>src</tt> attribute, this method executes the new JavaScript if necessary
 * (behavior varies by browser version). {@inheritDoc}
 */
@Override
protected void setAttributeNS(final String namespaceURI, final String qualifiedName, final String attributeValue,
        final boolean notifyAttributeChangeListeners, final boolean notifyMutationObservers) {
    // special additional processing for the 'src'
    if (namespaceURI != null || !"src".equals(qualifiedName)) {
        super.setAttributeNS(namespaceURI, qualifiedName, attributeValue, notifyAttributeChangeListeners,
                notifyMutationObservers);
        return;
    }

    final String oldValue = getAttributeNS(namespaceURI, qualifiedName);
    super.setAttributeNS(namespaceURI, qualifiedName, attributeValue, notifyAttributeChangeListeners,
            notifyMutationObservers);

    if (isAttachedToPage() && oldValue.isEmpty() && getFirstChild() == null) {
        final PostponedAction action = new PostponedAction(getPage()) {
            @Override
            public void execute() {
                executeScriptIfNeeded();
            }
        };
        final AbstractJavaScriptEngine<?> engine = getPage().getWebClient().getJavaScriptEngine();
        engine.addPostponedAction(action);
    }
}
 
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);
    }
}