Advertisement
Guest User

Untitled

a guest
May 21st, 2019
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function XBTNotification(){
  2.     let previousPrice = null;
  3.     let intervalId = null;
  4.  
  5.     function getCurrentPrice() {
  6.         const currentPriceSpan = document.querySelector('span.priceWidget') || {};
  7.         return parseFloat(currentPriceSpan.textContent || 0);
  8.     }
  9.    
  10.     function priceComparatorPercent(previousPrice, currentPrice) {
  11.         return (((currentPrice * 100) / previousPrice) - 100) * -1;
  12.     }
  13.  
  14.     function run(time){
  15.         return Notification.requestPermission()
  16.         .then(permission => {
  17.             if(permission !== 'granted'){
  18.                 return alert('Sem permissão não dá pra notificar...');
  19.             }
  20.             previousPrice = getCurrentPrice(); // for first call...
  21.             intervalId = setInterval(() => {
  22.                 const currentPrice = getCurrentPrice();
  23.                 const percentageVariation = priceComparatorPercent(previousPrice, currentPrice);
  24.                 new Notification(`Variação do XBT: ${percentageVariation}`);
  25.                 previousPrice = currentPrice; // after used current turn previous.
  26.             }, time);
  27.         })
  28.     }
  29.  
  30.     function stop(){
  31.         clearInterval(intervalId);
  32.     }
  33.  
  34.     return {
  35.         run,
  36.         stop
  37.     }
  38. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement