use of com.teamdev.jxbrowser.browser.Browser in project JxBrowser-Examples by TeamDev-IP.
the class JsFunctionCallback method main.
public static void main(String[] args) {
try (Engine engine = Engine.newInstance(OFF_SCREEN)) {
Browser browser = engine.newBrowser();
browser.mainFrame().ifPresent(frame -> {
JsObject jsObject = frame.executeJavaScript("window");
if (jsObject != null) {
// Inject JsFunctionCallback into JavaScript and associate it
// with the "window.sayHelloTo" JavaScript property.
jsObject.putProperty("sayHelloTo", (com.teamdev.jxbrowser.js.JsFunctionCallback) arguments -> "Hello, " + arguments[0]);
}
String greetings = frame.executeJavaScript("window.sayHelloTo('John')");
System.out.println(greetings);
});
}
}
use of com.teamdev.jxbrowser.browser.Browser in project JxBrowser-Examples by TeamDev-IP.
the class LoadHtml method main.
public static void main(String[] args) {
Engine engine = Engine.newInstance(HARDWARE_ACCELERATED);
Browser browser = engine.newBrowser();
SwingUtilities.invokeLater(() -> {
BrowserView view = BrowserView.newInstance(browser);
JFrame frame = new JFrame("Load HTML");
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.add(view, BorderLayout.CENTER);
frame.setSize(700, 500);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
});
browser.mainFrame().ifPresent(mainFrame -> {
mainFrame.loadHtml("<html><body><h1>Hello there!</h1></body></html>");
});
}
use of com.teamdev.jxbrowser.browser.Browser in project JxBrowser-Examples by TeamDev-IP.
the class LoadHtmlThroughInterceptRequest method main.
public static void main(String[] args) {
InterceptUrlRequestCallback interceptCallback = params -> {
if (params.urlRequest().url().endsWith("?load-html")) {
byte[] bytes = "<html><body>Hello!</body></html>".getBytes();
UrlRequestJob job = params.newUrlRequestJob(UrlRequestJob.Options.newBuilder(HttpStatus.OK).addHttpHeader(HttpHeader.of("Content-Type", "text/html")).build());
job.write(bytes);
job.complete();
return InterceptUrlRequestCallback.Response.intercept(job);
}
return InterceptUrlRequestCallback.Response.proceed();
};
Engine engine = Engine.newInstance(EngineOptions.newBuilder(HARDWARE_ACCELERATED).addScheme(Scheme.HTTP, interceptCallback).build());
Browser browser = engine.newBrowser();
SwingUtilities.invokeLater(() -> {
BrowserView view = BrowserView.newInstance(browser);
JFrame frame = new JFrame("Load Local Files");
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.add(view, BorderLayout.CENTER);
frame.setSize(700, 500);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
});
browser.navigation().loadUrl("http://localhost?load-html");
}
use of com.teamdev.jxbrowser.browser.Browser in project JxBrowser-Examples by TeamDev-IP.
the class CustomProtocol method main.
public static void main(String[] args) {
Engine engine = Engine.newInstance(EngineOptions.newBuilder(HARDWARE_ACCELERATED).addScheme(Scheme.of(PROTOCOL), new InterceptCustomSchemeCallback()).build());
Browser browser = engine.newBrowser();
invokeLater(() -> {
BrowserView view = BrowserView.newInstance(browser);
JFrame frame = new JFrame("Custom Protocol Handler");
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.add(view, BorderLayout.CENTER);
frame.setSize(700, 500);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
});
browser.navigation().loadUrl(PROTOCOL + "://hello");
}
use of com.teamdev.jxbrowser.browser.Browser in project JxBrowser-Examples by TeamDev-IP.
the class DefaultZoomLevel method main.
public static void main(String[] args) {
Engine engine = Engine.newInstance(HARDWARE_ACCELERATED);
Browser browser = engine.newBrowser();
SwingUtilities.invokeLater(() -> {
BrowserView view = BrowserView.newInstance(browser);
JFrame frame = new JFrame("Change Default Zoom Level");
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.add(view, BorderLayout.CENTER);
frame.setSize(700, 500);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
});
engine.zoomLevels().defaultLevel(ZoomLevel.P_300);
browser.navigation().loadUrl("https://www.google.com");
}
Aggregations