View Javadoc
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) JavaScript debugger to the specified client.
45       *
46       * @param client the client to which the visual debugger is to be attached
47       * @see <a href="https://developer.mozilla.org/en-US/docs/Tools">Mozilla Developer Tools</a>
48       */
49      public static void attachVisualDebugger(final WebClient client) {
50          final HtmlUnitContextFactory cf = client.getJavaScriptEngine().getContextFactory();
51          final Main main = Main.mainEmbedded(cf, (ScopeProvider) null, "HtmlUnit JavaScript Debugger");
52          main.getDebugFrame().setExtendedState(Frame.MAXIMIZED_BOTH);
53  
54          final SourceProvider sourceProvider = script -> {
55              String sourceName = script.getSourceName();
56              if (sourceName.endsWith("(eval)") || sourceName.endsWith("(Function)")) {
57                  return null; // script is result of eval call. Rhino already knows the source and we don't
58              }
59              if (sourceName.startsWith("script in ")) {
60                  sourceName = StringUtils.substringBetween(sourceName, "script in ", " from");
61                  for (final WebWindow ww : client.getWebWindows()) {
62                      final WebResponse wr = ww.getEnclosedPage().getWebResponse();
63                      if (sourceName.equals(wr.getWebRequest().getUrl().toString())) {
64                          return wr.getContentAsString();
65                      }
66                  }
67              }
68              return null;
69          };
70          main.setSourceProvider(sourceProvider);
71      }
72  }