use of com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine in project htmlunit by HtmlUnit.
the class WebSocket method callFunction.
void callFunction(final Function function, final Object[] args) {
if (function == null) {
return;
}
final Scriptable scope = function.getParentScope();
final JavaScriptEngine engine = (JavaScriptEngine) containingPage_.getWebClient().getJavaScriptEngine();
engine.callFunction(containingPage_, function, scope, this, args);
}
use of com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine 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.JavaScriptEngine 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);
}
use of com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine in project htmlunit by HtmlUnit.
the class WorkerJob method messagePosted.
void messagePosted(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()));
final JavaScriptEngine jsEngine = (JavaScriptEngine) owningWindow_.getWebWindow().getWebClient().getJavaScriptEngine();
final ContextAction<Object> action = cx -> {
executeEvent(cx, event);
return null;
};
final ContextFactory cf = jsEngine.getContextFactory();
final JavaScriptJob job = new WorkerJob(cf, action, "messagePosted: " + Context.toString(message));
final HtmlPage page = (HtmlPage) owningWindow_.getDocument().getPage();
owningWindow_.getWebWindow().getJobManager().addJob(job, page);
}
use of com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine in project htmlunit by HtmlUnit.
the class WebClientUtils method attachVisualDebugger.
/**
* Attaches a visual (GUI) debugger to the specified client.
* @param client the client to which the visual debugger is to be attached
* @see <a href="http://www.mozilla.org/rhino/debugger.html">Mozilla Rhino Debugger Documentation</a>
*/
public static void attachVisualDebugger(final WebClient client) {
final HtmlUnitContextFactory cf = ((JavaScriptEngine) client.getJavaScriptEngine()).getContextFactory();
final Main main = Main.mainEmbedded(cf, (ScopeProvider) null, "HtmlUnit JavaScript Debugger");
main.getDebugFrame().setExtendedState(Frame.MAXIMIZED_BOTH);
final SourceProvider sourceProvider = script -> {
String sourceName = script.getSourceName();
if (sourceName.endsWith("(eval)") || sourceName.endsWith("(Function)")) {
// script is result of eval call. Rhino already knows the source and we don't
return null;
}
if (sourceName.startsWith("script in ")) {
sourceName = StringUtils.substringBetween(sourceName, "script in ", " from");
for (final WebWindow ww : client.getWebWindows()) {
final WebResponse wr = ww.getEnclosedPage().getWebResponse();
if (sourceName.equals(wr.getWebRequest().getUrl().toString())) {
return wr.getContentAsString();
}
}
}
return null;
};
main.setSourceProvider(sourceProvider);
}
Aggregations