-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathindex.ts
120 lines (96 loc) · 3.35 KB
/
index.ts
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
import readline from "readline";
import { launchBrowser } from "./core/puppeteer";
const readlineInterface = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
/**
*
* @param question The question to ask
* @param timeout The time in milliseconds to wait for an answer. If no answer is given, it will reject the promise
* @returns User's input
*/
const input = (question: string, timeout?: number) => {
let timer: NodeJS.Timeout;
return new Promise<string>((resolve, reject) => {
readlineInterface.question(question, (answer) => {
clearTimeout(timer);
readlineInterface.close();
resolve(answer);
});
if (timeout) {
timer = setTimeout(() => {
reject(new Error("Question timeout"));
readlineInterface.close();
}, timeout);
}
});
};
/**
* Open ChatGPT and ask questions
* @param isChat If true, it will keep asking for questions. If false, it will only ask once. Default is `false`
* @returns The answer from ChatGPT
*/
const openChatGPT = async (isChat?: boolean) => {
console.log("Opening ChatGPT...");
const width = 480;
const height = 853;
const { page, browser } = await launchBrowser({
width,
height,
headless: true,
incognito: true,
});
page.setViewport({ width, height });
page.setUserAgent('Mozilla/5.0 (Windows NT 5.1; rv:5.0) Gecko/20100101 Firefox/5.0'); // Added to by pass the bot detection by cloudflare
await page.goto("https://chat.openai.com/", { waitUntil: "load" });
const textArea = "textarea#prompt-textarea";
try {
await page.waitForSelector(textArea);
} catch (error) {
await page.screenshot({ path: "error.png" });
await browser.close();
return Promise.reject(error);
}
console.log("ChatGPT is ready!");
let answer: string;
do {
let question: string;
try {
question = await input("Question: ", 60 * 1000); // If no input in 60 seconds, it will timeout and return an error
} catch (error) {
await browser.close();
return Promise.reject(error);
}
console.log("Processing...");
await page.type(textArea, question, {
delay: Math.random() * 50, // random delay between 0 and 50 ms
});
// check is the button is enabled by checking the attribute disabled
const btnSend = "button[data-testid='send-button']";
await page.waitForSelector(btnSend);
const isBtnDisabled = await page.$eval(btnSend, (el) =>
el.getAttribute("disabled")
);
if (!isBtnDisabled) await page.click(btnSend);
// check if the button is hidden. Meaning ChatGPT is still answering the question
await page.waitForSelector(btnSend, { hidden: true });
// check if the button is visible again. Meaning ChatGPT has answered the question
await page.waitForSelector(btnSend);
const messageEl = "div[data-message-author-role='assistant']";
await page.waitForSelector(messageEl);
// get the latest message from ChatGPT
answer = await page.$$eval(messageEl, (el) => {
const latest = el[el.length - 1];
return latest.innerText;
});
console.log("ChatGPT:", answer);
} while (isChat);
await new Promise(
(resolve) => setTimeout(resolve, Math.random() * 100 + 200) // random delay between 200 and 300 ms
);
await browser.close();
return answer;
};
// Add `true` as an argument to keep asking questions
openChatGPT();