forked from Rohaq/takeawayhygieneuk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fsarank.js
223 lines (200 loc) · 9.58 KB
/
fsarank.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
211
212
213
214
215
216
217
218
219
220
221
222
223
function sortDivs() {
var containerSelectors = ['div[data-test-id="openrestaurants"]',
'div[data-test-id="closedrestaurants"]',
'div[data-test-id="offlinerestaurants"]'];
var scores = ['-2', '-1', '1', '2', '3', '4', '5'];
var rankedSections = [];
for (var i in containerSelectors) {
var containerSelector = containerSelectors[i];
var container = document.querySelector(containerSelector);
rankedSections[i] = [];
if (container) {
for (var j in scores) {
var score = scores[j];
var selector = 'section[data-fsarank="' + score + '"]';
var sections = container.querySelectorAll(selector);
rankedSections[i].unshift(sections);
}
}
}
for (var i in rankedSections) {
for (var k in rankedSections[i]) {
var sections = rankedSections[i][k];
var containerSelector = containerSelectors[i];
var container = document.querySelector(containerSelector);
if (container && sections && sections.length>0) {
for(var r in sections) {
var section = sections[r];
if (section instanceof Node) {
container.appendChild(section);
} else {
//console.log('container '+i+', score '+scores[m]," section/restaurant was: ");
//console.dir(section);
//console.log(i,k,r);
}
}
}
}
}
}
function escapeHtml(str) {
var div = document.createElement('div');
div.appendChild(document.createTextNode(str));
return div.innerHTML;
}
var siteLookupTable = {
'www.just-eat.co.uk': 'justeat'
};
var restaurantsToCheck = null;
var currentSite = siteLookupTable[location.hostname];
var ratingsLookup = window.localStorage.getItem('ratingsLookup');
if (ratingsLookup == '""' || ratingsLookup == null) {
ratingsLookup = {};
} else {
ratingsLookup = JSON.parse(ratingsLookup);
}
var shiftLegendIcons = function () {
var legendDiv = document.querySelectorAll('div.c-listing-item-premier');
[].forEach.call(legendDiv, function(v,i,a) {
v.style.marginRight = '140px';
});
}
var updateElementWithScore = function (element,stub) {
//console.dir(ratingsLookup[urlStub]);
var fsaImgLink = ratingsLookup[stub]['imageUrl'];
var fsaDateStr = ratingsLookup[stub]['date'];
var fsaRating = ratingsLookup[stub]['rating'];
var ratingContent = `
<div class='fsapanel' alt="Rating Date ${escapeHtml(fsaDateStr)}" style="float: right;margin-right: 20px;vertical-align: top;">
<p class='fsarating' style="height: 31px;margin-top:0px;">
<img class='fsa${fsaRating}'>
</p>
</div>
`; // src='` + escapeHtml(fsaImgLink) + `'
var container = element.querySelector('div.c-listing-item-info');
var elDiv = element.querySelector('div.fsapanel');
if (elDiv) {
container.removeChild(elDiv);
}
element.parentElement.dataset.fsarank=fsaRating;
container.insertAdjacentHTML('afterbegin',ratingContent);
};
var updateElementNoScore = function (element) {
var fsaImgLink = chrome.extension.getURL('/images/ratings/fhrs_nodataavailable_en-gb.jpg');
var ratingContent = `
<div class='fsapanel' alt="No inspection information available." style="float: right;margin-right: 20px;vertical-align: top;">
<p class='fsarating' style="height: 31px;margin-top:0px;">
<img class='fsaNA'>
</p>
</div>
`; // src='` + escapeHtml(fsaImgLink) + `'
var container = element.querySelector('div.c-listing-item-info');
var elDiv = element.querySelector('div.fsapanel');
if (elDiv) {
container.removeChild(elDiv);
}
element.parentElement.dataset.fsarank=-2;
container.insertAdjacentHTML('afterbegin',ratingContent);
};
switch (currentSite) {
case 'justeat': {
var restaurants = document.querySelectorAll('section.c-listing-item a');
var count = restaurants.length;
[].forEach.call(restaurants, function(v,i,a) {
var name = v.querySelector('h3.c-listing-item-title').innerText;
shiftLegendIcons();
var container = v.querySelector('div.c-listing-item-info');
container.insertAdjacentHTML('afterbegin', '<div class="fsapanel" alt="Loading FSA Hygiene Rating" ' +
'style="float: right;margin-right: 20px;vertical-align: top;">' +
'<p class="fsarating" style="height: 31px;width=110px;margin-top:0px;">' +
'<img class="fsaloading">' +
'</p></div>'); //src="http://bowdb.alexbowyer.com/fsa/reload-61px.gif"
let stub = v.href.split('/')[3];
if (ratingsLookup[stub]) {
updateElementWithScore(v,stub);
}
else {
var worker = new Worker(chrome.runtime.getURL('fsagetaddress.js'));
worker.addEventListener('message', function (e) {
var namesToCheck = [e.data.name];
var punctRE = /[\u2000-\u206F\u2E00-\u2E7F\\!"#$@%&()*+,\-.\/:;<=>?@\[\]^_{|}~]/g;
var spaceRE = /\s+/g;
var cleanedName = e.data.name;
cleanedName = cleanedName.replace(punctRE, '').replace(spaceRE, ' ');
if (cleanedName!=e.data.name) {
namesToCheck.push(cleanedName);
}
var splitNameParts = cleanedName.split(' ');
var splitNamePartsCopy = splitNameParts.slice(0);
while (splitNameParts.length>1) {
splitNameParts = splitNameParts.slice(0, -1);
if (splitNameParts.length == 2) {
namesToCheck.push(splitNameParts[1]);
}
namesToCheck.push(splitNameParts.join(' '));
}
while (splitNamePartsCopy.length>1 && splitNamePartsCopy[0]!="The") {
splitNamePartsCopy = splitNamePartsCopy.slice(1);
if (splitNamePartsCopy.length == 2) {
namesToCheck.push(splitNamePartsCopy[1]);
}
namesToCheck.push(splitNamePartsCopy.join(' '));
}
for (var i in namesToCheck) {
var name = namesToCheck[i];
chrome.runtime.sendMessage({
'name': name,
'street': e.data.street,
'city': e.data.city,
'postcode': e.data.postcode
}, function (response) {
//console.dir(response);
if (response.success === true) {
var fsaRating = parseInt(response.rating);
var fsaKey = response.key;
var fsaDate = new Date(response.date);
var fsaImgLink = chrome.extension.getURL('/images/ratings/' + fsaKey + '.jpg');
var fsaDateStr = fsaDate.getDate() + '/' + (fsaDate.getMonth() + 1) + '/' + fsaDate.getFullYear();
if (!fsaRating) {
fsaRating = -1;
}
if (!ratingsLookup[e.data.stub]) {
ratingsLookup[e.data.stub] = {
key: fsaKey,
imageUrl: fsaImgLink,
date: fsaDateStr,
rating: fsaRating
};
}
window.localStorage.setItem('ratingsLookup', JSON.stringify(ratingsLookup));
//console.log('found score for '+e.data.stub+", ("+name+") - updating page");
updateElementWithScore(v, e.data.stub);
} else {
// nothing found
//console.log('no data for '+e.data.stub);
//console.log('no data found for '+e.data.stub+", ("+name+")");
updateElementNoScore(v);
}
});
}
}, false);
var dataForWorker = {
stub: v.href.split('/')[3],
name: name
};
worker.postMessage(dataForWorker); // Send data to our worker.
}
});
var ul = document.querySelector('ul.c-sortBy-popoverList');
var hygieneLi = '<li class="c-sortBy-item"><label class="o-formControl-label" for="fsarank" tabindex=“-1">' +
'<input class="o-formControl-input" type="radio" id="fsarank" name="sortbyColumn" value="fsarank">' +
'<span class="o-formControl-indicator o-formControl-indicator--radio"></span>Hygiene Rating</label></li>';
ul.insertAdjacentHTML('afterbegin',hygieneLi);
var hyLi = ul.querySelectorAll('ul.c-sortBy-popoverList li')[0];
hyLi.addEventListener("click", function() {
console.log('click');
sortDivs();
}, false);
// TODO wait until everything complete
}
};