如何从Java FX的webview获取上下文菜单?

时间:2018-09-20 14:29:18

标签: java javafx

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

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

这可能吗?

1 个答案:

答案 0 :(得分:1)

这是XY problem。根据您的评论:

  

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

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

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

  • 使用XPath
  • 搜索帧
  • 检查每个框架元素的src属性
  • resolve相对于原始HTML文档URI的src属性的值
  • 创建一个新的WebView并将新解析的URI加载到其中

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

    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 Element以便检查其目的地:

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();
    }
}