Skip to content

Instantly share code, notes, and snippets.

@varren
Last active August 29, 2015 14:22
Show Gist options
  • Save varren/1fb41536f2b95f69be4e to your computer and use it in GitHub Desktop.
Save varren/1fb41536f2b95f69be4e to your computer and use it in GitHub Desktop.
package ru.varren;
import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.concurrent.Worker;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.scene.layout.VBox;
import javafx.scene.web.WebEngine;
import javafx.scene.web.WebView;
import javafx.stage.Stage;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import java.io.File;
import java.io.IOException;
import java.util.Scanner;
public class Main extends Application{
public static void main(String[] args) {
launch(args);
}
public void start(Stage primaryStage) throws Exception {
VBox root = new VBox();
final WebView webView = new WebView();
webView.getEngine().getLoadWorker().stateProperty().addListener(
new ChangeListener<Worker.State>() {
public void changed(ObservableValue ov, Worker.State oldState, Worker.State newState) {
if (newState == Worker.State.SUCCEEDED) {
try {
WebEngine engine = webView.getEngine();
String highlightScript = readFile(Main.class.getResource("/script.js").getPath());
Document page = engine.getDocument();
Element jquery = page.createElement("script");
jquery.setAttribute("src",Main.class.getResource("/jquery-1.11.3.min.js".getPath());
Element jqueryHighlight = page.createElement("script");
jqueryHighlight.setTextContent(highlightScript);
Node head = page.getElementsByTagName("head").item(0);
head.appendChild(jquery);
head.appendChild(jqueryHighlight);
} catch (IOException e) {
e.printStackTrace();
}
}
}
});
webView.getEngine().load("http://stackoverflow.com/questions/30767692/cant-get-javascript-to-work-in-webview");
TextField textField = new TextField();
textField.setText("test");
Button button = new Button("Test Javascript");
button.setOnMouseClicked(e->{
webView.getEngine().executeScript("$('body').removeHighlight().highlight('" + textField.getText() + "')");
System.out.println("click");
});
root.getChildren().addAll(webView,textField,button);
Scene scene = new Scene(root,600,600);
primaryStage.setScene(scene);
primaryStage.show();
}
private String readFile(String pathname) throws IOException {
File file = new File(pathname);
StringBuilder fileContents = new StringBuilder((int)file.length());
String lineSeparator = System.getProperty("line.separator");
try (Scanner scanner = new Scanner(file)) {
while (scanner.hasNextLine()) {
fileContents.append(scanner.nextLine()).append(lineSeparator);
}
return fileContents.toString();
}
}
}
/*
highlight v5
Highlights arbitrary terms.
<http://johannburkard.de/blog/programming/javascript/highlight-javascript-text-higlighting-jquery-plugin.html>
MIT license.
Johann Burkard
<http://johannburkard.de>
<mailto:jb@eaio.com>
*/
jQuery.fn.highlight = function(pat) {
function innerHighlight(node, pat) {
var skip = 0;
if (node.nodeType == 3) {
var pos = node.data.toUpperCase().indexOf(pat);
pos -= (node.data.substr(0, pos).toUpperCase().length - node.data.substr(0, pos).length);
if (pos >= 0) {
var spannode = document.createElement('span');
spannode.className = 'highlight';
var middlebit = node.splitText(pos);
var endbit = middlebit.splitText(pat.length);
var middleclone = middlebit.cloneNode(true);
spannode.appendChild(middleclone);
middlebit.parentNode.replaceChild(spannode, middlebit);
skip = 1;
}
}
else if (node.nodeType == 1 && node.childNodes && !/(script|style)/i.test(node.tagName)) {
for (var i = 0; i < node.childNodes.length; ++i) {
i += innerHighlight(node.childNodes[i], pat);
}
}
return skip;
}
return this.length && pat && pat.length ? this.each(function() {
innerHighlight(this, pat.toUpperCase());
}) : this;
};
jQuery.fn.removeHighlight = function() {
return this.find("span.highlight").each(function() {
this.parentNode.firstChild.nodeName;
with (this.parentNode) {
replaceChild(this.firstChild, this);
normalize();
}
}).end();
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment