This repository has been archived by the owner on Aug 1, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 74
/
ge-cancellation-checker.phantom.js
210 lines (171 loc) · 6.75 KB
/
ge-cancellation-checker.phantom.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
// CLI usage:
// phantomjs [--ssl-protocol=any] ge-cancellation-checker.phantom.js [-v|--verbose]
var system = require('system');
var fs = require('fs');
var VERBOSE = false;
var loadInProgress = false;
// Calculate path of this file
var PWD = '';
var current_path_arr = system.args[0].split('/');
if (current_path_arr.length == 1) { PWD = '.'; }
else {
current_path_arr.pop();
PWD = current_path_arr.join('/');
}
// ...from command
var configFile = PWD + '/config.json'
system.args.forEach(function(val, i) {
if (val == '-v' || val == '--verbose') { VERBOSE = true; }
if (val == '--config') {
if (system.args.length == i) console.log('failed to set config - no option given');
else {
configFile = system.args[i+1];
}
}
});
try {
var settings = JSON.parse(fs.read(configFile));
if (!settings.username || !settings.password || !settings.init_url || !settings.enrollment_location_id) {
console.log('Missing username, password, enrollment location ID, and/or initial URL. Exiting...');
phantom.exit();
}
}
catch(e) {
console.log('Could not find ' + configFile);
phantom.exit();
}
function fireClick(el) {
var ev = document.createEvent("MouseEvents");
ev.initEvent("click", true, true);
el.dispatchEvent(ev);
}
var page = require('webpage').create();
page.settings.userAgent = 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2062.120 Safari/537.36';
page.onConsoleMessage = function(msg) {
if (!VERBOSE) { return; }
console.log(msg);
};
page.onError = function(msg, trace) {
if (!VERBOSE) { return; }
console.error('Error on page: ' + msg);
}
page.onCallback = function(query, msg) {
if (query == 'username') { return settings.username; }
if (query == 'password') { return settings.password; }
if (query == 'fireClick') {
return function() { return fireClick; } // @todo:david DON'T KNOW WHY THIS DOESN'T WORK! :( Just returns [Object object])
}
if (query == 'report-interview-time') {
if (VERBOSE) { console.log('Next available appointment is at: ' + msg); }
else { console.log(msg); }
return;
}
if (query == 'report-no-interviews') {
if (VERBOSE) { console.log('No new interviews available. Please try again later.'); }
else { console.log('None'); }
return;
}
if (query == 'fatal-error') {
console.log('Fatal error: ' + msg);
phantom.exit();
}
return null;
}
page.onLoadStarted = function() { loadInProgress = true; };
page.onLoadFinished = function() { loadInProgress = false; };
if (VERBOSE) { console.log('Please wait...'); }
page.open(settings.init_url);
var steps = [
function() { // Log in
page.evaluate(function() {
console.log('On GOES login page...');
document.querySelector('input[name=j_username]').value = window.callPhantom('username');
/* The GE Login page limits passwords to only 12 characters, but phantomjs can get around
this limitation, which causes the fatal error "Unable to find terms acceptance button" */
document.querySelector('input[name=j_password]').value = window.callPhantom('password').substring(0,12);
document.querySelector('form[action="/goes/security_check"]').submit();
console.log('Logging in...');
});
},
function() { // Accept terms
page.evaluate(function() {
submitHome();
console.log('Bypassing human check...');
});
},
function() { // main dashboard
page.evaluate(function() {
function fireClick(el) {
var ev = document.createEvent("MouseEvents");
ev.initEvent("click", true, true);
el.dispatchEvent(ev);
}
var $manageAptBtn = document.querySelector('.bluebutton[name=manageAptm]');
if (!$manageAptBtn) {
return window.callPhantom('fatal-error', 'Unable to find Manage Appointment button');
}
fireClick($manageAptBtn);
console.log('Entering appointment management...');
});
},
function() {
page.evaluate(function() {
function fireClick(el) {
var ev = document.createEvent("MouseEvents");
ev.initEvent("click", true, true);
el.dispatchEvent(ev);
}
var $rescheduleBtn = document.querySelector('input[name=reschedule]');
if (!$rescheduleBtn) {
return window.callPhantom('fatal-error', 'Unable to find reschedule button. Is it after or less than 24 hrs before your appointment?');
}
fireClick($rescheduleBtn);
console.log('Entering rescheduling selection page...');
});
},
function() {
page.evaluate(function(location_id) {
function fireClick(el) {
var ev = document.createEvent("MouseEvents");
ev.initEvent("click", true, true);
el.dispatchEvent(ev);
}
var elements = document.getElementsByName('selectedEnrollmentCenter');
for (i = 0; i < elements.length; i++) {
if (elements[i].value == location_id) {
elements[i].checked = true;
var location_name = elements[i].title;
}
}
fireClick(document.querySelector('input[name=next]'));
console.log('Choosing Location: ' + location_name);
}, settings.enrollment_location_id.toString());
},
function() {
page.evaluate(function() {
// If there are no more appointments available at all, there will be a message saying so.
try {
if (document.querySelector('span.SectionHeader').innerHTML == 'Appointments are Fully Booked') {
window.callPhantom('report-no-interviews');
return;
}
} catch(e) { }
// We made it! Now we have to scrape the page for the earliest available date
var date = document.querySelector('.date table tr:first-child td:first-child').innerHTML;
var month_year = document.querySelector('.date table tr:last-child td:last-child div').innerHTML;
var full_date = month_year.replace(',', ' ' + date + ',');
// console.log('');
window.callPhantom('report-interview-time', full_date)
// console.log('The next available appointment is on ' + full_date + '.');
});
}
];
var i = 0;
interval = setInterval(function() {
if (loadInProgress) { return; } // not ready yet...
if (typeof steps[i] != "function") {
return phantom.exit();
}
steps[i]();
i++;
}, 100);