Skip to content

Commit

Permalink
Set Bitwage as default
Browse files Browse the repository at this point in the history
  • Loading branch information
danielfc committed May 24, 2017
1 parent 26eae9d commit db7a1ab
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 28 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,5 @@ test/bower_components
package
app/scripts
.DS_Store
*.iml
/.idea
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ Livereload:
$ gulp watch
```

## Issues

The current `gulp package` command doesn't append all the background scripts such as jQuery.

## License

MIT (c) Kalnee. See [LICENSE](https://github.com/kalnee/btcbrl/blob/master/LICENSE.md) for details.
4 changes: 2 additions & 2 deletions app/manifest.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"name": "BTCBRL",
"version": "2.1.0",
"version": "2.1.1",
"manifest_version": 2,
"description": "This extension shows the current value of the BTCBRL (buy) / BTCUSD (sell) from different exchanges (Default FoxBit).",
"description": "This extension shows the current value of the BTCBRL (buy) / BTCUSD (sell) from different exchanges (Default Bitwage).",
"default_locale": "en",
"browser_action": {
"default_icon": "images/icon.png",
Expand Down
60 changes: 34 additions & 26 deletions app/scripts.babel/background.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
'use strict';

function request(url) {
return new Promise(function(resolve, reject) {
return new Promise(function (resolve, reject) {
var xhr = new XMLHttpRequest();
xhr.open('GET', url);
xhr.onload = function() {
xhr.onload = function () {
if (this.status >= 200 && this.status < 300) {
resolve(xhr.response);
} else {
Expand All @@ -14,7 +14,7 @@ function request(url) {
});
}
};
xhr.onerror = function() {
xhr.onerror = function () {
reject({
status: this.status,
statusText: xhr.statusText
Expand All @@ -36,48 +36,55 @@ function getBtcBrlFromBitwage() {
return request('https://www.bitwage.com/rates#historicaltickers');
}

function setRateBadge() {
var btcusd = undefined,
bitwage = undefined,
rates = {};
function setRates() {
let btcusd;
let rates = {};

getBtcUsd().then(function(res) {
getBtcUsd().then(function (res) {
btcusd = JSON.parse(res).ask;
return getBtcBrlFromFox(res);
}).then(function(res) {
}).then(function (res) {
let exchanges = JSON.parse(res).ticker_24h.exchanges;

let foxRate = exchanges.FOX.last;
let locRate = exchanges.LOC.last;
let b2uRate = exchanges.B2U.last;

let rate = (foxRate / btcusd).toFixed(2).toString().replace('.', ',');
chrome.browserAction.setBadgeText({
text: rate
});
let foxRate = exchanges.FOX ? exchanges.FOX.last : 0.0;
let locRate = exchanges.LOC ? exchanges.LOC.last : 0.0;
let b2uRate = exchanges.B2U ? exchanges.B2U.last : 0.0;

rates = {
fox: rate,
fox: (foxRate / btcusd).toFixed(2).toString().replace('.', ','),
loc: (locRate / btcusd).toFixed(2).toString().replace('.', ','),
b2u: (b2uRate / btcusd).toFixed(2).toString().replace('.', ',')
}
};

localStorage.setItem('rates', JSON.stringify(rates));
}).catch(function(err) {
}).catch(function (err) {
chrome.extension.getBackgroundPage().console.error(err);
chrome.browserAction.setBadgeText({
text: '...'
});
});
}

function setBadge() {
let rates = localStorage.getItem('rates');

if (rates) {
rates = JSON.parse(rates);
if (rates.bitwage) {
chrome.browserAction.setBadgeText({
text: rates.bitwage
});
}
}
}

function setRateFromBitwage() {
getBtcBrlFromBitwage().then(function(html) {
var usdbrl = $(html).find('[id*=\'keyUSDBRL\']').parent().next('div').text().trim().substr(-4);
getBtcBrlFromBitwage().then(function (html) {
let usdbrl = $(html).find('[id*=\'keyUSDBRL\']').parent().next('div').text().trim().substr(-4);
usdbrl = usdbrl.replace('.', ',');
console.log(`USDBRL extracted from Bitwage ${usdbrl}`);

var rates = localStorage.getItem('rates');
let rates = localStorage.getItem('rates');

if (!rates) {
rates = {
Expand All @@ -89,7 +96,8 @@ function setRateFromBitwage() {
}

localStorage.setItem('rates', JSON.stringify(rates));
}).catch(function(err) {
setBadge();
}).catch(function (err) {
chrome.extension.getBackgroundPage().console.error(err);
});
}
Expand All @@ -102,8 +110,8 @@ chrome.browserAction.setBadgeText({
});

function init() {
setRateBadge();
setRateFromBitwage();
setRates();
}

init();
Expand All @@ -113,6 +121,6 @@ chrome.alarms.create('btcbrl-alarm', {
periodInMinutes: 1
});

chrome.alarms.onAlarm.addListener(function(alarm) {
chrome.alarms.onAlarm.addListener(function (alarm) {
init();
});

0 comments on commit db7a1ab

Please sign in to comment.