From 30db4280ea37bdca236d3c80f992cd000d1b389b Mon Sep 17 00:00:00 2001 From: dutts Date: Fri, 28 Aug 2015 23:22:55 +0100 Subject: [PATCH] https support done, all requests to food.gov.uk made from background script --- chrome/addon/api.js | 97 +++++++++----------------------------- chrome/addon/background.js | 40 ++++++---------- chrome/addon/manifest.json | 2 +- 3 files changed, 37 insertions(+), 102 deletions(-) diff --git a/chrome/addon/api.js b/chrome/addon/api.js index f95e6da..e273393 100644 --- a/chrome/addon/api.js +++ b/chrome/addon/api.js @@ -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); @@ -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); } }); @@ -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++) { @@ -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; @@ -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"; @@ -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); } - }); -*/ }); \ No newline at end of file diff --git a/chrome/addon/background.js b/chrome/addon/background.js index af0c2df..dcc90ef 100644 --- a/chrome/addon/background.js +++ b/chrome/addon/background.js @@ -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}); - } - }); -*/ }); }); \ No newline at end of file diff --git a/chrome/addon/manifest.json b/chrome/addon/manifest.json index f3d636c..620fe42 100644 --- a/chrome/addon/manifest.json +++ b/chrome/addon/manifest.json @@ -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",