-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathVstheWorld.js
126 lines (97 loc) · 3.18 KB
/
VstheWorld.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
const puppeteer = require("puppeteer");
const { heads } = require("./headers");
const args = require("minimist")(process.argv.slice(2));
const roomCode = args["_"][0];
// These variables can be tweaked for performance reasons
const numberOfSessions = args["sessions"] || 334;
const sessionName = args["sessionname"] || '';
const timeoutMilliseconds = args["timeout"] || 1500000;
const checktimeMilliseconds = args["checktime"] || 5000;
let counter = 0;
let hasBad = false;
if (args["help"]) {
console.log(`
Example usage:
npm start ROOM_CODE -- [--sessions 334] [--timeout 1500000] [--sessionname a] [--checktime 5000]
Github: https://github.com/bprussell/VstheWorld
`)
return;
}
if (!roomCode) {
throw "Please provide room code as a first argument";
}
const url = "https://jackbox.tv";
process.setMaxListeners(Infinity);
// Timeout function
function timeoutPromise(ms) {
return new Promise((_, reject) => setTimeout(() => reject(new Error('Timeout')), ms));
}
async function runWithTimeout(browser, sessionId) {
try {
await Promise.race([
run(browser, sessionId),
timeoutPromise(checktimeMilliseconds)
]);
} catch (error) {
if (error.message === 'Timeout') {
hasBad = true;
console.log(`Session ${sessionId} timed out.`);
} else {
throw error; // Re-throw other errors
}
}
}
async function run(browser, sessionId) {
console.log('starting');
const page = await browser.newPage();
console.log('got page');
// Configure the navigation timeout
await page.setDefaultNavigationTimeout(0);
//Pick a random UA and apply it
const userAgent = heads[Math.floor(Math.random() * heads.length)];
await page.setUserAgent(userAgent);
//turns request interceptor on
await page.setRequestInterception(true);
//if the page makes a request to a resource type of image then abort that request
page.on('request', request => {
if (request.resourceType() === 'image')
request.abort();
else
request.continue();
});
console.log('about to go to jackbox');
// go to jackbox.tv
await page.goto(url);
console.log('got jackbox page');
// enter room code
await page.waitForSelector("#roomcode", { timeout: timeoutMilliseconds });
await page.type("#roomcode", roomCode);
console.log('entered room code');
// enter user name
await page.waitForSelector("#username", { timeout: timeoutMilliseconds });
await page.type("#username", "U" + sessionName + sessionId);
console.log('entered username U' + sessionName + sessionId);
// click "Join Audience" button once available
await page.waitForSelector(".audience", { timeout: timeoutMilliseconds });
await page.click("#button-join");
counter++;
console.log(`joined audience (${counter} total)`);
}
// Delay function
function delay(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
async function runAll() {
const browser = await puppeteer.launch();
for (let i = 0; i < numberOfSessions; i++) {
if (!hasBad) {
await runWithTimeout(browser, i);
} else {
console.log("waiting five minutes...");
await delay(315000);
console.log("waiting complete, trying again...");
hasBad = false;
}
}
}
runAll();