-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
executable file
·150 lines (134 loc) · 4.04 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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#! /usr/bin/env node
// __dirname is not defined in module
import { fileURLToPath } from "url";
import path, { dirname } from "path";
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
import express, { json, urlencoded } from "express";
import { readFile, writeFile } from "fs";
import open from "open";
// variables
const PORT = process.env.PORT || 3000;
// logs error to the console
var previous_task_ok = true;
var error_msg = "";
const callback_for_process = (err) => {
console.log(" error found >>>");
console.log(" " + err.message);
error_msg = err.message;
previous_task_ok = false;
};
process.on("uncaughtException", callback_for_process);
// declaring app and all the middleware
const app = express();
app.use(express.static(path.join(__dirname, "client")));
app.use(json());
app.use(urlencoded({ extended: true }));
// get previously saved data in json
app.get("/saved", (req, res) => {
readFile("saved.json", (err, data) => {
if (err) {
console.log(err);
return res.send({
commandList: [
`await page.click("span[title='puppeteer_ui']")`,
`await page.click("h3")`,
`await page.type("input", "\\n")`,
`await page.type("input", "github.com/adnangif")`,
`await page.goto("https://google.com/")`,
],
restartInstance: false,
runFromStart: false,
saveCommands: true,
}); // send a dummy object if file not found
} else {
return res.send(JSON.parse(data));
}
});
});
// save sent data in json
app.post("/", (req, res) => {
res.sendStatus(201);
let data = req.body;
// checks if the frontend wants to save the commands
if (data.saveCommands) {
data = JSON.stringify(data);
writeFile("saved.json", data, (err) => {
if (err) {
throw err;
} else {
console.log("saved in a file!");
}
});
} else {
console.log("no saving");
}
});
// ======================== PUPPETEER_____SECTION__START =================================
// puppeteer code injection starts here
// import puppeteer from "puppeteer";
import puppeteer from "puppeteer-extra";
import StealthPlugin from "puppeteer-extra-plugin-stealth"; // Some stealthy thing, I don't know what it does but it actually works
puppeteer.use(StealthPlugin());
// the website will think you are on SAMSUNG GALAXY S9
// useful for insta posting
const USER_AGENT =
"Mozilla/5.0 (Linux; Android 8.0.0; SM-G960F Build/R16NW) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.84 Mobile Safari/537.36";
let options = {
headless: false, // For debugging purpose
defaultViewport: {
width: 320,
height: 570,
},
};
const browser = await puppeteer.launch(options);
const delay = (n) => {
Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0, n);
};
let page = await browser.newPage();
page.setUserAgent(USER_AGENT);
// puppeteer runs from this function
const runInPuppeteer = (command) => {
console.log("");
console.log("<========>");
console.log(`At task: ${command}`);
try {
let cmd = command;
if (cmd.includes("await page")) {
cmd = cmd.replace("await ", "");
}
eval(cmd); // magic happens in this line :)
previous_task_ok = true;
} catch (error) {
// throw error;
error_msg = error.message;
console.log(error_msg);
previous_task_ok = false;
}
};
// ======================== PUPPETEER_____SECTION____END ===========================
// check if previous task succeeded
app.get("/previous", (req, res) => {
if (previous_task_ok) {
res.send({ message: "success" });
} else {
res.send({ message: error_msg });
}
});
// Get new command from here
app.post("/newCommand", (req, res) => {
res.sendStatus(201);
let newCommand = req.body;
runInPuppeteer(newCommand.command); // run command in puppeteer
});
// app listens on port 3000
app.listen(PORT, () => {
console.log("\n\n\n\n\n\n\n\n\n\n\n\n");
console.log("=======================*******==========================");
console.log("\n\n\n");
console.log("Opening browser: ");
console.log("SERVER STARTED AT:>>>>>> http://localhost:" + PORT);
console.log("\n\n\n");
});
// open app in a browser
await open("http://localhost:" + PORT);