-
Notifications
You must be signed in to change notification settings - Fork 0
/
background.js
62 lines (52 loc) · 1.61 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
var badgeColors = {2: "#A8A8A8", 7: "#007700", 15: "#dd7700", 25: "#dd0000"}
var streams = []
function setBadge(maxViewersCount) {
maxCount = maxViewersCount / 1000;
maxCount = Math.round(maxCount);
color = false;
for (badge in badgeColors) {
if (maxCount >= badge) {
color = badgeColors[badge]
}
}
if (color) {
chrome.browserAction.setBadgeText({text: maxCount.toString() + 'k'});
chrome.browserAction.setBadgeBackgroundColor({color: color});
}
else {
chrome.browserAction.setBadgeText({text: ''});
}
}
function prepareData(data){
var returnData = []
for (i=0; i<data.streams.length; i++) {
stream = data.streams[i];
returnData.push(
{
channel: stream.channel.display_name,
viewers: stream.viewers,
img: "http://static-cdn.jtvnw.net/previews/"+stream.name+"-320x240.jpg",
title: stream.channel.status,
url: stream.channel.url
})
}
return returnData;
}
function getStreams() {
$.get('https://api.twitch.tv/kraken/streams',
{ game: "StarCraft II: Wings of Liberty", limit: 10 },
function(data) {
streams = prepareData(data);
setBadge(streams[0].viewers);
})
}
chrome.extension.onMessage.addListener(function(request, sender, sendResponse) {
if (request.getStreams) {
console.log("got request: getStreams");
sendResponse(streams.slice(0, parseInt(request.getStreams)));
}
});
$(document).ready( function() {
getStreams();
setInterval(getStreams, 60000);
});