Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding data refresh frequency control, Upper and lower price notice #33

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file modified LICENSE
100644 → 100755
Empty file.
Empty file modified README.md
100644 → 100755
Empty file.
Empty file modified _locales/az_AZ/messages.json
100644 → 100755
Empty file.
Empty file modified _locales/de_DE/messages.json
100644 → 100755
Empty file.
2 changes: 1 addition & 1 deletion _locales/en_US/messages.json
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"badgeTooltip": {
"message": "1 bitcoin is worth $1 today",
"message": "1 bitcoin is worth $1",
"description": "Text that shows up when the mouse is hovering over the add-on's button in the browser's toolbar."
},
"extensionDescription": {
Expand Down
Empty file modified _locales/es_ES/messages.json
100644 → 100755
Empty file.
Empty file modified _locales/fa_IR/messages.json
100644 → 100755
Empty file.
Empty file modified _locales/fr_FR/messages.json
100644 → 100755
Empty file.
Empty file modified _locales/id_ID/messages.json
100644 → 100755
Empty file.
Empty file modified _locales/it_IT/messages.json
100644 → 100755
Empty file.
Empty file modified _locales/kk_KZ/messages.json
100644 → 100755
Empty file.
Empty file modified _locales/nl_NL/messages.json
100644 → 100755
Empty file.
Empty file modified _locales/pt_BR/messages.json
100644 → 100755
Empty file.
Empty file modified _locales/ru_RU/messages.json
100644 → 100755
Empty file.
Empty file modified _locales/uk_UA/messages.json
100644 → 100755
Empty file.
Empty file modified crowdin.yml
100644 → 100755
Empty file.
Empty file modified icons/icon-128.png
100644 → 100755
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Empty file modified icons/icon-16.png
100644 → 100755
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Empty file modified icons/icon-32.png
100644 → 100755
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Empty file modified icons/icon-48.png
100644 → 100755
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Empty file modified icons/icon-64.png
100644 → 100755
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Empty file modified icons/icon.svg
100644 → 100755
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion manifest.json
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"manifest_version": 2,
"name": "Yet Another Bitcoin Price Ticker",
"short_name": "YABPT",
"version": "1.5.3",
"version": "1.5.5",
"description": "__MSG_extensionDescription__",
"icons": {
"16": "icons/icon-16.png",
Expand Down
Empty file modified options/css/countrySelect.min.css
100644 → 100755
Empty file.
Empty file modified options/css/options.css
100644 → 100755
Empty file.
Empty file modified options/images/flags.png
100644 → 100755
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Empty file modified options/js/countrySelect.js
100644 → 100755
Empty file.
Empty file modified options/js/jquery-3.3.1.slim.min.js
100644 → 100755
Empty file.
33 changes: 25 additions & 8 deletions options/js/options.js
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,23 @@
var currencies = $.fn.countrySelect.getCountryData();

function saveOptions() {
setOption('currency', $('#currency').countrySelect('getSelectedCountryData').currency);
}
var option = {
currency: $('#currency').countrySelect('getSelectedCountryData').currency,
frequency: document.getElementById('frequency').value,
upperLimit: document.getElementById('upperLimit').value,
lowerLimit: document.getElementById('lowerLimit').value
}

setOption(option);

function setOption(option, value) {
var yabpt = {};
yabpt[option] = value;
}

function setOption(option) {
chrome.storage.local.set({
yabpt: yabpt
currency: option['currency'],
frequency: option['frequency'],
upperLimit: option['upperLimit'],
lowerLimit: option['lowerLimit']
});
}

Expand All @@ -32,20 +40,29 @@
desc.text(chrome.i18n.getMessage('option_' + pref + '_summary'));
});

chrome.storage.local.get('yabpt', function (res) {
chrome.storage.local.get('currency', function (res) {
var selectedCountry = 'us';
for (var i = 0; i < currencies.length; i++) {
var item = currencies[i];
if (res.yabpt.currency == item.currency) {
if (res.currency == item.currency) {
selectedCountry = item.iso2;
}
}
$('#currency').countrySelect('selectCountry', selectedCountry);
});
chrome.storage.local.get(function (res) {
document.getElementById('frequency').value = res.frequency;
document.getElementById('upperLimit').value = res.upperLimit;
document.getElementById('lowerLimit').value = res.lowerLimit;
});

}

document.addEventListener('DOMContentLoaded', restoreOptions);
$('#currency').change(function () {
saveOptions();
});
$('input').change(function () {
saveOptions();
});
})();
9 changes: 9 additions & 0 deletions options/options.html
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,15 @@
<br>
<description>Display current Bitcoin price in selected currency</description>
</label>
<div>
<span>Refresh frequency: every <input id='frequency' /> Seconds</span>
</div>
<div>
<span>Upper limit notice: reaching $ <input id='upperLimit' /> per bitcoin</span>
</div>
<div>
<span>Lower limit notice: reaching $ <input id='lowerLimit' /> per bitcoin</span>
</div>
</form>
<script src="js/options.js"></script>
</body>
Expand Down
Empty file modified package.json
100644 → 100755
Empty file.
97 changes: 77 additions & 20 deletions src/background.js
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,23 @@
(function () {
// Easy access to settings
var settings = {
currency: 'USD'
currency: 'USD',
frequency: '6',
upperLimit: '0',
lowerLimit: '0'
};

var intervals = [];


// Formats the price into currency suffixes
function formatPrice(price) {
var ranges = [
{ divider: 1e15, suffix: 'Q' },
{ divider: 1e12, suffix: 'T' },
{ divider: 1e9, suffix: 'B' },
{ divider: 1e6, suffix: 'M' },
{ divider: 1e3, suffix: 'K' }
{ divider: 1e15, suffix: 'Q' },
{ divider: 1e12, suffix: 'T' },
{ divider: 1e9, suffix: 'B' },
{ divider: 1e6, suffix: 'M' },
{ divider: 1e3, suffix: 'K' }
];

for (var i = 0; i < ranges.length; i++) {
Expand All @@ -27,30 +33,38 @@
var integerDigits = Math.floor(Math.log(intResult) * Math.LOG10E + 1);
switch (integerDigits) {
case 1:
return (Math.floor(10 * result) / 10).toFixed(1) + ranges[i].suffix;
return (Math.floor(10 * result) / 10).toFixed(1) + ranges[i].suffix;
case 2:
case 3:
default:
return Math.round(result) + ranges[i].suffix;
return Math.round(result) + ranges[i].suffix;
}
}
}
return price.toString();
}

function updateBadgeText(price) {
var refreshTime = getNowFormatDate();
var badgeText = formatPrice(price);
if (price>settings.upperLimit&&settings.upperLimit!='0') {
browser.tabs.create({url: "https://blockchain.info/"});
} else if (price<settings.lowerLimit&&settings.lowerLimit!='0') {
browser.tabs.create({url: "https://blockchain.info/"});
}


chrome.browserAction.setBadgeText({
text: badgeText
});


chrome.browserAction.setTitle({
title: chrome.i18n.getMessage("badgeTooltip", price.toLocaleString(navigator.language, {
currency: settings.currency,
style: 'currency',
currencyDisplay: 'symbol'
}))
})+ ' at '+refreshTime)
});
}

Expand All @@ -68,17 +82,15 @@
xhr.send();
}

function setupInterval() {
window.setInterval(function () {
updateBadge();
}, 600000);
}

function setupStorage() {
chrome.storage.local.get(['yabpt'], function (prefs) {
chrome.storage.local.get(function (prefs) {
if (prefs.yabpt === undefined) {
chrome.storage.local.set({
yabpt: settings
currency: settings.currency,
frequency: settings.frequency,
upperLimit: settings.upperLimit,
lowerLimit: settings.lowerLimit
});
}
});
Expand All @@ -102,19 +114,64 @@
}

function getCurrencyFromStorage() {
chrome.storage.local.get(['yabpt'], function (prefs) {
settings = prefs.yabpt;
chrome.storage.local.get(function (prefs) {
settings = {
currency: prefs.currency,
frequency: prefs.frequency,
upperLimit: prefs.upperLimit,
lowerLimit: prefs.lowerLimit
};
updateBadge();

intervals.forEach(clearInterval);
seconds = settings.frequency;
seconds = Number(seconds)*1000;
refreshSet = '( Every '+settings.frequency+' Seconds)';
intervalID = window.setInterval(function () {
updateBadge();
}, seconds);
intervals.push(intervalID);
});

}

function onStorageChanged() {
getCurrencyFromStorage();
}



function getNowFormatDate() {
    var date = new Date();
    var seperator1 = "-";
    var seperator2 = ":";
    var month = date.getMonth() + 1;
    var strDate = date.getDate();
var hours = date.getHours();
var minutes = date.getMinutes();
var seconds = date.getSeconds();
    if (month >= 1 && month <= 9) {
        month = "0" + month;
    }
if (hours >= 1 && hours <= 9) {
        hours = "0" + hours;
    }
if (minutes >= 1 && minutes <= 9) {
        minutes = "0" + minutes;
    }
if (seconds >= 1 && seconds <= 9) {
        seconds = "0" + seconds;
    }
    if (strDate >= 0 && strDate <= 9) {
        strDate = "0" + strDate;
    }
    var currentdate = date.getFullYear() + seperator1 + month + seperator1 + strDate
            + " " + hours + seperator2 + minutes
            + seperator2 + seconds;
    return currentdate;
}

setupBadge();
setupStorage();
getCurrencyFromStorage();
setupInterval();

})();
Empty file modified web-ext-config.js
100644 → 100755
Empty file.