Advertisement
Guest User

Untitled

a guest
May 21st, 2019
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.43 KB | None | 0 0
  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. stop();
  16. return Notification.requestPermission()
  17. .then(permission => {
  18. if(permission !== 'granted'){
  19. return alert('Sem permissão não dá pra notificar...');
  20. }
  21. previousPrice = getCurrentPrice(); // for first call...
  22. intervalId = setInterval(() => {
  23. const currentPrice = getCurrentPrice();
  24. const percentageVariation = priceComparatorPercent(previousPrice, currentPrice);
  25. if(percentageVariation >= 0.10 || percentageVariation <= 0.10){
  26. new Notification(`Variação do XBT: ${percentageVariation}`);
  27. }
  28. previousPrice = currentPrice; // after used current turn previous.
  29. }, time);
  30. })
  31. }
  32.  
  33. function stop(){
  34. if(intervalId){
  35. clearInterval(intervalId);
  36. }
  37. }
  38.  
  39. return {
  40. run,
  41. stop
  42. }
  43. }
  44.  
  45. const xbt = XBTNotification();
  46. xbt.run(3000);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement