Skip to content

Instantly share code, notes, and snippets.

@mageddo
Created March 16, 2016 16:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mageddo/1cca1be843c6d00d8ba5 to your computer and use it in GitHub Desktop.
Save mageddo/1cca1be843c6d00d8ba5 to your computer and use it in GitHub Desktop.
Notificador de status da entrega correios
(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