11import cbws from './websocket' ;
2- import { GoToPageResponse , UrlResponse , GetMarkdownResponse , HtmlReceived } from '@codebolt/types'
2+ import { GoToPageResponse , UrlResponse , GetMarkdownResponse , HtmlReceived , ExtractTextResponse , GetContentResponse } from '@codebolt/types'
33/**
44 * A module for interacting with a browser through WebSockets.
55 */
@@ -105,6 +105,7 @@ const cbbrowser = {
105105
106106 /**
107107 * Retrieves the PDF content of the current page.
108+ *
108109 */
109110 getPDF : ( ) => {
110111 cbws . getWebsocket . send ( JSON . stringify ( {
@@ -125,22 +126,43 @@ const cbbrowser = {
125126
126127 /**
127128 * Retrieves the content of the current page.
129+ * @returns {Promise<GetContentResponse> } A promise that resolves with the content.
128130 */
129- getContent : ( ) => {
130- cbws . getWebsocket . send ( JSON . stringify ( {
131- "type" : "browserEvent" ,
132- action : 'getContent'
133- } ) ) ;
131+ getContent : ( ) :Promise < GetContentResponse > => {
132+
133+ return new Promise ( ( resolve , reject ) => {
134+ cbws . getWebsocket . send ( JSON . stringify ( {
135+ "type" : "browserEvent" ,
136+ action : 'getContent'
137+ } ) ) ;
138+ cbws . getWebsocket . on ( 'message' , ( data : string ) => {
139+ const response = JSON . parse ( data ) ;
140+ if ( response . event === "getContentResponse" ) {
141+ resolve ( response ) ;
142+ }
143+ } ) ;
144+ } ) ;
134145 } ,
135146
136147 /**
137148 * Extracts text from the current page.
149+ * @returns {Promise<ExtractTextResponse> } A promise that resolves with the extracted text.
150+ *
138151 */
139- extractText : ( ) => {
140- cbws . getWebsocket . send ( JSON . stringify ( {
141- "type" : "browserEvent" ,
142- action : 'extractText'
143- } ) ) ;
152+ extractText : ( ) :Promise < ExtractTextResponse > => {
153+
154+ return new Promise ( ( resolve , reject ) => {
155+ cbws . getWebsocket . send ( JSON . stringify ( {
156+ "type" : "browserEvent" ,
157+ action : 'extractText'
158+ } ) ) ;
159+ cbws . getWebsocket . on ( 'message' , ( data : string ) => {
160+ const response = JSON . parse ( data ) ;
161+ if ( response . event === "extractTextResponse" ) {
162+ resolve ( response ) ;
163+ }
164+ } ) ;
165+ } ) ;
144166 } ,
145167
146168 /**
0 commit comments