Yes, and all the control is in the hands of the programmer; no predefined function, but still very simple.zikitrake wrote:Is there any way to check that a page has actually finished loading?
Some Information
EventID is user-defined and can be any number.
You can use the same number for every command.
You can change the number when executing the same command multiple times.
But, it becomes important to keep track of the number when you need to monitor an event, especially for returned data.
Other events are predefined, and once a Domain is enabled can be easily tracked. Personally, when I finish using a Domain I disable it.
Here is a simple test using CDP Control Panel.
1. Select browser.
2. Listen Port.
3. Connect DevTools.
4. Press F1 to open Display Area.
5. Right mouse click Page.navigate in Command List.
6. Choose Send Selected.
In the Display Area the only event you see is Page.navigate.
1. Enter 12345 in the EventID field.
2. Enter Page.enable in the Method field.
3. Press the Send Command button.
In the Display Area you see your event.
1. Right mouse click Page.navigate in Command List.
2. Choose Send Selected.
The event you're looking for is Page.frameStoppedLoading with a FrameId that matches the PageID; the DevTools Protocol is your friend.
The following script is a bit advanced, but should provide a better understanding of the WebGadget and answer your question.
Code: Select all
Enumeration
  #MainWindow
  #CB_WebGadget
  #ButtonGadget
EndEnumeration
#CB_WebGadget_User_Directory_Override = #True
#CB_WebGadget_User_Directory_Name = #PB_Compiler_Filename
#CB_WebSocket_Enable_DevTools_Protocol = #True
Enumeration #PB_Event_FirstCustomValue
  #CBWS_Event_Port_Listen_Connection
  #CBWS_Event_Page_Loaded
EndEnumeration
IncludeFile "..\CBWG Sources\cbwg_sources.pbi"
Procedure DevToolsEvent(JSONValue, Connection, PageID.s)
  If ExamineJSONMembers(JSONValue)
    SendData.SEND_DATA
    If NextJSONMember(JSONValue)
      JSONMemberValue = JSONMemberValue(JSONValue)
      Select JSONType(JSONMemberValue)
        Case #PB_JSON_Number
          If JSONMemberKey(JSONValue) = "id"
            EventID = GetJSONInteger(JSONMemberValue)
            Select EventID
              Case 12345 : Debug "Domain Page enabled"
            EndSelect
          EndIf
        Case #PB_JSON_String
          If JSONMemberKey(JSONValue) = "method"
            Method$ = GetJSONString(JSONMemberValue)
            Select Method$
              Case "Page.frameStoppedLoading"
                JSONParams = GetJSONMember(JSONValue, "params")
                If JSONParams
                  JSONOFrameId = GetJSONMember(JSONParams, "frameId")
                  If JSONOFrameId
                    FrameId$ = GetJSONString(JSONOFrameId)
                    If FrameId$ = PageID
                      PostEvent(#CBWS_Event_Page_Loaded)
                      SendData\EventID = 54321
                      SendData\Method = "Page.disable"
                      SendData\Params = #Null$
                      CB_WebSocket_Send(Connection, @SendData)
                    EndIf
                  EndIf
                EndIf
            EndSelect
          EndIf
      EndSelect
    EndIf
  EndIf
EndProcedure
WindowWidth = 800 : WindowHeight = 600
nFlags = #PB_Window_SystemMenu | #PB_Window_MinimizeGadget | #PB_Window_ScreenCentered
If OpenWindow(#MainWindow, 0, 0, WindowWidth, WindowHeight, "Chromium Browser WebGadget", nFlags)
  CB_WebGadget(#CB_WebGadget, 10, 50, 780, 540, #CB_WebGadget_Flat)
  ButtonGadget(#ButtonGadget, 10, 10, 150, 30, "Page.navigate")
  DisableGadget(#ButtonGadget, #True)
  cbwg_data.CBWG_DATA
  cbwg_data\GadgetNumber = #CB_WebGadget
  cbwg_data\SourceURL = "https://start.duckduckgo.com"
  CB_WebGadget_Init(cbwg_data)
  SendData.SEND_DATA
  Repeat
    If Connection
      NetworkClientEvent = NetworkClientEvent(Connection)
      Select NetworkClientEvent
        Case #PB_NetworkEvent_Data
          *Response = CB_WebSocket_Receive(Connection)
          If *Response
            Response$ = Trim(PeekS(*Response, -1, #PB_UTF8 | #PB_ByteLength))
            If Response$
              nCount = CountString(Response$, #LF$)
              If nCount
                For rtnCount = 1 To nCount + 1
                  ResponseItem$ = StringField(Response$, rtnCount, #LF$)
                  JSON_Event_1 = ParseJSON(#PB_Any, ResponseItem$)
                  JSONValue = JSONValue(JSON_Event_1)
                  If IsJSON(JSON_Event_1) And JSONType(JSONValue) = #PB_JSON_Object
                    DevToolsEvent(JSONValue, Connection, PageID$)
                    FreeJSON(JSON_Event_1)
                  EndIf
                Next
              Else
                JSON_Event_2 = ParseJSON(#PB_Any, Response$)
                JSONValue = JSONValue(JSON_Event_2)
                If IsJSON(JSON_Event_2) And JSONType(JSONValue) = #PB_JSON_Object
                  DevToolsEvent(JSONValue, Connection, PageID$)
                  FreeJSON(JSON_Event_2)
                EndIf
              EndIf
            EndIf
          EndIf
          FreeMemory(*Response)
        Case #PB_NetworkEvent_None
        Case #PB_NetworkEvent_Disconnect
      EndSelect
    EndIf
    Event = WindowEvent()
    Select Event
      Case #CBWS_Event_Port_Listen_Connection
        *EventData = EventData()
        If *EventData
          EventData$ = PeekS(*EventData, -1, #PB_UTF8)
          If Not Len(PageID$)
            JSON_Port = ParseJSON(#PB_Any, EventData$)
            If IsJSON(JSON_Port)
              NewList PortData.PORT_DATA()
              ExtractJSONList(JSONValue(JSON_Port), PortData())
              ForEach PortData()
                If PortData()\type = "page"
                  PageID$ = PortData()\id
                  Connection = CB_WebSocket_Connect(PageID$)
                  If Connection : DisableGadget(#ButtonGadget, #False) : EndIf
                  Break
                EndIf
              Next
              FreeJSON(JSON_Port)
            EndIf
          EndIf
          FreeMemory(*EventData)
        EndIf
      Case #CBWS_Event_Page_Loaded
        MessageRequester("Chromium Browser WebGadget", "Page Loaded", #PB_MessageRequester_Info)
      Case #PB_Event_Gadget
        Select EventGadget()
          Case #ButtonGadget
            If Connection
              SendData\EventID = 12345
              SendData\Method = "Page.enable"
              SendData\Params = #Null$
              CB_WebSocket_Send(Connection, @SendData)
              SendData\EventID = 20300
              SendData\Method = "Page.navigate"
              SendData\Params = #DQUOTE$ + "url" + #DQUOTE$ + ":" + #DQUOTE$ + "https://www.purebasic.fr/english/index.php" + #DQUOTE$
              CB_WebSocket_Send(Connection, @SendData)
            EndIf
        EndSelect
      Case #PB_Event_CloseWindow : Break
    EndSelect
  ForEver
EndIf
