Search in sources :

Example 1 with LambdaFunction

use of net.sourceforge.htmlunit.corejs.javascript.LambdaFunction in project htmlunit by HtmlUnit.

the class Blob method text.

/**
 * @return a Promise that resolves with a string containing the
 * contents of the blob, interpreted as UTF-8.
 */
@JsxFunction({ CHROME, EDGE, FF, FF_ESR })
public Object text() {
    final Scriptable scope = ScriptableObject.getTopLevelScope(this);
    final LambdaConstructor ctor = (LambdaConstructor) getProperty(scope, "Promise");
    try {
        final LambdaFunction resolve = (LambdaFunction) getProperty(ctor, "resolve");
        return resolve.call(Context.getCurrentContext(), this, ctor, new Object[] { getBackend().getText() });
    } catch (final IOException e) {
        final LambdaFunction reject = (LambdaFunction) getProperty(ctor, "reject");
        return reject.call(Context.getCurrentContext(), this, ctor, new Object[] { e.getMessage() });
    }
}
Also used : LambdaConstructor(net.sourceforge.htmlunit.corejs.javascript.LambdaConstructor) LambdaFunction(net.sourceforge.htmlunit.corejs.javascript.LambdaFunction) ScriptableObject(net.sourceforge.htmlunit.corejs.javascript.ScriptableObject) IOException(java.io.IOException) HtmlUnitScriptable(com.gargoylesoftware.htmlunit.javascript.HtmlUnitScriptable) Scriptable(net.sourceforge.htmlunit.corejs.javascript.Scriptable) JsxFunction(com.gargoylesoftware.htmlunit.javascript.configuration.JsxFunction)

Example 2 with LambdaFunction

use of net.sourceforge.htmlunit.corejs.javascript.LambdaFunction in project htmlunit by HtmlUnit.

the class HTMLMediaElement method play.

/**
 * Begins playback of the media.
 *
 * @return a {@link Promise} which is fulfilled when playback has been started,
 *         or is rejected if for any reason playback cannot be started
 */
@JsxFunction
public Object play() {
    if (getBrowserVersion().hasFeature(JS_PROMISE)) {
        final Scriptable scope = ScriptableObject.getTopLevelScope(this);
        final LambdaConstructor ctor = (LambdaConstructor) getProperty(scope, "Promise");
        final LambdaFunction reject = (LambdaFunction) getProperty(ctor, "reject");
        return reject.call(Context.getCurrentContext(), this, ctor, new Object[] { new DOMException("HtmlUnit does not support media play().", DOMException.NOT_FOUND_ERR) });
    }
    return Undefined.instance;
}
Also used : DOMException(com.gargoylesoftware.htmlunit.javascript.host.dom.DOMException) LambdaConstructor(net.sourceforge.htmlunit.corejs.javascript.LambdaConstructor) LambdaFunction(net.sourceforge.htmlunit.corejs.javascript.LambdaFunction) Scriptable(net.sourceforge.htmlunit.corejs.javascript.Scriptable) JsxFunction(com.gargoylesoftware.htmlunit.javascript.configuration.JsxFunction)

Example 3 with LambdaFunction

use of net.sourceforge.htmlunit.corejs.javascript.LambdaFunction in project htmlunit by HtmlUnit.

the class FontFaceSet method load.

/**
 * @param font a font specification using the CSS value syntax, e.g. "italic bold 16px Roboto"
 * @param text limit the font faces to those whose Unicode range contains at least one
 *          of the characters in text. This does not check for individual glyph coverage.
 * @return a Promise of an Array of FontFace loaded. The promise is fulfilled
 *          when all the fonts are loaded; it is rejected if one of the fonts failed to load.
 */
@JsxFunction
public Object load(final String font, final String text) {
    final Scriptable scope = ScriptableObject.getTopLevelScope(this);
    final LambdaConstructor ctor = (LambdaConstructor) getProperty(scope, "Promise");
    final LambdaFunction resolve = (LambdaFunction) getProperty(ctor, "resolve");
    return resolve.call(Context.getCurrentContext(), this, ctor, new Object[] { "" });
}
Also used : LambdaConstructor(net.sourceforge.htmlunit.corejs.javascript.LambdaConstructor) LambdaFunction(net.sourceforge.htmlunit.corejs.javascript.LambdaFunction) Scriptable(net.sourceforge.htmlunit.corejs.javascript.Scriptable) JsxFunction(com.gargoylesoftware.htmlunit.javascript.configuration.JsxFunction)

Example 4 with LambdaFunction

use of net.sourceforge.htmlunit.corejs.javascript.LambdaFunction in project htmlunit by HtmlUnit.

the class MediaDevices method getUserMedia.

@JsxFunction
public Object getUserMedia() {
    final Scriptable scope = ScriptableObject.getTopLevelScope(this);
    final LambdaConstructor ctor = (LambdaConstructor) getProperty(scope, "Promise");
    final LambdaFunction reject = (LambdaFunction) getProperty(ctor, "reject");
    return reject.call(Context.getCurrentContext(), this, ctor, new Object[] { new DOMException("HtmlUnit does not support media streaming.", DOMException.NOT_FOUND_ERR) });
}
Also used : DOMException(com.gargoylesoftware.htmlunit.javascript.host.dom.DOMException) LambdaConstructor(net.sourceforge.htmlunit.corejs.javascript.LambdaConstructor) LambdaFunction(net.sourceforge.htmlunit.corejs.javascript.LambdaFunction) Scriptable(net.sourceforge.htmlunit.corejs.javascript.Scriptable) JsxFunction(com.gargoylesoftware.htmlunit.javascript.configuration.JsxFunction)

Example 5 with LambdaFunction

use of net.sourceforge.htmlunit.corejs.javascript.LambdaFunction in project htmlunit by HtmlUnit.

the class AudioContext method decodeAudioData.

/**
 * The decodeAudioData() method of the BaseAudioContext Interface is used to asynchronously
 * decode audio file data contained in an ArrayBuffer. In this case the ArrayBuffer is
 * loaded from XMLHttpRequest and FileReader.
 * The decoded AudioBuffer is resampled to the AudioContext's sampling rate,
 * then passed to a callback or promise.
 * @param buffer An ArrayBuffer containing the audio data to be decoded, usually grabbed
 * from XMLHttpRequest, WindowOrWorkerGlobalScope.fetch() or FileReader
 * @param success A callback function to be invoked when the decoding successfully finishes.
 * The single argument to this callback is an AudioBuffer representing the decodedData
 * (the decoded PCM audio data). Usually you'll want to put the decoded data into
 * an AudioBufferSourceNode, from which it can be played and manipulated how you want.
 * @param error An optional error callback, to be invoked if an error occurs
 * when the audio data is being decoded.
 * @return the promise or null
 */
@JsxFunction
public Object decodeAudioData(final NativeArrayBuffer buffer, final Function success, final Function error) {
    final Window window = getWindow();
    final HtmlPage owningPage = (HtmlPage) window.getDocument().getPage();
    final JavaScriptEngine jsEngine = (JavaScriptEngine) window.getWebWindow().getWebClient().getJavaScriptEngine();
    if (error != null) {
        jsEngine.addPostponedAction(new PostponedAction(owningPage, "AudioContext.decodeAudioData") {

            @Override
            public void execute() throws Exception {
                jsEngine.callFunction(owningPage, error, getParentScope(), AudioContext.this, new Object[] {});
            }
        });
        return null;
    }
    final Scriptable scope = ScriptableObject.getTopLevelScope(this);
    final LambdaConstructor ctor = (LambdaConstructor) getProperty(scope, "Promise");
    final LambdaFunction reject = (LambdaFunction) getProperty(ctor, "reject");
    return reject.call(Context.getCurrentContext(), this, ctor, new Object[] {});
}
Also used : Window(com.gargoylesoftware.htmlunit.javascript.host.Window) LambdaConstructor(net.sourceforge.htmlunit.corejs.javascript.LambdaConstructor) HtmlPage(com.gargoylesoftware.htmlunit.html.HtmlPage) PostponedAction(com.gargoylesoftware.htmlunit.javascript.PostponedAction) ScriptableObject(net.sourceforge.htmlunit.corejs.javascript.ScriptableObject) LambdaFunction(net.sourceforge.htmlunit.corejs.javascript.LambdaFunction) Scriptable(net.sourceforge.htmlunit.corejs.javascript.Scriptable) JavaScriptEngine(com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine) JsxFunction(com.gargoylesoftware.htmlunit.javascript.configuration.JsxFunction)

Aggregations

LambdaConstructor (net.sourceforge.htmlunit.corejs.javascript.LambdaConstructor)6 LambdaFunction (net.sourceforge.htmlunit.corejs.javascript.LambdaFunction)6 Scriptable (net.sourceforge.htmlunit.corejs.javascript.Scriptable)6 JsxFunction (com.gargoylesoftware.htmlunit.javascript.configuration.JsxFunction)5 DOMException (com.gargoylesoftware.htmlunit.javascript.host.dom.DOMException)3 HtmlUnitScriptable (com.gargoylesoftware.htmlunit.javascript.HtmlUnitScriptable)2 ScriptableObject (net.sourceforge.htmlunit.corejs.javascript.ScriptableObject)2 HtmlPage (com.gargoylesoftware.htmlunit.html.HtmlPage)1 JavaScriptEngine (com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine)1 PostponedAction (com.gargoylesoftware.htmlunit.javascript.PostponedAction)1 Window (com.gargoylesoftware.htmlunit.javascript.host.Window)1 IOException (java.io.IOException)1