use of com.gargoylesoftware.htmlunit.javascript.PostponedAction in project htmlunit by HtmlUnit.
the class HtmlPage method initialize.
/**
* Initialize this page.
* @throws IOException if an IO problem occurs
* @throws FailingHttpStatusCodeException if the server returns a failing status code AND the property
* {@link com.gargoylesoftware.htmlunit.WebClientOptions#setThrowExceptionOnFailingStatusCode(boolean)} is set
* to true.
*/
@Override
public void initialize() throws IOException, FailingHttpStatusCodeException {
final WebWindow enclosingWindow = getEnclosingWindow();
final boolean isAboutBlank = getUrl() == UrlUtils.URL_ABOUT_BLANK;
if (isAboutBlank) {
// a frame contains first a faked "about:blank" before its real content specified by src gets loaded
if (enclosingWindow instanceof FrameWindow && !((FrameWindow) enclosingWindow).getFrameElement().isContentLoaded()) {
return;
}
// save the URL that should be used to resolve relative URLs in this page
if (enclosingWindow instanceof TopLevelWindow) {
final TopLevelWindow topWindow = (TopLevelWindow) enclosingWindow;
final WebWindow openerWindow = topWindow.getOpener();
if (openerWindow != null && openerWindow.getEnclosedPage() != null) {
baseUrl_ = openerWindow.getEnclosedPage().getWebResponse().getWebRequest().getUrl();
}
}
}
if (!isAboutBlank) {
setReadyState(READY_STATE_INTERACTIVE);
getDocumentElement().setReadyState(READY_STATE_INTERACTIVE);
}
executeDeferredScriptsIfNeeded();
executeEventHandlersIfNeeded(Event.TYPE_DOM_DOCUMENT_LOADED);
loadFrames();
// see Node.initInlineFrameIfNeeded()
if (!isAboutBlank) {
if (hasFeature(FOCUS_BODY_ELEMENT_AT_START)) {
setElementWithFocus(getBody());
}
setReadyState(READY_STATE_COMPLETE);
getDocumentElement().setReadyState(READY_STATE_COMPLETE);
}
// frame initialization has a different order
boolean isFrameWindow = enclosingWindow instanceof FrameWindow;
boolean isFirstPageInFrameWindow = false;
if (isFrameWindow) {
isFrameWindow = ((FrameWindow) enclosingWindow).getFrameElement() instanceof HtmlFrame;
final History hist = enclosingWindow.getHistory();
if (hist.getLength() > 0 && UrlUtils.URL_ABOUT_BLANK == hist.getUrl(0)) {
isFirstPageInFrameWindow = hist.getLength() <= 2;
} else {
isFirstPageInFrameWindow = enclosingWindow.getHistory().getLength() < 2;
}
}
if (isFrameWindow && !isFirstPageInFrameWindow) {
executeEventHandlersIfNeeded(Event.TYPE_LOAD);
}
for (final FrameWindow frameWindow : getFrames()) {
if (frameWindow.getFrameElement() instanceof HtmlFrame) {
final Page page = frameWindow.getEnclosedPage();
if (page != null && page.isHtmlPage()) {
((HtmlPage) page).executeEventHandlersIfNeeded(Event.TYPE_LOAD);
}
}
}
if (!isFrameWindow) {
executeEventHandlersIfNeeded(Event.TYPE_LOAD);
if (!isAboutBlank && enclosingWindow.getWebClient().isJavaScriptEnabled() && hasFeature(EVENT_FOCUS_ON_LOAD)) {
final HtmlElement body = getBody();
if (body != null) {
final Event event = new Event((Window) enclosingWindow.getScriptableObject(), Event.TYPE_FOCUS);
body.fireEvent(event);
}
}
}
try {
while (!afterLoadActions_.isEmpty()) {
final PostponedAction action = afterLoadActions_.remove(0);
action.execute();
}
} catch (final IOException e) {
throw e;
} catch (final Exception e) {
throw new RuntimeException(e);
}
executeRefreshIfNeeded();
}
use of com.gargoylesoftware.htmlunit.javascript.PostponedAction in project htmlunit by HtmlUnit.
the class HtmlImage method setAttributeNS.
/**
* {@inheritDoc}
*/
@Override
protected void setAttributeNS(final String namespaceURI, final String qualifiedName, final String value, final boolean notifyAttributeChangeListeners, final boolean notifyMutationObservers) {
final HtmlPage htmlPage = getHtmlPageOrNull();
if (SRC_ATTRIBUTE.equals(qualifiedName) && value != ATTRIBUTE_NOT_DEFINED && htmlPage != null) {
final String oldValue = getAttributeNS(namespaceURI, qualifiedName);
if (!oldValue.equals(value)) {
super.setAttributeNS(namespaceURI, qualifiedName, value, notifyAttributeChangeListeners, notifyMutationObservers);
// onload handlers may need to be invoked again, and a new image may need to be downloaded
onloadProcessed_ = false;
downloaded_ = false;
isComplete_ = false;
width_ = -1;
height_ = -1;
if (imageData_ != null) {
imageData_.close();
imageData_ = null;
}
final String readyState = htmlPage.getReadyState();
if (READY_STATE_LOADING.equals(readyState)) {
final PostponedAction action = new PostponedAction(getPage(), "HtmlImage.setAttributeNS") {
@Override
public void execute() throws Exception {
doOnLoad();
}
};
htmlPage.addAfterLoadAction(action);
return;
}
doOnLoad();
return;
}
}
super.setAttributeNS(namespaceURI, qualifiedName, value, notifyAttributeChangeListeners, notifyMutationObservers);
}
use of com.gargoylesoftware.htmlunit.javascript.PostponedAction in project htmlunit by HtmlUnit.
the class HTMLCollectionFrames method postMessage.
/**
* Posts a message.
* @param message the object passed to the window
* @param targetOrigin the origin this window must be for the event to be dispatched
* @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 String targetOrigin, final Object transfer) {
final WebWindow webWindow = getWebWindow();
final Page page = webWindow.getEnclosedPage();
final URL currentURL = page.getUrl();
if (!"*".equals(targetOrigin) && !"/".equals(targetOrigin)) {
final URL targetURL;
try {
targetURL = new URL(targetOrigin);
} catch (final Exception e) {
throw Context.throwAsScriptRuntimeEx(new Exception("SyntaxError: Failed to execute 'postMessage' on 'Window': Invalid target origin '" + targetOrigin + "' was specified (reason: " + e.getMessage() + "."));
}
if (getPort(targetURL) != getPort(currentURL)) {
return;
}
if (!targetURL.getHost().equals(currentURL.getHost())) {
return;
}
if (!targetURL.getProtocol().equals(currentURL.getProtocol())) {
return;
}
}
final MessageEvent event = new MessageEvent();
final String origin = currentURL.getProtocol() + "://" + currentURL.getHost() + ':' + currentURL.getPort();
event.initMessageEvent(Event.TYPE_MESSAGE, false, false, message, origin, "", this, transfer);
event.setParentScope(this);
event.setPrototype(getPrototype(event.getClass()));
final JavaScriptEngine jsEngine = (JavaScriptEngine) webWindow.getWebClient().getJavaScriptEngine();
final PostponedAction action = new PostponedAction(page, "Window.postMessage") {
@Override
public void execute() throws Exception {
final ContextAction<Object> contextAction = cx -> dispatchEvent(event);
final ContextFactory cf = jsEngine.getContextFactory();
cf.call(contextAction);
}
};
jsEngine.addPostponedAction(action);
}
use of com.gargoylesoftware.htmlunit.javascript.PostponedAction in project htmlunit by HtmlUnit.
the class MessagePort method postMessage.
/**
* 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 WebWindow webWindow = w.getWebWindow();
final Page page = webWindow.getEnclosedPage();
final URL currentURL = page.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) webWindow.getWebClient().getJavaScriptEngine();
final PostponedAction action = new PostponedAction(page, "MessagePort.postMessage") {
@Override
public void execute() throws Exception {
final ContextFactory cf = jsEngine.getContextFactory();
cf.call(cx -> port1_.dispatchEvent(event));
}
};
jsEngine.addPostponedAction(action);
}
}
use of com.gargoylesoftware.htmlunit.javascript.PostponedAction in project htmlunit by HtmlUnit.
the class MutationObserver method attributeReplaced.
/**
* {@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, "MutationObserver.attributeReplaced") {
@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