-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpoor-auto-gpt.js
39 lines (29 loc) · 1.08 KB
/
poor-auto-gpt.js
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
30
31
32
33
34
35
36
37
38
const puppeteer = require('puppeteer');
const { convert } = require('html-to-text');
const args = process.argv.slice(2);
const query = args.join(' ');
const options = {
wordwrap: 130,
};
(async () => {
const browser = await puppeteer.connect({ browserURL: 'http://127.0.0.1:9999/json', defaultViewport: null });
const pages = await browser.pages();
const chatGPTUrl = 'https://chat.openai.com/';
let page = pages.filter(p => p.url() === chatGPTUrl)[0];
if (page) {
await page.type('textarea', query);
await page.keyboard.press('Enter');
await page.waitForFunction(() => {
const element = Array.from(document.querySelectorAll('div')).find(el => el.textContent.includes('Regenerate response'));
if (element) {
return true;
}
return false;
}, { timeout: 30000 });
const searchResults = await page.$$eval('div.markdown', elements => elements.map(el => el.innerHTML));
console.log(convert(searchResults[searchResults.length - 1], options));
} else {
console.log('ChatGPT tab not found.');
}
browser.disconnect();
})();