基本上我需要以字符串的形式获取数据(与问题无关)并通过Jquery将其附加到JavaFx WebView中。我在这个链接中找到了如何嵌入Jquery并将jquery函数调用到webview:https://community.oracle.com/message/10446280?tstart=1。'
解释代码: 我在一个eclipse rcp项目中工作,其中主视图部分是一个容纳webview的javafx场景的容器。 Web引擎加载页面。下面的代码,抓取视图,获取场景,然后获取webview。 EditorPart抓取当前活动编辑器的内容(最终将注入到webview中)。现在我只有一些临时代码用于测试。可以在链接中更好地解释ExecuteJquery。
守则:
HandlerUtil.getActiveWorkbenchWindow(event).getActivePage().showView("com.interpro.emmeclipse.views.PageBuilderPart");
FXSceneProvider view = (FXSceneProvider) page.findView("com.interpro.emmeclipse.views.PageBuilderPart");
Scene scene = (Scene) view.getScene();
AnchorPane node = (AnchorPane)scene.getRoot();
WebView webview = (WebView) node.getChildren().get(0);
final WebEngine engine = webview.getEngine();
final IEditorPart activeEditor = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getActiveEditor();
if (activeEditor != null) {
final IDocument doc = (IDocument) activeEditor.getAdapter(IDocument.class);
if(doc != null){
System.out.println(doc.get());
engine.documentProperty().addListener(new ChangeListener<Document>() {
@Override public void changed(ObservableValue<? extends Document> prop, Document oldDoc, Document newDoc) {
executejQuery(
engine,
"$(\"#content\").append(<div class='ui-statusbar'><h5 class='ui-title'>Test</h5></div>);"
);
}
});
}
}
/**
* Executes a script which may reference jQuery function on a document.
* Checks if the document loaded in a webEngine has a version of jQuery corresponding to
* the minimum required version loaded, and, if not, then loads jQuery into the document
* from the default JQUERY_LOCATION.
* @param engine the webView engine to be used.
* @Param jQueryLocation the location of the jQuery script to be executed.
* @param minVersion the minimum version of jQuery which needs to be included in the document.
* @param script provided javascript script string (which may include use of jQuery functions on the document).
* @return the result of the script execution.
*/
private static Object executejQuery(final WebEngine engine, String minVersion, String jQueryLocation, String script) {
return engine.executeScript(
"(function(window, document, version, callback) { "
+ "var j, d;"
+ "var loaded = false;"
+ "if (!(j = window.jQuery) || version > j.fn.jquery || callback(j, loaded)) {"
+ " var script = document.createElement(\"script\");"
+ " script.type = \"text/javascript\";"
+ " script.src = \"" + jQueryLocation + "\";"
+ " script.onload = script.onreadystatechange = function() {"
+ " if (!loaded && (!(d = this.readyState) || d == \"loaded\" || d == \"complete\")) {"
+ " callback((j = window.jQuery).noConflict(1), loaded = true);"
+ " j(script).remove();"
+ " }"
+ " };"
+ " document.documentElement.childNodes[0].appendChild(script) "
+ "} "
+ "})(window, document, \"" + minVersion + "\", function($, jquery_loaded) {" + script + "});"
);
}
private static Object executejQuery(final WebEngine engine, String minVersion, String script) {
return executejQuery(engine, DEFAULT_JQUERY_MIN_VERSION, JQUERY_LOCATION, script);
}
private Object executejQuery(final WebEngine engine, String script) {
return executejQuery(engine, DEFAULT_JQUERY_MIN_VERSION, script);
}
堆栈跟踪:
netscape.javascript.JSException: SyntaxError: Unexpected token '<'
at com.sun.webpane.platform.WebPage.twkExecuteScript(Native Method)
at com.sun.webpane.platform.WebPage.executeScript(Unknown Source)
at javafx.scene.web.WebEngine.executeScript(Unknown Source)
at com.interpro.emmeclipse.commands.OpenPageBuilder.executejQuery(OpenPageBuilder.java:102)
at com.interpro.emmeclipse.commands.OpenPageBuilder.executejQuery(OpenPageBuilder.java:123)
at com.interpro.emmeclipse.commands.OpenPageBuilder.executejQuery(OpenPageBuilder.java:127)
at com.interpro.emmeclipse.commands.OpenPageBuilder.access$0(OpenPageBuilder.java:126)
at com.interpro.emmeclipse.commands.OpenPageBuilder$1.changed(OpenPageBuilder.java:69)
at com.interpro.emmeclipse.commands.OpenPageBuilder$1.changed(OpenPageBuilder.java:1)
at com.sun.javafx.binding.ExpressionHelper$SingleChange.fireValueChangedEvent(Unknown Source)
at com.sun.javafx.binding.ExpressionHelper.fireValueChangedEvent(Unknown Source)
at javafx.beans.property.ReadOnlyObjectPropertyBase.fireValueChangedEvent(Unknown Source)
at javafx.scene.web.WebEngine$DocumentProperty.invalidate(Unknown Source)
at javafx.scene.web.WebEngine$DocumentProperty.access$300(Unknown Source)
at javafx.scene.web.WebEngine$LoadWorker.dispatchLoadEvent(Unknown Source)
at javafx.scene.web.WebEngine$LoadWorker.access$600(Unknown Source)
at javafx.scene.web.WebEngine$PageLoadListener.dispatchLoadEvent(Unknown Source)
at com.sun.webpane.platform.WebPage.fireLoadEvent(Unknown Source)
at com.sun.webpane.platform.WebPage.fwkFireLoadEvent(Unknown Source)
at com.sun.webpane.webkit.network.URLLoader.twkDidFinishLoading(Native Method)
at com.sun.webpane.webkit.network.URLLoader.access$1300(Unknown Source)
at com.sun.webpane.webkit.network.URLLoader$6.run(Unknown Source)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(Unknown Source)
at org.eclipse.swt.internal.win32.OS.DispatchMessageW(Native Method)
at org.eclipse.swt.internal.win32.OS.DispatchMessage(OS.java:2546)
at org.eclipse.swt.widgets.Display.readAndDispatch(Display.java:3756)
at org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine$9.run(PartRenderingEngine.java:1053)
at org.eclipse.core.databinding.observable.Realm.runWithDefault(Realm.java:332)
at org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine.run(PartRenderingEngine.java:942)
at org.eclipse.e4.ui.internal.workbench.E4Workbench.createAndRunUI(E4Workbench.java:86)
at org.eclipse.ui.internal.Workbench$5.run(Workbench.java:588)
at org.eclipse.core.databinding.observable.Realm.runWithDefault(Realm.java:332)
at org.eclipse.ui.internal.Workbench.createAndRunWorkbench(Workbench.java:543)
at org.eclipse.ui.PlatformUI.createAndRunWorkbench(PlatformUI.java:149)
at org.eclipse.ui.internal.ide.application.IDEApplication.start(IDEApplication.java:124)
at org.eclipse.equinox.internal.app.EclipseAppHandle.run(EclipseAppHandle.java:196)
at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.runApplication(EclipseAppLauncher.java:110)
at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.start(EclipseAppLauncher.java:79)
at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:353)
at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:180)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.eclipse.equinox.launcher.Main.invokeFramework(Main.java:629)
at org.eclipse.equinox.launcher.Main.basicRun(Main.java:584)
at org.eclipse.equinox.launcher.Main.run(Main.java:1438)
at org.eclipse.equinox.launcher.Main.main(Main.java:1414)
线程“JavaFX应用程序线程”中的异常
答案 0 :(得分:1)
我认为由于缺少引号,下一行会产生错误:
"$(\"#content\").append(<div class='ui-statusbar'><h5 class='ui-title'>Test</h5></div>);"
尝试
"$(\"#content\").append(\"<div class='ui-statusbar'><h5 class='ui-title'>Test</h5></div>\");"