3

我希望我的程序自动单击更多,然后单击 javafx 中带有 jquery 的以下 youtube 页面的音译

我想在源代码中转录的目标,如果可能的话,我对 jquery 不太了解希望你能帮助我

        package templates;

        import java.net.URL;
        import java.util.ResourceBundle;
        import javafx.beans.value.ChangeListener;
        import javafx.beans.value.ObservableValue;
        import javafx.event.ActionEvent;
        import javafx.fxml.FXML;
        import javafx.fxml.Initializable;
        import javafx.scene.control.Button;
        import javafx.scene.control.TextArea;
        import javafx.scene.web.WebEngine;
        import javafx.scene.web.WebView;
        import org.w3c.dom.Document;

        /**
         *
         * @author Administrator
         */
        public class FXMLDocumentController implements Initializable {

            @FXML
            private WebView browser;

            @FXML
            private Button btnScan;

            @FXML
            private TextArea txtAvailable;
  ////////////////////////////////////////////////////////////////////////      
            @FXML
            void btnScan(ActionEvent event) {
                final WebEngine engine = browser.getEngine();
                engine.load("http://www.youtube.com/watch?v=8nECBJ1xRRA");
                engine.documentProperty().addListener((ObservableValue<? extends Document> prop, Document oldDoc, Document newDoc) -> {
                    executejQuery(
                            engine,
                            "$(\"span.yt-uix-button-content\").click(function(event){});"
                    );
                });
    System.out.println((String) webEngine.executeScript("document.documentElement.outerHTML"));
            }
          ////////////////////////////////////////////////////////////////////////
            @Override
            public void initialize(URL url, ResourceBundle rb) {
                // TODO
            }

            public static final String DEFAULT_JQUERY_MIN_VERSION = "1.7.2";
            public static final String JQUERY_LOCATION = "http://code.jquery.com/jquery-1.7.2.min.js";

            /**
             * 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');}");
            }

            /**
             * 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);
            }
        }
4

0 回答 0