/
background.js
84 lines (78 loc) · 2.58 KB
/
background.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
// ao lançar a app
chrome.app.runtime.onLaunched.addListener(openTab);
chrome.app.runtime.onRestarted.addListener(openTab);
function openTab() {
window.open('window.html', '_rcWindow');
}
// recuperar a última box escolhida
chrome.storage.sync.get('lastBox', function(opt) {
if (opt.lastBox) {
// há uma box selecionada... não é preciso procurar
BOX.init({ id : opt.lastBox, });
} else {
findBoxes(function(boxes) {
if (!(boxes && boxes.length)) return;
// usemos a primeira box para começar
BOX.init({ id : boxes[0] });
});
}
});
// tratar dos alarmes
chrome.alarms.create('scheduler', { delayInMinutes : 1 , periodInMinutes : 5 });
chrome.alarms.onAlarm.addListener(function(alarm) {
if (alarm.name == 'scheduler') {
chrome.storage.sync.get({favorites:[]}, function(opt) {
scheduleAlarms(opt.favorites);
});
} else {
var prog = BOX.getEvent(parseInt(alarm.name));
handleStartProgram(prog);
}
});
function scheduleAlarms(favorites) {
var favChannels = [];
$.each(favorites, function(idx,favorite) {
favChannels.push(favorite.channel);
});
BOX.getEPG(function(prog) {
$.each(favorites, function(idx,favorite) {
if (getName(prog) == favorite.name && prog.channel.locator == favorite.channel.locator) {
loading = false;
if (!prog.image) {
loadImageAsBlob(prog.imageSrc, function(blobSrc) {
prog.image = blobSrc;
chrome.alarms.create(prog.id + '', { when : prog.startDate.getTime() });
});
} else {
chrome.alarms.create(prog.id + '', { when : prog.startDate.getTime() });
}
}
});
}, favChannels);
}
// mostrar notificação
function handleStartProgram(prog) {
play('notify.mp3');
var secondsLeft = (prog.startDate.getTime() - new Date())/1000;
var desc = '';
if (prog.seasonNumber || prog.episodeNumber) {
desc += prog.name + ' (T' + prog.seasonNumber + ' E' + prog.episodeNumber + ')';
}
var notifOpts = {
type: 'basic', title: getName(prog), message: desc ? desc : 'NOS IRIS Control Panel',
iconUrl: prog.image ? prog.image : 'tv.png', eventTime : 8000,
buttons : [ { title : 'Ver ' + prog.channel.name , iconUrl : 'tv.png' } ]
};
if (secondsLeft < 0) { // já começou a dar!
notifOpts.type = 'progress';
notifOpts.progress = Math.round((-secondsLeft/prog.duration) * 100);
return; // fix
}
var dateObj = new Date();
chrome.notifications.create(prog.id + '', notifOpts, function(x) {});
console.log(prog, notifOpts, notId);
}
chrome.notifications.onButtonClicked.addListener(function(notificationId, buttonIndex) {
var prog = BOX.getEvent(parseInt(notificationId));
BOX.setCh(prog.channel);
});