use of com.teamdev.jxbrowser.frame.Frame in project JxBrowser-Examples by TeamDev-IP.
the class PrintSettings method main.
public static void main(String[] args) {
Engine engine = Engine.newInstance(HARDWARE_ACCELERATED);
Browser browser = engine.newBrowser();
browser.set(PrintCallback.class, (params, tell) -> tell.print());
// #docfragment "Callback"
browser.set(PrintHtmlCallback.class, (params, tell) -> {
// #docfragment "Configure settings"
SystemPrinter<HtmlSettings> printer = params.printers().defaultPrinter().orElseThrow(IllegalStateException::new);
PrintJob<HtmlSettings> printJob = printer.printJob();
printJob.settings().paperSize(ISO_A4).colorModel(COLOR).enablePrintingBackgrounds().disablePrintingHeaderFooter().orientation(PORTRAIT).apply();
// #enddocfragment "Configure settings"
// #docfragment "Subscribe to PrintCompleted"
printJob.on(PrintCompleted.class, event -> {
if (event.isSuccess()) {
System.out.println("Printing is completed successfully.");
} else {
System.out.println("Printing has failed.");
}
});
// #enddocfragment "Subscribe to PrintCompleted"
// #docfragment "Proceed"
tell.proceed(printer);
// #enddocfragment "Proceed"
});
// #enddocfragment "Callback"
browser.navigation().loadUrlAndWait("https://google.com");
browser.mainFrame().ifPresent(Frame::print);
}
use of com.teamdev.jxbrowser.frame.Frame in project JxBrowser-Examples by TeamDev-IP.
the class DomGetAttributes method main.
public static void main(String[] args) {
Engine engine = Engine.newInstance(OFF_SCREEN);
Browser browser = engine.newBrowser();
browser.mainFrame().ifPresent(mainFrame -> {
mainFrame.loadHtml("<html><body><a href='#' id='link' title='link title'>Link</a></body></html>");
});
browser.mainFrame().flatMap(Frame::document).flatMap(Document::documentElement).flatMap(element -> element.findElementById("link")).ifPresent(linkElement -> linkElement.attributes().forEach(DomGetAttributes::print));
}
use of com.teamdev.jxbrowser.frame.Frame in project JxBrowser-Examples by TeamDev-IP.
the class DomSelectOption 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("DOM Select Option");
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.add(view, BorderLayout.CENTER);
frame.setSize(700, 500);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
});
browser.navigation().on(FrameLoadFinished.class, event -> browser.mainFrame().flatMap(Frame::document).flatMap(Document::documentElement).flatMap(element -> element.findElementById("select-tag")).ifPresent(selectElement -> {
Object[] options = ((SelectElement) selectElement).options().toArray();
((OptionElement) options[2]).select();
System.out.println(selectElement.innerHtml());
}));
browser.mainFrame().ifPresent(mainFrame -> {
mainFrame.loadHtml("<html><body><select id='select-tag'>\n" + " <option value=\"volvo\">Volvo</option>\n" + " <option value=\"saab\">Saab</option>\n" + " <option value=\"opel\">Opel</option>\n" + " <option value=\"audi\">Audi</option>\n" + "</select></body></html>");
});
}
use of com.teamdev.jxbrowser.frame.Frame in project JxBrowser-Examples by TeamDev-IP.
the class ContentListening method main.
public static void main(String[] args) {
// #docfragment "engine-creation"
Engine engine = Engine.newInstance(EngineOptions.newBuilder(HARDWARE_ACCELERATED).build());
Browser browser = engine.newBrowser();
// #enddocfragment "engine-creation"
// #docfragment "embed-browser-view"
SwingUtilities.invokeLater(() -> {
BrowserView view = BrowserView.newInstance(browser);
JFrame frame = new JFrame("Content Listening");
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.add(view, BorderLayout.CENTER);
frame.setSize(700, 500);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
});
// #enddocfragment "embed-browser-view"
// #docfragment "inject-js"
browser.set(InjectJsCallback.class, params -> {
Frame frame = params.frame();
String window = "window";
JsObject jsObject = frame.executeJavaScript(window);
if (jsObject == null) {
throw new IllegalStateException(format("'%s' JS object not found", window));
}
jsObject.putProperty("java", new JavaObject());
return Response.proceed();
});
// #enddocfragment "inject-js"
// #docfragment "frame-load-finished"
browser.navigation().on(FrameLoadFinished.class, event -> {
String javaScript = load("observer.js");
event.frame().executeJavaScript(javaScript);
});
// #enddocfragment "frame-load-finished"
// #docfragment "load-page"
String html = load("index.html");
String base64Html = Base64.getEncoder().encodeToString(html.getBytes(UTF_8));
String dataUrl = "data:text/html;base64," + base64Html;
browser.navigation().loadUrl(dataUrl);
// #enddocfragment "load-page"
}
use of com.teamdev.jxbrowser.frame.Frame in project convertigo by convertigo.
the class ApplicationComponentEditor method createBrowser.
private void createBrowser(Composite parent) {
browserScroll = new ScrolledComposite(parent, SWT.H_SCROLL | SWT.V_SCROLL);
browserScroll.setLayoutData(new GridData(GridData.FILL, GridData.FILL, true, true));
browserScroll.setExpandHorizontal(true);
browserScroll.setExpandVertical(true);
Composite canvas = new Composite(browserScroll, SWT.NONE);
browserScroll.setContent(canvas);
GridLayout gl = new GridLayout(1, false);
gl.marginBottom = gl.marginTop = gl.marginLeft = gl.marginRight = gl.marginHeight = gl.marginWidth = gl.horizontalSpacing = gl.verticalSpacing = 0;
canvas.setLayout(gl);
c8oBrowser = new C8oBrowser(canvas, SWT.NONE, applicationEditorInput.application.getProject());
browserGD = new GridData(SWT.CENTER, SWT.CENTER, true, true);
c8oBrowser.setLayoutData(browserGD);
browser = c8oBrowser.getBrowser();
debugUrl = c8oBrowser.getDebugUrl();
browserInterface = new ApplicationComponentBrowserImpl();
browser.set(InjectJsCallback.class, params -> {
String url = params.frame().browser().url();
if (baseUrl != null && url.startsWith(baseUrl)) {
try {
Frame frame = params.frame();
JsObject sessionStorage = frame.executeJavaScript("sessionStorage");
frame.executeJavaScript("sessionStorage.setItem('_c8ocafsession_storage_mode', 'session');\n" + "navigator.__defineGetter__('userAgent', function(){ return '" + deviceOS.agent() + "'});\n" + IOUtils.toString(getClass().getResourceAsStream("inject.js"), "UTF-8"));
sessionStorage.call("setItem", "_c8ocafsession_storage_mode", "session");
if (!dataset.equals("none")) {
String json = FileUtils.readFileToString(new File(datasetDir, dataset + ".json"), "UTF-8");
sessionStorage.call("setItem", "_c8ocafsession_storage_data", json);
} else {
sessionStorage.call("setItem", "_c8ocafsession_storage_data", null);
}
JsObject window = frame.executeJavaScript("window");
window.putProperty("java", browserInterface);
} catch (Exception e) {
Engine.logStudio.info("onScriptContextCreate failed for '" + url + "' with baseUrl '" + baseUrl + "': " + e.getMessage());
}
}
// browser.setZoomLevel(zoomFactor.zoomLevel());
return Response.proceed();
});
browser.set(ShowContextMenuCallback.class, (params, tell) -> {
com.teamdev.jxbrowser.ui.Point location = params.location();
highlightPoint(location.x(), location.y());
tell.close();
});
browser.engine().permissions().set(RequestPermissionCallback.class, (params, tell) -> {
tell.grant();
});
}
Aggregations