-
Notifications
You must be signed in to change notification settings - Fork 0
/
uberprufungScript.js
134 lines (109 loc) · 4.79 KB
/
uberprufungScript.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
// Sparklines on hover over restarant name
// restaurant closures
// restaurant cleanliness score
// link to restaurant cleanliness
// http://www.inspections.vcha.ca/Inspection/Show/100c94cd-01a1-41f1-8d03-5d16f8e967e7
// restaurant closures --- pdf ?
var rootUrl = 'http://www.inspections.vcha.ca/';
var restaurantName = '';
var latestInspectionUrl = '';
var restaurantFound = false;
var inspectionReportAvailable = false;
var uberContainer;
function getFirstRestaurantLink(data) {
return $(data).find('#facilities-grid tr:nth-child(2) a').attr('href');
}
function getFirstInspectionLink(data) {
var a = $(data).find('table.tabular .left a')[1];
return rootUrl + $(a).attr('href');
}
// Searches for the given restaurant and returns the data as a promise
function getSearchResultsByRestaurantName(restaurantName) {
var searchUrl = rootUrl + 'Facility?report-type=ffffffff-ffff-ffff-ffff-fffffffffff1&sort-by=Name&search-term=' + encodeURIComponent(restaurantName) + '&submit-search=Search';
console.log('making a request to: ' + searchUrl);
return $.get(searchUrl, function(data) {
// TODO: better search method
// If no results, try shortening restaurant name
// special characters
// click on most relevant name
// how to handle chains? - based on user location?
});
}
// Given data from getSearchResultsByRestaurantName, it extracts the first restaurant result and makes a request to get the page with the inspection reports
// some restaurants don't have inspection reports
function getInspectionReports(data) {
var firstRestaurantLink = getFirstRestaurantLink(data);
if (typeof firstRestaurantLink != 'undefined') {
restaurantFound = true;
return $.get(rootUrl + firstRestaurantLink, function(data) {});
}
}
// Given page with list of inspection reports from getInspectionReports, it extracts the first inspection link and makes a get request
function getLatestInspectionReportDetail(data) {
// Extract latest inspection link
if (restaurantFound) {
latestInspectionUrl = getFirstInspectionLink(data);
if (typeof latestInspectionUrl != 'undefined') {
inspectionReportAvailable = true;
return $.get(latestInspectionUrl, function(data) {});
}
}
}
function createUberprufungInfoBox() {
uberContainer = $('<div class="safetyContainer"></div>');
uberContainer.append("<p>Überprüfung</p>");
var loadingGif = chrome.extension.getURL('loading.gif');
uberContainer.append("<div id='uberLoading'><img src='" + loadingGif + "'/>Searching for restaurant info ...</div>");
var pageTitleElement = $('.biz-page-header')[0];
$(pageTitleElement).after(uberContainer);
}
// inserts link to safety info page after restaurant title
function insertSafetyInfoToDOM(data) {
$('#uberLoading').remove();
var totalObserv = getTotalNewObservations(data);
if (!restaurantFound) {
var rootUrlElement = getLinkElement(rootUrl, rootUrl);
uberContainer.append("<p>" + restaurantName + " was not found. Conduct a manual search yourself here: </p>");
uberContainer.append(rootUrlElement);
} else if (!inspectionReportAvailable) {
uberContainer.append("<p>No inspection reports found</p>");
} else {
var restaurantSafetyLink = getLinkElement('Latest inspection link', latestInspectionUrl);
uberContainer.append("<p>New observations: " + totalObserv + "</p>");
uberContainer.append(restaurantSafetyLink);
}
}
function getLinkElement(text, url) {
var link = document.createElement("a");
link.text = text;
link.setAttribute('href', url);
return link;
}
// Get the page with the observation table and return total count of new observations
function getTotalNewObservations(data) {
// safety rating is calculated using the latest inspection report
// it adds up the number of 'new observations'
// How to calculate the range??
// Gets all of the new observations in the table
if (inspectionReportAvailable) {
var observations = $(data).find('.tabular td.left').next();
// add up the total and return it
return Array.prototype.reduce.call(observations, function(val1, val2) {
return val1 + parseInt($(val2).text().trim());
}, 0);
}
}
function addSafetyRating(restaurantName) {
// Todo: Error handling!!!!!!!
return getSearchResultsByRestaurantName(restaurantName)
.then(getInspectionReports)
.then(getLatestInspectionReportDetail)
.then(insertSafetyInfoToDOM);
}
document.onreadystatechange = function() {
if (document.readyState == "complete") {
createUberprufungInfoBox();
restaurantName = document.getElementsByClassName('biz-page-title')[0].textContent.trim();
addSafetyRating(restaurantName);
}
}