Search in sources :

Example 96 with JsxFunction

use of com.gargoylesoftware.htmlunit.javascript.configuration.JsxFunction in project htmlunit by HtmlUnit.

the class HTMLCollectionFrames method alert.

/**
 * The JavaScript function {@code alert()}.
 * @param message the message
 */
@JsxFunction
public void alert(final Object message) {
    // use Object as parameter and perform String conversion by ourself
    // this allows to place breakpoint here and "see" the message object and its properties
    final String stringMessage = Context.toString(message);
    final AlertHandler handler = getWebWindow().getWebClient().getAlertHandler();
    if (handler == null) {
        if (LOG.isWarnEnabled()) {
            LOG.warn("window.alert(\"" + stringMessage + "\") no alert handler installed");
        }
    } else {
        handler.handleAlert(document_.getPage(), stringMessage);
    }
}
Also used : AlertHandler(com.gargoylesoftware.htmlunit.AlertHandler) JsxFunction(com.gargoylesoftware.htmlunit.javascript.configuration.JsxFunction)

Example 97 with JsxFunction

use of com.gargoylesoftware.htmlunit.javascript.configuration.JsxFunction in project htmlunit by HtmlUnit.

the class HTMLCollectionFrames method open.

/**
 * Opens a new window.
 *
 * @param url when a new document is opened, <i>url</i> is a String that specifies a MIME type for the document.
 *        When a new window is opened, <i>url</i> is a String that specifies the URL to render in the new window
 * @param name the name
 * @param features the features
 * @param replace whether to replace in the history list or no
 * @return the newly opened window, or {@code null} if popup windows have been disabled
 * @see com.gargoylesoftware.htmlunit.WebClientOptions#isPopupBlockerEnabled()
 * @see <a href="http://msdn.microsoft.com/en-us/library/ms536651.aspx">MSDN documentation</a>
 */
@JsxFunction
public WindowProxy open(final Object url, final Object name, final Object features, final Object replace) {
    String urlString = null;
    if (!Undefined.isUndefined(url)) {
        urlString = Context.toString(url);
    }
    String windowName = "";
    if (!Undefined.isUndefined(name)) {
        windowName = Context.toString(name);
    }
    String featuresString = null;
    if (!Undefined.isUndefined(features)) {
        featuresString = Context.toString(features);
    }
    final WebClient webClient = getWebWindow().getWebClient();
    if (webClient.getOptions().isPopupBlockerEnabled()) {
        if (LOG.isDebugEnabled()) {
            LOG.debug("Ignoring window.open() invocation because popups are blocked.");
        }
        return null;
    }
    boolean replaceCurrentEntryInBrowsingHistory = false;
    if (!Undefined.isUndefined(replace)) {
        replaceCurrentEntryInBrowsingHistory = Context.toBoolean(replace);
    }
    if ((featuresString != null || replaceCurrentEntryInBrowsingHistory) && LOG.isDebugEnabled()) {
        LOG.debug("window.open: features and replaceCurrentEntryInBrowsingHistory " + "not implemented: url=[" + urlString + "] windowName=[" + windowName + "] features=[" + featuresString + "] replaceCurrentEntry=[" + replaceCurrentEntryInBrowsingHistory + "]");
    }
    // if specified name is the name of an existing window, then hold it
    if (StringUtils.isEmpty(urlString) && !"".equals(windowName)) {
        try {
            final WebWindow webWindow = webClient.getWebWindowByName(windowName);
            return getProxy(webWindow);
        } catch (final WebWindowNotFoundException e) {
        // nothing
        }
    }
    final URL newUrl = makeUrlForOpenWindow(urlString);
    final WebWindow newWebWindow = webClient.openWindow(newUrl, windowName, webWindow_);
    return getProxy(newWebWindow);
}
Also used : WebClient(com.gargoylesoftware.htmlunit.WebClient) WebWindowNotFoundException(com.gargoylesoftware.htmlunit.WebWindowNotFoundException) URL(java.net.URL) WebWindow(com.gargoylesoftware.htmlunit.WebWindow) JsxFunction(com.gargoylesoftware.htmlunit.javascript.configuration.JsxFunction)

Example 98 with JsxFunction

use of com.gargoylesoftware.htmlunit.javascript.configuration.JsxFunction in project htmlunit by HtmlUnit.

the class HTMLCollectionFrames method scrollByPages.

/**
 * Scrolls the window content down by the specified number of pages.
 * @param pages the number of pages to scroll down
 */
@JsxFunction({ FF, FF_ESR })
public void scrollByPages(final int pages) {
    final HTMLElement body = document_.getBody();
    if (body != null) {
        body.setScrollTop(body.getScrollTop() + (getInnerHeight() * pages));
        final Event event = new Event(body, Event.TYPE_SCROLL);
        body.fireEvent(event);
    }
}
Also used : HTMLElement(com.gargoylesoftware.htmlunit.javascript.host.html.HTMLElement) MouseEvent(com.gargoylesoftware.htmlunit.javascript.host.event.MouseEvent) HtmlAttributeChangeEvent(com.gargoylesoftware.htmlunit.html.HtmlAttributeChangeEvent) Event(com.gargoylesoftware.htmlunit.javascript.host.event.Event) MessageEvent(com.gargoylesoftware.htmlunit.javascript.host.event.MessageEvent) JsxFunction(com.gargoylesoftware.htmlunit.javascript.configuration.JsxFunction)

Example 99 with JsxFunction

use of com.gargoylesoftware.htmlunit.javascript.configuration.JsxFunction in project htmlunit by HtmlUnit.

the class HTMLCollectionFrames method matchMedia.

/**
 * Returns a new MediaQueryList object representing the parsed results of the specified media query string.
 *
 * @param mediaQueryString the media query
 * @return a new MediaQueryList object
 */
@JsxFunction
public MediaQueryList matchMedia(final String mediaQueryString) {
    final MediaQueryList mediaQueryList = new MediaQueryList(mediaQueryString);
    mediaQueryList.setParentScope(this);
    mediaQueryList.setPrototype(getPrototype(mediaQueryList.getClass()));
    return mediaQueryList;
}
Also used : MediaQueryList(com.gargoylesoftware.htmlunit.javascript.host.css.MediaQueryList) JsxFunction(com.gargoylesoftware.htmlunit.javascript.configuration.JsxFunction)

Example 100 with JsxFunction

use of com.gargoylesoftware.htmlunit.javascript.configuration.JsxFunction in project htmlunit by HtmlUnit.

the class XMLHTTPRequest method open.

/**
 * Initializes the request and specifies the method, URL, and authentication information for the request.
 * @param method the HTTP method used to open the connection, such as GET, POST, PUT, or PROPFIND;
 *      for XMLHTTP, this parameter is not case-sensitive; the verbs TRACE and TRACK are not allowed.
 * @param url the requested URL; this can be either an absolute URL or a relative URL
 * @param asyncParam indicator of whether the call is asynchronous; the default is {@code true} (the call
 *     returns immediately); if set to {@code true}, attach an <code>onreadystatechange</code> property
 *     callback so that you can tell when the <code>send</code> call has completed
 * @param user the name of the user for authentication
 * @param password the password for authentication
 */
@JsxFunction
public void open(final String method, final Object url, final Object asyncParam, final Object user, final Object password) {
    if (method == null || "null".equals(method)) {
        throw Context.reportRuntimeError("Type mismatch (method is null).");
    }
    if (url == null || "null".equals(url)) {
        throw Context.reportRuntimeError("Type mismatch (url is null).");
    }
    state_ = STATE_UNSENT;
    openedMultipleTimes_ = webRequest_ != null;
    sent_ = false;
    webRequest_ = null;
    webResponse_ = null;
    if ("".equals(method) || "TRACE".equalsIgnoreCase(method)) {
        throw Context.reportRuntimeError("Invalid procedure call or argument (method is invalid).");
    }
    if ("".equals(url)) {
        throw Context.reportRuntimeError("Invalid procedure call or argument (url is empty).");
    }
    // defaults to true if not specified
    boolean async = true;
    if (!Undefined.isUndefined(asyncParam)) {
        async = ScriptRuntime.toBoolean(asyncParam);
    }
    final String urlAsString = Context.toString(url);
    // (URL + Method + User + Password) become a WebRequest instance.
    containingPage_ = (HtmlPage) getWindow().getWebWindow().getEnclosedPage();
    try {
        final URL fullUrl = containingPage_.getFullyQualifiedUrl(urlAsString);
        final WebRequest request = new WebRequest(fullUrl);
        request.setCharset(UTF_8);
        request.setAdditionalHeader(HttpHeader.REFERER, containingPage_.getUrl().toExternalForm());
        request.setHttpMethod(HttpMethod.valueOf(method.toUpperCase(Locale.ROOT)));
        // password is ignored if no user defined
        if (user != null && !Undefined.isUndefined(user)) {
            final String userCred = user.toString();
            String passwordCred = "";
            if (password != null && !Undefined.isUndefined(password)) {
                passwordCred = password.toString();
            }
            request.setCredentials(new UsernamePasswordCredentials(userCred, passwordCred));
        }
        webRequest_ = request;
    } catch (final MalformedURLException e) {
        if (LOG.isErrorEnabled()) {
            LOG.error("Unable to initialize XMLHTTPRequest using malformed URL '" + urlAsString + "'.");
        }
        return;
    } catch (final IllegalArgumentException e) {
        if (LOG.isErrorEnabled()) {
            LOG.error("Unable to initialize XMLHTTPRequest using illegal argument '" + e.getMessage() + "'.");
        }
        webRequest_ = null;
    }
    // Async stays a boolean.
    async_ = async;
    // Change the state!
    setState(STATE_OPENED, null);
}
Also used : MalformedURLException(java.net.MalformedURLException) WebRequest(com.gargoylesoftware.htmlunit.WebRequest) URL(java.net.URL) UsernamePasswordCredentials(org.apache.http.auth.UsernamePasswordCredentials) JsxFunction(com.gargoylesoftware.htmlunit.javascript.configuration.JsxFunction)

Aggregations

JsxFunction (com.gargoylesoftware.htmlunit.javascript.configuration.JsxFunction)133 DomNode (com.gargoylesoftware.htmlunit.html.DomNode)30 HtmlPage (com.gargoylesoftware.htmlunit.html.HtmlPage)20 ScriptableObject (net.sourceforge.htmlunit.corejs.javascript.ScriptableObject)19 WebWindow (com.gargoylesoftware.htmlunit.WebWindow)16 URL (java.net.URL)14 IOException (java.io.IOException)11 Scriptable (net.sourceforge.htmlunit.corejs.javascript.Scriptable)11 WebClient (com.gargoylesoftware.htmlunit.WebClient)10 DomElement (com.gargoylesoftware.htmlunit.html.DomElement)10 SimpleRange (com.gargoylesoftware.htmlunit.html.impl.SimpleRange)10 Event (com.gargoylesoftware.htmlunit.javascript.host.event.Event)10 SgmlPage (com.gargoylesoftware.htmlunit.SgmlPage)8 HtmlUnitScriptable (com.gargoylesoftware.htmlunit.javascript.HtmlUnitScriptable)8 MessageEvent (com.gargoylesoftware.htmlunit.javascript.host.event.MessageEvent)8 HTMLElement (com.gargoylesoftware.htmlunit.javascript.host.html.HTMLElement)8 NameValuePair (com.gargoylesoftware.htmlunit.util.NameValuePair)8 Range (org.w3c.dom.ranges.Range)8 HtmlAttributeChangeEvent (com.gargoylesoftware.htmlunit.html.HtmlAttributeChangeEvent)7 HtmlElement (com.gargoylesoftware.htmlunit.html.HtmlElement)7