Skip to content

Commit

Permalink
Upgraded to manifest format 2
Browse files Browse the repository at this point in the history
  • Loading branch information
Adrian Smith committed Jun 11, 2013
1 parent 15cbe95 commit 6e9d57b
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 77 deletions.
74 changes: 0 additions & 74 deletions background.html

This file was deleted.

65 changes: 65 additions & 0 deletions background.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
const GBP_TO_EUR_RATE = 'gbp_to_eur_rate';
const DATE_LAST_RETRIEVED = 'date_rate_last_retrieved';

function todayAsString() {
var todayDate = new Date();
return todayDate.getDate() + '/' + todayDate.getMonth() + '/' + todayDate.getFullYear();
}

/**
* Fetch the latest GBP to EUR exchange rate by issuing a GET request to
* http://download.finance.yahoo.com
*
* @param callback Function If the response from fetching url has a
* HTTP status of 200, this function is called with a JSON decoded
* response. Otherwise, this function is called with null.
*/
function fetchExchangeRate(callback) {
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function(data) {
if (xhr.readyState == 4) {
if (xhr.status == 200) {
console.log('xhr.responseText - ' + xhr.responseText);
var rate = parseFloat(xhr.responseText.replace(/[\r\n]/g, ""));
console.log('parsed rate - ' + rate);
// The amount added to rate below is to account for a
// difference between the GBP to EURO rate retrieved from
// Yahoo and the one Amazon will use. It's not exact but
// reflects an average markup Amazon applies to the open
// market rate
rate = rate + 0.04;
gbpToEurRate = rate;
localStorage.setItem(GBP_TO_EUR_RATE, rate.toString());
localStorage.setItem(DATE_LAST_RETRIEVED, todayAsString());
callback(gbpToEurRate);
} else {
callback(null);
}
}
}

var url = 'http://download.finance.yahoo.com/d/quotes.csv?s=GBPEUR=X&f=l1&e=.csv';
xhr.open('GET', url, true);
xhr.send();
};

/**
* Handles data sent via chrome.extension.sendRequest().
* @param request Object Data sent in the request.
* @param sender Object Origin of the request.
* @param callback Function The method to call when the request completes.
*/
function onMessage(message, sender, callback) {
if (message == 'fetchExchangeRate') {
var gbpToEurRate = localStorage.getItem(GBP_TO_EUR_RATE);
var dateRateLastRetrieved = localStorage.getItem(DATE_LAST_RETRIEVED);
if (gbpToEurRate == null || dateRateLastRetrieved != todayAsString()) {
fetchExchangeRate(callback);
} else {
callback(gbpToEurRate);
}
}
};

// Wire up the listener
chrome.runtime.onMessage.addListener(onMessage);
2 changes: 1 addition & 1 deletion contentscript.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,5 +111,5 @@
}

// Send a message back to the extenstion to retrieve the latest GBP to EUR exchange rate
chrome.extension.sendRequest({'action' : 'fetchExchangeRate'}, updatePageWithIrishPrice);
chrome.runtime.sendMessage('fetchExchangeRate', updatePageWithIrishPrice);
})();
8 changes: 6 additions & 2 deletions manifest.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"manifest_version": 2,
"name": "Amazon Prices for Ireland",
"version": "1.7",
"version": "1.8",
"description": "Decorate product pages on amazon.co.uk with the equivalent Irish price. This price will be in euro and include the Irish VAT rate.",
"icons": {
"128": "images/logo-128.png",
Expand All @@ -10,7 +11,10 @@
"permissions": [
"http://download.finance.yahoo.com/*"
],
"background_page" : "background.html",
"background": {
"scripts": [ "background.js" ],
"persistent": false
},
"content_scripts": [
{
"matches": ["http://www.amazon.co.uk/*",
Expand Down

0 comments on commit 6e9d57b

Please sign in to comment.