1 /*
2 * Copyright (c) 2002-2026 Gargoyle Software Inc.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 * https://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
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 * Utility class containing miscellaneous {@link WebClient}-related methods.
30 *
31 * @author Daniel Gredler
32 * @author Ronald Brill
33 */
34 public final class WebClientUtils {
35
36 /**
37 * Disallow instantiation of this class.
38 */
39 private WebClientUtils() {
40 // Empty.
41 }
42
43 /**
44 * Attaches a visual (GUI) debugger to the specified client.
45 * @param client the client to which the visual debugger is to be attached
46 * @see <a href="http://www.mozilla.org/rhino/debugger.html">Mozilla Rhino Debugger Documentation</a>
47 */
48 public static void attachVisualDebugger(final WebClient client) {
49 final HtmlUnitContextFactory cf = client.getJavaScriptEngine().getContextFactory();
50 final Main main = Main.mainEmbedded(cf, (ScopeProvider) null, "HtmlUnit JavaScript Debugger");
51 main.getDebugFrame().setExtendedState(Frame.MAXIMIZED_BOTH);
52
53 final SourceProvider sourceProvider = script -> {
54 String sourceName = script.getSourceName();
55 if (sourceName.endsWith("(eval)") || sourceName.endsWith("(Function)")) {
56 return null; // script is result of eval call. Rhino already knows the source and we don't
57 }
58 if (sourceName.startsWith("script in ")) {
59 sourceName = StringUtils.substringBetween(sourceName, "script in ", " from");
60 for (final WebWindow ww : client.getWebWindows()) {
61 final WebResponse wr = ww.getEnclosedPage().getWebResponse();
62 if (sourceName.equals(wr.getWebRequest().getUrl().toString())) {
63 return wr.getContentAsString();
64 }
65 }
66 }
67 return null;
68 };
69 main.setSourceProvider(sourceProvider);
70 }
71
72 }