This repository has been archived by the owner on Sep 7, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
124 lines (72 loc) · 3.45 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
'use strict';
const p = require('puppeteer');
const EventEmitter = require('events');
class Compass extends EventEmitter {
BASE_URL = null;
LOGIN_URL = null;
browser = null;
page = null;
settings = {showChrome: false, pageDelay: 2500};
constructor(school_prefix, settings) {
super();
if(typeof school_prefix != 'string' && typeof school_prefix != 'undefined')
throw(new Error(`Invalid type for school_prefix, ${typeof school_prefix} not string`));
if(typeof settings != 'object' && typeof settings != 'undefined')
throw(new Error(`Invalid type for settings, ${typeof settings} not object`));
if(!school_prefix)
throw(new Error("Missing school_prefix constructor"));
this.BASE_URL = `https://${school_prefix}.compass.education/`;
this.LOGIN_URL = `https://${school_prefix}.compass.education/login.aspx?sessionstate=disabled`;
if(settings != undefined) this.settings = settings;
if(!this.settings.showChrome) this.settings.showChrome = false;
if(!this.settings.pageDelay) this.settings.pageDelay = 2500;
this.init();
}
async login(login) {
if(this.page == null)
throw(new Error(`Compass instance not initialized`))
await this.page.goto(this.LOGIN_URL, { waitUntil: 'networkidle2' })
.catch((err) => { throw(new Error(`Unable to establish a link with LOGIN_URL (${this.LOGIN_URL})`)); });
/* Write Username & Password */
await this.page.type('input[name="username"]', login.username, { delay: 5 })
.catch((err) => { throw(new Error(`Unable to find the Username Field`)); });
await this.page.type('input[name="password"]', login.password, { delay: 5 })
.catch((err) => { throw(new Error(`Unable to find the Password Field`)); });
await this.page.waitFor(500);
await this.page.click('input[name="button1"]')
.catch((err) => { throw(new Error(`Unable to find the Sign in Button`)); });
await this.page.waitFor(this.settings.pageDelay);
const error = await this.page.evaluate(() => document.getElementById('username-error') ? document.getElementById('username-error').innerText : null);
if(error != null) {
throw `Unable to log in, '${error}'`;
}
this.emit('logged-in')
await this.page.waitFor(this.settings.pageDelay);
}
async getClasses() {
const classText = await this.page.evaluate(() => [...document.querySelectorAll('.ext-evt-bd')].map(elem => elem.innerText));
var classes = [];
for(var i in classText) {
var newClass = {};
var classData = classText[i].split(' ');
newClass.time = classData[0].slice(0, -1);
newClass.name = classData[3];
newClass.room = classData[5];
newClass.teacher = classData[7];
classes.push(newClass);
}
return classes;
}
async returnHome() {
await this.page.goto(this.BASE_URL, { waitUntil: 'networkidle2' });
await this.page.waitFor(this.settings.pageDelay);
}
async init() {
this.browser = await p.launch({
headless: !this.settings.showChrome
});
this.page = await this.browser.newPage();
this.emit('initialized');
}
}
module.exports = Compass;