-
Notifications
You must be signed in to change notification settings - Fork 31
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
Add Dynamic Cryptocurrency Selection and Display #46
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,7 +9,7 @@ | |
|
||
<meta | ||
name="description" | ||
content="Replace your browser New Tab page with a Bitcoin price chart" | ||
content="Replace your browser New Tab page with a Crypto price chart" | ||
/> | ||
<meta name="image" content="https://i.imgur.com/pHG5fBk.jpg" /> | ||
@@socialMediaTags | ||
|
@@ -80,8 +80,13 @@ <h1 id="clock" class="time mb+ text-center mt">-:-</h1> | |
</a> | ||
</div> | ||
|
||
<div class="toggle-switch text-center mb+"> | ||
<label> <input type="radio" name="crypto" value="bitcoin" checked /> Bitcoin </label> | ||
<label> <input type="radio" name="crypto" value="ethereum" /> Ethereum </label> | ||
</div> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I see your point, and I tend to like it at one side, but on another - I prefer much more to see this in the future #54 , instead as a playable call-to-action here. |
||
|
||
<h2 class="chart-period mb text-center"> | ||
Bitcoin price is | ||
<span id="crypto-type">Crypto</span> price is | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
<span id="price-now">...</span> | ||
<span id="change"></span> | ||
</h2> | ||
|
@@ -123,6 +128,7 @@ <h2 class="chart-period mb text-center"> | |
<a href="https://github.com/superKalo/crypto-tab">Open Source</a> | ||
| | ||
<a href="https://github.com/cmihaylov/bitcoin-price-api">API</a> | ||
<!-- TODO: Change the source to the new API --> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The new API is in another repo? |
||
</small> | ||
</small> | ||
</p> | ||
|
@@ -145,10 +151,11 @@ <h2 class="chart-period mb text-center"> | |
<script src="js/message.js"></script> | ||
<script src="js/loader.js"></script> | ||
<script src="js/apiCecoAdapter.js"></script> | ||
<script src="js/apiBoyoAdapter.js"></script> | ||
<script src="js/api.js"></script> | ||
<script src="js/chart.js"></script> | ||
<script src="js/clock.js"></script> | ||
<script src="js/bitcoin.js"></script> | ||
<script src="js/crypto.js"></script> | ||
<script src="js/script.js"></script> | ||
</body> | ||
</html> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
window.App.apiBoyoAdapter = { | ||
mapData: function (response, dateLabelFormat) { | ||
return response | ||
.map((_rec) => ({ | ||
value: _rec.value, | ||
timestamp: dayjs | ||
.utc(_rec.timestamp * 1000) | ||
.local() | ||
.format(dateLabelFormat), | ||
})) | ||
.reverse(); | ||
}, | ||
|
||
getCryptoRatesForPeriod: function (period, cryptoType) { | ||
return new Promise((resolve, reject) => { | ||
chrome.runtime.sendMessage( | ||
{ type: 'getCryptoPrice', period: period, cryptoType: cryptoType }, | ||
(response) => { | ||
if (response && !response.error) { | ||
resolve(response.data); | ||
} else { | ||
reject(response.error || `Failed to retrieve ${cryptoType} price data`); | ||
} | ||
} | ||
); | ||
}); | ||
}, | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,33 @@ | ||
let bitcoinPriceData = {}; | ||
let cryptoPriceData = { | ||
bitcoin: {}, | ||
ethereum: {}, | ||
}; | ||
|
||
async function fetchBitcoinPrice(period) { | ||
async function fetchCryptoPrice(period, cryptoType) { | ||
const endpoints = { | ||
ALL: 'bitcoin/all', | ||
ONE_YEAR: 'bitcoin/year', | ||
ONE_MONTH: 'bitcoin/month', | ||
ONE_WEEK: 'bitcoin/week', | ||
ONE_DAY: 'bitcoin/day', | ||
ONE_HOUR: 'bitcoin/hour', | ||
NOW: 'bitcoin/now', | ||
ALL: `${cryptoType}/all`, | ||
ONE_YEAR: `${cryptoType}/year`, | ||
ONE_MONTH: `${cryptoType}/month`, | ||
ONE_WEEK: `${cryptoType}/week`, | ||
ONE_DAY: `${cryptoType}/day`, | ||
ONE_HOUR: `${cryptoType}/hour`, | ||
NOW: `${cryptoType}/now`, | ||
}; | ||
|
||
try { | ||
const response = await fetch(`https://api.crypto-tab.com/v1/${endpoints[period]}`); | ||
const response = await fetch(`http://localhost:3000/v1/${endpoints[period]}`); // TODO: Update to use the deployed API (maybe use a config file to store the base URL) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🙄 |
||
const data = await response.json(); | ||
bitcoinPriceData[period] = data; | ||
cryptoPriceData[cryptoType][period] = data; | ||
} catch (error) { | ||
console.error(`Error fetching Bitcoin price for ${period}:`, error); | ||
console.error(`Error fetching ${cryptoType} price for ${period}:`, error); | ||
} | ||
} | ||
|
||
chrome.runtime.onMessage.addListener((request, sender, sendResponse) => { | ||
if (request.type === 'getBitcoinPrice') { | ||
const period = request.period; | ||
fetchBitcoinPrice(period).then(() => { | ||
sendResponse({ data: bitcoinPriceData[period], cached: false }); | ||
if (request.type === 'getCryptoPrice') { | ||
const { period, cryptoType } = request; | ||
fetchCryptoPrice(period, cryptoType).then(() => { | ||
sendResponse({ data: cryptoPriceData[cryptoType][period], cached: false }); | ||
}); | ||
return true; // Indicates that the response will be sent asynchronously | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would be great to display Bitcoin or Ethereum in here.