1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package org.htmlunit.util;
16
17 import java.awt.Frame;
18
19 import org.apache.commons.lang3.StringUtils;
20 import org.htmlunit.WebClient;
21 import org.htmlunit.WebResponse;
22 import org.htmlunit.WebWindow;
23 import org.htmlunit.corejs.javascript.tools.debugger.Main;
24 import org.htmlunit.corejs.javascript.tools.debugger.ScopeProvider;
25 import org.htmlunit.corejs.javascript.tools.debugger.SourceProvider;
26 import org.htmlunit.javascript.HtmlUnitContextFactory;
27
28
29
30
31
32
33 public final class WebClientUtils {
34
35
36
37
38 private WebClientUtils() {
39
40 }
41
42
43
44
45
46
47 public static void attachVisualDebugger(final WebClient client) {
48 final HtmlUnitContextFactory cf = client.getJavaScriptEngine().getContextFactory();
49 final Main main = Main.mainEmbedded(cf, (ScopeProvider) null, "HtmlUnit JavaScript Debugger");
50 main.getDebugFrame().setExtendedState(Frame.MAXIMIZED_BOTH);
51
52 final SourceProvider sourceProvider = script -> {
53 String sourceName = script.getSourceName();
54 if (sourceName.endsWith("(eval)") || sourceName.endsWith("(Function)")) {
55 return null;
56 }
57 if (sourceName.startsWith("script in ")) {
58 sourceName = StringUtils.substringBetween(sourceName, "script in ", " from");
59 for (final WebWindow ww : client.getWebWindows()) {
60 final WebResponse wr = ww.getEnclosedPage().getWebResponse();
61 if (sourceName.equals(wr.getWebRequest().getUrl().toString())) {
62 return wr.getContentAsString();
63 }
64 }
65 }
66 return null;
67 };
68 main.setSourceProvider(sourceProvider);
69 }
70
71 }