-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathbrowseTask.js
More file actions
29 lines (22 loc) · 787 Bytes
/
browseTask.js
File metadata and controls
29 lines (22 loc) · 787 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
const puppeteer = require('puppeteer');
class BrowseTask {
constructor(url, headless = "new", ws = null) {
this.url = url;
this.headless = headless;
this.ws = ws;
}
async perform() {
const browser = await puppeteer.launch({ headless: this.headless });
const page = await browser.newPage();
await page.goto(this.url, { waitUntil: 'networkidle2' }); // waits until all network requests are finished
const content = await page.content();
await browser.close();
console.log('Content:', content);
// If WebSocket connection is available, send the content to the client
if (this.ws && this.ws.readyState === this.ws.OPEN) {
this.ws.send(`Content: ${content}`);
}
return content;
}
}
module.exports = BrowseTask;