Skip to content

Commit

Permalink
https support done, all requests to food.gov.uk made from background …
Browse files Browse the repository at this point in the history
…script
  • Loading branch information
dutts committed Aug 28, 2015
1 parent a6f59de commit 30db428
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 102 deletions.
97 changes: 21 additions & 76 deletions chrome/addon/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ function HideElement(element) {
element.style.display = 'none';
}

function RemoveElement(elementSelector, parentElement) {
parentElement = typeof parentElement !== 'undefined' ? parentElement : document;
var el = parentElement.querySelector(elementSelector);
el.parentNode.removeChild(el);
}

function AppendImg(element, filename) {
var img = document.createElement('img');
img.src = chrome.extension.getURL(filename);
Expand Down Expand Up @@ -49,7 +55,7 @@ $(scoreFilterSlider).slider({
max: 5,
step: 1,
slide: function( event, ui ) {
ApplyFilter(ui.values, restaurantEntries);
ApplyFilter(ui.values, restaurantEntries, document.getElementById('nomorvom_config_excludeNoData_checkbox').checked);
}
});

Expand Down Expand Up @@ -82,20 +88,15 @@ config.appendChild(excludeNoDataLabel);
var restaurantsDiv = document.querySelector("div.restaurants");
restaurantsDiv.insertBefore(config, restaurantsDiv.firstChild);

/// new stuff

var port = chrome.runtime.connect({name:"scorelookup"});

// Set up the listener for the result returned from the addon script
port.onMessage.addListener(function(restaurantScore) {
//console.log("lookup: "+ restaurantScore.id + "(" + restaurantScore.rating + ")");
console.log(restaurantScore.resp);

//console.log("id " + restaurantScore.id + ", rating " + restaurantScore.rating);
// find the score placeholder for the restaurant we've got a result for
var restaurantScorePlaceholder = $("div.restaurant[data-nomorvom-id='"+restaurantScore.id+"'] div#nomorvom");
restaurantScorePlaceholder.attr("data-rating", restaurantScore.rating);
$("p#nomorvom_loading", restaurantScorePlaceholder).remove();
$("div#nomorvom_progressbar", restaurantScorePlaceholder).remove();
var restaurantScorePlaceholder = document.querySelector("div.restaurant[data-nomorvom-id='"+restaurantScore.id+"'] div#nomorvom");
restaurantScorePlaceholder.setAttribute('data-rating', restaurantScore.rating);
RemoveElement('p#nomorvom_loading', restaurantScorePlaceholder);
RemoveElement('div#nomorvom_progressbar', restaurantScorePlaceholder);

if (restaurantScore.rating > 0) {
for (var i = 0; i < restaurantScore.rating; i++) {
Expand All @@ -107,35 +108,24 @@ port.onMessage.addListener(function(restaurantScore) {
}

var resultText = document.createElement('div');
resultText.id = "hygieneScore"
resultText.style.fontWeight = "bold";
resultText.style.margin = "0px 5px";
resultText.id = "nomorvom_hygieneScore"

if (restaurantScore.rating == "AwaitingInspection") {
$(resultText).text("This takeaway is awaiting inspection");
resultText.textContent = "This takeaway is awaiting inspection";
restaurantScore.rating = 0;
}
else {
if (restaurantScore.rating == -1) {
$(resultText).text("Sorry, no food hygiene data found");
resultText.textContent = "Sorry, no food hygiene data found";
}
else {
$(resultText).text("Hygiene Score : " + restaurantScore.rating + "/5");
resultText.textContent = "Hygiene Score : " + restaurantScore.rating + "/5";
}
}
restaurantScorePlaceholder.append(resultText);
restaurantScorePlaceholder.appendChild(resultText);

// Filter accordingly
var ratingFilterRange = $(scoreFilterSlider).slider("values");
//var excludeNoData = $(excludeNoDataCheckbox).prop('checked');
//if ( ((rating == -1) && excludeNoData) || (rating < ratingFilterRange[0]) || (rating > ratingFilterRange[1]) ) {
if ((restaurantScore.rating < ratingFilterRange[0]) || (restaurantScore.rating > ratingFilterRange[1])) {
$("div.restaurant[data-nomorvom-id='"+restaurantScore.id+"']").hide();
}
else
{
$("div.restaurant[data-nomorvom-id='"+restaurantScore.id+"']").show();
}
ApplyFilter($(scoreFilterSlider).slider("values"), restaurantEntries, document.getElementById('nomorvom_config_excludeNoData_checkbox').checked);
});

var restaurantId = 0;
Expand All @@ -148,7 +138,7 @@ Array.prototype.forEach.call(restaurantEntries, function (el, i) {
port.postMessage({id:restaurantId, name:name, address:address});

var scorePlaceholder = document.createElement('div');
scorePlaceholder.id = "nomorvom"
scorePlaceholder.id = "nomorvom";

var loadingText = document.createElement('p');
loadingText.id = "nomorvom_loading";
Expand All @@ -169,54 +159,9 @@ Array.prototype.forEach.call(restaurantEntries, function (el, i) {

scorePlaceholder.setAttribute('data-rating', 0);

el.setAttribute('data-nomorvom-id', restaurantId);

el.appendChild(scorePlaceholder);

restaurantId++;
/*
var rating = 0;
$.ajax({
url: url,
type: 'GET',
dataType: 'json',
cache: false,
success: function (data, status) {
var rating = -1;
loadingText.parentNode.removeChild(loadingText);
loaderImg.parentNode.removeChild(loaderImg);
var resultText = document.createElement('div');
resultText.id = "nomorvom_hygieneScore"
if (data.establishments.length > 0) {
rating = data.establishments[0].RatingValue;
for (var i = 0; i < rating; i++) {
AppendImg(scorePlaceholder, '48-fork-and-knife-icon.png');
}
for (var i = 0; i < 5 - rating; i++) {
AppendImg(scorePlaceholder, 'toilet-paper-icon_32.png');
}
if (rating == "AwaitingInspection") {
resultText.textContent = "This takeaway is awaiting inspection";
rating = 0;
}
else {
resultText.textContent = "Hygiene Score : " + rating + "/5";
}
}
else {
resultText.textContent = "Sorry, no food hygiene data found";
rating = -1;
}
scorePlaceholder.appendChild(resultText);
scorePlaceholder.setAttribute('data-rating', rating);
ApplyFilter($(scoreFilterSlider).slider("values"), restaurantEntries, document.getElementById('nomorvom_config_excludeNoData_checkbox').checked);
},
error: function (error) { },
beforeSend: function (xhr) { xhr.setRequestHeader('x-api-version', 2); }
});
*/
});
40 changes: 15 additions & 25 deletions chrome/addon/background.js
Original file line number Diff line number Diff line change
@@ -1,40 +1,30 @@
chrome.runtime.onConnect.addListener(function(port){
console.assert(port.name == "scorelookup");
port.onMessage.addListener(function(msg) {
console.log("foo");
//port.postMessage({resp:msg});
port.onMessage.addListener(function(restaurant) {

var url = "http://api.ratings.food.gov.uk/Establishments?name=" + encodeURIComponent(restaurant.name) + "&address=" + encodeURIComponent(restaurant.address);

var url = "https://api.ratings.food.gov.uk/Establishments?name=" + encodeURIComponent(msg.name) + "&address=" + encodeURIComponent(msg.address);
var rating = 0;

var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function() {
if (xhr.readyStste == 4)
if (xhr.readyState == 4)
{
var resp = JSON.parse(xhr.responseText);
port.postMessage({resp:resp});
//console.log(resp);
if (resp.establishments.length > 0) {
rating = resp.establishments[0].RatingValue;
}
else {
rating = -1;
}
console.log(restaurant.id + " " + rating);
port.postMessage({id:restaurant.id, rating:rating});
}
};
xhr.open("GET", url, true);
xhr.setRequestHeader('x-api-version', 2);
xhr.setRequestHeader('Content-Type','application/json');
xhr.setRequestHeader('Accept','application/json');
xhr.send();
/*
var Request = require("sdk/request").Request;
var foodLookupRequest = Request({
url: url,
headers: {'x-api-version':2, 'Content-Type':'application/json', 'Accept':'application/json'},
onComplete: function (response) {
if (response.json != null) {
if (response.json.establishments.length > 0) {
rating = response.json.establishments[0].RatingValue;
}
else {
rating = -1;
}
}
port.postMessage({id:restaurant.id, rating:rating});
}
});
*/
});
});
2 changes: 1 addition & 1 deletion chrome/addon/manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"manifest_version": 2,
"name": "NomOrVom",
"version": "0.14",
"version": "0.17",
"description": "How clean is your takeaway? Never eat from a dodgy takeaway again. Food.gov.uk hygiene scores in your Just Eat search results",
"author": "Richard Dutton",
"icons": { "32": "toilet-paper-icon_32.png",
Expand Down

0 comments on commit 30db428

Please sign in to comment.