Attribute VB_Name = "TrackworkResult_aspx"

Option Explicit


Public Sub test()
  'horse_id = "HK_2022_H195"
  'row_offset = 0
  Dim horse_id As String
  Dim row_offset As String


  ' Create a new instance of the WebDriver.
  Dim driver As New WebDriver

  ' Set the path to the WebDriver executable.
  driver.SetBinary "C:\Users\logic\AppData\Local\Chromium\Application\chrome.exe"

  ' Start Chrome.
  driver.Start "chrome"


    Dim not_use As String
    not_use = TrackworkResult_aspx.Scrape(driver, "HK_2022_H195", 0, "2024/04/20")


    driver.Quit

End Sub

Public Function Scrape(driver As WebDriver, horse_id As String, ByVal row_offset As Integer, ByVal s_date As String)

  Dim script_content As String

  ' Create a new instance of the WebDriver.
  'Dim driver As New WebDriver

  ' Set the path to the WebDriver executable.
  'driver.SetBinary "C:\Users\logic\AppData\Local\Chromium\Application\chrome.exe"

  ' Start Chrome.
  'driver.Start "chrome"

  ' Navigate to a website.
  driver.Get "https://racing.hkjc.com/racing/information/English/Trackwork/TrackworkResult.aspx?HorseId=" & horse_id

  ' document.querySelectorAll('#racecardlist tbody tr')[2].textContent
  Dim i, j As Integer


  Dim startCell As Range
  Set startCell = Worksheets("Sheet1").Range("A3")

  Dim all_row As String

  script_content = "{" & _
  "list_e = [];" & _
  "document.querySelector('div.performance table').querySelectorAll('tr').forEach((r,i) => {" & _
  "    if (i > 0) list_e.push(r.textContent.replace(/\n +/g,'___').slice(3,-3));" & _
  "});" & _
  "return list_e.join('|||');" & _
  "}"

  all_row = driver.ExecuteScript(script_content)

  Dim table_rows As Variant
  table_rows = Split(all_row, "|||")

  For i = 0 To UBound(table_rows) - 1

    Dim td_s As Variant

    Dim event_date As String
    td_s = Split(table_rows(i), "___")
    event_date = td_s(0)

    Dim event_person as String
    td_s = Split(table_rows(i), "___")
    event_person = td_s(3)


    Dim date_diff As Integer
    date_diff = DateDiff("d", Common.ParseDDMMYYYY(event_date), s_date)

    If (date_diff <= 20) Then
      If (InStr(td_s(1), "Barrier Trial") > 0) Then
        startCell.Offset(row_offset, 24).value = "Y"
        If (startCell.Offset(row_offset, 25).value = "") Then
          startCell.Offset(row_offset, 25).value = event_date
        Else
          startCell.Offset(row_offset, 25).value = startCell.Offset(row_offset, 25).value & "," & event_date
        End If
      End If


      If (InStr(td_s(1), "Gallop") > 0) Then
        ' Gallop by R.B. ?
        if (InStr(event_person, "R.B.") > 0) Then
          startCell.Offset(row_offset, 26).value = "Y"
        end if

        If (startCell.Offset(row_offset, 27).value = "") Then
          startCell.Offset(row_offset, 27).value = event_date
        Else
          startCell.Offset(row_offset, 27).value = startCell.Offset(row_offset, 27).value & "," & event_date
          ' Dim current_cell As Variant
          ' Set current_cell = startCell.Offset(0, 27)
          ' current_cell.Hyperlinks.Add Anchor:=current_cell, Address:="https://racing.hkjc.com/racing/information/Chinese/Horse/Horse.aspx?HorseId=" & horse_id
        End If
      End If
    End If

  Next

  'driver.Quit

End Function





