Advertisement
Guest User

Untitled

a guest
May 21st, 2019
65
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.                 if(percentageVariation >= 0.10){
  25.                     new Notification(`Variação do XBT: ${percentageVariation}`);
  26.                 }
  27.                 previousPrice = currentPrice; // after used current turn previous.
  28.             }, time);
  29.         })
  30.     }
  31.  
  32.     function stop(){
  33.         clearInterval(intervalId);
  34.     }
  35.  
  36.     return {
  37.         run,
  38.         stop
  39.     }
  40. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement