-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.mjs
64 lines (40 loc) · 1.38 KB
/
index.mjs
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
import { chromium } from "@playwright/test";
const headers = {
"user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36"
};
const chatgptToken = async (username, password) => {
const browser = await chromium.launch();
const context = await browser.newContext();
const page = await context.newPage();
await page.setExtraHTTPHeaders(headers)
await page.goto("https://chat.openai.com/auth/login")
await page.waitForSelector('button:nth-child(1)')
await page.click('button:nth-child(1)')
await page.waitForSelector('h1')
await page.fill('#username', username);
await page.click('button[type="submit"]')
await page.waitForSelector('#password')
await page.waitForSelector('button[type="submit"]')
await page.waitForSelector('h1')
await page.fill('#password', password);
await page.click('button[type="submit"]')
let ok = true;
try {
await page.waitForURL("https://chat.openai.com/chat", { timeout: 10000 })
} catch (err) {
ok = false;
}
if (ok) {
const cookies = (await context.storageState()).cookies
const sessionTokenItem = cookies.find(item => item.name == "__Secure-next-auth.session-token")
const token = sessionTokenItem.value;
await browser.close();
return token;
} else {
await browser.close();
return false;
}
}
export {
chatgptToken
}