1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
package Torello.Browser;

import Torello.Browser.Example01;
import Torello.Browser.WebSocketSender;

import Torello.Browser.BrowserAPI.Page;
import Torello.Browser.JavaScriptAPI.RunTime;

import Torello.Java.Additional.Ret2;
import Torello.HTML.Tools.Images.IF;

import java.io.File;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import javax.json.JsonString;

/**
 * This example code  saves a picture of the Sam Altman Web Page to disk.
 */
public class Example02
{
    private Example02() { }

    /** Run this method, save the image. */
    public static void main(String[] argv) throws Exception
    {
        // 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();
    }

    /**
     * Chat-GPT wrote this method; it waits for the page to load
     * 
     * @see RunTime#evaluate
     * @see RunTime.RemoteObject
     * @see RunTime.ExceptionDetails
     */
    public static void waitForLoad(final WebSocketSender pws, final int tries, final int ms)
        throws Exception
    {
        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);
        }
    }
}