forked from NeoCat/twicli
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathnotify_desktop.js
345 lines (303 loc) · 10.1 KB
/
notify_desktop.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
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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
/*
* Copyright (c) 2014 Alex Gibson
* Released under the MIT license
* https://github.com/alexgibson/notify.js
*/
(function(global, factory) {
if (typeof define === 'function' && define.amd) {
// AMD environment
define(function() {
return factory(global, global.document);
});
} else if (typeof module !== 'undefined' && module.exports) {
// CommonJS environment
module.exports = factory(global, global.document);
} else {
// Browser environment
global.Notify = factory(global, global.document);
}
} (typeof window !== 'undefined' ? window : this, function (w, d) {
'use strict';
function isFunction (item) {
return typeof item === 'function';
}
function Notify(title, options) {
if (typeof title !== 'string') {
throw new Error('Notify(): first arg (title) must be a string.');
}
this.title = title;
this.options = {
icon: '',
body: '',
tag: '',
notifyShow: null,
notifyClose: null,
notifyClick: null,
notifyError: null,
timeout: null
};
this.permission = null;
if (!Notify.isSupported) {
return;
}
//User defined options for notification content
if (typeof options === 'object') {
for (var i in options) {
if (options.hasOwnProperty(i)) {
this.options[i] = options[i];
}
}
//callback when notification is displayed
if (isFunction(this.options.notifyShow)) {
this.onShowCallback = this.options.notifyShow;
}
//callback when notification is closed
if (isFunction(this.options.notifyClose)) {
this.onCloseCallback = this.options.notifyClose;
}
//callback when notification is clicked
if (isFunction(this.options.notifyClick)) {
this.onClickCallback = this.options.notifyClick;
}
//callback when notification throws error
if (isFunction(this.options.notifyError)) {
this.onErrorCallback = this.options.notifyError;
}
}
}
// true if the browser supports HTML5 Notification
Notify.isSupported = 'Notification' in w;
// true if the permission is not granted
Notify.needsPermission = !(Notify.isSupported && Notification.permission === 'granted');
// returns current permission level ('granted', 'default', 'denied' or null)
Notify.permissionLevel = (Notify.isSupported ? Notification.permission : null);
// asks the user for permission to display notifications. Then calls the callback functions is supplied.
Notify.requestPermission = function (onPermissionGrantedCallback, onPermissionDeniedCallback) {
if (!Notify.isSupported) {
return;
}
w.Notification.requestPermission(function (perm) {
switch (perm) {
case 'granted':
Notify.needsPermission = false;
if (isFunction(onPermissionGrantedCallback)) {
onPermissionGrantedCallback();
}
break;
case 'denied':
if (isFunction(onPermissionDeniedCallback)) {
onPermissionDeniedCallback();
}
break;
}
});
};
Notify.prototype.show = function () {
if (!Notify.isSupported) {
return;
}
this.myNotify = new Notification(this.title, {
'body': this.options.body,
'tag' : this.options.tag,
'icon' : this.options.icon
});
if (this.options.timeout && !isNaN(this.options.timeout)) {
setTimeout(this.close.bind(this), this.options.timeout * 1000);
}
this.myNotify.addEventListener('show', this, false);
this.myNotify.addEventListener('error', this, false);
this.myNotify.addEventListener('close', this, false);
this.myNotify.addEventListener('click', this, false);
};
Notify.prototype.onShowNotification = function (e) {
if (this.onShowCallback) {
this.onShowCallback(e);
}
};
Notify.prototype.onCloseNotification = function (e) {
if (this.onCloseCallback) {
this.onCloseCallback(e);
}
this.destroy();
};
Notify.prototype.onClickNotification = function (e) {
if (this.onClickCallback) {
this.onClickCallback(e);
}
};
Notify.prototype.onErrorNotification = function (e) {
if (this.onErrorCallback) {
this.onErrorCallback(e);
}
this.destroy();
};
Notify.prototype.destroy = function () {
this.myNotify.removeEventListener('show', this, false);
this.myNotify.removeEventListener('error', this, false);
this.myNotify.removeEventListener('close', this, false);
this.myNotify.removeEventListener('click', this, false);
};
Notify.prototype.close = function () {
this.myNotify.close();
};
Notify.prototype.handleEvent = function (e) {
switch (e.type) {
case 'show':
this.onShowNotification(e);
break;
case 'close':
this.onCloseNotification(e);
break;
case 'click':
this.onClickNotification(e);
break;
case 'error':
this.onErrorNotification(e);
break;
}
};
return Notify;
}));
/**
* Author : oui
* notify_desktop.js
*/
// ワーディング
langResources['Your browser does not support desktop notice.'] = ['このブラウザはデスクトップ通知をサポートしていません。'];
langResources['Notification'] = ['通知設定'];
if(Notify.needsPermission){
if(Notify.isSupported){
Notify.requestPermission();
}else{
alert(_('Your browser does not support desktop notice.'));
}
}
var s_tl = 0,
s_rep = 1,
s_dm = 2,
s_rt = 3,
s_fav = 4,
s_post = 5,
s_flw = 6,
s_cls = 7,
s_men = 8,
ttext,
icon_img;
// 設定読み書き
var notification = (readCookie('notification') || 'false').split(',');
var len = notification.length;
if(len>0){
for(var i=0; i<len; i++){
(notification[i] === 'true') ? notification[i] = true : notification[i] = false;
}
}
function setNotificationSettings() {
var settings = [];
var len = $('notification_pref').getElementsByTagName('input').length;
for(var i=0; i<len; i++){
settings.push($('notification_'+i).checked);
}
notification = settings;
writeCookie('notification', settings.join(","), 3652);
}
function getChecked(c) {
return c ? 'checked' : '';
}
registerPlugin({
cnt: 0,
update: function() { this.cnt++; },
gotNewMessage: function(tw) {
if (this.cnt <= 1) return;
var uname = tw.user.screen_name;
// RTされ
if(notification[s_rt] && tw.retweeted_status && tw.retweeted_status.user.screen_name == myname){
icon_img = tw.user.profile_image_url;
ttext = text(tw.retweeted_status);
showNotification('Retweeted by @'+uname);
// 通常のポスト
}else{
if(!notification[s_tl]) return;
if(uname == myname) return;
icon_img = tw.user.profile_image_url;
ttext = text(tw);
showNotification('Timeline: @'+uname);
}
},
// リプライ・Mention
noticeNewReply: function(replies){
if(!notification[s_rep] && !notification[s_men]) return;
for(var i=0; i<replies.length; i++){
if(replies[i].retweeted_status) return;
var uname = replies[i].user.screen_name;
if(uname == myname) return;
ttext = text(replies[i]);
icon_img = replies[i].user.profile_image_url;
if(notification[s_rep] && (replies[i].in_reply_to_status_id || ttext.charAt(0) === '@')){
showNotification('Reply by @'+uname);
}else if(notification[s_men]){
showNotification('Mention by @'+uname);
}
}
},
// DM
newDM: function(data){
if(!notification[s_dm]) return;
var uname = data.sender_screen_name;
if(uname == myname) return;
icon_img = data.sender.profile_image_url;
ttext = text(data);
showNotification('Direct Message by @'+uname);
},
// ふぁぼられ
favorited: function(tw){
if(!notification[s_fav]) return;
var uname = tw.source.screen_name;
if(uname == myname) return;
icon_img = tw.source.profile_image_url;
ttext = text(tw.target_object);
showNotification('Favorited by @'+uname);
},
// 自分のポスト
postQueued: function(str) {
if(!notification[s_post]) return;
icon_img = '';
ttext = str;
showNotification('Post: ');
},
// フォローされ
followed: function(data){
if(!notification[s_flw]) return;
if(data.screen_name == myname) return;
icon_img = data.profile_image_url;
ttext = '';
showNotification('Followed by @'+data.screen_name);
},
// 設定タブに要素を追加
miscTab: function(ele) {
var e = document.createElement("div");
e.innerHTML =
'<a href="javascript:var s = $(\'notification_pref\').style; s.display = s.display==\'block\'?\'none\':\'block\';void(0)"><b>▼'+_('Notification')+'</b></a>' +
'<form id="notification_pref" style="display:none;" onSubmit="setNotificationSettings(); return false;">' +
'<label><input type="checkbox" id="notification_'+s_tl+'" '+getChecked(notification[s_tl])+'>Timeline</label>'+
'<label><input type="checkbox" id="notification_'+s_rep+'" '+getChecked(notification[s_rep])+'>Reply</label>'+
'<label><input type="checkbox" id="notification_'+s_dm+'" '+getChecked(notification[s_dm])+'>DM</label>'+
'<label><input type="checkbox" id="notification_'+s_rt+'" '+getChecked(notification[s_rt])+'>Retweeted</label>'+
'<label><input type="checkbox" id="notification_'+s_men+'" '+getChecked(notification[s_men])+'>Mentioned</label>'+
'<label><input type="checkbox" id="notification_'+s_fav+'" '+getChecked(notification[s_fav])+'>Favorited</label>'+
'<label><input type="checkbox" id="notification_'+s_post+'" '+getChecked(notification[s_post])+'>Post</label>'+
'<label><input type="checkbox" id="notification_'+s_flw+'" '+getChecked(notification[s_flw])+'>Followed</label><br>'+
'<label><input type="checkbox" id="notification_'+s_cls+'" '+getChecked(notification[s_cls])+'>Auto Close</label><br>'+
'<button type="submit">'+_('Apply')+'</button></form>';
$("pref").appendChild(e);
}
});
function showNotification(title) {
var autoClose = null;
if(notification[s_cls]) autoClose = 3;
var notify = new Notify(title, {
body: ttext,
icon: icon_img,
timeout: autoClose
});
notify.show();
}