Java Code Examples for net.sourceforge.htmlunit.corejs.javascript.ScriptableObject#get()

The following examples show how to use net.sourceforge.htmlunit.corejs.javascript.ScriptableObject#get() . 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: Window.java    From htmlunit with Apache License 2.0 6 votes vote down vote up
/**
 * Creates a modal dialog box that displays the specified HTML document.
 * @param url the URL of the document to load and display
 * @param arguments object to be made available via <tt>window.dialogArguments</tt> in the dialog window
 * @param features string that specifies the window ornaments for the dialog window
 * @return the value of the {@code returnValue} property as set by the modal dialog's window
 * @see <a href="http://msdn.microsoft.com/en-us/library/ms536759.aspx">MSDN Documentation</a>
 * @see <a href="https://developer.mozilla.org/en/DOM/window.showModalDialog">Mozilla Documentation</a>
 */
@JsxFunction(IE)
public Object showModalDialog(final String url, final Object arguments, final String features) {
    final WebWindow webWindow = getWebWindow();
    final WebClient client = webWindow.getWebClient();
    try {
        final URL completeUrl = ((HtmlPage) getDomNodeOrDie()).getFullyQualifiedUrl(url);
        final DialogWindow dialog = client.openDialogWindow(completeUrl, webWindow, arguments);
        // TODO: Theoretically, we shouldn't return until the dialog window has been close()'ed...
        // But we have to return so that the window can be close()'ed...
        // Maybe we can use Rhino's continuation support to save state and restart when
        // the dialog window is close()'ed? Would only work in interpreted mode, though.
        final ScriptableObject jsDialog = dialog.getScriptableObject();
        return jsDialog.get("returnValue", jsDialog);
    }
    catch (final IOException e) {
        throw Context.throwAsScriptRuntimeEx(e);
    }
}
 
Example 2
Source File: HtmlPage.java    From HtmlUnit-Android with Apache License 2.0 6 votes vote down vote up
private static String getAttributeValue(final DomElement element, final String attribute) {
    // first try real attributes
    String value = element.getAttribute(attribute);

    if (DomElement.ATTRIBUTE_NOT_DEFINED == value
            && !(element instanceof HtmlObject)) {
        // second try are JavaScript attributes
        // ...but applets/objects are a bit special so ignore them
        final Object o = element.getScriptableObject();
        if (o instanceof ScriptableObject) {
            final ScriptableObject scriptObject = (ScriptableObject) o;
            // we have to make sure the scriptObject has a slot for the given attribute.
            // just using get() may use e.g. getWithPreemption().
            if (scriptObject.has(attribute, scriptObject)) {
                final Object jsValue = scriptObject.get(attribute, scriptObject);
                if (jsValue != Scriptable.NOT_FOUND && jsValue instanceof String) {
                    value = (String) jsValue;
                }
            }
        }
    }
    return value;
}
 
Example 3
Source File: Event.java    From HtmlUnit-Android with Apache License 2.0 6 votes vote down vote up
/**
 * JavaScript constructor.
 *
 * @param type the event type
 * @param details the event details (optional)
 */
@JsxConstructor({CHROME, FF, EDGE})
public void jsConstructor(final String type, final ScriptableObject details) {
    boolean bubbles = false;
    boolean cancelable = false;

    if (details != null && details != Undefined.instance) {
        final Boolean detailBubbles = (Boolean) details.get("bubbles");
        if (detailBubbles != null) {
            bubbles = detailBubbles.booleanValue();
        }

        final Boolean detailCancelable = (Boolean) details.get("cancelable");
        if (detailCancelable != null) {
            cancelable = detailCancelable.booleanValue();
        }
    }
    initEvent(type, bubbles, cancelable);
}
 
Example 4
Source File: PointerEvent.java    From HtmlUnit-Android with Apache License 2.0 6 votes vote down vote up
private static Object getValue(final ScriptableObject object, final String name, final Object defaulValue) {
    Object value = object.get(name);
    if (value != null) {
        if (defaulValue instanceof String) {
            value = String.valueOf(value);
        }
        else if (defaulValue instanceof Double) {
            value = (double) Context.toNumber(value);
        }
        else if (defaulValue instanceof Number) {
            value = (int) Context.toNumber(value);
        }
        else {
            value = Context.toBoolean(value);
        }
    }
    else {
        value = defaulValue;
    }
    return value;
}
 
Example 5
Source File: Window.java    From HtmlUnit-Android with Apache License 2.0 6 votes vote down vote up
/**
 * Creates a modal dialog box that displays the specified HTML document.
 * @param url the URL of the document to load and display
 * @param arguments object to be made available via <tt>window.dialogArguments</tt> in the dialog window
 * @param features string that specifies the window ornaments for the dialog window
 * @return the value of the {@code returnValue} property as set by the modal dialog's window
 * @see <a href="http://msdn.microsoft.com/en-us/library/ms536759.aspx">MSDN Documentation</a>
 * @see <a href="https://developer.mozilla.org/en/DOM/window.showModalDialog">Mozilla Documentation</a>
 */
@JsxFunction({IE, FF})
public Object showModalDialog(final String url, final Object arguments, final String features) {
    final WebWindow webWindow = getWebWindow();
    final WebClient client = webWindow.getWebClient();
    try {
        final URL completeUrl = ((HtmlPage) getDomNodeOrDie()).getFullyQualifiedUrl(url);
        final DialogWindow dialog = client.openDialogWindow(completeUrl, webWindow, arguments);
        // TODO: Theoretically, we shouldn't return until the dialog window has been close()'ed...
        // But we have to return so that the window can be close()'ed...
        // Maybe we can use Rhino's continuation support to save state and restart when
        // the dialog window is close()'ed? Would only work in interpreted mode, though.
        final ScriptableObject jsDialog = dialog.getScriptableObject();
        return jsDialog.get("returnValue", jsDialog);
    }
    catch (final IOException e) {
        throw Context.throwAsScriptRuntimeEx(e);
    }
}
 
Example 6
Source File: HtmlPage.java    From htmlunit with Apache License 2.0 6 votes vote down vote up
private String getAttributeValue(final DomElement element, final String attribute) {
    // first try real attributes
    String value = element.getAttribute(attribute);

    if (DomElement.ATTRIBUTE_NOT_DEFINED == value
            && getWebClient().isJavaScriptEngineEnabled()
            && !(element instanceof HtmlApplet)
            && !(element instanceof HtmlObject)) {
        // second try are JavaScript attributes
        // ...but applets/objects are a bit special so ignore them
        final Object o = element.getScriptableObject();
        if (o instanceof ScriptableObject) {
            final ScriptableObject scriptObject = (ScriptableObject) o;
            // we have to make sure the scriptObject has a slot for the given attribute.
            // just using get() may use e.g. getWithPreemption().
            if (scriptObject.has(attribute, scriptObject)) {
                final Object jsValue = scriptObject.get(attribute, scriptObject);
                if (jsValue != Scriptable.NOT_FOUND && jsValue instanceof String) {
                    value = (String) jsValue;
                }
            }
        }
    }
    return value;
}
 
Example 7
Source File: Event.java    From htmlunit with Apache License 2.0 6 votes vote down vote up
/**
 * JavaScript constructor.
 *
 * @param type the event type
 * @param details the event details (optional)
 */
@JsxConstructor({CHROME, FF, FF68, FF60})
public void jsConstructor(final String type, final ScriptableObject details) {
    boolean bubbles = false;
    boolean cancelable = false;

    if (details != null && !Undefined.isUndefined(details)) {
        final Boolean detailBubbles = (Boolean) details.get("bubbles");
        if (detailBubbles != null) {
            bubbles = detailBubbles.booleanValue();
        }

        final Boolean detailCancelable = (Boolean) details.get("cancelable");
        if (detailCancelable != null) {
            cancelable = detailCancelable.booleanValue();
        }
    }
    initEvent(type, bubbles, cancelable);
}
 
Example 8
Source File: PointerEvent.java    From htmlunit with Apache License 2.0 6 votes vote down vote up
private static Object getValue(final ScriptableObject object, final String name, final Object defaulValue) {
    Object value = object.get(name);
    if (value != null) {
        if (defaulValue instanceof String) {
            value = String.valueOf(value);
        }
        else if (defaulValue instanceof Double) {
            value = Context.toNumber(value);
        }
        else if (defaulValue instanceof Number) {
            value = (int) Context.toNumber(value);
        }
        else {
            value = Context.toBoolean(value);
        }
    }
    else {
        value = defaulValue;
    }
    return value;
}
 
Example 9
Source File: HashChangeEvent.java    From htmlunit with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
@JsxConstructor({CHROME, FF, FF68, FF60})
public void jsConstructor(final String type, final ScriptableObject details) {
    super.jsConstructor(type, details);

    String oldURL = "";
    String newURL = "";
    if (details != null && !Undefined.isUndefined(details)) {
        oldURL = (String) details.get("oldURL");
        newURL = (String) details.get("newURL");
    }
    oldURL_ = oldURL;
    newURL_ = newURL;
}
 
Example 10
Source File: Console.java    From HtmlUnit-Android with Apache License 2.0 5 votes vote down vote up
/**
 * Implementation of console {@code dir} function. This method does not enter recursively
 * in the passed object, nor prints the details of objects or functions.
 * @param o the object to be printed
 */
@JsxFunction
public void dir(final Object o) {
    if (o instanceof ScriptableObject) {
        final ScriptableObject obj = (ScriptableObject) o;
        final Object[] ids = obj.getIds();
        if (ids != null && ids.length > 0) {
            final StringBuilder sb = new StringBuilder();
            for (Object id : ids) {
                final Object value = obj.get(id);
                if (value instanceof Delegator) {
                    sb.append(id + ": " + ((Delegator) value).getClassName() + "\n");
                }
                else if (value instanceof SimpleScriptable) {
                    sb.append(id + ": " + ((SimpleScriptable) value).getClassName() + "\n");
                }
                else if (value instanceof BaseFunction) {
                    sb.append(id + ": function " + ((BaseFunction) value).getFunctionName() + "()\n");
                }
                else {
                    sb.append(id + ": " + value  + "\n");
                }
            }
            getWebConsole().info(sb.toString());
        }
    }
}
 
Example 11
Source File: PopStateEvent.java    From htmlunit with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
@JsxConstructor({CHROME, FF, FF68, FF60})
public void jsConstructor(final String type, final ScriptableObject details) {
    super.jsConstructor(type, details);

    if (details != null && !Undefined.isUndefined(details)) {
        state_ = details.get("state");
    }
}
 
Example 12
Source File: EventHandler.java    From HtmlUnit-Android with Apache License 2.0 5 votes vote down vote up
/**
 * Builds a function that will execute the JavaScript code provided.
 * @param node the element for which the event is build
 * @param eventName the event for which this handler is created
 * @param jsSnippet the JavaScript code
 */
public EventHandler(final DomNode node, final String eventName, final String jsSnippet) {
    node_ = node;
    eventName_ = eventName;

    jsSnippet_ = "function on" + eventName + "(event) {" + jsSnippet + "\n}";

    final ScriptableObject w = node.getPage().getEnclosingWindow().getScriptableObject();
    final Scriptable function = (Scriptable) w.get("Function", w);
    setPrototype(function.getPrototype());
}
 
Example 13
Source File: CloseEvent.java    From htmlunit with Apache License 2.0 5 votes vote down vote up
/**
 * JavaScript constructor.
 *
 * @param type the event type
 * @param details the event details (optional)
 */
@Override
@JsxConstructor({CHROME, FF, FF68, FF60})
public void jsConstructor(final String type, final ScriptableObject details) {
    super.jsConstructor(type, details);

    int code = 0;
    String reason = "";
    boolean wasClean = false;

    if (details != null && !Undefined.isUndefined(details)) {
        final Double detailCode = (Double) details.get("code");
        if (detailCode != null) {
            code = detailCode.intValue();
        }

        final String detailReason = (String) details.get("reason");
        if (detailReason != null) {
            reason = detailReason;
        }

        final Boolean detailWasClean = (Boolean) details.get("wasClean");
        if (detailWasClean != null) {
            wasClean = detailWasClean.booleanValue();
        }
    }
    code_ = code;
    reason_ = reason;
    wasClean_ = wasClean;
}
 
Example 14
Source File: CloseEvent.java    From HtmlUnit-Android with Apache License 2.0 5 votes vote down vote up
/**
 * JavaScript constructor.
 *
 * @param type the event type
 * @param details the event details (optional)
 */
@Override
@JsxConstructor({CHROME, FF, EDGE})
public void jsConstructor(final String type, final ScriptableObject details) {
    super.jsConstructor(type, details);

    int code = 0;
    String reason = "";
    boolean wasClean = false;

    if (details != null && details != Undefined.instance) {
        final Double detailCode = (Double) details.get("code");
        if (detailCode != null) {
            code = detailCode.intValue();
        }

        final String detailReason = (String) details.get("reason");
        if (detailReason != null) {
            reason = detailReason;
        }

        final Boolean detailWasClean = (Boolean) details.get("wasClean");
        if (detailWasClean != null) {
            wasClean = detailWasClean.booleanValue();
        }
    }
    code_ = code;
    reason_ = reason;
    wasClean_ = wasClean;
}
 
Example 15
Source File: PopStateEvent.java    From HtmlUnit-Android with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
@JsxConstructor({CHROME, FF, EDGE})
public void jsConstructor(final String type, final ScriptableObject details) {
    super.jsConstructor(type, details);

    if (details != null && details != Undefined.instance) {
        state_ = details.get("state");
    }
}
 
Example 16
Source File: HashChangeEvent.java    From HtmlUnit-Android with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 */
@Override
@JsxConstructor({CHROME, FF, EDGE})
public void jsConstructor(final String type, final ScriptableObject details) {
    super.jsConstructor(type, details);

    String oldURL = "";
    String newURL = "";
    if (details != null && details != Undefined.instance) {
        oldURL = (String) details.get("oldURL");
        newURL = (String) details.get("newURL");
    }
    oldURL_ = oldURL;
    newURL_ = newURL;
}
 
Example 17
Source File: Console.java    From htmlunit with Apache License 2.0 5 votes vote down vote up
/**
 * Implementation of console {@code dir} function. This method does not enter recursively
 * in the passed object, nor prints the details of objects or functions.
 * @param o the object to be printed
 */
@JsxFunction
public void dir(final Object o) {
    if (o instanceof ScriptableObject) {
        final ScriptableObject obj = (ScriptableObject) o;
        final Object[] ids = obj.getIds();
        if (ids != null && ids.length > 0) {
            final StringBuilder sb = new StringBuilder();
            for (Object id : ids) {
                final Object value = obj.get(id);
                if (value instanceof Delegator) {
                    sb.append(id + ": " + ((Delegator) value).getClassName() + "\n");
                }
                else if (value instanceof SimpleScriptable) {
                    sb.append(id + ": " + ((SimpleScriptable) value).getClassName() + "\n");
                }
                else if (value instanceof BaseFunction) {
                    sb.append(id + ": function " + ((BaseFunction) value).getFunctionName() + "()\n");
                }
                else {
                    sb.append(id + ": " + value  + "\n");
                }
            }
            getWebConsole().info(sb.toString());
        }
    }
}