001package Torello.Browser;
002
003import Torello.Browser.Example01;
004import Torello.Browser.WebSocketSender;
005
006import Torello.Browser.BrowserAPI.Page;
007import Torello.Browser.JavaScriptAPI.RunTime;
008
009import Torello.Java.Additional.Ret2;
010import Torello.HTML.Tools.Images.IF;
011
012import java.io.File;
013import javax.imageio.ImageIO;
014import java.awt.image.BufferedImage;
015import javax.json.JsonString;
016
017/**
018 * This example code  saves a picture of the Sam Altman Web Page to disk.
019 */
020public class Example02
021{
022    private Example02() { }
023
024    /** Run this method, save the image. */
025    public static void main(String[] argv) throws Exception
026    {
027        // Opening a WebSocket Browser-Connection to the currently running Chrome-Instance
028        final WebSocketSender bws = Example01.STEP_01_openBrowserWebSocket();
029
030        // Close any currently opened pages / tabs inside the browser
031        Example01.STEP_02_closeAllPages(bws);
032
033        // Open a Browser-Page (using 'bws') for reading Sam Altman's Wikipedia Profile
034        final String targetID = Example01.STEP_03_openSamAltmanPage(bws);
035
036        // Create / Build a WebSocket-Connection object to the newly opened Sam Altman Page.
037        final WebSocketSender pws = Example01.STEP_04_getPageWebSocket(targetID);
038
039        // Enable the Page domain
040        System.out.println("\nPage.enable()");
041        Page.enable(null /* Boolean */).exec(pws).await();
042
043        // Chat-GPT and Gemini are fighting.  Chat-GPT told me to add this.
044        RunTime.enable().exec(pws).await();
045        waitForLoad(pws, 10, 1000);
046
047        final String b64Img = Page
048            // .captureScreenshot("png", null, null, null, null, null).exec(pws).await();
049            .captureScreenshot()
050            .accept("format", "png")
051            .accept("fromSurface", true)
052            .build()
053            .exec(pws)
054            .await();
055
056        final BufferedImage img = IF.decodeBase64ToImage(b64Img, IF.JPG);
057
058        ImageIO.write(img, "jpg", new File("image.jpg"));
059
060        bws.disconnect();
061        pws.disconnect();
062    }
063
064    /**
065     * Chat-GPT wrote this method; it waits for the page to load
066     * 
067     * @see RunTime#evaluate
068     * @see RunTime.RemoteObject
069     * @see RunTime.ExceptionDetails
070     */
071    public static void waitForLoad(final WebSocketSender pws, final int tries, final int ms)
072        throws Exception
073    {
074        for (int i = 0; i < tries; i++)
075        {
076            /*
077            // We have "The Builder" class now!  This is no longer needed.
078            final RunTime.evaluate$$RET r = RunTime.evaluate(
079                    "document.readyState",
080                    null, null, null, null, null, 
081                    null, null, null, null, null,
082                    null, null, null, null, null
083                )
084                .exec(pws)
085                .await();
086            */
087
088           final RunTime.evaluate$$RET r = RunTime
089                .evaluate()
090                .accept("expression", "document.readyState")
091                .accept("returnByValue", true)
092                .build()
093                .exec(pws)
094                .await();
095
096            final String val = ((JsonString) r.result.value).getString();
097
098            if ("complete".equalsIgnoreCase(val))
099            {
100                System.out.println("COMPLETED");
101                return;
102            }
103            
104            Thread.sleep(ms);
105        }
106    }
107}