Java Code Examples for com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine#callFunction()

The following examples show how to use com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine#callFunction() . 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: 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 3
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 4
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 5
Source File: WebSocket.java    From htmlunit with Apache License 2.0 5 votes vote down vote up
private void callFunction(final Function function, final Object[] args) {
    if (function == null) {
        return;
    }
    final Scriptable scope = function.getParentScope();
    final JavaScriptEngine engine
        = (JavaScriptEngine) containingPage_.getWebClient().getJavaScriptEngine();
    engine.callFunction(containingPage_, function, scope, WebSocket.this, args);
}
 
Example 6
Source File: HtmlPage.java    From htmlunit with Apache License 2.0 5 votes vote down vote up
private ScriptResult executeJavaScriptFunction(final Function function, final Scriptable thisObject,
        final Object[] args, final DomNode htmlElementScope) {

    final JavaScriptEngine engine = (JavaScriptEngine) getWebClient().getJavaScriptEngine();
    final Object result = engine.callFunction(this, function, thisObject, args, htmlElementScope);

    return new ScriptResult(result);
}
 
Example 7
Source File: WebSocket.java    From HtmlUnit-Android with Apache License 2.0 5 votes vote down vote up
private void callFunction(final Function function, final Object[] args) {
    if (function == null) {
        return;
    }
    final Scriptable scope = function.getParentScope();
    final JavaScriptEngine engine
        = (JavaScriptEngine) containingPage_.getWebClient().getJavaScriptEngine();
    engine.callFunction(containingPage_, function, scope, this, args);
}
 
Example 8
Source File: HtmlPage.java    From HtmlUnit-Android with Apache License 2.0 5 votes vote down vote up
private ScriptResult executeJavaScriptFunction(final Function function, final Scriptable thisObject,
        final Object[] args, final DomNode htmlElementScope) {

    final JavaScriptEngine engine = (JavaScriptEngine) getWebClient().getJavaScriptEngine();
    final Object result = engine.callFunction(this, function, thisObject, args, htmlElementScope);

    return new ScriptResult(result, getWebClient().getCurrentWindow().getEnclosedPage());
}
 
Example 9
Source File: Geolocation.java    From htmlunit with Apache License 2.0 4 votes vote down vote up
private void doGetPosition() {
    final String os = System.getProperty("os.name").toLowerCase(Locale.ROOT);
    String wifiStringString = null;
    if (os.contains("win")) {
        wifiStringString = getWifiStringWindows();
    }
    if (wifiStringString != null) {
        String url = PROVIDER_URL_;
        if (url.contains("?")) {
            url += '&';
        }
        else {
            url += '?';
        }
        url += "browser=firefox&sensor=true";
        url += wifiStringString;

        while (url.length() >= 1900) {
            url = url.substring(0, url.lastIndexOf("&wifi="));
        }

        if (LOG.isInfoEnabled()) {
            LOG.info("Invoking URL: " + url);
        }

        try (WebClient webClient = new WebClient(BrowserVersion.FIREFOX_60)) {
            final Page page = webClient.getPage(url);
            final String content = page.getWebResponse().getContentAsString();
            if (LOG.isDebugEnabled()) {
                LOG.debug("Receieved Content: " + content);
            }
            final double latitude = Double.parseDouble(getJSONValue(content, "lat"));
            final double longitude = Double.parseDouble(getJSONValue(content, "lng"));
            final double accuracy = Double.parseDouble(getJSONValue(content, "accuracy"));

            final Coordinates coordinates = new Coordinates(latitude, longitude, accuracy);
            coordinates.setPrototype(getPrototype(coordinates.getClass()));

            final Position position = new Position(coordinates);
            position.setPrototype(getPrototype(position.getClass()));

            final WebWindow ww = getWindow().getWebWindow();
            final JavaScriptEngine jsEngine =
                    (JavaScriptEngine) ww.getWebClient().getJavaScriptEngine();
            jsEngine.callFunction((HtmlPage) ww.getEnclosedPage(), successHandler_, this,
                    getParentScope(), new Object[] {position});
        }
        catch (final Exception e) {
            LOG.error("", e);
        }
    }
    else {
        if (LOG.isErrorEnabled()) {
            LOG.error("Operating System not supported: " + os);
        }
    }
}
 
Example 10
Source File: Geolocation.java    From HtmlUnit-Android with Apache License 2.0 4 votes vote down vote up
private void doGetPosition() {
    final String os = System.getProperty("os.name").toLowerCase(Locale.ROOT);
    String wifiStringString = null;
    if (os.contains("win")) {
        wifiStringString = getWifiStringWindows();
    }
    if (wifiStringString != null) {
        String url = PROVIDER_URL_;
        if (url.contains("?")) {
            url += '&';
        }
        else {
            url += '?';
        }
        url += "browser=firefox&sensor=true";
        url += wifiStringString;

        while (url.length() >= 1900) {
            url = url.substring(0, url.lastIndexOf("&wifi="));
        }

        if (LOG.isInfoEnabled()) {
            LOG.info("Invoking URL: " + url);
        }

        try (WebClient webClient = new WebClient(BrowserVersion.FIREFOX_45)) {
            final Page page = webClient.getPage(url);
            final String content = page.getWebResponse().getContentAsString();
            if (LOG.isDebugEnabled()) {
                LOG.debug("Receieved Content: " + content);
            }
            final double latitude = Double.parseDouble(getJSONValue(content, "lat"));
            final double longitude = Double.parseDouble(getJSONValue(content, "lng"));
            final double accuracy = Double.parseDouble(getJSONValue(content, "accuracy"));

            final Coordinates coordinates = new Coordinates(latitude, longitude, accuracy);
            coordinates.setPrototype(getPrototype(coordinates.getClass()));

            final Position position = new Position(coordinates);
            position.setPrototype(getPrototype(position.getClass()));

            final WebWindow ww = getWindow().getWebWindow();
            final JavaScriptEngine jsEngine =
                    (JavaScriptEngine) ww.getWebClient().getJavaScriptEngine();
            jsEngine.callFunction((HtmlPage) ww.getEnclosedPage(), successHandler_, this,
                    getParentScope(), new Object[] {position});
        }
        catch (final Exception e) {
            LOG.error("", e);
        }
    }
    else {
        LOG.error("Operating System not supported: " + os);
    }
}