java JAVAFX / WebView / WebEngine FireBugLite 还是其他一些调试器?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17387981/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-01 01:50:15  来源:igfitidea点击:

JAVAFX / WebView / WebEngine FireBugLite or Some other debugger?

javawebviewjavafxfirebug-lite

提问by roosevelt

I am developing an app and need to run a debugger on the WebView/WebEngine part of the application so I can better debug my application. But the code I found across the internet to inject Firebug Lite is not working for some reason.

我正在开发一个应用程序,需要在应用程序的 WebView/WebEngine 部分运行调试器,以便更好地调试我的应用程序。但是我在互联网上找到的用于注入 Firebug Lite 的代码由于某种原因不起作用。

The Javascript code itself works fine if I run it on the Firefox Console, but not when the same code is executed through the JavaFX webview/webengine. The netbeans console doesn't throw any errors others... so I am not sure why the FirebugLite UI is not rendering/loading.

如果我在 Firefox 控制台上运行 Javascript 代码本身就可以正常工作,但在通过 JavaFX webview/webengine 执行相同的代码时就不行了。netbeans 控制台不会抛出任何其他错误......所以我不确定为什么 FirebugLite UI 没有呈现/加载。

What could be causing this, any other alternatives?

什么可能导致这种情况,还有其他选择吗?

I'm using JavaFX 2.2

我正在使用 JavaFX 2.2

// Doesn't work....

// 不起作用....

webView.getEngine().executeScript("if (!document.getElementById('FirebugLite')){E = document['createElement' + 'NS'] && document.documentElement.namespaceURI;E = E ? document['createElement' + 'NS'](E, 'script') : document['createElement']('script');E['setAttribute']('id', 'FirebugLite');E['setAttribute']('src', 'https://getfirebug.com/' + 'firebug-lite.js' + '#startOpened');E['setAttribute']('FirebugLite', '4');(document['getElementsByTagName']('head')[0] || document['getElementsByTagName']('body')[0]).appendChild(E);E = new Image;E['setAttribute']('src', 'https://getfirebug.com/' + '#startOpened');}"); 

回答by roosevelt

I was able to fix the problem. It seems like the current stable version of FirebugLite works well for traditional browsers but something is different that makes it fail for an application viewed by the JAVAFX WebView.

我能够解决这个问题。FirebugLite 的当前稳定版本似乎适用于传统浏览器,但有些不同之处使其无法用于 JAVAFX WebView 查看的应用程序。

I was able to add Firebug to my application by using an uncompressed version of FirebugLite

我能够使用 FirebugLite 的未压缩版本将 Firebug 添加到我的应用程序中

<script type='text/javascript' src='http://getfirebug.com/releases/lite/1.2/firebug-lite-compressed.js'></script>

The solution came from: Testing IE6 with Firebug Lite

解决方案来自:Testing IE6 with Firebug Lite

回答by jewelsea

The command you provided in your question works for me (well mostly).

您在问题中提供的命令对我有用(大部分情况下)。

Perhaps you are not waiting until the WebView has loaded a document before trying to trigger Firebug.

也许您不会等到 WebView 加载文档后才尝试触发 Firebug。

For example, the following code will launch Firebug Lite for me (JavaFX 8b103, OS X 10.8).

例如,以下代码将为我启动 Firebug Lite(JavaFX 8b103,OS X 10.8)。

import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.scene.Scene;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebView;
import javafx.stage.Stage;
import org.w3c.dom.Document;

public class WebViewWithDebugger extends Application {
  public static void main(String[] args) { launch(args); }
  @Override public void start(Stage primaryStage) {
    final WebView webView = new WebView();
    final WebEngine engine = webView.getEngine();
    engine.load("http://docs.oracle.com/javafx/2/get_started/animation.htm");
    engine.documentProperty().addListener(new ChangeListener<Document>() {
      @Override public void changed(ObservableValue<? extends Document> prop, Document oldDoc, Document newDoc) {
        enableFirebug(engine);
      }
    });
    primaryStage.setScene(new Scene(webView));
    primaryStage.show();
  }

  /**
   * Enables Firebug Lite for debugging a webEngine.
   * @param engine the webEngine for which debugging is to be enabled.
   */
  private static void enableFirebug(final WebEngine engine) {
    engine.executeScript("if (!document.getElementById('FirebugLite')){E = document['createElement' + 'NS'] && document.documentElement.namespaceURI;E = E ? document['createElement' + 'NS'](E, 'script') : document['createElement']('script');E['setAttribute']('id', 'FirebugLite');E['setAttribute']('src', 'https://getfirebug.com/' + 'firebug-lite.js' + '#startOpened');E['setAttribute']('FirebugLite', '4');(document['getElementsByTagName']('head')[0] || document['getElementsByTagName']('body')[0]).appendChild(E);E = new Image;E['setAttribute']('src', 'https://getfirebug.com/' + '#startOpened');}"); 
  }
}

debugging

调试

Firebug Lite itself doesn't seem particularly great at debugging web pages (at least under WebView for me). The console, html, css and dom panels seemed to be fine, as well as the inspect option. So some useful info there. The script portion showed the scripts but I didn't see anyway to set breakpoints, watches, etc in the scripts.

Firebug Lite 本身似乎并不擅长调试网页(至少在我的 WebView 下)。控制台、html、css 和 dom 面板似乎都很好,还有检查选项。所以那里有一些有用的信息。脚本部分显示了脚本,但我没有看到在脚本中设置断点、监视等。

回答by alistair

I found with the JavaFX Webview that the best way of quickly debugging javascript was to do the following:

我发现使用 JavaFX Webview 快速调试 javascript 的最佳方法是执行以下操作:

webView.getEngine().setOnAlert(new EventHandler<WebEvent<String>>() {
    @Override 
    public void handle(WebEvent<String> event) {
        System.out.println(event.getData());
    }
});  

That pipes through all your alert("whatever");pieces of code so you can see what's going on.

它会通过您的所有alert("whatever");代码片段,以便您可以看到发生了什么。