View Javadoc
1   /*
2    * Copyright (c) 2002-2025 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   */
33  public final class WebClientUtils {
34  
35      /**
36       * Disallow instantiation of this class.
37       */
38      private WebClientUtils() {
39          // Empty.
40      }
41  
42      /**
43       * Attaches a visual (GUI) debugger to the specified client.
44       * @param client the client to which the visual debugger is to be attached
45       * @see <a href="http://www.mozilla.org/rhino/debugger.html">Mozilla Rhino Debugger Documentation</a>
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; // script is result of eval call. Rhino already knows the source and we don't
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  }