-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbackground.js
181 lines (159 loc) · 4.82 KB
/
background.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
var loggedIn = $.Deferred();
var profilePageLoad = loggedIn
.then(function() {
return $.get('https://www.mealbookings.cai.cam.ac.uk/profile.php');
})
var profileLoad = profilePageLoad
.then(function(data) {
return {
vegetarian: $(data).find('input[name=vegetarian]').prop('checked'),
requirements: $(data).find('input[name=requirements]').val(),
};
});
var analyticsLoad = profilePageLoad.then(function(d) {
var user = $(d).find('.login_footer').first().text();
var crsid = /\((.+)\)/.exec(user)[1];
var version = chrome.app.getDetails().version;
return {from: crsid, v: version};
});
var normalizeName = function(name) {
name = name.trim().toLowerCase();
name = name.replace(/ +\(?early\)?/, '');
name = name.replace(/^early +/, '');
name = name.replace(/harvey court/, 'H.C.');
name = name.replace(/ +hall$/, '');
name = name.replace(/1st/, 'first');
name = name.replace(/^pre term/, 'pre-term');
return name;
}
var hallPageLoad = loggedIn
.then(function() {
return $.get('https://www.mealbookings.cai.cam.ac.uk/');
})
.then(function(mainPage) {
return $(mainPage);
})
var messagesLoad = hallPageLoad
.then(function($mainPage) {
return $mainPage.find('.message .announcement');
});
var parseAllergens = function(text) {
let re = /^([A-Z]+)= (.*)$/gm;
let mapping = {}
for (let match; (match = re.exec(text)) !== null;) {
mapping[match[1]] = match[2]
}
return mapping;
};
var defaultAllergens = `
C= Celery
G= Gluten/Cereal
SF= Crustaceans/Shelfish
E= Egg
F= Fish
L= Lupin
D= Dairy products/Milk
MO= Molluscs (mussels, octopus, Snail, scallops)
M= Mustard
N= Nuts
P= Peanuts
SE= Sesame
SO= Soya products
SU= Sulphur Dioxide (wine)
`; // taken from https://intranet.cai.cam.ac.uk/eating-caius
var allergensLoad = messagesLoad
.then(function($messages) {
return Object.merge.apply(
null,
$messages.filter(function() {
return $(this).text().includes('allergens');
}).map(function() {
return parseAllergens($(this).text());
}).get()
) || parseAllergens(defaultAllergens);
})
var hallNameLoad = hallPageLoad
.then(function($mainPage) {
var bookingIds = $mainPage.find(".list a").map(function() {
var m = /\?event=(\d+)/.exec($(this).attr('href'));
if(m)
return +m[1];
}).get();
var pageLoaders = bookingIds.map(function(id) {
return $.get('https://www.mealbookings.cai.cam.ac.uk/index.php', {event: id}).then(function(d) {
var header = $(d).find('h1');
var title = header.text();
if(/^Current bookings for/.test(title))
return undefined;
else if(/^Error:/.test(title))
return null;
var description = header.nextAll('p').first().text();
var dataTable = header.nextAll('.table').first();
var data = {};
dataTable.find('tr').each(function() {
var key = $(this).find('th').text().replace(/:$/,'').toLowerCase();
var value = $(this).find('td').text();
data[key] = value;
});
return {id: id, name: normalizeName(title), description: description, data: data}
})
});
return $.whenAll(pageLoaders)
})
.then(function(titles) {
// remove non-existant halls
return titles.filter(function(x) { return x; });
});
var lessCompiled = (function() {
return $.get(chrome.extension.getURL('style.less')).then(function(lessStyle) {
var d = $.Deferred();
less.render(lessStyle, {
paths: ['.'], // Specify search paths for @import directives
filename: 'style-wrapper.less' // Specify a filename, for better error messages
}, function (err, out) {
if (err) {
return d.reject(err);
}
d.resolve(out.css);
});
return d.promise();
});
})();
chrome.extension.onRequest.addListener(function(request, sender, sendResponse) {
loggedIn.resolve();
analyticsLoad.then(function(payload) {
$.getJSON('http://meals.efw27.user.srcf.net:8989/ping', payload);
});
if(request.action == 'getTemplates') {
var templates = document.querySelectorAll('#templates > div');
var templatePayload = {};
for (var i = 0; i < templates.length; i++) {
var name = templates[i].getAttribute('data-name');
var theme = templates[i].getAttribute('data-theme') || 'default';
if(templatePayload[name] == null)
templatePayload[name] = {};
templatePayload[name][theme] = templates[i].innerHTML;
}
sendResponse(templatePayload);
}
else if(request.action == 'getPreferences') {
profileLoad.then(function(x) {
sendResponse(x);
});
}
else if(request.action == 'getAllergens') {
allergensLoad.then(function(x) {
sendResponse(x);
});
}
else if(request.action == 'loadHallTypes') {
hallNameLoad.then(function(x) {
sendResponse(x);
});
}
else if(request.action == 'getLess') {
lessCompiled.then(function(x) {
sendResponse(x);
});
}
});