Search in sources :

Example 1 with SourceProvider

use of net.sourceforge.htmlunit.corejs.javascript.tools.debugger.SourceProvider 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

WebClient (com.gargoylesoftware.htmlunit.WebClient)1 WebResponse (com.gargoylesoftware.htmlunit.WebResponse)1 WebWindow (com.gargoylesoftware.htmlunit.WebWindow)1 HtmlUnitContextFactory (com.gargoylesoftware.htmlunit.javascript.HtmlUnitContextFactory)1 JavaScriptEngine (com.gargoylesoftware.htmlunit.javascript.JavaScriptEngine)1 Frame (java.awt.Frame)1 Main (net.sourceforge.htmlunit.corejs.javascript.tools.debugger.Main)1 ScopeProvider (net.sourceforge.htmlunit.corejs.javascript.tools.debugger.ScopeProvider)1 SourceProvider (net.sourceforge.htmlunit.corejs.javascript.tools.debugger.SourceProvider)1 StringUtils (org.apache.commons.lang3.StringUtils)1