PureBasic WebGadget — Events

Window Events (3)

#CrWS_Event_DevTools_Connect

This event is used to establish a network connection to the Chrome DevTools Protocol (CDP).

*PageData = EventData()
NewList PageData.PAGE_DATA()

If Cr_WebSocket_PageData(*PageData, PageData())
  If Not Connection
    LastElement(PageData())
    PageID$ = PageData()\id
    Connection = Cr_WebSocket_Connect(PageID$)
  EndIf
EndIf
FreeList(PageData())

#CrWS_Event_DevTools_Disconnect

When a disconnection occurs, there are three possible approaches:

  1. Ignore the event if the disconnection was intentional.
  2. Create a new connection to the Chrome DevTools Protocol (CDP).
  3. End the program gracefully.

#CrWS_Event_DevTools_Callback

Protocol events can be categorized into two types:

  1. User Event:
  2. Method Event:

Guidelines for processing protocol events:

  1. Name your event procedures carefully to simplify tracking and debugging.
  2. Handle events in separate procedures to avoid variable conflicts.
  3. Ensure procedure names match the corresponding protocol event for clarity.
JSONEvent = EventData()
JSONValue = JSONValue(JSONEvent)

If JSONType(JSONValue) = #PB_JSON_Object
  If ExamineJSONMembers(JSONValue)
    If NextJSONMember(JSONValue)
      JSONMemberValue = JSONMemberValue(JSONValue)

      Select JSONType(JSONMemberValue)
        Case #PB_JSON_Number
          If JSONMemberKey(JSONValue) = "id"
            JSONResult = GetJSONMember(JSONValue, "result")

            If JSONResult
              User_Event = GetJSONInteger(JSONMemberValue)

              Select User_Event
                Case #UE_Page_Navigate
                  UE_Page_Navigate(JSONResult)
                Case #UE_Target_SetDiscoverTargets
                  UE_Target_SetDiscoverTargets(JSONResult)
              EndSelect
            EndIf
          EndIf
        Case #PB_JSON_String
          If JSONMemberKey(JSONValue) = "method"
            JSONParams = GetJSONMember(JSONValue, "params")

            If JSONParams
              Method_Event$ = GetJSONString(JSONMemberValue)

              Select Method_Event$
                Case "Target.targetCreated"
                  ME_Target_TargetCreated(JSONParams)
                Case "Target.targetInfoChanged"
                  ME_Target_TargetInfoChanged(JSONParams)
              EndSelect
            EndIf
          EndIf
      EndSelect
    EndIf
  EndIf
EndIf
FreeJSON(JSONEvent)