/** Detaches the debugger from the current ContextFactory. */
 public void detach() {
   if (listener != null) {
     contextFactory.removeListener(listener);
     contextFactory = null;
     listener = null;
   }
 }
  /**
   * Tries to uncompress the JavaScript code in the provided response.
   *
   * @param response the response to uncompress
   * @return a new response with uncompressed JavaScript code or the original response in case of
   *     failure
   */
  protected WebResponse uncompressJavaScript(final WebResponse response) {
    final WebRequest request = response.getWebRequest();
    final String scriptName = request.getUrl().toString();
    final String scriptSource = response.getContentAsString();

    // skip if it is already formatted? => TODO

    final ContextFactory factory = new ContextFactory();
    final ContextAction action =
        new ContextAction() {
          public Object run(final Context cx) {
            cx.setOptimizationLevel(-1);
            final Script script = cx.compileString(scriptSource, scriptName, 0, null);
            return cx.decompileScript(script, 4);
          }
        };

    try {
      final String decompileScript = (String) factory.call(action);
      final List<NameValuePair> responseHeaders =
          new ArrayList<NameValuePair>(response.getResponseHeaders());
      for (int i = responseHeaders.size() - 1; i >= 0; i--) {
        if ("content-encoding".equalsIgnoreCase(responseHeaders.get(i).getName())) {
          responseHeaders.remove(i);
        }
      }
      final WebResponseData wrd =
          new WebResponseData(
              decompileScript.getBytes(),
              response.getStatusCode(),
              response.getStatusMessage(),
              responseHeaders);
      return new WebResponse(
          wrd,
          response.getWebRequest().getUrl(),
          response.getWebRequest().getHttpMethod(),
          response.getLoadTime());
    } catch (final Exception e) {
      LOG.warn("Failed to decompress JavaScript response. Delivering as it.", e);
    }

    return response;
  }
 /** Attaches the debugger to the given ContextFactory. */
 public void attachTo(ContextFactory factory) {
   detach();
   this.contextFactory = factory;
   this.listener = new DimIProxy(this, IPROXY_LISTEN);
   factory.addListener(this.listener);
 }