This repository has been archived by the owner on Jul 17, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 60
/
run-headless.js
119 lines (98 loc) · 3.06 KB
/
run-headless.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
var cri = require('chrome-remote-interface');
var fs = require('fs');
var repl = require('readline');
var util = require('util');
var config = require('./run-headless.config.json');
cri(async (client) => {
try {
await demLogics(client);
}
catch (e) {
console.error(e);
}
finally {
client.close();
console.log('Have a nice day!');
}
}).on('error', console.error);
async function demLogics(client) {
var { DOM, Page, Runtime } = client;
await Promise.all([
DOM.enable(),
Page.enable(),
Runtime.enable()
]);
await Page.navigate({ url: config.siteUrl + '/users/login/' });
await Page.loadEventFired();
await screenshot('pics/login-pre.png', client);
var url = await getUrl(client);
if (!/login-add($|\?)/.test(url)) {
console.log('Need to authenticate');
await loginToSE(client);
}
else {
console.log('Cool, already logged in');
}
await injectBot(client);
console.log('Injected bot');
}
async function loginToSE(client) {
var { DOM, Page } = client;
var { root: { nodeId: documentId } } = await DOM.getDocument({ depth: 0 });
await Promise.all([
type('#login-form #email', config.email, client, documentId),
type('#login-form #password', config.password, client, documentId)
]);
await screenshot('pics/login-filled.png', client);
await click('#login-form #submit-button', client);
await Page.loadEventFired();
await screenshot('pics/login-post.png', client);
}
async function injectBot(client) {
var { Page, Runtime } = client;
await Page.navigate({ url: config.roomUrl });
await Page.loadEventFired();
await screenshot('pics/chat.png', client);
await Runtime.evaluate({
expression: `
var script = document.createElement('script');
script.src = 'https://rawgit.com/Zirak/SO-ChatBot/master/master.js';
script.onload = function() {
bot.activateDevMode();
console.log('Loaded bot');
bot.adapter.out.add('I will derive!');
};
document.head.appendChild(script);`
});
}
// rando utils because fml
async function type(selector, value, { DOM }, documentId) {
var { nodeId } = await DOM.querySelector({
selector,
nodeId: documentId
});
await DOM.setAttributeValue({
nodeId,
name: 'value',
value
});
}
async function click(selector, { Runtime }) {
// as of yet, the only way to dispatch mouse events is via coordinates
// that's rather lame so we do something lamer
await Runtime.evaluate({
expression: `document.querySelector('${selector}').click()`
});
// plz no kill me i have family, kidnap me and depand ransom
// or just kill them
}
async function getUrl({ Runtime }) {
var { result: { value } } = await Runtime.evaluate({
expression: 'location.href'
});
return value;
}
async function screenshot(ssName, { Page }) {
var ss = new Buffer((await Page.captureScreenshot()).data, 'base64');
return await util.promisify(fs.writeFile)(ssName, ss, 'base64');
}