forked from antimatter15/surplus
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackground.html
More file actions
262 lines (237 loc) · 7.82 KB
/
Copy pathbackground.html
File metadata and controls
262 lines (237 loc) · 7.82 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
<iframe id="frame"></iframe>
<style>
iframe {
width: 440px;
height: 300px;
}
</style>
<script>
/*
This used to be a fairly simple bit of code, but then I added
stuff with very little forethought. Now it's a mess.
*/
var frame = document.getElementById('frame');
frame.src = "http://images.google.com/?extension=surplus&authuser="+(localStorage.auth_user||0);
//frame.src = 'https://plus.google.com/u/'+(localStorage.auth_user||0)+'/settings/general?extension=surplus'
//frame.src = 'https://plus.google.com/u/'+(localStorage.auth_user||0)+'/116347431032639424492/about'
var heightstate = 0;
var login_error = false;
var visible = false;
var share_visible = false;
var current_user = '';
var last_close = 0;
function start_load(){
//yay its been loaded!
if(!global_notifications_port){
console.log("loaded popup and notification port not found")
setTimeout(function(){
if(!global_notifications_port) location.reload();
}, 3141)
}
if(!share_visible) ensure_open();
}
function evacuate(){
if(!share_visible) ensure_closed();
console.log('restoring window')
document.adoptNode(frame);
document.body.appendChild(frame);
/*
chrome.browserAction.setPopup({popup:''});
setTimeout(function(){
chrome.browserAction.setPopup({popup:'popup.html'});
}, 1000)
*/
}
var global_port;
var global_port_src = '';
var global_notifications_port;
function shift_frame(){
frame.style.height = (500 + (heightstate++ % 7) - 3)+'px'; //hopefully users dont notice 3px diffs
}
function open_popup(){
global_port.postMessage({action: 'notification'});
shift_frame();
share_visible = false;
if(global_notifications_port){
global_notifications_port.postMessage({action: 'sharevisible', value: share_visible})
}
visible = !visible;
setTimeout(manualUpdate, 1000);
}
function open_share(){
ensure_closed();
shift_frame()
share_visible = true;
if(localStorage.nocurrentpage == 'yes'){
global_notifications_port.postMessage({action: 'sharevisible', value: share_visible})
global_port.postMessage({action: 'share'});
}else{
chrome.tabs.getSelected(null, function(tab){
console.log("Got current tab", tab)
global_notifications_port.postMessage({action: 'sharevisible', value: share_visible, current_url: tab.url})
global_port.postMessage({action: 'share'});
})
}
}
function close_share(){
share_visible = false;
shift_frame()
global_notifications_port.postMessage({action: 'sharevisible', value: share_visible})
global_notifications_port.postMessage({action: 'sharehide'})
global_port.postMessage({action: 'share'});
}
function ensure_open(){
if(!visible) open_popup();
}
function ensure_closed(){
if(visible) open_popup();
}
var last_updated_time = 0;
var update_queued = false;
function manualUpdate(){
if(new Date - last_updated_time > 3000){
forceUpdate();
last_updated_time = +new Date;
update_queued = false;
}else if(update_queued == false){
update_queued = true;
setTimeout(manualUpdate, 4000);
}
}
function forceUpdate(){
if(global_notifications_port)
global_notifications_port.postMessage({action:'xhr', url:'https://plus.google.com/u/'+(localStorage.auth_user||0)+'/_/n/guc'});
else if(parseInt(last_recorded_number) > -1){
console.log("nawt yedt")
drawIcon(last_recorded_number);
}
}
function handleCount(text){
var num = JSON.parse(text.substr(4))[1];
drawIcon(num);
getNotifications(num);
console.log('Got Notifications',num);
}
setInterval(function(){manualUpdate()}, 20 * 1000);
function drawIcon(num){
num += '';
var c = document.createElement('canvas').getContext('2d');
var none = (num.replace(/[\s0]/g,'') == '');
var img = new Image();
img.src = none ? 'img/old.png' : 'img/new.png';
img.onload = function(){
c.drawImage(img, 0, 0);
c.font = "bold 13px arial,sans-serif";
c.fillStyle = none ? '#CCC' : '#fff';
c.fillText(num+'', 6, 14);
chrome.browserAction.setIcon({imageData: c.getImageData(0,0,19,19)})
}
}
var connect_times = 0;
var last_recorded_number = -1;
var last_recorded_time = 0;
chrome.extension.onConnect.addListener(function(port) {
if(port.name == 'chell'){ // i dont know why i'm naming this after portal 2 characters
global_port = port;
port.onMessage.addListener(function(msg){
current_user = msg.user;
if(msg.error){
login_error = msg.error;
drawIcon("!!");
}else{
global_port_src = msg.src;
if(last_recorded_number != msg.num && (+new Date - last_recorded_time) > 2000){
setTimeout(manualUpdate, 100);
last_recorded_time = +new Date;
last_recorded_number = msg.num;
}
}
})
}else{ // if(port.name == 'wheatley'){
//console.log('recieved potential wheatley');
function poll(){
if(global_port_src){
//console.log('found global port source', global_port_src, port.name);
if(global_port_src == port.name){
global_notifications_port = port;
port.postMessage({action: 'accept', user: current_user});
port.onMessage.addListener(function(msg){
if(msg.action == 'notifications')
showqueue = showqueue.concat(msg.messages);
else if(msg.action == 'xhr'){
handleCount(msg.response)
}else if(msg.action == 'share'){
open_share();
}else if(msg.action == 'sharevisible' && msg.value == false){
if(share_visible){
close_share();
setTimeout(ensure_open, 100)
}
}
})
}
}else setTimeout(poll, 500);
}
poll();
}
});
function getNotifications(num){
num += '';
chrome.idle.queryState(parseInt(localStorage.idle_duration||"15") * 60, function(state){
if(state == 'idle') return;
if(num.replace(/[\s0]/g,'') != '' && localStorage.notify == 'yes'){
//now time for notifications stuff!
console.log('trying to display notifications');
var is_changed = false;
if(!visible){
console.log('opening popup');
ensure_open();
is_changed = true;
}
setTimeout(function(){
console.log('requesting notifications');
global_notifications_port.postMessage({action: 'notifications', num: parseInt(num)})
setTimeout(function(){
if(is_changed){
ensure_closed();
console.log('closing popup');
}
}, 100);
}, 1000);
}
})
}
var showqueue = [];
var notify_message = '', notify_image = '';
function showMessage(){
var msg = showqueue.shift();
if(msg){
if(msg[0] == notify_message) return showMessage(); //dont repeat messages
if(localStorage.notify_sound == 'yes') (new Audio("notify.ogg")).play();
window.notify_message = msg[0];
window.notify_image = msg[1];
var notification = window.webkitNotifications.createHTMLNotification('notification.html');;
notification.show();
notification.onclick = function(){
chrome.tabs.create({url:'https://plus.google.com/u/'+(localStorage.auth_user||0)+'/notifications/all'})
}
setTimeout(function(){
notification.cancel();
}, parseInt(localStorage.autodismiss || 5) * 1000);
}
}
setInterval(showMessage, 6000); //seems like a good duration
/*
chrome.windows.create({
url: url,
width: width,
height: height,
type: 'popup'})
*/
setTimeout(function(){
if(!global_port) location.reload();
}, 8000);
setInterval(function(){
if(login_error) location.reload();
}, 15000);
</script>