-
Notifications
You must be signed in to change notification settings - Fork 1
/
poor-gpt.js
69 lines (56 loc) · 1.77 KB
/
poor-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
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
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,
timeout: 0,
protocolTimeout: 120000,
});
const pages = await browser.pages();
const chatGPTUrl = "https://chat.openai.com/";
let page = pages.filter((p) => p.url().startsWith(chatGPTUrl))[0];
if (page) {
if (query != "Cg==" && query.length > 3) {
let queryDecode = Buffer.from(query, "base64").toString();
let messages = [];
if (queryDecode.indexOf("POOR_GPT_SEP") > -1) {
messages = queryDecode.split("POOR_GPT_SEP");
} else {
messages[0] = queryDecode;
}
for (let i = 0; i < messages.length; i++) {
const message = messages[i];
await page.type("textarea", message);
await page.keyboard.down("Shift");
await page.keyboard.press("Enter");
await page.keyboard.up("Shift");
}
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: 60000 }
);
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();
})();