首页 > 解决方案 > JavaFX WebEngine - 无法加载本地存储的 firebug-lite.js

问题描述

我正在尝试制作一种包装器类来创建使用 JavaFX WebView 作为其 GUI 的应用程序(我称之为类Application),我可以轻松地在我的其他应用程序中使用它(我制作了一个包装器项目的 jar,然后 jar 可以可以在任何其他项目中导入)。我认为在我的包装器中包含一些调试功能很重要,所以我将 'firebug-lite.js' 和 'firebug-lite.css' 版本 1.2 下载到我的项目文件夹中的 ./src/firebug/ 目录中(所以不需要互联网)。经过多次尝试,我来到以下代码Application.debug()

public void debug() {
    // file is inside the jar, in ./firebug/ directory, so I use getResourceAsStream()
    BufferedReader br = new BufferedReader(new InputStreamReader(
            getClass().getResourceAsStream("/firebug/firebug-lite.js")));
    String line;
    String wholeFirebugCode = "";
    try {
        // reading firebug-lite.js line-by-line, not including \n characters
        while ((line = br.readLine()) != null) {
            wholeFirebugCode += line;
        }
        if (wholeFirebugCode.isEmpty()) System.out.println("!");
        else {
            // everything is OK, file is read, so add firebug to our page
            engine.executeScript("document.body.innerHTML += \""+
                "<script type='text/javascript'>" + 
                wholeFirebugCode +
                "</script>\"");
        }
    } catch (IOException e) {
        printError(e, "during load of /firebug/firebug-lite.js from within jar (WebEngineApp.jar)");
    }
}

因此,我构建了一个 jar,检查它是否具有 ./firebug/firebug-lite.js(确实如此),将该 jar 包含在示例项目中并尝试使用以下代码:

// constructor works fine
Application app = new Application("title of window", path_to_sample_html_page,
width, height, bridge);
app.run();
try {//we wait a few til page is fully loaded (just for sure)
    TimeUnit.SECONDS.sleep(5);
} catch (InterruptedException e) {
    printError(e, "");
}
// here I check if java-js connection is established and page is loaded. It is
Platform.runLater(app::test);
// here I run the code which cause problem
Platform.runLater(app::debug);

但这不起作用。我使用 Java 8,如果有什么不同的话:

Exception in thread "JavaFX Application Thread" netscape.javascript.JSException: SyntaxError: Unexpected identifier 'css'
at com.sun.webkit.dom.JSObject.fwkMakeException(JSObject.java:146)
at com.sun.webkit.WebPage.twkExecuteScript(Native Method)
at com.sun.webkit.WebPage.executeScript(WebPage.java:1509)
at javafx.scene.web.WebEngine.executeScript(WebEngine.java:1005)
at emeshka.webengineapp.Application.debug(Application.java:157)
at Main.lambda$main$0(Main.java:41)
(... etc)

文件似乎被正确读取,我已经通过记录检查了它。如果我尝试使用“firebug-lite-compressed.js”,并且如果我使用这种代码方法,也会发生同样的事情,包括:

engine.executeScript(wholeFirebugCode);

我完全迷路了。不可能是萤火虫在语法上不正确。示例 html 页面不包含 js。我怎样才能让萤火虫工作?我尝试使用这些需要互联网的解决方案,但它们只是没有效果,并且没有显示错误: JavaFX WebViewJAVAFX / WebView / WebEngine FireBugLite 或其他调试器中的 Html/Javascript 调试?

标签: javajavafxjavafx-webengine

解决方案


推荐阅读