-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.coffee
331 lines (251 loc) · 9.49 KB
/
main.coffee
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
class MinutaAjaxDownloader
URI: "https://dennikn.sk/api/minute"
ARTICLE_REGEX: /(<article[\s\S]*?<\/article>)/ig
xhrDownload: (responseType, URI, successCallback, errorCallback) ->
xhr = new XMLHttpRequest()
xhr.open("GET", URI)
xhr.responseType = responseType
xhr.onload = successCallback
xhr.onerror = errorCallback
xhr.send(null)
getMessages: (response) =>
JSON.parse(response)
class MinutaAjaxMessageParser
HTML_REGEX: /(<([^>]+)>)/ig
YOUTUBE_REGEX: /youtube\.com\/embed\/(.*?)[\/\?]/
PRIORITY_STICKY: "sticky"
PRIORITY_IMPORTANT: "important"
message: null
parse: (message) ->
@message = message
return {
thumbnail: @getFigure()
timePretty: @getTimePretty()
text: @getText()
html: @getHtml()
excerpt: @getHtmlExcerpt()
id: @getId()
targetUrl: @getTargetUrl()
priority: @getPriority()
topics: @getTopics()
categories: @getCategories()
}
getFigure: =>
return @message['image']['large'] if @message['image']? and @message['image']['large']?
if @message['embed']?
matches = @message['embed']['html'].match(@YOUTUBE_REGEX)
if matches? and matches.length == 2
return "http://img.youtube.com/vi/" + matches[1] + "/mqdefault.jpg"
getTimePretty: =>
# expecting 2017-05-16T06:14:53 format (no explicit timezone, in UTC)
date = new Date(@message['created']);
userTimezoneOffset = date.getTimezoneOffset() * -60000;
return (new Date(date.getTime() + userTimezoneOffset)).timeNow()
getText: ->
value = @message['content']['main'].replace(@HTML_REGEX, "")
return @decodeHtml(value)
getHtmlExcerpt: ->
@message['content']['main']
getHtml: ->
if @message['content']['extended']?
html = @message['content']['extended']
else
html = @message['content']['main']
figure = @getFigure()
if figure?
html += "<p><img src='" + figure + "' /></p>"
return html
getId: =>
@message['id'].toString()
getTargetUrl: =>
@message['url']
getPriority: =>
return @PRIORITY_IMPORTANT if @message['important']? and @message['important'] == true
return @PRIORITY_STICKY if @message['sticky']? and @message['sticky'] == true
getTopics: =>
topics = {}
return topics unless @message['tag']
for tag in @message['tag']
topics[tag['slug']] = tag['name']
topics
getCategories: =>
return @message['cat']
decodeHtml: (html) ->
txt = document.createElement "textarea"
txt.innerHTML = html
return txt.value
class Minuta
thumbnail: null
time: null
message: null
id: null
targetUrl: null
priority: null
topics: null
constructor: (@thumbnail, @time, @message, @id, @targetUrl, @priority) ->
class Notifier
NO_TOPIC: ""
LOGO: "/images/icon512.png"
BUTTONS: [
chrome.i18n.getMessage("readMoreButton")
]
DEFAULT_SYNC_SETTINGS:
"sound": "no-sound"
"interval": 5
"messageCount": 10
"displayTime": 10
"notificationClick": "open"
"snooze": "off"
"lastSync": Date.now()
"selectedCategories": []
DEFAULT_LOCAL_SETTINGS:
"topics": {}
DEFAULT_NOTIFICATION_OPTIONS:
type : "basic"
title: chrome.i18n.getMessage("notificationTitle")
message: null
priority: 1
notificationSound: null
currentSettings: {}
downloader: null
parser: null
topics: {}
constructor: (@downloader, @parser) ->
chrome.storage.local.clear()
chrome.notifications.onClosed.addListener @notificationClosed;
chrome.notifications.onClicked.addListener @notificationClicked;
chrome.notifications.onButtonClicked.addListener @notificationBtnClick;
run: (forceSilent) =>
downloader = new @downloader();
parser = new @parser();
@reloadSettings =>
@downloadMessages downloader, parser, forceSilent
downloadMessages: (downloader, parser, silently) =>
if not silently
silently = @currentSettings['snooze'] != 'off' and @currentSettings['snooze'] > Date.now()
minutesInterval =
if @currentSettings['interval']? and parseInt(@currentSettings['interval']) >= 1
then parseInt(@currentSettings['interval']) else 1
chrome.storage.local.remove("stickyTopicMessage");
downloader.xhrDownload "text", downloader.URI, (event) =>
rawMessages = downloader.getMessages event.target.response;
storage = {}
messages = {}
topics = {}
for rawMessage in rawMessages.timeline
break if Object.keys(messages).length == parseInt(@currentSettings['messageCount'])
message = parser.parse rawMessage
for own key, value of message.topics
topics[key] = value
if Object.keys(messages).length < parseInt(@currentSettings['messageCount'])
messages[message.id] = message
@currentSettings['lastSync'] = new Date()
chrome.storage.local.set {"latestMessageIds": Object.keys(messages)}
chrome.storage.local.get Object.keys(messages), (alreadyNotifiedMessages) =>
delay = 0
for id, message of messages
continue if message.id of alreadyNotifiedMessages
categories = (category['slug'] for category in message.categories)
if silently || (@currentSettings['selectedCategories'].length > 0 && !intersection(categories, @currentSettings['selectedCategories']))
storage[message.id] = {
"skipped": true
"targetUrl": message.targetUrl
"excerpt": message.excerpt
"html": message.html
"timePretty": message.timePretty
"thumbnail": message.thumbnail
"categories": message.categories
} if message.excerpt.length > 10
else
do (message) =>
setTimeout =>
@notifyArticle message
, delay
delay += 100 # because we're trying to be sure it gets executed in proper order
if silently and Object.keys(storage).length
console.log "silent iteration, skipping following messages..."
console.log storage
chrome.storage.local.set storage
setTimeout @run.bind(this, false), 60000 * minutesInterval
, (event) =>
console.log "recovering from network error..."
setTimeout @run.bind(this, false), 60000 * minutesInterval
reloadSettings: (callback) =>
chrome.storage.sync.get @DEFAULT_SYNC_SETTINGS, (val) =>
val['selectedCategories'] = [] if val['selectedCategories'] == null
@currentSettings = val;
chrome.storage.sync.clear()
chrome.storage.sync.set val
if val['sound'] != 'no-sound' and (not @notificationSound? or @notificationSound.src.indexOf(val['sound']) == -1)
@notificationSound = new Audio('sounds/' + val['sound'] + '.mp3')
chrome.storage.local.get @DEFAULT_LOCAL_SETTINGS, (wal) =>
@topics = wal['topics']
callback() if callback?
notifyArticle: (message) =>
options = JSON.parse(JSON.stringify(@DEFAULT_NOTIFICATION_OPTIONS));
meta =
"targetUrl": message.targetUrl
"excerpt": message.excerpt
"html": message.html
"skipped": false
"timePretty": message.timePretty
"thumbnail": message.thumbnail
"categories": message.categories
"topics": message.topics
unless (message.id? and message.text?)
console.warn("Could not parse the message from the source, skipping...");
return false;
options.message = message.text
if message.categories[message.categories.length-1]['slug'] != 'hlavna'
options.title = message.categories[message.categories.length - 1]['name']
if message.timePretty?
options.title = "[" + message.timePretty + "] " + options.title
if message.thumbnail?
options.type = "image"
options.imageUrl = message.thumbnail
@doNotify message.id, options, meta
return true
doNotify: (id, options, meta) ->
options.iconUrl = chrome.runtime.getURL(@LOGO);
options.buttons = [];
for button in @BUTTONS
options.buttons.push { title: button };
storage = {};
storage[id] = meta;
chrome.storage.local.set storage;
chrome.notifications.create id, options, @creationCallback
creationCallback: (notID) =>
console.log "The nofitication '" + notID + " 'was created."
setTimeout =>
chrome.notifications.clear(notID);
, 1000 * @currentSettings['displayTime']
@notificationSound.play() if @notificationSound?
notificationClosed: (notID, bByUser) =>
console.log "The notification '" + notID + "' was closed"
notificationClicked: (notID) =>
console.log "The notification '" + notID + "' was clicked"
chrome.notifications.clear notID
if @currentSettings['notificationClick'] == 'open'
@openMessage notID
notificationBtnClick: (notID) =>
@openMessage notID
openMessage: (notID) =>
chrome.storage.local.get notID, (val) ->
targetUrl = val[notID].targetUrl + "?ref=ext";
console.log targetUrl
chrome.tabs.create {url: targetUrl}
intersection = (a, b) ->
[a, b] = [b, a] if a.length > b.length
res = (value for value in a when value in b)
return res.length > 0
########################
# MAIN
########################
Date.prototype.timeNow = ->
return (if @getHours() < 10 then "0" else "") + @getHours() + ":" +
(if @getMinutes() < 10 then "0" else "") + @getMinutes() + ":" +
(if @getSeconds() < 10 then "0" else "") + @getSeconds()
notifier = new Notifier(MinutaAjaxDownloader, MinutaAjaxMessageParser)
notifier.run true
#chrome.webRequest.onErrorOccurred.addListener (details) ->
# console.log details