-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackground.js
34 lines (31 loc) · 1.05 KB
/
background.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
chrome.runtime.onInstalled.addListener(function(){
chrome.storage.local.set({status: 0}, function(innerObj){
chrome.storage.local.get(['status'], function(storageObj){
console.log('intial status is ', storageObj)
})
})
})
function getScores(request, sender, sendResponse){
fetch(`http://api.ratings.food.gov.uk/Establishments?name=${request.name}&address=${request.postCode}`, {
method: 'GET',
headers: {
"X-API-VERSION": 2,
"crossDomain": true,
}
})
.then(response => response.json())
.then(json => {
if (json.establishments[0]) {
sendResponse({score: json.establishments[0].RatingValue, id: request.id});
} else {
sendResponse({score: "Not yet scored", id: request.id});
}
})
}
chrome.runtime.onMessage.addListener(
function(request, sender, sendResponse) {
if (request.type == "fetch") {
getScores(request, sender, sendResponse)
return true
}
});