Saving OuterHTML using Await Implementation with TWebView

Saving OuterHTML using Await Implementation with TWebView

Postby CharlesKwon » Mon Aug 14, 2023 1:57 pm

Saving OuterHTML using Await Implementation with TWebView

1. Fivewin features a powerful Web Control class called TWebView.
2. The web rendering operates in async mode.
3. Consequently, users familiar with procedural synchronous modes are facing difficulties in retrieving information from the web.
4. Therefore, I have implemented a synchronous class for TWebView, which is supported by the 'await' technique in other languages.
5. The implementation is named TAWaitWeb. To facilitate more convenient usage, I have created user-friendly functions using TAWaitWeb as follows:

AWait_QuerySelector(oWeb, cQuery, nSecond)
AWait_QuerySelectAll(oWeb, cQuery, nSecond)
AWait_GetOuterHTML(oWeb, cSelector, nSecond)

6. The example scenario is as follows:
6-1.Navigate to google.com.
6-2.Use AWait_QuerySelector() to wait until the document's readyState becomes 'complete'.
6-3.Once complete, use AWait_GetOuterHTML() to display the outer HTML.


Reagrds,
Charles KWON

Code: Select all  Expand view  RUN

/*
 c)copyright 2023- charleskwonohjun@gmail.com ( Seoul, Korea )
*/


#include "FiveWin.ch"

STATIC oWebView  

FUNCTION Main()
    LOCAL oDlg

    DEFINE DIALOG oDlg TITLE "WebView Test" SIZE 1200, 700

         oDlg:bStart := { || GetOuterHtml( oDlg  ) }

    ACTIVATE DIALOG oDlg CENTER
     
RETURN NIL


STATIC FUNCTION GetOuterHtml( oDlg  )
   LOCAL cStr
   LOCAL nCounter := 0
   LOCAL lOk  := .f.

   oWebView := TWebView():New( , oDlg:hWnd )
   oWebView:Navigate( "https://www.google.com/" )

   Sleep(300)
   SysWait()
   
   WHILE .T.
     
      ++nCounter
      cStr := AWait_QuerySelector( oWebView, "document.readyState", 3000 )  

      IF cStr == "complete"
         lOk := .t.
         EXIT
      ENDIF      

      IF nCounter == 6
         EXIT
      ENDIF  

   ENDDO

   IF !lOk
      MsgStop("Timeout! Try again")
      RETURN NIL
   ENDIF
   
   ?AWait_GetOuterHTML( oWebView, "html", 3000 )

RETURN NIL  

 


Code: Select all  Expand view  RUN


/*

TAWaitWeb.prg

This is a class for Fivewin TWebView that retrieves data from the WebView in Sync mode.
c)copyright 2023- charleskwonohjun@gmail.com ( Seoul, Korea )

*/


#include "fivewin.ch"

FUNCTION AWait_QuerySelector( oWeb, cQuery, nSecond )
    LOCAL oFunc
    LOCAL cReturn := ""

    oFunc := TAWaitWeb():New( oWeb )    
    oFunc:Query( cQuery, nSecond )

    cReturn := oFunc:cReturn    

RETURN cReturn

FUNCTION AWait_QuerySelectAll( oWeb, cQuery, nSecond )
    LOCAL oFunc
    LOCAL cReturn := ""

    oFunc := TAWaitWeb():New( oWeb )    
    oFunc:QuerySelectAll( cQuery, nSecond )

    cReturn := oFunc:cReturn    

RETURN cReturn


FUNCTION AWait_GetOuterHTML( oWeb, cSelector, nSecond )
    LOCAL oFunc
    LOCAL cReturn := ""

    oFunc := TAWaitWeb():New( oWeb )
    oFunc:getOuterHTML( cSelector, nSecond )

    cReturn := oFunc:cReturn    

RETURN cReturn


CLASS TAWaitWeb

    DATA bOldBind
    DATA oWeb
    DATA cQuery
    DATA nSecond

    DATA cReturn

    METHOD New( oWeb, cQuery, nSecond ) CONSTRUCTOR
    METHOD GetBind( cJson, cCalls, ... )
    METHOD Query( cQuery, nSecond )
    METHOD QuerySelectAll( cQuery, nSecond )
   
    METHOD GetOuterHtml( cSelecter, nSecond )
   

ENDCLASS    

METHOD New( oWeb ) CLASS TAWaitWeb

    ::oWeb := oWeb
    ::oWeb:Bind( "SendToFWH" )  
     
RETURN Self

METHOD Query( cQuery, nSecond, cReturn ) CLASS TAWaitWeb
    LOCAL nCounter := 0
    LOCAL lReturn  := .f.
    LOCAL cEval    := "SendToFWH($QUERY$, 'WebFunc')"
    LOCAL nStartSec := GetTickCount()

    DEFAULT nSecond := 500
   
    ::bOldBind := ::oWeb:bOnBind
    ::oWeb:bOnBind := { | cJson, cCalls, ... | ::GetBind( cJson, cCalls, ... ) }

    cEval := STRTRAN( cEval, "$QUERY$", cQuery )
   
    ::cReturn := ""

    WHILE .T.
        ::oWeb:Eval( cEval )

        sleep(100)
        SysWait()
       
        IF !EMPTY( ::cReturn )
            lReturn := .t.
            EXIT
        ENDIF    

        IF GetTickCount() - nStartSec  >= nSecond
           EXIT
        ENDIF
    ENDDO  

    cReturn := ::cReturn

    ::oWeb:bOnBind := ::bOldBind

RETURN lReturn

METHOD QuerySelectAll( cQuery, nSecond, cReturn ) CLASS TAWaitWeb
    LOCAL nCounter := 0
    LOCAL lReturn  := .f.
    LOCAL cEval    := "SendToFWH(querySelectorAll($QUERY$), 'WebFunc')"
    LOCAL nStartSec := GetTickCount()

    DEFAULT nSecond := 500
   
    ::bOldBind := ::oWeb:bOnBind
    ::oWeb:bOnBind := { | cJson, cCalls, ... | ::GetBind( cJson, cCalls, ... ) }

    cEval := STRTRAN( cEval, "$QUERY$", cQuery )
   
    ::cReturn := ""

    WHILE .T.
        ::oWeb:Eval( cEval )

        sleep(100)
        SysWait()
       
        IF !EMPTY( ::cReturn )
           lReturn := .t.
           EXIT
        ENDIF    

        IF GetTickCount() - nStartSec  >= nSecond
           EXIT
        ENDIF    
    ENDDO  

    cReturn := ::cReturn

    ::oWeb:bOnBind := ::bOldBind
   
RETURN lReturn


METHOD GetOuterHtml( cSelecter, nSecond ) CLASS TAWaitWeb
    LOCAL cQuery := "SendToFWH( document.querySelector('$SELECTER$').outerHTML, 'savehtml')"
    LOCAL cEval
    LOCAL nStartSec := GetTickCount()
    LOCAL lReturn  := .f.

    DEFAULT nSecond := 500

    ::bOldBind := ::oWeb:bOnBind
    ::oWeb:bOnBind := { | cJson, cCalls, ... | ::GetBind( cJson, cCalls, ... ) }

    cEval := STRTRAN( cQuery , "$SELECTER$", cSelecter )
   
    ::cReturn := ""

    WHILE .T.
        ::oWeb:Eval( cEval )

        sleep(100)
        SysWait()        

        IF !EMPTY( ::cReturn )
            lReturn := .t.
            EXIT
        ENDIF    

        IF GetTickCount() - nStartSec  >= nSecond
           EXIT
        ENDIF  
    ENDDO      
       
    ::oWeb:bOnBind := ::bOldBind
   
RETURN lReturn


METHOD GetBind( cJson, cCalls, ... ) CLASS TAWaitWeb
    LOCAL hResponse := hash()
   
    hb_jsondecode(cJson ,@hResponse )
   
    ::cReturn := ""

    IF LEN( hResponse ) >= 1
       ::cReturn := hResponse[1]    
    ENDIF  

RETURN NIL
 
User avatar
CharlesKwon
 
Posts: 28
Joined: Sun Nov 02, 2014 7:03 am

Re: Saving OuterHTML using Await Implementation with TWebView

Postby cmsoft » Mon Aug 14, 2023 2:45 pm

Excelente aporte!! Muchas gracias!
User avatar
cmsoft
 
Posts: 1291
Joined: Wed Nov 16, 2005 9:14 pm
Location: Mercedes - Bs As. Argentina

Re: Saving OuterHTML using Await Implementation with TWebView

Postby leandro » Mon Aug 14, 2023 6:29 pm

Cesar buenas tardes, como estas?

Nos podrías explicar por que es un buen aporte :oops: , disculpa mi ignorancia. Es que no entiendo como podemos usar esta clase?

Es algo similar al jquery, que pena por la comparación, pero es algo así?

De antemano gracias
Saludos
LEANDRO AREVALO
Bogotá (Colombia)
https://hymlyma.com
https://hymplus.com/
leandroalfonso111@gmail.com
leandroalfonso111@hotmail.com

[ Embarcadero C++ 7.60 for Win32 ] [ FiveWin 23.07 ] [ xHarbour 1.3.0 Intl. (SimpLex) (Build 20230914) ]
User avatar
leandro
 
Posts: 1676
Joined: Wed Oct 26, 2005 2:49 pm
Location: Colombia

Re: Saving OuterHTML using Await Implementation with TWebView

Postby cmsoft » Mon Aug 14, 2023 8:50 pm

Leandro, no soy un experto ni mucho menos, pero entiendo que esto nos permite obtener el codigo armado despues de una llamada asincronica dentro de la pagina.
User avatar
cmsoft
 
Posts: 1291
Joined: Wed Nov 16, 2005 9:14 pm
Location: Mercedes - Bs As. Argentina

Re: Saving OuterHTML using Await Implementation with TWebView

Postby leandro » Mon Aug 14, 2023 11:53 pm

Lo que significa que le podemos agregar información adicional, como por ejemplo una tabla?
Saludos
LEANDRO AREVALO
Bogotá (Colombia)
https://hymlyma.com
https://hymplus.com/
leandroalfonso111@gmail.com
leandroalfonso111@hotmail.com

[ Embarcadero C++ 7.60 for Win32 ] [ FiveWin 23.07 ] [ xHarbour 1.3.0 Intl. (SimpLex) (Build 20230914) ]
User avatar
leandro
 
Posts: 1676
Joined: Wed Oct 26, 2005 2:49 pm
Location: Colombia

Re: Saving OuterHTML using Await Implementation with TWebView

Postby CharlesKwon » Tue Aug 15, 2023 1:49 am

Leandro, Parece que quieres acceder a elementos del DOM para modificar sus valores. Es posible lograr esto utilizando el código que proporcioné anteriormente.

leandro wrote:Lo que significa que le podemos agregar información adicional, como por ejemplo una tabla?
User avatar
CharlesKwon
 
Posts: 28
Joined: Sun Nov 02, 2014 7:03 am

Re: Saving OuterHTML using Await Implementation with TWebView

Postby Antonio Linares » Tue Aug 15, 2023 5:42 pm

regards, saludos

Antonio Linares
www.fivetechsoft.com
User avatar
Antonio Linares
Site Admin
 
Posts: 42118
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain

Re: Saving OuterHTML using Await Implementation with TWebView

Postby leandro » Tue Aug 15, 2023 10:31 pm

Genial era lo que buscábamos... :D :D :D

Voy a agregar la clase e intentar entender su funcionamiento, cualquier cosa comento.

Un pequeño avance utilizando webview

https://forums.fivetechsupport.com/viewtopic.php?f=6&t=43606&sid=9f4f68bc0d697e7760e7e4d03300354b
Saludos
LEANDRO AREVALO
Bogotá (Colombia)
https://hymlyma.com
https://hymplus.com/
leandroalfonso111@gmail.com
leandroalfonso111@hotmail.com

[ Embarcadero C++ 7.60 for Win32 ] [ FiveWin 23.07 ] [ xHarbour 1.3.0 Intl. (SimpLex) (Build 20230914) ]
User avatar
leandro
 
Posts: 1676
Joined: Wed Oct 26, 2005 2:49 pm
Location: Colombia

Re: Saving OuterHTML using Await Implementation with TWebView

Postby CharlesKwon » Wed Aug 16, 2023 6:42 am

Reading the article I recently posted will help you automate the DOM more easily.
The address is as follows.
http://www.charleskwon.com/?p=935

Regards,
Charles KWON


leandro wrote:Genial era lo que buscábamos... :D :D :D

Voy a agregar la clase e intentar entender su funcionamiento, cualquier cosa comento.

Un pequeño avance utilizando webview

https://forums.fivetechsupport.com/viewtopic.php?f=6&t=43606&sid=edb9b3ec1bcdc7893989e4eb4c6319f4
User avatar
CharlesKwon
 
Posts: 28
Joined: Sun Nov 02, 2014 7:03 am

Re: Saving OuterHTML using Await Implementation with TWebView

Postby leandro » Wed Aug 16, 2023 9:41 pm

Hola buenas tardes...

Agregue la clase a nuestro proyecto en FIVEDIT, sin hacer el llamado de la clase dentro la aplicación ni ninguna de sus funciones. Pero al intentar compilar sale el siguiente error:

Code: Select all  Expand view  RUN

xHarbour 1.2.3 Intl. (SimpLex) (Build 20221118)
Copyright 1999-2022, http://www.xharbour.org http://www.harbour-project.org/
Compiling 'z:\prg\R32_menu.prg'...

100

200

100

100

100

100

100

200

300

400

500

600

700

800

900

1000

1100

100

200

100

200

300

100

200

100

200

300

400

500

600

700

800

900

1000

1100

1200

1300

1400

1500

1600

1700

1800

1900

2000

2100

2200

100

200

300

400

500

100

200

100

200

300

400

500

600

700

800

900

1000

1100

1200

1300

1400

1500

1600

1700

1800

1900

2000

2100

2200

2300

2400

2500

2600

2700

2800

2900
Generating C source output to 'C:\Fivedit\R32_menu.c'...
Done.

Lines 2907, Functions/Procedures 34, pCodes 13468
Embarcadero C++ 7.40 for Win32 Copyright (c) 1993-2018 Embarcadero Technologies, Inc.
C:\Fivedit䑌剐랰宄୉
Turbo Incremental Link 6.90 Copyright (c) 1997-2017 Embarcadero Technologies, Inc.
Fatal: Unable to open file 'TAWAITWEB.OBJ'
Link Error

 


No logro ver cual es el detalle del error :(
Saludos
LEANDRO AREVALO
Bogotá (Colombia)
https://hymlyma.com
https://hymplus.com/
leandroalfonso111@gmail.com
leandroalfonso111@hotmail.com

[ Embarcadero C++ 7.60 for Win32 ] [ FiveWin 23.07 ] [ xHarbour 1.3.0 Intl. (SimpLex) (Build 20230914) ]
User avatar
leandro
 
Posts: 1676
Joined: Wed Oct 26, 2005 2:49 pm
Location: Colombia

Re: Saving OuterHTML using Await Implementation with TWebView

Postby cnavarro » Wed Aug 16, 2023 10:55 pm

Leandro, comprueba que con el copy/paste no te "has llevado" algún carácter raro
Cristobal Navarro
Hay dos tipos de personas: las que te hacen perder el tiempo y las que te hacen perder la noción del tiempo
El secreto de la felicidad no está en hacer lo que te gusta, sino en que te guste lo que haces
User avatar
cnavarro
 
Posts: 6549
Joined: Wed Feb 15, 2012 8:25 pm
Location: España

Re: Saving OuterHTML using Await Implementation with TWebView

Postby CharlesKwon » Thu Aug 17, 2023 1:10 am

Cristobal Navarro, tienes razón.
TAWaitWeb es una clase que he creado y hecho pública. Puedes visitar la URL de abajo, copiar el código de TAWaitWeb y usarlo según lo necesites.

http://www.charleskwon.com/?p=935

Regards,
Charles KWON



cnavarro wrote:Leandro, comprueba que con el copy/paste no te "has llevado" algún carácter raro
User avatar
CharlesKwon
 
Posts: 28
Joined: Sun Nov 02, 2014 7:03 am

Re: Saving OuterHTML using Await Implementation with TWebView

Postby Otto » Thu Aug 17, 2023 8:21 am

Dear Charles,

Thank you so much for your work.
Best regards,
Otto



I asked ChatGPT and Google Bard to explain me the program. I think this answer could be helpful for others, too.


Alright, I understand the program. This program's primary purpose is to create a synchronous-like experience for web scraping using the TWebView control of FiveWin, which is asynchronous by default.

I will break it down:

1. **Overview**:
- The program introduces a class `TAWaitWeb` that lets developers make synchronous calls using the TWebView control, typically used in an asynchronous manner.
- This class allows waiting for specific elements to be loaded on the web page, ensuring the content is available for extraction.
- The idea is to simulate the 'await' functionality seen in other programming languages.

2. **Main Function**:
- A Dialog (`oDlg`) is created titled "WebView Test".
- The `GetOuterHtml` function is set to execute when the dialog starts (`oDlg:bStart`).
- The Dialog is activated in the center.

3. **GetOuterHtml Function**:
- It initializes the TWebView control, which navigates to "https://www.google.com/".
- There's a loop (`WHILE .T.`) that waits for the document to be completely loaded. The function `AWait_QuerySelector` checks the `readyState` of the document.
- If the document isn't completely loaded in six attempts (or iterations), it will exit the loop and show an error "Timeout! Try again".
- If the document loads successfully, it displays the entire HTML of the page using the function `AWait_GetOuterHTML`.

4. **TAWaitWeb Class**:
- This class is the heart of the program. It acts as a synchronous wrapper over TWebView.
- Data members include:
- `bOldBind`: Presumably used to store previous bindings.
- `oWeb`: Instance of TWebView.
- `cQuery`, `nSecond`, `cReturn`: Parameters used for querying.
- Methods include:
- `New`: Constructor that binds the 'SendToFWH' function for callback purposes.
- `Query`: Runs a specific query on the web page.
- `QuerySelectAll`: Selects all matching elements on the web page.
- `GetOuterHtml`: Retrieves the outerHTML of the specified selector.
- `GetBind`: Callback function that gets called when `SendToFWH` is invoked from the web page.

5. **Synchronous-like Behavior**:
- Functions like `Query`, `QuerySelectAll`, and `GetOuterHtml` utilize a loop that repeatedly checks if a certain condition is met (e.g., a specific element is loaded).
- This loop will exit either when the desired result is achieved or after a certain time has passed (`nSecond`), whichever happens first.
- This mechanism ensures that the program waits for the web content to be available but doesn't get stuck indefinitely if the content isn't found.

6. **Asynchronous to Synchronous Transition**:
- The asynchronous nature of web operations is tackled using the `::oWeb:Eval` method, which essentially runs JavaScript within the TWebView instance.
- By binding a custom function `SendToFWH` and using `::oWeb:bOnBind` to capture results, the program achieves synchronous-like behavior by waiting for specific results before proceeding.

In summary, this program provides a way for developers familiar with synchronous programming to interact with the inherently asynchronous TWebView control by emulating synchronous behavior. This can be particularly useful in scenarios where one needs to ensure a webpage is fully loaded or a specific element is present before taking further action.




Sure, I can help you understand this program.

The program you have provided is a FiveWin application that uses a custom class called TAWaitWeb to retrieve data from a web page in a synchronous manner.

The TAWaitWeb class implements three functions:

AWait_QuerySelector()
AWait_QuerySelectAll()
AWait_GetOuterHTML()
These functions all take a web page URL and a timeout value as input, and they return the requested data if it is available within the timeout period. If the data is not available within the timeout period, the function will return an empty string.

The main function of the program creates a dialog box and then calls the GetOuterHtml() function to retrieve the outer HTML of the Google homepage. The GetOuterHtml() function first calls the AWait_QuerySelector() function to wait until the document's readyState becomes 'complete'. Once the document is ready, the GetOuterHtml() function calls the AWait_GetOuterHTML() function to retrieve the outer HTML.

The program uses the FiveWin TWebView class to interact with the web page. The TWebView class provides a number of methods for loading web pages, executing JavaScript, and retrieving data from web pages.

The program also uses the FiveWin SysWait() function to pause the execution of the program for a specified number of milliseconds. This is used to prevent the program from consuming too much CPU resources while it is waiting for the web page to load.

I hope this explanation is helpful. Let me know if you have any other questions.

Here are some additional details about the program:

The program is written in the FiveWin programming language. FiveWin is a rapid application development (RAD) tool for Windows.
The program uses the TAWaitWeb class to retrieve data from a web page in a synchronous manner. This means that the program will not continue execution until the data has been retrieved.
The program uses the FiveWin TWebView class to interact with the web page. The TWebView class provides a number of methods for loading web pages, executing JavaScript, and retrieving data from web pages.
The program uses the FiveWin SysWait() function to pause the execution of the program for a specified number of milliseconds. This is used to prevent the program from consuming too much CPU resources while it is waiting for the web page to load.



The _QuerySelector(), QuerySelectAll(), and GetOuterHTML() functions are all methods of the TAWaitWeb class.

The _QuerySelector() function takes a web page URL and a CSS selector as input, and it returns the first element on the page that matches the selector.
The QuerySelectAll() function takes a web page URL and a CSS selector as input, and it returns all elements on the page that match the selector.
The GetOuterHTML() function takes a web page URL and an element selector as input, and it returns the outer HTML of the element that matches the selector.
These functions all use the FiveWin TWebView class to interact with the web page. The TWebView class provides a number of methods for loading web pages, executing JavaScript, and retrieving data from web pages.

The _QuerySelector() function works by first loading the web page in the TWebView control. Then, it executes a JavaScript function that uses the document.querySelector() method to get the first element on the page that matches the CSS selector. The function then returns the value of the element.

The QuerySelectAll() function works in a similar way, but it uses the document.querySelectorAll() method to get all elements on the page that match the CSS selector.

The GetOuterHTML() function works by first getting the element that matches the selector using the _QuerySelector() or QuerySelectAll() functions. Then, it gets the outer HTML of the element using the element.outerHTML property.
********************************************************************
mod harbour - Vamos a la conquista de la Web
modharbour.org
https://www.facebook.com/groups/modharbour.club
********************************************************************
User avatar
Otto
 
Posts: 6336
Joined: Fri Oct 07, 2005 7:07 pm

Re: Saving OuterHTML using Await Implementation with TWebView

Postby CharlesKwon » Thu Aug 17, 2023 9:51 am

Hello Mr. Otto, thank you for the great idea. I will add the newly implemented functions and methods, and post the manual that comes out from consulting with ChatGPT on charleskwon.com.

Regards,
Charles KWON

Otto wrote:Dear Charles,

Thank you so much for your work.
Best regards,
Otto



I asked ChatGPT and Google Bard to explain me the program. I think this answer could be helpful for others, too.


Alright, I understand the program. This program's primary purpose is to create a synchronous-like experience for web scraping using the TWebView control of FiveWin, which is asynchronous by default.

I will break it down:

1. **Overview**:
- The program introduces a class `TAWaitWeb` that lets developers make synchronous calls using the TWebView control, typically used in an asynchronous manner.
- This class allows waiting for specific elements to be loaded on the web page, ensuring the content is available for extraction.
- The idea is to simulate the 'await' functionality seen in other programming languages.

2. **Main Function**:
- A Dialog (`oDlg`) is created titled "WebView Test".
- The `GetOuterHtml` function is set to execute when the dialog starts (`oDlg:bStart`).
- The Dialog is activated in the center.

3. **GetOuterHtml Function**:
- It initializes the TWebView control, which navigates to "https://www.google.com/".
- There's a loop (`WHILE .T.`) that waits for the document to be completely loaded. The function `AWait_QuerySelector` checks the `readyState` of the document.
- If the document isn't completely loaded in six attempts (or iterations), it will exit the loop and show an error "Timeout! Try again".
- If the document loads successfully, it displays the entire HTML of the page using the function `AWait_GetOuterHTML`.

4. **TAWaitWeb Class**:
- This class is the heart of the program. It acts as a synchronous wrapper over TWebView.
- Data members include:
- `bOldBind`: Presumably used to store previous bindings.
- `oWeb`: Instance of TWebView.
- `cQuery`, `nSecond`, `cReturn`: Parameters used for querying.
- Methods include:
- `New`: Constructor that binds the 'SendToFWH' function for callback purposes.
- `Query`: Runs a specific query on the web page.
- `QuerySelectAll`: Selects all matching elements on the web page.
- `GetOuterHtml`: Retrieves the outerHTML of the specified selector.
- `GetBind`: Callback function that gets called when `SendToFWH` is invoked from the web page.

5. **Synchronous-like Behavior**:
- Functions like `Query`, `QuerySelectAll`, and `GetOuterHtml` utilize a loop that repeatedly checks if a certain condition is met (e.g., a specific element is loaded).
- This loop will exit either when the desired result is achieved or after a certain time has passed (`nSecond`), whichever happens first.
- This mechanism ensures that the program waits for the web content to be available but doesn't get stuck indefinitely if the content isn't found.

6. **Asynchronous to Synchronous Transition**:
- The asynchronous nature of web operations is tackled using the `::oWeb:Eval` method, which essentially runs JavaScript within the TWebView instance.
- By binding a custom function `SendToFWH` and using `::oWeb:bOnBind` to capture results, the program achieves synchronous-like behavior by waiting for specific results before proceeding.

In summary, this program provides a way for developers familiar with synchronous programming to interact with the inherently asynchronous TWebView control by emulating synchronous behavior. This can be particularly useful in scenarios where one needs to ensure a webpage is fully loaded or a specific element is present before taking further action.




Sure, I can help you understand this program.

The program you have provided is a FiveWin application that uses a custom class called TAWaitWeb to retrieve data from a web page in a synchronous manner.

The TAWaitWeb class implements three functions:

AWait_QuerySelector()
AWait_QuerySelectAll()
AWait_GetOuterHTML()
These functions all take a web page URL and a timeout value as input, and they return the requested data if it is available within the timeout period. If the data is not available within the timeout period, the function will return an empty string.

The main function of the program creates a dialog box and then calls the GetOuterHtml() function to retrieve the outer HTML of the Google homepage. The GetOuterHtml() function first calls the AWait_QuerySelector() function to wait until the document's readyState becomes 'complete'. Once the document is ready, the GetOuterHtml() function calls the AWait_GetOuterHTML() function to retrieve the outer HTML.

The program uses the FiveWin TWebView class to interact with the web page. The TWebView class provides a number of methods for loading web pages, executing JavaScript, and retrieving data from web pages.

The program also uses the FiveWin SysWait() function to pause the execution of the program for a specified number of milliseconds. This is used to prevent the program from consuming too much CPU resources while it is waiting for the web page to load.

I hope this explanation is helpful. Let me know if you have any other questions.

Here are some additional details about the program:

The program is written in the FiveWin programming language. FiveWin is a rapid application development (RAD) tool for Windows.
The program uses the TAWaitWeb class to retrieve data from a web page in a synchronous manner. This means that the program will not continue execution until the data has been retrieved.
The program uses the FiveWin TWebView class to interact with the web page. The TWebView class provides a number of methods for loading web pages, executing JavaScript, and retrieving data from web pages.
The program uses the FiveWin SysWait() function to pause the execution of the program for a specified number of milliseconds. This is used to prevent the program from consuming too much CPU resources while it is waiting for the web page to load.



The _QuerySelector(), QuerySelectAll(), and GetOuterHTML() functions are all methods of the TAWaitWeb class.

The _QuerySelector() function takes a web page URL and a CSS selector as input, and it returns the first element on the page that matches the selector.
The QuerySelectAll() function takes a web page URL and a CSS selector as input, and it returns all elements on the page that match the selector.
The GetOuterHTML() function takes a web page URL and an element selector as input, and it returns the outer HTML of the element that matches the selector.
These functions all use the FiveWin TWebView class to interact with the web page. The TWebView class provides a number of methods for loading web pages, executing JavaScript, and retrieving data from web pages.

The _QuerySelector() function works by first loading the web page in the TWebView control. Then, it executes a JavaScript function that uses the document.querySelector() method to get the first element on the page that matches the CSS selector. The function then returns the value of the element.

The QuerySelectAll() function works in a similar way, but it uses the document.querySelectorAll() method to get all elements on the page that match the CSS selector.

The GetOuterHTML() function works by first getting the element that matches the selector using the _QuerySelector() or QuerySelectAll() functions. Then, it gets the outer HTML of the element using the element.outerHTML property.
User avatar
CharlesKwon
 
Posts: 28
Joined: Sun Nov 02, 2014 7:03 am

Re: Saving OuterHTML using Await Implementation with TWebView

Postby Otto » Thu Aug 17, 2023 10:22 am

Charles, thank you so much.
Best regards,
Otto
********************************************************************
mod harbour - Vamos a la conquista de la Web
modharbour.org
https://www.facebook.com/groups/modharbour.club
********************************************************************
User avatar
Otto
 
Posts: 6336
Joined: Fri Oct 07, 2005 7:07 pm

Next

Return to FiveWin for Harbour/xHarbour

Who is online

Users browsing this forum: Silvio.Falconi and 55 guests