-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
59 lines (54 loc) · 1.65 KB
/
main.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
const express = require('express')
const puppeteer = require('puppeteer');
const app = express()
var bodyParser = require('body-parser');
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.get('/', (req, res) => {
res.sendFile(__dirname + '/main.html')
})
async function main(data) {
const browser = await puppeteer.launch({ headless: true, args: ['--no-sandbox'] });
const page = await browser.newPage();
await page.goto('http://www.magtifun.ge/', { waitUntil: 'networkidle0' }); // wait until page load
await page.type('#user', data.phone);
await page.type('#password', data.password);
// click and wait for navigation
await page.evaluate(() => {
document.querySelector('input[type=submit]').click();
});
await Promise.all([
page.waitForNavigation({ waitUntil: 'networkidle0' }),
]);
await page.type('#recipient', data.phone2);
await page.type('#message_body', data.text);
await page.evaluate(() => {
document.querySelector('input[type=submit]').click();
});
await browser.close();
}
app.post('/api',(req, res) => {
const data = req.body;
let errors = [];
if(!data.phone) {
errors.push("phone")
}
if(!data.password) {
errors.push("password")
}
if(!data.phone2) {
errors.push("phone2")
}
if(!data.text) {
res.type('application/json')
errors.push("text")
}
if(errors.length>0) {
res.type('application/json')
res.json({status:"failed",error:errors})
return;
}
main(data)
res.json({status:"success"})
})
app.listen( process.env.PORT || 3000)