|
| 1 | +import cbws from './websocket'; |
| 2 | + |
| 3 | +/** |
| 4 | + * A module for interacting with a browser through WebSockets. |
| 5 | + */ |
| 6 | +const cbbrowser = { |
| 7 | + |
| 8 | + /** |
| 9 | + * Opens a new page in the browser. |
| 10 | + */ |
| 11 | + newPage: () => { |
| 12 | + cbws.getWebsocket.send(JSON.stringify({ |
| 13 | + "type": "browserEvent", |
| 14 | + action: 'newPage' |
| 15 | + })); |
| 16 | + }, |
| 17 | + |
| 18 | + /** |
| 19 | + * Retrieves the current URL of the browser's active page. |
| 20 | + * @returns {Promise<any>} A promise that resolves with the URL. |
| 21 | + */ |
| 22 | + getUrl: () => { |
| 23 | + return new Promise((resolve, reject) => { |
| 24 | + cbws.getWebsocket.send(JSON.stringify({ |
| 25 | + "type": "browserEvent", |
| 26 | + action: 'getUrl' |
| 27 | + })); |
| 28 | + cbws.getWebsocket.on('message', (data: string) => { |
| 29 | + const response = JSON.parse(data); |
| 30 | + if (response.event === "getUrlResponse") { |
| 31 | + resolve(response); |
| 32 | + } |
| 33 | + }); |
| 34 | + }); |
| 35 | + }, |
| 36 | + |
| 37 | + /** |
| 38 | + * Navigates to a specified URL. |
| 39 | + * @param {string} url - The URL to navigate to. |
| 40 | + * @returns {Promise<any>} A promise that resolves when navigation is complete. |
| 41 | + */ |
| 42 | + goToPage: (url: string) => { |
| 43 | + return new Promise((resolve, reject) => { |
| 44 | + cbws.getWebsocket.send(JSON.stringify({ |
| 45 | + "type": "browserEvent", |
| 46 | + action: 'goToPage', |
| 47 | + url |
| 48 | + })); |
| 49 | + cbws.getWebsocket.on('message', (data: string) => { |
| 50 | + const response = JSON.parse(data); |
| 51 | + if (response.event === "goToPageResponse") { |
| 52 | + resolve(response); |
| 53 | + } |
| 54 | + }); |
| 55 | + }); |
| 56 | + }, |
| 57 | + |
| 58 | + /** |
| 59 | + * Takes a screenshot of the current page. |
| 60 | + */ |
| 61 | + screenshot: () => { |
| 62 | + cbws.getWebsocket.send(JSON.stringify({ |
| 63 | + "type": "browserEvent", |
| 64 | + action: 'screenshot' |
| 65 | + })); |
| 66 | + }, |
| 67 | + |
| 68 | + /** |
| 69 | + * Retrieves the HTML content of the current page. |
| 70 | + * @returns {Promise<string>} A promise that resolves with the HTML content. |
| 71 | + */ |
| 72 | + getHTML: () => { |
| 73 | + return new Promise((resolve, reject) => { |
| 74 | + cbws.getWebsocket.send(JSON.stringify({ |
| 75 | + "type": "browserEvent", |
| 76 | + action: 'getHTML' |
| 77 | + })); |
| 78 | + cbws.getWebsocket.on('message', (data: string) => { |
| 79 | + const response = JSON.parse(data); |
| 80 | + if (response.event === "htmlReceived") { |
| 81 | + resolve(response.htmlResponse); |
| 82 | + } |
| 83 | + }); |
| 84 | + }); |
| 85 | + }, |
| 86 | + |
| 87 | + /** |
| 88 | + * Retrieves the Markdown content of the current page. |
| 89 | + * @returns {Promise<any>} A promise that resolves with the Markdown content. |
| 90 | + */ |
| 91 | + getMarkdown: () => { |
| 92 | + return new Promise((resolve, reject) => { |
| 93 | + cbws.getWebsocket.send(JSON.stringify({ |
| 94 | + "type": "browserEvent", |
| 95 | + action: 'getMarkdown' |
| 96 | + })); |
| 97 | + cbws.getWebsocket.on('message', (data: string) => { |
| 98 | + const response = JSON.parse(data); |
| 99 | + if (response.event === "getMarkdownResponse") { |
| 100 | + resolve(response); |
| 101 | + } |
| 102 | + }); |
| 103 | + }); |
| 104 | + }, |
| 105 | + |
| 106 | + /** |
| 107 | + * Retrieves the PDF content of the current page. |
| 108 | + */ |
| 109 | + getPDF: () => { |
| 110 | + cbws.getWebsocket.send(JSON.stringify({ |
| 111 | + "type": "browserEvent", |
| 112 | + action: 'getPDF' |
| 113 | + })); |
| 114 | + }, |
| 115 | + |
| 116 | + /** |
| 117 | + * Converts the PDF content of the current page to text. |
| 118 | + */ |
| 119 | + pdfToText: () => { |
| 120 | + cbws.getWebsocket.send(JSON.stringify({ |
| 121 | + "type": "browserEvent", |
| 122 | + action: 'pdfToText' |
| 123 | + })); |
| 124 | + }, |
| 125 | + |
| 126 | + /** |
| 127 | + * Retrieves the content of the current page. |
| 128 | + */ |
| 129 | + getContent: () => { |
| 130 | + cbws.getWebsocket.send(JSON.stringify({ |
| 131 | + "type": "browserEvent", |
| 132 | + action: 'getContent' |
| 133 | + })); |
| 134 | + }, |
| 135 | + |
| 136 | + /** |
| 137 | + * Extracts text from the current page. |
| 138 | + */ |
| 139 | + extractText: () => { |
| 140 | + cbws.getWebsocket.send(JSON.stringify({ |
| 141 | + "type": "browserEvent", |
| 142 | + action: 'extractText' |
| 143 | + })); |
| 144 | + }, |
| 145 | + |
| 146 | + /** |
| 147 | + * Closes the current page. |
| 148 | + */ |
| 149 | + close: () => { |
| 150 | + cbws.getWebsocket.send(JSON.stringify({ |
| 151 | + "type": "browserEvent", |
| 152 | + action: 'close' |
| 153 | + })); |
| 154 | + }, |
| 155 | + |
| 156 | + /** |
| 157 | + * Scrolls the current page in a specified direction by a specified number of pixels. |
| 158 | + * @param {string} direction - The direction to scroll. |
| 159 | + * @param {string} pixels - The number of pixels to scroll. |
| 160 | + * @returns {Promise<any>} A promise that resolves when the scroll action is complete. |
| 161 | + */ |
| 162 | + scroll: (direction: string, pixels: string) => { |
| 163 | + return new Promise((resolve, reject) => { |
| 164 | + cbws.getWebsocket.send(JSON.stringify({ |
| 165 | + "type": "browserEvent", |
| 166 | + action: 'scroll', |
| 167 | + direction, |
| 168 | + pixels |
| 169 | + })); |
| 170 | + cbws.getWebsocket.on('message', (data: string) => { |
| 171 | + const response = JSON.parse(data); |
| 172 | + if (response.event === "scrollResponse") { |
| 173 | + resolve(response); |
| 174 | + } |
| 175 | + }); |
| 176 | + }); |
| 177 | + }, |
| 178 | + |
| 179 | + /** |
| 180 | + * Types text into a specified element on the page. |
| 181 | + * @param {string} elementid - The ID of the element to type into. |
| 182 | + * @param {string} text - The text to type. |
| 183 | + * @returns {Promise<any>} A promise that resolves when the typing action is complete. |
| 184 | + */ |
| 185 | + type: (elementid: string, text: string) => { |
| 186 | + return new Promise((resolve, reject) => { |
| 187 | + cbws.getWebsocket.send(JSON.stringify({ |
| 188 | + "type": "browserEvent", |
| 189 | + action: 'type', |
| 190 | + text, |
| 191 | + elementid |
| 192 | + })); |
| 193 | + cbws.getWebsocket.on('message', (data: string) => { |
| 194 | + const response = JSON.parse(data); |
| 195 | + if (response.event === "typeResponse") { |
| 196 | + resolve(response); |
| 197 | + } |
| 198 | + }); |
| 199 | + }); |
| 200 | + }, |
| 201 | + |
| 202 | + /** |
| 203 | + * Clicks on a specified element on the page. |
| 204 | + * @param {string} elementid - The ID of the element to click. |
| 205 | + * @returns {Promise<any>} A promise that resolves when the click action is complete. |
| 206 | + */ |
| 207 | + click: (elementid: string) => { |
| 208 | + return new Promise((resolve, reject) => { |
| 209 | + cbws.getWebsocket.send(JSON.stringify({ |
| 210 | + "type": "browserEvent", |
| 211 | + action: 'click', |
| 212 | + elementid |
| 213 | + })); |
| 214 | + cbws.getWebsocket.on('message', (data: string) => { |
| 215 | + const response = JSON.parse(data); |
| 216 | + if (response.event === "clickResponse") { |
| 217 | + resolve(response); |
| 218 | + } |
| 219 | + }); |
| 220 | + }); |
| 221 | + }, |
| 222 | + |
| 223 | + /** |
| 224 | + * Simulates the Enter key press on the current page. |
| 225 | + * @returns {Promise<any>} A promise that resolves when the Enter action is complete. |
| 226 | + */ |
| 227 | + enter: () => { |
| 228 | + return new Promise((resolve, reject) => { |
| 229 | + cbws.getWebsocket.send(JSON.stringify({ |
| 230 | + "type": "browserEvent", |
| 231 | + action: 'enter' |
| 232 | + })); |
| 233 | + cbws.getWebsocket.on('message', (data: string) => { |
| 234 | + const response = JSON.parse(data); |
| 235 | + if (response.event === "EnterResponse") { |
| 236 | + resolve(response); |
| 237 | + } |
| 238 | + }); |
| 239 | + }); |
| 240 | + }, |
| 241 | + |
| 242 | + /** |
| 243 | + * Performs a search on the current page using a specified query. |
| 244 | + * @param {string} elementid - The ID of the element to perform the search in. |
| 245 | + * @param {string} query - The search query. |
| 246 | + * @returns {Promise<any>} A promise that resolves with the search results. |
| 247 | + */ |
| 248 | + search: (elementid: string, query: string) => { |
| 249 | + return new Promise((resolve, reject) => { |
| 250 | + cbws.getWebsocket.send(JSON.stringify({ |
| 251 | + "type": "browserEvent", |
| 252 | + action: 'search', |
| 253 | + elementid, |
| 254 | + query |
| 255 | + })); |
| 256 | + cbws.getWebsocket.on('message', (data: string) => { |
| 257 | + const response = JSON.parse(data); |
| 258 | + if (response.event === "searchResponse") { |
| 259 | + resolve(response); |
| 260 | + } |
| 261 | + }); |
| 262 | + }); |
| 263 | + } |
| 264 | +} |
| 265 | + |
| 266 | +export default cbbrowser; |
0 commit comments