-
Notifications
You must be signed in to change notification settings - Fork 1
/
fsainsert.js
145 lines (134 loc) · 5.57 KB
/
fsainsert.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
// Never trust data you didn't prepare yourself :P
function escapeHtml(str) {
var div = document.createElement('div');
div.appendChild(document.createTextNode(str));
return div.innerHTML;
}
var siteLookupTable = {
'www.just-eat.co.uk': 'justeat',
'hungryhouse.co.uk': 'hungryhouse',
'deliveroo.co.uk': 'deliveroo',
'www.kukd.com': 'kukd'
};
var currentSite = siteLookupTable[location.hostname];
var businessName;
var businessStreet;
var businessCity;
var businessPostcode;
// Let's get our search parameters out of the page!
switch (currentSite) {
case 'justeat':
businessName = $('div.details > h1.name').text().trim();
businessStreet = $('div.details > p.address > span#street').text().replace(/\s+/g, ' ').trim();
businessCity = $('div.details > p.address > span#city').text().replace(/\s+/g, ' ').trim();
businessPostcode = $('div.details > p.address > span#postcode').text().replace(/\s+/g, ' ').trim();
break;
case 'hungryhouse':
// Because of course they change the header element for the reviews page.
if (window.location.pathname.match(/\/reviews$/)) {
businessName = $('.restMainInfoHeader > div.headerLeft > div.reviewPageRestTitle > span:nth-of-type(1)').text().replace(/\s+/g, ' ').trim();
} else {
businessName = $('.restMainInfoHeader > div.headerLeft > h1 > span:nth-of-type(1)').text().replace(/\s+/g, ' ').trim();
}
businessStreet = $('.menuAddress > .address > span:nth-of-type(1)').text().replace(/\s+/g, ' ').trim();
businessCity = $('.menuAddress > .address > span:nth-of-type(2)').text().replace(/\s+/g, ' ').trim();
businessPostcode = $('.menuAddress > .address > span:nth-of-type(3)').text().replace(/\s+/g, ' ').trim();
if ( businessPostcode === '' ) {
businessPostcode = businessCity;
businessCity = '';
}
break;
case 'deliveroo':
businessName = $('div.restaurant--main > div.restaurant__details > h1.restaurant__name').text().replace(/^(.+)-.+/, "$1").trim();
businessAddress = $('div.restaurant--main > div.restaurant__details > div.restaurant__metadata > div.metadata__details > small.address').text().split(',');
businessStreet = businessAddress[0].trim();
businessCity = businessAddress[businessAddress.length-2].trim();
businessPostcode = businessAddress[businessAddress.length-1].trim();
businessPostcode = businessPostcode.replace(/^(.{2,4})(.{3})$/, "$1 $2");
break;
case 'kukd':
businessName = $('section.headermaink > div.container:nth-of-type(1) > h1 > b').text().trim();
businessAddress = $('section.headermaink > div.container:nth-of-type(1) > h2').text().split(',');
businessStreet = businessAddress[0].trim();
businessCity = businessAddress[businessAddress.length-2].trim();
businessPostcode = businessAddress[businessAddress.length-1].trim();
break;
}
chrome.runtime.sendMessage({
'name': businessName,
'street': businessStreet,
'city': businessCity,
'postcode': businessPostcode
}, function(response) {
var ratingContent;
if ( response.success === true ) {
var fsaRating = 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();
ratingContent =
`
<div class='fsapanel'>
<p class='fsarating'>
<img src='` + escapeHtml(fsaImgLink) + `'>
</p>
<p class='fsadate'>
<label class='fsadatelabel'>Rating Date:</label> <span>` + escapeHtml(fsaDateStr) + `</span>
</p>
</div>
`;
} else {
// Vary our response based on the number of results.
if ( response.results === 0 ) {
ratingContent =
`
<div class='fsapanel fsanorating'>
<p class="fsaheader">No FSA Hygiene rating found!</p>
<p>Try searching on <a href="http://ratings.food.gov.uk/" target="_blank">the FSA website!</a></p>
</div>
`;
} else if (response.results > 1) {
ratingContent =
`
<div class='fsapanel fsanorating'>
<p class="fsaheader">FSA returned more than one takeaway!</p>
<p>Try searching on <a href="http://ratings.food.gov.uk/" target="_blank">the FSA website!</a></p>
</div>
`;
}
}
// Finally, add it to the page.
var targetElement;
var targetOp;
switch (currentSite) {
case 'justeat':
targetElement = 'div.restaurantOverview > div.details';
targetOp = 'append';
break;
case 'hungryhouse':
targetElement = 'div#restMainInfoWrapper';
targetOp = 'append';
break;
case 'deliveroo':
targetElement = 'div.restaurant__details > div.restaurant__metadata';
targetOp = 'append';
break;
case 'kukd':
if (window.location.pathname.match(/\/menu$/)) {
targetElement = 'div#checkoutHide > div.ordermodes:nth-of-type(3)';
} else if (window.location.pathname.match(/\/(info|reviews)$/)) {
targetElement = 'div.mb40 > div.mt20 > div:first-of-type';
}
targetOp = 'after';
break;
}
switch (targetOp) {
case 'append':
$(targetElement).append(ratingContent);
break;
case 'after':
$(targetElement).after(ratingContent);
break;
}
});