Last active
August 15, 2016 00:16
-
-
Save Telematica/22a87a94adc38152b362e400b661237c to your computer and use it in GitHub Desktop.
Bitso Web Desktop Notifications
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;(function(window) { | |
Notification | |
.requestPermission() | |
.then(function(result) { | |
if ('granted' === result) { | |
openIframe(); | |
window.timer = setInterval( | |
function() { | |
var change = '', | |
operator = '', | |
prices = [ | |
{ | |
type : document.querySelector('title').textContent.match(/\(\d+.*\)/)[0].match(/BTC|ETH/)[0], | |
price : parseFloat(document.querySelector('title').textContent.match(/\(\d+.*\)/)[0].replace(/\(|\)|\/|BTC|ETH/g,'')) | |
}, | |
{ | |
type : parent.frames[0].document.title.match(/\(\d+.*\)/)[0].match(/BTC|ETH/)[0], | |
price : parseFloat(parent.frames[0].document.title.match(/\(\d+.*\)/)[0].replace(/\(|\)|\/|BTC|ETH/g,'')) | |
} | |
]; | |
for(price in prices) { | |
if (undefined === window[prices[price].type]) { | |
window[prices[price].type] = prices[price].price; | |
} else { | |
if (prices[price].price < window[prices[price].type] ) { | |
change = 'plunged'; | |
operator = '-'; | |
} else if (prices[price].price > window[prices[price].type] ) { | |
change = 'surged'; | |
operator = '+'; | |
} | |
} | |
if (change) { | |
var time = new Date().toString().match(/\d+:\d+:\d+/), | |
diff = window[prices[price].type] - prices[price].price, | |
options = { | |
icon: 'https://bitso.com/assets/images/b/logo.png', | |
requireInteraction: true | |
}; | |
//Cache Previous price | |
window[prices[price].type] = prices[price].price; | |
var N = new Notification( | |
prices[price].type + ' price has ' + change + ': (' + time + ') ' + window[prices[price].type] + ' ' + operator + diff, options | |
).onclick = function(){ | |
window.focus(); | |
this.close(); | |
}; | |
return N; | |
} | |
} | |
}, | |
100 | |
); | |
} | |
}); | |
function openIframe() { | |
var main = document.getElementById('main'), | |
container = main.getElementsByClassName('container')[0], | |
iframe = document.createElement('iframe'); | |
iframe.src = 'https://bitso.com/trade/market/eth/mxn'; | |
main.insertBefore(iframe, container); | |
} | |
})(window); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment