-
Notifications
You must be signed in to change notification settings - Fork 0
/
yung_gif.js
106 lines (88 loc) · 3.47 KB
/
yung_gif.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
/*
Yung GIF
Check latest 25 /r/gifs and reminds you when there's something new, productivity tool.
*/
function Yung_GIF(){
this.icons = {'one': 'crapicon.png', 'omg': 'omgicon.png', 'sad': 'sadboy.png'};
this.seen = new Pingu(1000, 'seen');
this.unseen = new Pingu(25, 'unseen');
this.running = null;
// Initialize settings for a new install
if(!localStorage['YUNG_GIF_sources']){
localStorage['YUNG_GIF_sources'] = '{"your_first_source":{"url":"/r/gifs","limit":25}}';
}
}
// Initialize all the events
Yung_GIF.prototype.watch = function(){
// If icon is clicked, check for new and show.
chrome.browserAction.onClicked.addListener(function(){
window.YUNG_GIF.check_new();
window.YUNG_GIF.bruk();
});
// Set alarm to check for new gifs every 5 minutes
chrome.alarms.create("chckn", {periodInMinutes: 5.0});
chrome.alarms.onAlarm.addListener(function(alarm){
if(alarm.name == "chckn"){
window.YUNG_GIF.check_new();
}
});
// A listener to check YUNG GIFwuuut
chrome.runtime.onMessage.addListener(function(message){
if(message == "checknow!"){
window.YUNG_GIF.check_new();
}
});
};
// bruk() shows unseen gifs.
Yung_GIF.prototype.bruk = function(){
while(this.unseen.length > 0){
gif_url = this.unseen.pop();
chrome.tabs.create({url: gif_url});
this.seen.unshift(gif_url);
}
chrome.browserAction.setIcon({path: this.icons.one});
chrome.browserAction.setTitle({title: "wait more..."});
chrome.browserAction.setBadgeText({text: ""});
}
// Check Reddit for new content and notify if that's the case
Yung_GIF.prototype.check_new = function(){
var me = this;
var sources = JSON.parse(localStorage['YUNG_GIF_sources']);
// Update the sizes of our Pingu
me.unseen.max_size = 0;
$.each(sources,function(i, source){
me.unseen.max_size += parseInt(source.limit);
});
me.unseen.resize();
$.each(sources, function(i, source){
var le_url = 'http://api.reddit.com'+source.url+"?limit="+source.limit
$.getJSON(le_url, function(yolo){
// keep the hottest at the beginning so if new GIFs are added, hottest ones stay
$.each(yolo.data.children.reverse(), function(i,e){
if(me.seen.concat(me.unseen).indexOf(e.data.url) == -1) {
me.unseen.unshift(e.data.url);
}
});
// is there something new?
if(me.unseen.length > 0){
chrome.browserAction.setIcon({path: me.icons.omg});
chrome.browserAction.setTitle({title: "NEW GIFS FOR U"});
chrome.browserAction.setBadgeText({text: ""+me.unseen.length});
}else{
chrome.browserAction.setIcon({path: me.icons.one});
chrome.browserAction.setTitle({title: "wait more..."});
chrome.browserAction.setBadgeText({text: ""});
}
console.log("Checked for new stuff on "+le_url+" at "+Date());
}).error(function(){
chrome.browserAction.setIcon({path: me.icons.sad});
chrome.browserAction.setTitle({title: "Cannot connect to "+le_url+"."});
console.log("Failed to check for new stuff on "+le_url+" at "+Date());
});
});
}
window.YUNG_GIF = new Yung_GIF();
window.YUNG_GIF.watch()
console.log("Initialized Yung GIF.");
// Check one time at the start
window.YUNG_GIF.check_new();