-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathbackground.js
More file actions
278 lines (255 loc) 路 8.65 KB
/
Copy pathbackground.js
File metadata and controls
278 lines (255 loc) 路 8.65 KB
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
var host='www.facebook.com';
var pathNew='notifications';
var pathZero='';
var counter=0;
var newNotif=false;
var protocol='https';
var domain='facebook.com/messages';
var alwaysNew=false;
var showZero=false;
var deskNoti=null;
var showNoti=true;
var timeNoti=20000;
var timerVar=null;
var timerDelay=2000;
var playSound=true;
var audio=new Audio('ding.ogg');
window.onload=init;
var BADGE_NEW={color:[204,0,51,255]};
var BADGE_ACTIVE={color:[204,0,51,255]};
var BADGE_LOADING={color:[204,204,51,255]};
var BADGE_INACTIVE={color:[204,0,51,255]};
function loadData(){
var xhr=new XMLHttpRequest();
xhr.open('GET','https://www.facebook.com/',true);
xhr.onreadystatechange=function(){
if(xhr.readyState==4){
chrome.browserAction.setBadgeBackgroundColor(BADGE_INACTIVE);
var xmlDoc=xhr.responseText;
if( (xmlDoc.indexOf('notificationsCountValue') > 0) || (xmlDoc.indexOf('notifications:{unseen_count:') > 0) )
{
var lastCounter=counter;
var fReq=0;
var fMes=0;
var fNot=0;
if(xmlDoc.indexOf('notificationsCountValue') > 0)
{
// Request Value
loc=xmlDoc.indexOf('requestsCountValue');
if(loc>0){
var myString=xmlDoc.substr(loc,80);
fReq=parseInt(myString.substring(myString.indexOf('>')+1,myString.indexOf('<')));
}
// Message Value
loc=xmlDoc.indexOf('messagesCountValue');
if(loc>0){
var myString=xmlDoc.substr(loc,80);
fMes=parseInt(myString.substring(myString.indexOf('>')+1,myString.indexOf('<')));
}
// Notification Value
loc=xmlDoc.indexOf('notificationsCountValue');
if(loc>0){
var myString=xmlDoc.substr(loc,80);
fNot=parseInt(myString.substring(myString.indexOf('>')+1,myString.indexOf('<')));
}
}
else if(xmlDoc.indexOf('notifications:{unseen_count:') > 0)
{
// Friends requests
loc=xmlDoc.indexOf('friending_possibilities:{unseen_count:');
if(loc>0){
var myString=xmlDoc.substr(loc+30,80);
fReq=parseInt(myString.substring(myString.indexOf(':')+1,myString.indexOf(',')));
}
// Messages
loc=xmlDoc.indexOf('inbox:{unread_count:');
if(loc>0){
var myString=xmlDoc.substr(loc+10,80);
fMes=parseInt(myString.substring(myString.indexOf(':')+1,myString.indexOf(',')));
}
// notifications
loc = xmlDoc.indexOf('notifications:{unseen_count:');
if(loc>0){
var myString=xmlDoc.substr(loc+20,80);
fNot = parseInt(myString.substring(myString.indexOf(':')+1,myString.indexOf(',')));
}
}
var badgeTitle='Facebook - Online';
if(fReq>0) badgeTitle+='\n> '+fReq+' Requests';
if(fMes>0) badgeTitle+='\n> '+fMes+' Messages';
if(fNot>0) badgeTitle+='\n> '+fNot+' Notifications';
counter=fReq+fMes+fNot;
chrome.browserAction.setIcon({path:'images/ico19.png'});
chrome.browserAction.setTitle({title:badgeTitle});
if(!showZero&&counter==0)chrome.browserAction.setBadgeText({text:''});
else chrome.browserAction.setBadgeText({text:counter+''});
if(counter>lastCounter && fReq>0)
{
newNotif=true;
if(playSound)audio.play();
if(showNoti)
{
if(deskNoti)deskNoti.close();
deskNoti=new Notification('Electronic Request Received',{ body: 'You have '+counter+' new request(s)', icon: '/images/freiendrequest.png'});
deskNoti.onclick=function(){openPageReq();this.close()};
if(timeNoti)
{
window.setTimeout(function() { deskNoti.close(); }, timeNoti);
}
}
}
if(counter>lastCounter && fMes>0)
{
newNotif=true;
if(playSound)audio.play();
if(showNoti)
{
if(deskNoti)deskNoti.close();
deskNoti=new Notification('Electronic Message Received',{ body: 'You have '+counter+' new message(s)', icon: '/images/Chat.png'});
deskNoti.onclick=function(){openPageMess();this.close()};
if(timeNoti)
{
window.setTimeout(function() { deskNoti.close(); }, timeNoti);
}
}
}
if(counter>lastCounter && fNot>0)
{
newNotif=true;
if(playSound)audio.play();
if(showNoti)
{
if(deskNoti)deskNoti.close();
deskNoti=new Notification('Electronic Notification Received',{ body: 'You have '+counter+' new notification(s)', icon: '/images/notifications.png'});
deskNoti.onclick=function(){openPageNoti();this.close()};
if(timeNoti)
{
window.setTimeout(function() { deskNoti.close(); }, timeNoti);
}
}
}
if(newNotif)chrome.browserAction.setBadgeBackgroundColor(BADGE_NEW);
else if(counter>0)chrome.browserAction.setBadgeBackgroundColor(BADGE_ACTIVE);
}
else{
chrome.browserAction.setIcon({path:'images/ico19_offline.png'});
chrome.browserAction.setTitle({title:'--Disconnected--'});
chrome.browserAction.setBadgeText({text:'?'});
return;
}
}
else return;
}
xhr.send(null);
window.clearTimeout(timerVar);
timerVar=window.setTimeout(loadData,timerDelay);
}
function init(){
pathNew=(localStorage.pathNew||localStorage.pathNew=='')?localStorage.pathNew:pathNew;
pathZero=(localStorage.pathZero||localStorage.pathZero=='')?localStorage.pathZero:pathZero;
domain=(localStorage.useMess=='yes')?'messenger.com':'facebook.com/messages';
alwaysNew=(localStorage.alwaysNew)?(localStorage.alwaysNew=='yes'):false;
showZero=(localStorage.showZero)?(localStorage.showZero=='yes'):false;
playSound=(localStorage.playSound)?(localStorage.playSound=='yes'):true;
showNoti=(localStorage.showNoti)?(localStorage.showNoti=='yes'):true;
timeNoti=parseInt(localStorage.timeNoti||'20000');
timerDelay=parseInt(localStorage.refreshInterval||'30000');
chrome.browserAction.setIcon({path:'images/ico19_offline.png'});
chrome.browserAction.setBadgeText({text:'...'});
chrome.browserAction.setBadgeBackgroundColor(BADGE_LOADING);
loadData();
control.init();
}
function init1(){
pathNew=(localStorage.pathNew||localStorage.pathNew=='')?localStorage.pathNew:pathNew;
pathZero=(localStorage.pathZero||localStorage.pathZero=='')?localStorage.pathZero:pathZero;
domain=(localStorage.useMess=='yes')?'messenger.com':'facebook.com/messages';
alwaysNew=(localStorage.alwaysNew)?(localStorage.alwaysNew=='yes'):false;
showZero=(localStorage.showZero)?(localStorage.showZero=='yes'):false;
playSound=(localStorage.playSound)?(localStorage.playSound=='yes'):true;
showNoti=(localStorage.showNoti)?(localStorage.showNoti=='yes'):true;
timeNoti=parseInt(localStorage.timeNoti||'20000');
timerDelay=parseInt(localStorage.refreshInterval||'30000');
chrome.browserAction.setIcon({path:'images/ico19_offline.png'});
chrome.browserAction.setBadgeText({text:'...'});
chrome.browserAction.setBadgeBackgroundColor(BADGE_LOADING);
loadData();
control.init();
}
function tabCallback(tab){
chrome.tabs.onRemoved.addListener(function(tabId){if(tabId==tab.id)loadData();});
chrome.windows.update(tab.windowId,{focused:true});
}
function openUrl(uri){
chrome.windows.getAll({populate:true},function(windows){
if(windows.length<1){
chrome.windows.create({url:uri,focused:true});
return;
}
else if(!alwaysNew){
for(var i=0;i<windows.length;i++){
var tabs=windows[i].tabs;
for(var j=0;j<tabs.length;j++){
if(tabs[j].url.indexOf(uri)!=-1){
chrome.tabs.update(tabs[j].id,{selected:true},tabCallback);
return;
}
}
}
}
chrome.tabs.getSelected(null,function(tab){
if(tab.url=='chrome://newtab/')
chrome.tabs.update(tab.id,{url:uri},tabCallback);
else
chrome.tabs.create({url:uri},tabCallback);
});
});
}
//Don't change anything here, you will fuck up everything.
function openPage(){
if(counter>0)
openUrl(protocol+'://'+host+'/'+pathNew);
else
openUrl(protocol+'://'+host+'/'+pathZero);
newNotif=false;
loadData();
}
function openPageReq(){
if(counter>0)
openUrl(protocol+'://'+host+'/friends/requests');
else
openUrl(protocol+'://'+host+'/'+pathZero);
newNotif=false;
loadData();
}
function openPageMess(){
if(counter>0)
openUrl(protocol+'://'+domain);
else
openUrl(protocol+'://'+host+'/'+pathZero);
newNotif=false;
loadData();
}
function openPageNoti(){
if(counter>0)
openUrl(protocol+'://'+host+'/'+pathNew);
else
openUrl(protocol+'://'+host+'/'+pathZero);
newNotif=false;
loadData();
}
chrome.browserAction.onClicked.addListener(function(tab){
openPage();
iconClick();
});
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-9028814-2']);
_gaq.push(['_trackPageview']);
(function(){
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = 'https://ssl.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
function iconClick(){
_gaq.push(['_trackEvent', 'icon', 'clicked']);
};