如何在 Java FX 中从 Web 视图中获取上下文菜单?



我想从javafx.scene.web.WebView对象获取默认上下文菜单项。然后我想以编程方式触发一个菜单项,例如:

table.getContextMenu().getItems().get(0).fire();

这可能吗?

这是一个 XY 问题。 根据您的评论:

实际上,我只想触发"在新窗口中打开框架"菜单项。

因此,您实际上根本不需要访问 WebView 的上下文菜单,您只想在新窗口中打开文档中的框架。

WebView 的 WebEngine 始终将 HTML 文档存储为 XML 文档对象。 WebEngine 完成文档加载后,您可以:

  • 使用 XPath 搜索帧
  • 检查每个框架元素的src属性
  • 根据原始 HTML 文档的 URI 解析src属性的值
  • 创建一个新的 Web 视图并在其中加载该新解析的 URI

首先,您必须等待 Web 引擎完成文档加载。 在加载页面之前,必须添加侦听器:

WebEngine engine = webView.getEngine();
engine.getLoadWorker().stateProperty().addListener(
(o, old, state) -> updateFrameList(state));
engine.load(url);
// ...
private void updateFrameList(Worker.State loadState) {
if (loadState == Worker.State.SUCCEEDED) {
Document doc = webView.getEngine().getDocument();

获得完全加载的文档后,可以使用 javax.xml.xpath 包来搜索框架:

NodeList frames;
try {
XPath xpath = XPathFactory.newInstance().newXPath();
frames = (NodeList) xpath.evaluate("//*" +
"[local-name() = 'frame'" +
" or local-name() = 'FRAME'" + 
" or local-name() = 'iframe'" + 
" or local-name() = 'IFRAME']" + 
"[@src]", doc, XPathConstants.NODESET);
} catch (XPathException e) {
throw new RuntimeException(e);
}

由于 XPath 表达式以//*开头,它只匹配元素,因此可以安全地将每个结果转换为 DOM 元素以检查其目标:

URI docURI = URI.create(webView.getEngine().getLocation());
int count = frames.getLength();
for (int i = 0; i < count; i++) {
Element frame = (Element) frames.item(i);
URI frameLocation = docURI.resolve(frame.getAttribute("src"));
// Show frameLocation in new window...
}

最简单的部分是显示框架的内容:

WebView frameView = new WebView();
frameView.getEngine().load(frameLocation.toString());
Stage stage = new Stage();
stage.setTitle("Frame content");
stage.setScene(new Scene(new BorderPane(frameView)));
stage.show();

这是一个将这一切组合在一起的程序:

import java.net.URI;
import java.util.Collection;
import java.util.Objects;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathFactory;
import javax.xml.xpath.XPathException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import javafx.application.Application;
import javafx.concurrent.Worker;
import javafx.geometry.Insets;
import javafx.geometry.Pos;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.ComboBox;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebView;
public class FrameOpener
extends Application {
private static class FrameInfo {
final String name;
final String uri;
FrameInfo(String name,
String uri) {
this.name = name;
this.uri = uri;
}
@Override
public boolean equals(Object obj) {
if (obj instanceof FrameInfo) {
FrameInfo other = (FrameInfo) obj;
return Objects.equals(this.name, other.name) &&
Objects.equals(this.uri, other.uri);
}
return false;
}
@Override
public int hashCode() {
return Objects.hash(name, uri);
}
@Override
public String toString() {
return name;
}
}
private ComboBox<FrameInfo> frameList;
private WebView webView;
@Override
public void start(Stage stage) {
String url;
Collection<String> params = getParameters().getRaw();
if (params.isEmpty()) {
url = "https://docs.oracle.com/javase/10/docs/api/index.html" +
"?overview-summary.html";
} else {
url = params.iterator().next();
}
frameList = new ComboBox<>();
Label label = new Label("_Frame:");
label.setMnemonicParsing(true);
label.setLabelFor(frameList);
Button showFrameButton = new Button("_Show frame");
showFrameButton.setMnemonicParsing(true);
showFrameButton.setOnAction(e -> showFrame());
webView = new WebView();
WebEngine engine = webView.getEngine();
engine.getLoadWorker().stateProperty().addListener(
(o, old, state) -> updateFrameList(state));
engine.load(url);
HBox framePane = new HBox(6, label, frameList, showFrameButton);
framePane.setAlignment(Pos.BASELINE_CENTER);
framePane.setFillHeight(true);
framePane.setPadding(new Insets(12));
Scene scene = new Scene(
new BorderPane(webView, null, null, framePane, null));
stage.setScene(scene);
stage.setTitle("Frame Opener");
stage.show();
}
private void updateFrameList(Worker.State loadState) {
if (loadState == Worker.State.SUCCEEDED) {
Document doc = webView.getEngine().getDocument();
NodeList frames;
try {
XPath xpath = XPathFactory.newInstance().newXPath();
frames = (NodeList) xpath.evaluate("//*" +
"[local-name() = 'frame'" +
" or local-name() = 'FRAME'" + 
" or local-name() = 'iframe'" + 
" or local-name() = 'IFRAME']" + 
"[@src]", doc, XPathConstants.NODESET);
} catch (XPathException e) {
throw new RuntimeException(e);
}
URI docURI = URI.create(webView.getEngine().getLocation());
int count = frames.getLength();
FrameInfo[] newFrameInfo = new FrameInfo[count];
for (int i = 0; i < count; i++) {
Element frame = (Element) frames.item(i);
URI frameLocation = docURI.resolve(frame.getAttribute("src"));
newFrameInfo[i] = new FrameInfo(
frame.getAttribute("name"), frameLocation.toString());
}
frameList.getItems().setAll(newFrameInfo);
if (newFrameInfo.length > 0) {
frameList.setValue(newFrameInfo[0]);
}
}
}
private void showFrame() {
FrameInfo info = frameList.getValue();
WebView frameView = new WebView();
frameView.getEngine().load(info.uri);
Stage stage = new Stage();
stage.setTitle(info.name);
stage.setScene(new Scene(new BorderPane(frameView)));
stage.show();
}
}

最新更新