-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
134 lines (122 loc) Β· 4.32 KB
/
index.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
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
import puppeteer from 'puppeteer'
import axios from 'axios'
import qrcode from 'qrcode-terminal'
import * as dotenv from 'dotenv'
dotenv.config()
const isEmpty = (something) => something === "" || something === null || something === undefined;
const getText = async () => {
let text, value;
if (isEmpty(process.env.RANDOM_SENTENCES)) {
try {
await axios.get('https://quotable.io/random')
.then(function (response) {
const data = response.data;
value = `"*${data.content}*" - ***${data.author}***`
text = `"${data.content}" - ${data.author}`
})
.catch((err) => {
const errorMsg = "Error Status: " + err?.response?.status + ", " + err?.response?.statusText;
console.log(errorMsg);
console.log("-------------------------------");
text = errorMsg
value = errorMsg
});
} catch (error) {
const msg = "Something wrong!";
console.log(error.message);
text = msg;
value = msg;
}
} else {
const randomSentences = process.env.RANDOM_SENTENCES.split('|');
text = randomSentences[Math.floor(Math.random() * randomSentences.length)];
value = text;
}
return {
text: text,
value: value
};
}
console.log("Starting...");
(async () => {
console.log('Initial browser π');
const browser = await puppeteer.launch({
headless: 'new',
args: [
'--no-sandbox',
// '--window-size=1920,1080'
],
// defaultViewport: {
// width:1920,
// height:1080
// }
});
try {
const page = await browser.newPage();
await page.setUserAgent('Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.90 Safari/537.36')
page.setDefaultNavigationTimeout(60 * 1000);
let pages = await browser.pages();
await pages[0].close();
console.log("π Opening Login page Discord");
await page.goto(`https://discord.com/login`, { waitUntil: ['load', 'networkidle0'] });
await page.waitForSelector('div[class^=qrCode_]');
await page.waitForTimeout(5000);
const qrBase64 = await page.evaluate(() => {
const base64 = btoa(unescape(encodeURIComponent(document.querySelector("div[class^=qrCode_]").getInnerHTML())));
const qrcode = 'data:image/svg+xml;base64,' + base64;
return qrcode;
});
const pageQRParser = await browser.newPage()
await pageQRParser.goto('https://qrcode-parser.netlify.app/', { waitUntil: ['load', 'networkidle0'] })
await pageQRParser.evaluate((externalVar) => {
document.querySelector('#image-base64').value = externalVar;
return null;
}, qrBase64);
await pageQRParser.click('#parse-image-base64');
await page.waitForTimeout(2000);
const qrValue = await pageQRParser.evaluate(async () => {
return await new Promise(resolve => { // <-- return the data to node.js from browser
resolve(document.querySelector("#content2").getInnerHTML())
})
})
// await pageQRParser.close();
if (qrValue === null) {
console.log("Something wrong while parsing QrCode π");
console.log("Convert Base64 this to image and scan manually: ")
console.log(qrBase64)
await page.waitForTimeout(15000);
} else {
console.log("Scan this barcode:");
qrcode.generate(qrValue);
console.log("You have 10 seconds to scan barcode π");
await page.waitForTimeout(10000);
}
console.log("π Go to channel: " + process.env.CHANNEL_URL);
await page.goto(process.env.CHANNEL_URL)
await page.waitForSelector('div[role=textbox]');
await page.waitForTimeout(4000);
let count = 0;
while (true) {
count++;
const data = await getText();
await page.type('div[role=textbox]', data.value);
await page.keyboard.press('Enter');
console.log("Count: " + count);
if (process.env.DEBUG_OUTPUT === 'true') {
if (isEmpty(process.env.RANDOM_SENTENCES)) {
console.log("βοΈ Sending Quote:");
console.log(data.text);
} else {
console.log("βοΈ Sending Text: " + data.value);
}
}
console.log("-------------------------------");
await page.waitForTimeout(process.env.INTERVAL * 1000);
}
} finally {
await browser.close();
}
})().catch((e) => {
console.log(e);
process.exitCode = 1;
});