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
|
const { JSDOM } = require('jsdom');
const errors = require('../errors');
const http = require('../http');
// eslint-disable-next-line max-len
function submitForm({ dom, jar, asIs, runScripts, hook, method = 'POST', actionRoot, extraParams, followRedirects = true }) {
let url = dom.window.document.getElementsByTagName('form')[0].action;
if (url.startsWith('/'))
{
url = url.substring(1);
}
if (url.indexOf('http') === -1)
{
url = actionRoot + url;
}
const params = getParams(dom, extraParams);
const data = {
url,
jar,
asIs,
followRedirects,
runScripts,
hook,
data: params,
method
};
return getDOM(data);
}
function getParams(dom, extra = {})
{
const params = {};
Array.prototype.forEach.call(
dom.window.document.getElementsByTagName('input'),
input => (input.name !== '' ? params[input.name] = input.value : '')
);
return { ...params, ...extra };
}
async function getDOM({ url, jar, method = 'GET', data = '', runScripts, hook, followRedirects, asIs })
{
let result = await http({
url,
method,
data,
jar,
followRedirects
});
if (asIs)
{
return result;
}
if (result.indexOf('<script>$(function() { startup() });</script>') !== -1)
{
result = result
.replace('<script>$(function() { startup() });</script>', '')
.replace('console.log(user+" "+pwd);', '');
}
return new JSDOM(result, {
runScripts: runScripts ? 'dangerously' : 'outside-only',
beforeParse(window) {
if (hook) {
hook(window)
}
},
cookieJar: jar
});
}
function extractStart(html) {
if (html.includes('Votre adresse IP est provisoirement suspendue')) { // Top 10 anime betrayals
throw errors.BANNED.drop();
}
if (html.includes('Le site n\'est pas disponible')) {
throw errors.CLOSED.drop();
}
if (!html.includes('PRONOTE')) {
throw errors.WRONG_CREDENTIALS.drop();
}
html = html.replace(/ /ug, '').replace(/\n/ug, '');
const from = 'Start(';
const to = ')}catch';
const start = html.substring(html.indexOf(from) + from.length, html.indexOf(to));
const json = start.
replace(/(['"])?([a-z0-9A-Z_]+)(['"])?:/gu, '"$2": ').
replace(/'/gu, '"');
return JSON.parse(json);
}
module.exports = {
submitForm,
getDOM,
getParams,
extractStart
};
|