Java Code Examples for com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine#addPostponedAction()
The following examples show how to use
com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine#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: MessagePort.java From htmlunit with Apache License 2.0 | 6 votes |
/** * Posts a message. * @param message the object passed to the window * @param transfer an optional sequence of Transferable objects * @see <a href="https://developer.mozilla.org/en-US/docs/Web/API/window.postMessage">MDN documentation</a> */ @JsxFunction public void postMessage(final String message, final Object transfer) { if (port1_ != null) { final Window w = getWindow(); final URL currentURL = w.getWebWindow().getEnclosedPage().getUrl(); final MessageEvent event = new MessageEvent(); final String origin = currentURL.getProtocol() + "://" + currentURL.getHost() + ':' + currentURL.getPort(); event.initMessageEvent(Event.TYPE_MESSAGE, false, false, message, origin, "", w, transfer); event.setParentScope(port1_); event.setPrototype(getPrototype(event.getClass())); final JavaScriptEngine jsEngine = (JavaScriptEngine) w.getWebWindow().getWebClient().getJavaScriptEngine(); final PostponedAction action = new PostponedAction(w.getWebWindow().getEnclosedPage()) { @Override public void execute() throws Exception { final ContextFactory cf = jsEngine.getContextFactory(); cf.call(cx -> port1_.dispatchEvent(event)); } }; jsEngine.addPostponedAction(action); } }
Example 2
Source File: AudioContext.java From htmlunit with Apache License 2.0 | 6 votes |
/** * 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 Promise 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) { @Override public void execute() throws Exception { jsEngine.callFunction(owningPage, error, getParentScope(), AudioContext.this, new Object[] {}); } }); return null; } final Promise promise = Promise.reject(Context.getCurrentContext(), AudioContext.this, new Object[] {}, null); return promise; }
Example 3
Source File: MutationObserver.java From htmlunit with Apache License 2.0 | 5 votes |
/** * {@inheritDoc} */ @Override public void characterDataChanged(final CharacterDataChangeEvent event) { final ScriptableObject target = event.getCharacterData().getScriptableObject(); if (subtree_ || target == node_) { final MutationRecord mutationRecord = new MutationRecord(); final Scriptable scope = getParentScope(); mutationRecord.setParentScope(scope); mutationRecord.setPrototype(getPrototype(mutationRecord.getClass())); mutationRecord.setType("characterData"); mutationRecord.setTarget(target); if (characterDataOldValue_) { mutationRecord.setOldValue(event.getOldValue()); } final Window window = getWindow(); final HtmlPage owningPage = (HtmlPage) window.getDocument().getPage(); final JavaScriptEngine jsEngine = (JavaScriptEngine) window.getWebWindow().getWebClient().getJavaScriptEngine(); jsEngine.addPostponedAction(new PostponedAction(owningPage) { @Override public void execute() throws Exception { final NativeArray array = new NativeArray(new Object[] {mutationRecord}); ScriptRuntime.setBuiltinProtoAndParent(array, scope, TopLevel.Builtins.Array); jsEngine.callFunction(owningPage, function_, scope, MutationObserver.this, new Object[] {array}); } }); } }
Example 4
Source File: MutationObserver.java From htmlunit with Apache License 2.0 | 5 votes |
/** * {@inheritDoc} */ @Override public void attributeReplaced(final HtmlAttributeChangeEvent event) { final HtmlElement target = event.getHtmlElement(); if (subtree_ || target == node_.getDomNodeOrDie()) { final String attributeName = event.getName(); if (attributeFilter_ == null || attributeFilter_.contains(attributeName)) { final MutationRecord mutationRecord = new MutationRecord(); final Scriptable scope = getParentScope(); mutationRecord.setParentScope(scope); mutationRecord.setPrototype(getPrototype(mutationRecord.getClass())); mutationRecord.setAttributeName(attributeName); mutationRecord.setType("attributes"); mutationRecord.setTarget(target.getScriptableObject()); if (attributeOldValue_) { mutationRecord.setOldValue(event.getValue()); } final Window window = getWindow(); final HtmlPage owningPage = (HtmlPage) window.getDocument().getPage(); final JavaScriptEngine jsEngine = (JavaScriptEngine) window.getWebWindow().getWebClient().getJavaScriptEngine(); jsEngine.addPostponedAction(new PostponedAction(owningPage) { @Override public void execute() throws Exception { final NativeArray array = new NativeArray(new Object[] {mutationRecord}); ScriptRuntime.setBuiltinProtoAndParent(array, scope, TopLevel.Builtins.Array); jsEngine.callFunction(owningPage, function_, scope, MutationObserver.this, new Object[] {array}); } }); } } }
Example 5
Source File: MessagePort.java From HtmlUnit-Android with Apache License 2.0 | 5 votes |
/** * Posts a message. * @param message the object passed to the window * @param transfer an optional sequence of Transferable objects * @see <a href="https://developer.mozilla.org/en-US/docs/Web/API/window.postMessage">MDN documentation</a> */ @JsxFunction public void postMessage(final String message, final Object transfer) { if (port1_ != null) { final Window w = getWindow(); final URL currentURL = w.getWebWindow().getEnclosedPage().getUrl(); final MessageEvent event = new MessageEvent(); final String origin = currentURL.getProtocol() + "://" + currentURL.getHost() + ':' + currentURL.getPort(); event.initMessageEvent(Event.TYPE_MESSAGE, false, false, message, origin, "", w, transfer); event.setParentScope(port1_); event.setPrototype(getPrototype(event.getClass())); final JavaScriptEngine jsEngine = (JavaScriptEngine) w.getWebWindow().getWebClient().getJavaScriptEngine(); final PostponedAction action = new PostponedAction(w.getWebWindow().getEnclosedPage()) { @Override public void execute() throws Exception { final ContextAction contextAction = new ContextAction() { @Override public Object run(final Context cx) { return port1_.dispatchEvent(event); } }; final ContextFactory cf = jsEngine.getContextFactory(); cf.call(contextAction); } }; jsEngine.addPostponedAction(action); } }
Example 6
Source File: MutationObserver.java From HtmlUnit-Android with Apache License 2.0 | 5 votes |
/** * {@inheritDoc} */ @Override public void characterDataChanged(final CharacterDataChangeEvent event) { final ScriptableObject target = event.getCharacterData().getScriptableObject(); if (subtree_ || target == node_) { final MutationRecord mutationRecord = new MutationRecord(); final Scriptable scope = getParentScope(); mutationRecord.setParentScope(scope); mutationRecord.setPrototype(getPrototype(mutationRecord.getClass())); mutationRecord.setType("characterData"); mutationRecord.setTarget(target); if (characterDataOldValue_) { mutationRecord.setOldValue(event.getOldValue()); } final Window window = getWindow(); final HtmlPage owningPage = (HtmlPage) window.getDocument().getPage(); final JavaScriptEngine jsEngine = (JavaScriptEngine) window.getWebWindow().getWebClient().getJavaScriptEngine(); jsEngine.addPostponedAction(new PostponedAction(owningPage) { @Override public void execute() throws Exception { final NativeArray array = new NativeArray(new Object[] {mutationRecord}); ScriptRuntime.setBuiltinProtoAndParent(array, scope, TopLevel.Builtins.Array); jsEngine.callFunction(owningPage, function_, scope, MutationObserver.this, new Object[] {array}); } }); } }
Example 7
Source File: MutationObserver.java From HtmlUnit-Android with Apache License 2.0 | 5 votes |
/** * {@inheritDoc} */ @Override public void attributeReplaced(final HtmlAttributeChangeEvent event) { final HtmlElement target = event.getHtmlElement(); if (subtree_ || target == node_.getDomNodeOrDie()) { final String attributeName = event.getName(); if (attributeFilter_ == null || attributeFilter_.contains(attributeName)) { final MutationRecord mutationRecord = new MutationRecord(); final Scriptable scope = getParentScope(); mutationRecord.setParentScope(scope); mutationRecord.setPrototype(getPrototype(mutationRecord.getClass())); mutationRecord.setAttributeName(attributeName); mutationRecord.setType("attributes"); mutationRecord.setTarget((ScriptableObject) target.getScriptableObject()); if (attributeOldValue_) { mutationRecord.setOldValue(event.getValue()); } final Window window = getWindow(); final HtmlPage owningPage = (HtmlPage) window.getDocument().getPage(); final JavaScriptEngine jsEngine = (JavaScriptEngine) window.getWebWindow().getWebClient().getJavaScriptEngine(); jsEngine.addPostponedAction(new PostponedAction(owningPage) { @Override public void execute() throws Exception { final NativeArray array = new NativeArray(new Object[] {mutationRecord}); ScriptRuntime.setBuiltinProtoAndParent(array, scope, TopLevel.Builtins.Array); jsEngine.callFunction(owningPage, function_, scope, MutationObserver.this, new Object[] {array}); } }); } } }