use of com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine in project htmlunit by HtmlUnit.
the class HTMLCollectionFrames method animateAnimationsFrames.
/**
* Invokes all the animation callbacks registered for this window by
* calling {@link #requestAnimationFrame(Object)} once.
* @return the number of pending animation callbacks
*/
public int animateAnimationsFrames() {
final List<AnimationFrame> animationFrames = new ArrayList<>(animationFrames_);
animationFrames_.clear();
final double now = System.nanoTime() / 1_000_000d;
final Object[] args = { now };
final WebWindow ww = getWindow().getWebWindow();
final JavaScriptEngine jsEngine = (JavaScriptEngine) ww.getWebClient().getJavaScriptEngine();
for (final AnimationFrame animationFrame : animationFrames) {
jsEngine.callFunction((HtmlPage) ww.getEnclosedPage(), animationFrame.callback_, this, getParentScope(), args);
}
return animationFrames_.size();
}
use of com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine in project htmlunit by HtmlUnit.
the class XMLDocument method makeScriptableFor.
/**
* {@inheritDoc}
*/
@Override
public HtmlUnitScriptable makeScriptableFor(final DomNode domNode) {
final HtmlUnitScriptable scriptable;
// TODO: cleanup, getScriptObject() should be used!!!
if (domNode instanceof DomElement && !(domNode instanceof HtmlElement)) {
if (domNode instanceof SvgElement) {
final Class<? extends HtmlUnitScriptable> javaScriptClass = ((JavaScriptEngine) getWindow().getWebWindow().getWebClient().getJavaScriptEngine()).getJavaScriptClass(domNode.getClass());
try {
scriptable = javaScriptClass.newInstance();
} catch (final Exception e) {
throw Context.throwAsScriptRuntimeEx(e);
}
} else {
scriptable = new Element();
}
} else if (domNode instanceof DomAttr) {
scriptable = new Attr();
} else {
return super.makeScriptableFor(domNode);
}
scriptable.setPrototype(getPrototype(scriptable.getClass()));
scriptable.setParentScope(getParentScope());
scriptable.setDomNode(domNode);
return scriptable;
}
use of com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine in project htmlunit by HtmlUnit.
the class XMLHttpRequest method send.
/**
* Sends the specified content to the server in an HTTP request and receives the response.
* @param content the body of the message being sent with the request
*/
@JsxFunction
public void send(final Object content) {
if (webRequest_ == null) {
return;
}
if (!async_ && timeout_ > 0) {
Context.throwAsScriptRuntimeEx(new RuntimeException("Synchronous requests must not set a timeout."));
return;
}
prepareRequestContent(content);
if (timeout_ > 0) {
webRequest_.setTimeout(timeout_);
}
final Window w = getWindow();
final WebWindow ww = w.getWebWindow();
final WebClient client = ww.getWebClient();
final AjaxController ajaxController = client.getAjaxController();
final HtmlPage page = (HtmlPage) ww.getEnclosedPage();
final boolean synchron = ajaxController.processSynchron(page, webRequest_, async_);
if (synchron) {
doSend();
} else {
// Create and start a thread in which to execute the request.
final ContextFactory cf = ((JavaScriptEngine) client.getJavaScriptEngine()).getContextFactory();
final ContextAction<Object> action = new ContextAction<Object>() {
@Override
public Object run(final Context cx) {
// KEY_STARTING_SCOPE maintains a stack of scopes
@SuppressWarnings("unchecked") Deque<Scriptable> stack = (Deque<Scriptable>) cx.getThreadLocal(JavaScriptEngine.KEY_STARTING_SCOPE);
if (null == stack) {
stack = new ArrayDeque<>();
cx.putThreadLocal(JavaScriptEngine.KEY_STARTING_SCOPE, stack);
}
stack.push(w);
try {
doSend();
} finally {
stack.pop();
}
return null;
}
@Override
public String toString() {
return "XMLHttpRequest " + webRequest_.getHttpMethod() + " '" + webRequest_.getUrl() + "'";
}
};
final JavaScriptJob job = BackgroundJavaScriptFactory.theFactory().createJavascriptXMLHttpRequestJob(cf, action);
if (LOG.isDebugEnabled()) {
LOG.debug("Starting XMLHttpRequest thread for asynchronous request");
}
jobID_ = ww.getJobManager().addJob(job, page);
if (getBrowserVersion().hasFeature(XHR_FIRE_STATE_OPENED_AGAIN_IN_ASYNC_MODE)) {
// quite strange but IE seems to fire state loading twice
// in async mode (at least with HTML of the unit tests)
fireJavascriptEvent(Event.TYPE_READY_STATE_CHANGE);
}
if (!getBrowserVersion().hasFeature(XHR_LOAD_START_ASYNC)) {
fireJavascriptEvent(Event.TYPE_LOAD_START);
}
}
}
use of com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine in project htmlunit by HtmlUnit.
the class NodeList method forEach.
/**
* Calls the {@code callback} given in parameter once for each value pair in the list, in insertion order.
* @param callback function to execute for each element
*/
@JsxFunction({ CHROME, EDGE, FF, FF_ESR })
public void forEach(final Object callback) {
final List<DomNode> nodes = getElements();
final WebClient client = getWindow().getWebWindow().getWebClient();
final HtmlUnitContextFactory cf = ((JavaScriptEngine) client.getJavaScriptEngine()).getContextFactory();
final ContextAction<Object> contextAction = cx -> {
final Function function = (Function) callback;
final Scriptable scope = getParentScope();
for (int i = 0; i < nodes.size(); i++) {
function.call(cx, scope, NodeList.this, new Object[] { nodes.get(i).getScriptableObject(), i, NodeList.this });
}
return null;
};
cf.call(contextAction);
}
use of com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine in project htmlunit by HtmlUnit.
the class MutationObserver method characterDataChanged.
/**
* {@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, "MutationObserver.characterDataChanged") {
@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 });
}
});
}
}
Aggregations