Package Torello.Browser
Class Example02
- java.lang.Object
-
- Torello.Browser.Example02
-
public class Example02 extends java.lang.Object
This example code saves a picture of the Sam Altman Web Page to disk.
Hi-Lited Source-Code:- View Here: Torello/Browser/Example02.java
- Open New Browser-Tab: Torello/Browser/Example02.java
File Size: 3,395 Bytes Line Count: 107 '\n' Characters Found
-
-
Method Summary
Example Steps Modifier and Type Method static voidmain(String[] argv)static voidwaitForLoad(WebSocketSender pws, int tries, int ms)
-
-
-
Method Detail
-
main
public static void main(java.lang.String[] argv) throws java.lang.Exception
Run this method, save the image.- Throws:
java.lang.Exception- Code:
- Exact Method Body:
// Opening a WebSocket Browser-Connection to the currently running Chrome-Instance final WebSocketSender bws = Example01.STEP_01_openBrowserWebSocket(); // Close any currently opened pages / tabs inside the browser Example01.STEP_02_closeAllPages(bws); // Open a Browser-Page (using 'bws') for reading Sam Altman's Wikipedia Profile final String targetID = Example01.STEP_03_openSamAltmanPage(bws); // Create / Build a WebSocket-Connection object to the newly opened Sam Altman Page. final WebSocketSender pws = Example01.STEP_04_getPageWebSocket(targetID); // Enable the Page domain System.out.println("\nPage.enable()"); Page.enable(null /* Boolean */).exec(pws).await(); // Chat-GPT and Gemini are fighting. Chat-GPT told me to add this. RunTime.enable().exec(pws).await(); waitForLoad(pws, 10, 1000); final String b64Img = Page // .captureScreenshot("png", null, null, null, null, null).exec(pws).await(); .captureScreenshot() .accept("format", "png") .accept("fromSurface", true) .build() .exec(pws) .await(); final BufferedImage img = IF.decodeBase64ToImage(b64Img, IF.JPG); ImageIO.write(img, "jpg", new File("image.jpg")); bws.disconnect(); pws.disconnect();
-
waitForLoad
public static void waitForLoad(WebSocketSender pws, int tries, int ms) throws java.lang.Exception
Chat-GPT wrote this method; it waits for the page to load- Throws:
java.lang.Exception- See Also:
RunTime.evaluate(java.lang.String, java.lang.String, java.lang.Boolean, java.lang.Boolean, java.lang.Integer, java.lang.Boolean, java.lang.Boolean, java.lang.Boolean, java.lang.Boolean, java.lang.Boolean, java.lang.Number, java.lang.Boolean, java.lang.Boolean, java.lang.Boolean, java.lang.String, Torello.Browser.JavaScriptAPI.RunTime.SerializationOptions),RunTime.RemoteObject,RunTime.ExceptionDetails- Code:
- Exact Method Body:
for (int i = 0; i < tries; i++) { /* // We have "The Builder" class now! This is no longer needed. final RunTime.evaluate$$RET r = RunTime.evaluate( "document.readyState", null, null, null, null, null, null, null, null, null, null, null, null, null, null, null ) .exec(pws) .await(); */ final RunTime.evaluate$$RET r = RunTime .evaluate() .accept("expression", "document.readyState") .accept("returnByValue", true) .build() .exec(pws) .await(); final String val = ((JsonString) r.result.value).getString(); if ("complete".equalsIgnoreCase(val)) { System.out.println("COMPLETED"); return; } Thread.sleep(ms); }
-
-