Created
March 16, 2016 16:33
-
-
Save mageddo/1cca1be843c6d00d8ba5 to your computer and use it in GitHub Desktop.
Notificador de status da entrega correios
This file contains 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($){ | |
var codigo = 'PI861904134BR'; | |
var ultimoRastreio; | |
permissionsNotificar(); | |
acompanharRastreio(); | |
function acompanharRastreio(){ | |
window.intervalo = setInterval(() => { | |
rastrear(codigo, (err, data) => { | |
if(err){ | |
return console.error(err); | |
} | |
var o = data[data.length -1]; | |
if(ultimoRastreio){ | |
if(data.length != ultimoRastreio.length){ | |
notificar(o.data, o.detalhes); | |
} | |
} | |
console.debug('novo status: ' + o.data, o); | |
// notificar(o.data, o.detalhes); | |
ultimoRastreio = data; | |
}); | |
}, /*10 * 1000*/10 * 60 * 1000); | |
} | |
function rastrear(codigo, callback){ | |
$.ajax({ | |
url: 'http://developers.agenciaideias.com.br/correios/rastreamento/json/' + codigo, | |
dataType: 'json', | |
success(data){ | |
callback(null, data); | |
}, | |
error(err){ | |
callback(err); | |
} | |
}) | |
}; | |
function notificar(title, message){ | |
if (!Notification) { | |
alert('Desktop notifications not available in your browser. Try Chromium.'); | |
return; | |
} | |
if (Notification.permission !== "granted") | |
Notification.requestPermission(); | |
else { | |
var notification = new Notification(title, { | |
icon: 'http://www.cursocenpre.com.br/wp-content/uploads/2015/07/CORREIOS.jpg', | |
body: message, | |
}); | |
} | |
} | |
function permissionsNotificar(){ | |
if (Notification.permission !== "granted") | |
Notification.requestPermission(); | |
} | |
})(jQuery); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment