Search in sources :

Example 6 with JavaScriptEngine

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);
}
Also used : Scriptable(net.sourceforge.htmlunit.corejs.javascript.Scriptable) JavaScriptEngine(com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine)

Example 7 with JavaScriptEngine

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);
    }
}
Also used : WebWindow(com.gargoylesoftware.htmlunit.WebWindow) ContextFactory(net.sourceforge.htmlunit.corejs.javascript.ContextFactory) MessageEvent(com.gargoylesoftware.htmlunit.javascript.host.event.MessageEvent) Page(com.gargoylesoftware.htmlunit.Page) PostponedAction(com.gargoylesoftware.htmlunit.javascript.PostponedAction) URL(java.net.URL) WebWindow(com.gargoylesoftware.htmlunit.WebWindow) JavaScriptEngine(com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine) JsxFunction(com.gargoylesoftware.htmlunit.javascript.configuration.JsxFunction)

Example 8 with JavaScriptEngine

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);
}
Also used : AbstractJavaScriptConfiguration(com.gargoylesoftware.htmlunit.javascript.configuration.AbstractJavaScriptConfiguration) WindowOrWorkerGlobalScopeMixin(com.gargoylesoftware.htmlunit.javascript.host.WindowOrWorkerGlobalScopeMixin) URL(java.net.URL) Script(net.sourceforge.htmlunit.corejs.javascript.Script) HtmlUnitScriptable(com.gargoylesoftware.htmlunit.javascript.HtmlUnitScriptable) FF(com.gargoylesoftware.htmlunit.javascript.configuration.SupportedBrowser.FF) EDGE(com.gargoylesoftware.htmlunit.javascript.configuration.SupportedBrowser.EDGE) ClassConfiguration(com.gargoylesoftware.htmlunit.javascript.configuration.ClassConfiguration) BrowserVersion(com.gargoylesoftware.htmlunit.BrowserVersion) ContextAction(net.sourceforge.htmlunit.corejs.javascript.ContextAction) FF_ESR(com.gargoylesoftware.htmlunit.javascript.configuration.SupportedBrowser.FF_ESR) Event(com.gargoylesoftware.htmlunit.javascript.host.event.Event) JavaScriptEngine(com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine) MimeType(com.gargoylesoftware.htmlunit.util.MimeType) Context(net.sourceforge.htmlunit.corejs.javascript.Context) WebRequest(com.gargoylesoftware.htmlunit.WebRequest) Scriptable(net.sourceforge.htmlunit.corejs.javascript.Scriptable) JsxSetter(com.gargoylesoftware.htmlunit.javascript.configuration.JsxSetter) Window(com.gargoylesoftware.htmlunit.javascript.host.Window) CHROME(com.gargoylesoftware.htmlunit.javascript.configuration.SupportedBrowser.CHROME) IOException(java.io.IOException) JavaScriptJob(com.gargoylesoftware.htmlunit.javascript.background.JavaScriptJob) HtmlPage(com.gargoylesoftware.htmlunit.html.HtmlPage) WebResponse(com.gargoylesoftware.htmlunit.WebResponse) JsxGetter(com.gargoylesoftware.htmlunit.javascript.configuration.JsxGetter) JsxFunction(com.gargoylesoftware.htmlunit.javascript.configuration.JsxFunction) JsxClass(com.gargoylesoftware.htmlunit.javascript.configuration.JsxClass) List(java.util.List) ContextFactory(net.sourceforge.htmlunit.corejs.javascript.ContextFactory) IE(com.gargoylesoftware.htmlunit.javascript.configuration.SupportedBrowser.IE) WindowOrWorkerGlobalScope(com.gargoylesoftware.htmlunit.javascript.host.WindowOrWorkerGlobalScope) Log(org.apache.commons.logging.Log) WebClient(com.gargoylesoftware.htmlunit.WebClient) LogFactory(org.apache.commons.logging.LogFactory) Undefined(net.sourceforge.htmlunit.corejs.javascript.Undefined) Function(net.sourceforge.htmlunit.corejs.javascript.Function) JS_WORKER_IMPORT_SCRIPTS_ACCEPTS_ALL(com.gargoylesoftware.htmlunit.BrowserVersionFeatures.JS_WORKER_IMPORT_SCRIPTS_ACCEPTS_ALL) MessageEvent(com.gargoylesoftware.htmlunit.javascript.host.event.MessageEvent) EventTarget(com.gargoylesoftware.htmlunit.javascript.host.event.EventTarget) BasicJavaScriptJob(com.gargoylesoftware.htmlunit.javascript.background.BasicJavaScriptJob) ContextFactory(net.sourceforge.htmlunit.corejs.javascript.ContextFactory) JavaScriptJob(com.gargoylesoftware.htmlunit.javascript.background.JavaScriptJob) BasicJavaScriptJob(com.gargoylesoftware.htmlunit.javascript.background.BasicJavaScriptJob) HtmlPage(com.gargoylesoftware.htmlunit.html.HtmlPage) MessageEvent(com.gargoylesoftware.htmlunit.javascript.host.event.MessageEvent) JavaScriptEngine(com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine) JsxFunction(com.gargoylesoftware.htmlunit.javascript.configuration.JsxFunction)

Example 9 with JavaScriptEngine

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);
}
Also used : AbstractJavaScriptConfiguration(com.gargoylesoftware.htmlunit.javascript.configuration.AbstractJavaScriptConfiguration) WindowOrWorkerGlobalScopeMixin(com.gargoylesoftware.htmlunit.javascript.host.WindowOrWorkerGlobalScopeMixin) URL(java.net.URL) Script(net.sourceforge.htmlunit.corejs.javascript.Script) HtmlUnitScriptable(com.gargoylesoftware.htmlunit.javascript.HtmlUnitScriptable) FF(com.gargoylesoftware.htmlunit.javascript.configuration.SupportedBrowser.FF) EDGE(com.gargoylesoftware.htmlunit.javascript.configuration.SupportedBrowser.EDGE) ClassConfiguration(com.gargoylesoftware.htmlunit.javascript.configuration.ClassConfiguration) BrowserVersion(com.gargoylesoftware.htmlunit.BrowserVersion) ContextAction(net.sourceforge.htmlunit.corejs.javascript.ContextAction) FF_ESR(com.gargoylesoftware.htmlunit.javascript.configuration.SupportedBrowser.FF_ESR) Event(com.gargoylesoftware.htmlunit.javascript.host.event.Event) JavaScriptEngine(com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine) MimeType(com.gargoylesoftware.htmlunit.util.MimeType) Context(net.sourceforge.htmlunit.corejs.javascript.Context) WebRequest(com.gargoylesoftware.htmlunit.WebRequest) Scriptable(net.sourceforge.htmlunit.corejs.javascript.Scriptable) JsxSetter(com.gargoylesoftware.htmlunit.javascript.configuration.JsxSetter) Window(com.gargoylesoftware.htmlunit.javascript.host.Window) CHROME(com.gargoylesoftware.htmlunit.javascript.configuration.SupportedBrowser.CHROME) IOException(java.io.IOException) JavaScriptJob(com.gargoylesoftware.htmlunit.javascript.background.JavaScriptJob) HtmlPage(com.gargoylesoftware.htmlunit.html.HtmlPage) WebResponse(com.gargoylesoftware.htmlunit.WebResponse) JsxGetter(com.gargoylesoftware.htmlunit.javascript.configuration.JsxGetter) JsxFunction(com.gargoylesoftware.htmlunit.javascript.configuration.JsxFunction) JsxClass(com.gargoylesoftware.htmlunit.javascript.configuration.JsxClass) List(java.util.List) ContextFactory(net.sourceforge.htmlunit.corejs.javascript.ContextFactory) IE(com.gargoylesoftware.htmlunit.javascript.configuration.SupportedBrowser.IE) WindowOrWorkerGlobalScope(com.gargoylesoftware.htmlunit.javascript.host.WindowOrWorkerGlobalScope) Log(org.apache.commons.logging.Log) WebClient(com.gargoylesoftware.htmlunit.WebClient) LogFactory(org.apache.commons.logging.LogFactory) Undefined(net.sourceforge.htmlunit.corejs.javascript.Undefined) Function(net.sourceforge.htmlunit.corejs.javascript.Function) JS_WORKER_IMPORT_SCRIPTS_ACCEPTS_ALL(com.gargoylesoftware.htmlunit.BrowserVersionFeatures.JS_WORKER_IMPORT_SCRIPTS_ACCEPTS_ALL) MessageEvent(com.gargoylesoftware.htmlunit.javascript.host.event.MessageEvent) EventTarget(com.gargoylesoftware.htmlunit.javascript.host.event.EventTarget) BasicJavaScriptJob(com.gargoylesoftware.htmlunit.javascript.background.BasicJavaScriptJob) ContextFactory(net.sourceforge.htmlunit.corejs.javascript.ContextFactory) JavaScriptJob(com.gargoylesoftware.htmlunit.javascript.background.JavaScriptJob) BasicJavaScriptJob(com.gargoylesoftware.htmlunit.javascript.background.BasicJavaScriptJob) HtmlPage(com.gargoylesoftware.htmlunit.html.HtmlPage) MessageEvent(com.gargoylesoftware.htmlunit.javascript.host.event.MessageEvent) JavaScriptEngine(com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine)

Example 10 with JavaScriptEngine

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);
}
Also used : Main(net.sourceforge.htmlunit.corejs.javascript.tools.debugger.Main) Frame(java.awt.Frame) HtmlUnitContextFactory(com.gargoylesoftware.htmlunit.javascript.HtmlUnitContextFactory) ScopeProvider(net.sourceforge.htmlunit.corejs.javascript.tools.debugger.ScopeProvider) WebWindow(com.gargoylesoftware.htmlunit.WebWindow) WebClient(com.gargoylesoftware.htmlunit.WebClient) SourceProvider(net.sourceforge.htmlunit.corejs.javascript.tools.debugger.SourceProvider) JavaScriptEngine(com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine) StringUtils(org.apache.commons.lang3.StringUtils) WebResponse(com.gargoylesoftware.htmlunit.WebResponse) WebResponse(com.gargoylesoftware.htmlunit.WebResponse) HtmlUnitContextFactory(com.gargoylesoftware.htmlunit.javascript.HtmlUnitContextFactory) SourceProvider(net.sourceforge.htmlunit.corejs.javascript.tools.debugger.SourceProvider) Main(net.sourceforge.htmlunit.corejs.javascript.tools.debugger.Main) JavaScriptEngine(com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine) WebWindow(com.gargoylesoftware.htmlunit.WebWindow)

Aggregations

JavaScriptEngine (com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine)25 WebClient (com.gargoylesoftware.htmlunit.WebClient)12 Scriptable (net.sourceforge.htmlunit.corejs.javascript.Scriptable)12 HtmlPage (com.gargoylesoftware.htmlunit.html.HtmlPage)10 ScriptableObject (net.sourceforge.htmlunit.corejs.javascript.ScriptableObject)10 JsxFunction (com.gargoylesoftware.htmlunit.javascript.configuration.JsxFunction)9 Window (com.gargoylesoftware.htmlunit.javascript.host.Window)9 ContextFactory (net.sourceforge.htmlunit.corejs.javascript.ContextFactory)9 HtmlUnitScriptable (com.gargoylesoftware.htmlunit.javascript.HtmlUnitScriptable)8 IOException (java.io.IOException)8 Context (net.sourceforge.htmlunit.corejs.javascript.Context)8 WebWindow (com.gargoylesoftware.htmlunit.WebWindow)7 ContextAction (net.sourceforge.htmlunit.corejs.javascript.ContextAction)7 JsxClass (com.gargoylesoftware.htmlunit.javascript.configuration.JsxClass)6 EventTarget (com.gargoylesoftware.htmlunit.javascript.host.event.EventTarget)6 List (java.util.List)6 Function (net.sourceforge.htmlunit.corejs.javascript.Function)6 WebResponse (com.gargoylesoftware.htmlunit.WebResponse)5 PostponedAction (com.gargoylesoftware.htmlunit.javascript.PostponedAction)5 JavaScriptJob (com.gargoylesoftware.htmlunit.javascript.background.JavaScriptJob)5