-
Notifications
You must be signed in to change notification settings - Fork 0
/
nowthischrome.js
129 lines (98 loc) · 2.78 KB
/
nowthischrome.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/****************************
* BEGIN Helper functions
****************************/
var formatPostDate = function(timestamp) {
var date = new Date(timestamp);
return date.getHours() + ':' + ('0' + date.getMinutes()).slice(-2) + ' ' + (date.getMonth() + 1) + '/' + date.getDate() + '/' + date.getFullYear()
}
var readPosts = function() {
return JSON.parse(localStorage.posts);
}
var updateBadge = function() {
if(count = readPosts().length) {
chrome.browserAction.setBadgeText({text: '' + count});
} else {
chrome.browserAction.setBadgeText({text: ''});
}
return true;
}
var saveLastUpdate = function() {
var now = new Date().getTime();
localStorage.lastUpdate = now;
localStorage.lastUpdateFormatted = formatPostDate(now);
return true;
}
var savePosts = function(posts, shouldSort) {
if(typeof shouldSort == 'undefined') { shouldSort = true; }
localStorage.posts = JSON.stringify(posts);
shouldSort && sortPosts();
updateBadge();
return true;
}
var sortPosts = function() {
var posts = readPosts();
posts.sort(function(a, b) {
if(a.created_ts < b.created_ts) return -1;
if(a.created_ts > b.created_ts) return 1;
return 0;
});
savePosts(posts, false);
return true;
}
var reset = function() {
localStorage.clear();
savePosts([]);
return true;
}
/****************************
* END Helper functions
****************************/
/****************************
* BEGIN Core functions
****************************/
var checkNewPosts = function() {
var url = 'https://www.rebelmouse.com/core/frontpage/new_posts/?site_id=40552'
if(localStorage.lastPost) {
url += '&since_id=' + localStorage.lastPost;
}
jx.load(url, function(data) {
if(data.new_posts_count) {
data.new_posts_list.forEach(function(post) {
addNewPost(post);
});
localStorage.lastPost = data.new_posts_list[0];
}
}, 'json');
saveLastUpdate();
return true;
}
var addNewPost = function(id) {
var url = 'https://www.rebelmouse.com/core/feeds/site/posts/40552/' + id;
jx.load(url, function(data) {
var post = {
id: id
, title: data.headline
, body: data.body
, thumb: data.image210x
, link: data.original_url
, created_ts: data.created_ts
, created: formatPostDate(data.created_ts * 1000)
};
var posts = readPosts();
posts.push(post);
savePosts(posts);
}, 'json');
return true;
}
/****************************
* END Core functions
****************************/
/****************************
* Application init...
****************************/
if(!localStorage.posts) { savePosts([]); } // Init first time use...
document.addEventListener('DOMContentLoaded', function() {
updateBadge();
checkNewPosts();
setInterval(checkNewPosts, 60 * 1000);
});