use of com.gargoylesoftware.htmlunit.javascript.host.event.Event in project htmlunit by HtmlUnit.
the class NamedAttrNodeMapImpl method dblClick.
/**
* Simulates double-clicking on this element, returning the page in the window that has the focus
* after the element has been clicked. Note that the returned page may or may not be the same
* as the original page, depending on the type of element being clicked, the presence of JavaScript
* action listeners, etc. Note also that {@link #click(boolean, boolean, boolean)} is automatically
* called first.
*
* @param shiftKey {@code true} if SHIFT is pressed during the double-click
* @param ctrlKey {@code true} if CTRL is pressed during the double-click
* @param altKey {@code true} if ALT is pressed during the double-click
* @param <P> the page type
* @return the page that occupies this element's window after the element has been double-clicked
* @exception IOException if an IO error occurs
*/
@SuppressWarnings("unchecked")
public <P extends Page> P dblClick(final boolean shiftKey, final boolean ctrlKey, final boolean altKey) throws IOException {
if (isDisabledElementAndDisabled()) {
return (P) getPage();
}
// call click event first
P clickPage = click(shiftKey, ctrlKey, altKey);
if (clickPage != getPage()) {
if (LOG.isDebugEnabled()) {
LOG.debug("dblClick() is ignored, as click() loaded a different page.");
}
return clickPage;
}
// call click event a second time
clickPage = click(shiftKey, ctrlKey, altKey);
if (clickPage != getPage()) {
if (LOG.isDebugEnabled()) {
LOG.debug("dblClick() is ignored, as click() loaded a different page.");
}
return clickPage;
}
final Event event;
final WebClient webClient = getPage().getWebClient();
if (webClient.getBrowserVersion().hasFeature(EVENT_ONDOUBLECLICK_USES_POINTEREVENT)) {
event = new PointerEvent(this, MouseEvent.TYPE_DBL_CLICK, shiftKey, ctrlKey, altKey, MouseEvent.BUTTON_LEFT, 0);
} else {
event = new MouseEvent(this, MouseEvent.TYPE_DBL_CLICK, shiftKey, ctrlKey, altKey, MouseEvent.BUTTON_LEFT);
}
final ScriptResult scriptResult = fireEvent(event);
if (scriptResult == null) {
return clickPage;
}
return (P) webClient.getCurrentWindow().getEnclosedPage();
}
use of com.gargoylesoftware.htmlunit.javascript.host.event.Event 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.host.event.Event in project htmlunit by HtmlUnit.
the class HTMLCollectionFrames method scrollByLines.
/**
* Scrolls the window content down by the specified number of lines.
* @param lines the number of lines to scroll down
*/
@JsxFunction({ FF, FF_ESR })
public void scrollByLines(final int lines) {
final HTMLElement body = document_.getBody();
if (body != null) {
body.setScrollTop(body.getScrollTop() + (19 * lines));
final Event event = new Event(body, Event.TYPE_SCROLL);
body.fireEvent(event);
}
}
use of com.gargoylesoftware.htmlunit.javascript.host.event.Event in project htmlunit by HtmlUnit.
the class HTMLCollectionFrames method scrollBy.
/**
* Scrolls the window content the specified distance.
* @param x the horizontal distance to scroll by
* @param y the vertical distance to scroll by
*/
@JsxFunction
public void scrollBy(final int x, final int y) {
final HTMLElement body = document_.getBody();
if (body != null) {
body.setScrollLeft(body.getScrollLeft() + x);
body.setScrollTop(body.getScrollTop() + y);
final Event event = new Event(body, Event.TYPE_SCROLL);
body.fireEvent(event);
}
}
use of com.gargoylesoftware.htmlunit.javascript.host.event.Event in project htmlunit by HtmlUnit.
the class WorkerJob method postMessage.
/**
* Posts a message to the {@link Worker} in the page's context.
* @param message the message
*/
@JsxFunction
public void postMessage(final Object message) {
final MessageEvent event = new MessageEvent();
event.initMessageEvent(Event.TYPE_MESSAGE, false, false, message, origin_, "", owningWindow_, Undefined.instance);
event.setParentScope(owningWindow_);
event.setPrototype(owningWindow_.getPrototype(event.getClass()));
if (LOG.isDebugEnabled()) {
LOG.debug("[DedicatedWorker] postMessage: {}" + message);
}
final JavaScriptEngine jsEngine = (JavaScriptEngine) owningWindow_.getWebWindow().getWebClient().getJavaScriptEngine();
final ContextAction<Object> action = cx -> {
worker_.getEventListenersContainer().executeCapturingListeners(event, null);
final Object[] args = { event };
worker_.getEventListenersContainer().executeBubblingListeners(event, args);
return null;
};
final ContextFactory cf = jsEngine.getContextFactory();
final JavaScriptJob job = new WorkerJob(cf, action, "postMessage: " + Context.toString(message));
final HtmlPage page = (HtmlPage) owningWindow_.getDocument().getPage();
owningWindow_.getWebWindow().getJobManager().addJob(job, page);
}
Aggregations