-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackground.html
executable file
·217 lines (203 loc) · 7.42 KB
/
background.html
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
<html>
<head>
<script type="text/javascript" src="js/jquery.js"></script>
<script type="text/javascript" src="js/sha1.js"></script>
<script type="text/javascript" src="js/icon_creator.js"></script>
<script type="text/javascript" src="js/timeline.js"></script>
<script type="text/javascript" src="js/persistence.js"></script>
<script type="text/javascript" src="js/options_backend.js"></script>
<script type="text/javascript" src="js/fanfou_lib.js"></script>
</head>
<body>
<script type="text/javascript">
$.ajaxSetup({
timeout: OptionsBackend.get("timeout")
})
var MAX_TWEET_SIZE = 140;
function FanfouManager(user, passwd){
this.unreadTweets = {}
this.readTweets = {};
this.shouldNotReadMap = {};
this.injectTweets = null;
this.newTweetsCallback = null;
this.saveMessage = '';
this.isComposing = false
this.currentTimeline = "home"
this.timelines = {};
this.timelines["home"] = new Timeline("home", this, OptionsBackend.get("home_refresh_interval"));
this.timelines["mentions"] = new Timeline("mentions", this, OptionsBackend.get("memtions_refresh_interval"));
this.timelines["dms"] = new Timeline("dms", this, OptionsBackend.get("dms_refresh_interval"));
//this.timelines["favorites"] = new Timeline("favorites", this, 12000);
var that = this
this.ffBackend = new FanfouLib(user, passwd, function(){
that.giveMeTweets.call(that, "home", function(){});
that.giveMeTweets.call(that, "mentions", function(){});
that.giveMeTweets.call(that, "dms", function(){});
});
}
FanfouManager.prototype.eachTimeline = function(callback){
for (var id in this.timelines){
callback.call(id, this.timelines[id]);
}
}
FanfouManager.prototype.updateAlert = function(){
var unreadNewCount = 0;
var unreadNewTweets = [];
this.eachTimeline(function(timeline){
unreadNewCount += timeline.newTweetsCount()[1];
unreadNewTweets = unreadNewTweets.concat(timeline.getNewUnreadTweets());
})
if (unreadNewCount == 0){
chrome.browserAction.setBadgeText({text : ""});
}else{
var title = unreadNewCount + "条新消息";
chrome.browserAction.setTitle({title: title});
chrome.browserAction.setBadgeText({text : unreadNewCount + ""});
}
if (unreadNewTweets.length > 0){
//desktop notify
console.log("unreadNewTweets", unreadNewTweets);
this.showTweetsNotifications(unreadNewTweets);
}
}
FanfouManager.prototype.safeTweetsNotifications = function(tweetsToNotify, shouldChangeOption){
this.showTweetsNotifications(tweetsToNotify, true);
}
FanfouManager.prototype.showTweetsNotifications = function(tweetsToNotify, forceOnPage){
if (!tweetsToNotify || tweetsToNotify.length == 0){
return;
}
//这里会做显示数量限制
var maxTweetsNotifications = OptionsBackend.get("mex_notifications");
if (maxTweetsNotifications != -1 && tweetsToNotify.length > maxTweetsNotifications){
tweetsToNotify.splice(maxTweetsNotifications, tweetsToNotify.length - maxTweetsNotifications);
}
this.injectTweets = tweetsToNotify;
var notification_style = OptionsBackend.get("notification_style");
if (!forceOnPage && notification_style == "desktop"){
var notificationCenter = window.notifications || window.webkitNotifications;
var authStatus = notificationCenter.checkPermission();
if(authStatus == 1 || authStatus == 2) { //Not Allowed or Denied
throw 'Desktop notifications not allowed';
}
var notificationDisplayed = false;
var OnDisplayFunc = function(){
notificationDisplayed = true;
}
var that = this;
setTimeout(function(){
if (!notificationDisplayed){
for (var c=0;c<tweetsToNotify.length; ++c){
that.shouldNotReadMap[tweetsToNotify[c].id] = true;
}
that.safeTweetsNotifications(tweetsToNotify, false);
}
}, 1000);
for (var t = 0; t < tweetsToNotify.length; ++t){
var notification = notificationCenter.createHTMLNotification(
chrome.extension.getURL("notification.html")
);
(function inClosure(tweet){
notification.noclose = function(e){
setTimeout(function(){
that.readTweet(tweet.id);
}, 200)
}
})(tweetsToNotify[t]);
notification.ondisplay = OnDisplayFunc;
notification.show();
}
}else{
}
}
FanfouManager.prototype.registerNewTweetsCallback = function(callback){
this.newTweetsCallback = callback;
}
FanfouManager.prototype.readTweet = function(id){
if (this.shouldNotReadMap[id]){
delete this.shouldNotReadMap[id];
return;
}
this.readTweets[id] = true;
delete this.unreadTweets[id];
this.notifyNewTweets();
}
FanfouManager.prototype.isTweetRead = function(id){
return !this.unreadTweets[id]
}
FanfouManager.prototype.notifyNewTweets = function(timelineId, size, unreadSize){
if (this.newTweetsCallback){
var that = this;
this.eachTimeline(function(timeline){
var newTweets = timeline.newTweetsCount();
try{
that.newTweetsCallback(newTweets[0], newTweets[1], timeline.timelineId);
}catch(e){}
});
}
this.updateAlert();
}
FanfouManager.prototype.postTweet = function(callback, msg, replyId, postStatus, imgFiles){
if (postStatus == "retweet"){
return this.ffBackend.retweet(callback, msg, replyId);
}else{
return this.ffBackend.tweet(callback, msg, replyId, imgFiles);
}
}
FanfouManager.prototype.favorite = function(callback, id, isNeed){
if (isNeed){
return this.ffBackend.favorite(callback, id);
}else{
return this.ffBackend.unfavorite(callback, id);
}
}
FanfouManager.prototype.showTweet = function(callback, id){
return this.ffBackend.showTweet(callback, id);
}
FanfouManager.prototype.trashTweet = function(callback, id){
return this.ffBackend.destroy(callback, id);
}
FanfouManager.prototype.giveMeTweets = function(timelineId, callback, syncNew, cacheOnly){
return this.timelines[timelineId].giveMeTweets(callback, syncNew, cacheOnly);
}
FanfouManager.prototype.newTweetsCount = function(timelineId){
return this.timelines[timelineId].newTweetsCount();
}
FanfouManager.prototype.updateNewTweets = function(){
this.timelines[this.currentTimeline].updateNewTweets();
this.updateAlert();
}
FanfouManager.prototype.getCurrentTimeline = function(){
return this.timelines[this.currentTimeline];
}
FanfouManager.prototype.currentError = function(){
return this.timelines[this.currentTimeline].currentError;
}
FanfouManager.prototype.stopAll = function(){
this.eachTimeline(function(timeline){
timeline.stopTimer();
delete timeline;
})
}
FanfouManager.prototype.clear = function(){
this.unreadTweets = {}
this.readTweets = {};
this.newTweetsCallback = null;
this.saveMessage = '';
this.isComposing = false
this.currentTimeline = "home"
}
FanfouManager.prototype.signout = function(){
this.stopAll();
this.clear();
FanfouManager.instance = null;
}
function signin(user, passwd){
FanfouManager.instance = new FanfouManager(user, passwd);
}
if (localStorage.remember && localStorage.logged){
signin(localStorage.username, localStorage.password);
}
</script>
</body>
</html>