Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function XBTNotification(){
- let previousPrice = null;
- let intervalId = null;
- function getCurrentPrice() {
- const currentPriceSpan = document.querySelector('span.priceWidget') || {};
- return parseFloat(currentPriceSpan.textContent || 0);
- }
- function priceComparatorPercent(previousPrice, currentPrice) {
- return (((currentPrice * 100) / previousPrice) - 100) * -1;
- }
- function run(time){
- return Notification.requestPermission()
- .then(permission => {
- if(permission !== 'granted'){
- return alert('Sem permissão não dá pra notificar...');
- }
- previousPrice = getCurrentPrice(); // for first call...
- intervalId = setInterval(() => {
- const currentPrice = getCurrentPrice();
- const percentageVariation = priceComparatorPercent(previousPrice, currentPrice);
- new Notification(`Variação do XBT: ${percentageVariation}`);
- previousPrice = currentPrice; // after used current turn previous.
- }, time);
- })
- }
- function stop(){
- clearInterval(intervalId);
- }
- return {
- run,
- stop
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement