-
Notifications
You must be signed in to change notification settings - Fork 0
/
settings.js
398 lines (359 loc) · 14.6 KB
/
settings.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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
// As of 4/16/14: Changing session control from trying to use one session for multiple tabs to separate sessions per tab
// As of 4/16/14: Getting rid of JID/RID/SID chrome.storage save, instead saving JID/PASS to chrome.storage
$(document).ready(function() {
// RESTORE ALL SAVED PREFERENCES
// (have to shape this up => value ONLY IF checkbox === true)
// [UPGRADE] too DRY, make Whisper method
chrome.storage.sync.get(function (result) {
// Get onoff_checkbox
if (result.onoff_checkbox === true) {
$('#onoff_checkbox').prop('checked', true);
// Get onoff value
if (result.onoff) {
// Loop through result per keycode, convert to character
var characters = "";
for (var x in result.onoff) {
var letter = String.fromCharCode(x);
characters += letter;
}
characters.split('').join(' '); // [FIX] buggy, no effect
$('#onoff').attr('value', characters);
} else {
$('#onoff').val('');
$('#onoff').attr('placeholder', 'Enter hotkey letters');
}
} else {
$('#onoff_checkbox').prop('checked', false);
$('#onoff').attr('placeholder', 'Enter hotkey letters');
}
// Get first_checkbox
if (result.first_checkbox === true) {
$('#first_checkbox').prop('checked', true);
// Get first value
if (result.first) {
// Loop through result per keycode, convert to character
var characters = "";
for (var x in result.first) {
var letter = String.fromCharCode(x);
characters += letter;
}
characters.split('').join(' '); // [FIX] buggy, no effect
$('#first').attr('value', characters);
$('#first_link').html(result.first_name+' (click to change)');
Whisper.first_jid = result.first_jid;
Whisper.first_name = result.first_name;
} else {
$('#first').val('');
$('#first').attr('placeholder', 'Enter hotkey letters');
}
} else {
$('#first_checkbox').prop('checked', false);
$('#first_link').html('no one (click to assign)');
$('#first').attr('placeholder', 'Enter hotkey letters');
}
// Get second_checkbox
if (result.second_checkbox === true) {
$('#second_checkbox').prop('checked', true);
// Get second value
if (result.second) {
// Loop through result per keycode, convert to character
var characters = "";
for (var x in result.second) {
var letter = String.fromCharCode(x);
characters += letter;
}
characters.split('').join(' '); // [FIX] buggy, no effect
$('#second').attr('value', characters);
$('#second_link').html(result.second_name+' (click to change)');
Whisper.second_jid = result.second_jid;
Whisper.second_name = result.second_name;
} else {
$('#second').val('');
$('#second').attr('placeholder', 'Enter hotkey letters');
}
} else {
$('#second_checkbox').prop('checked', false);
$('#second_link').html('no one (click to assign)');
$('#second').attr('placeholder', 'Enter hotkey letters');
}
// Get third_checkbox
if (result.third_checkbox === true) {
$('#third_checkbox').prop('checked', true);
// Get third value
if (result.third) {
// Loop through result per keycode, convert to character
var characters = "";
for (var x in result.third) {
var letter = String.fromCharCode(x);
characters += letter;
}
characters.split('').join(' '); // [FIX] buggy, no effect
$('#third').attr('value', characters);
$('#third_link').html(result.third_name+' (click to change)');
Whisper.third_jid = result.third_jid;
Whisper.third_name = result.third_name;
} else {
$('#third').val('');
$('#third').attr('placeholder', 'Enter hotkey letters');
}
} else {
$('#third_checkbox').prop('checked', false);
$('#third_link').html('no one (click to assign)');
$('#third').attr('placeholder', 'Enter hotkey letters');
}
// Get fourth_checkbox
if (result.fourth_checkbox === true) {
$('#fourth_checkbox').prop('checked', true);
// Get fourth value
if (result.fourth) {
// Loop through result per keycode, convert to character
var characters = "";
for (var x in result.fourth) {
var letter = String.fromCharCode(x);
characters += letter;
}
characters.split('').join(' '); // [FIX] buggy, no effect
$('#fourth').attr('value', characters);
$('#fourth_link').html(result.fourth_name+' (click to change)');
Whisper.fourth_jid = result.fourth_jid;
Whisper.fourth_name = result.fourth_name;
} else {
$('#fourth').val('');
$('#fourth').attr('placeholder', 'Enter hotkey letters');
}
} else {
$('#fourth_checkbox').prop('checked', false);
$('#fourth_link').html('no one (click to assign)');
$('#fourth').attr('placeholder', 'Enter hotkey letters');
}
// Get fifth_checkbox
if (result.fifth_checkbox === true) {
$('#fifth_checkbox').prop('checked', true);
// Get fifth value
if (result.fifth) {
// Loop through result per keycode, convert to character
var characters = "";
for (var x in result.fifth) {
var letter = String.fromCharCode(x);
characters += letter;
}
characters.split('').join(' '); // [FIX] buggy, no effect
$('#fifth').attr('value', characters);
$('#fifth_link').html(result.fifth_name+' (click to change)');
Whisper.fifth_jid = result.fifth_jid;
Whisper.fifth_name = result.fifth_name;
} else {
$('#fifth').val('');
$('#fifth').attr('placeholder', 'Enter hotkey letters');
}
} else {
$('#fifth_checkbox').prop('checked', false);
$('#fifth_link').html('no one (click to assign)');
$('#fifth').attr('placeholder', 'Enter hotkey letters');
}
// Get timeout_checkbox
if (result.timeout_checkbox === true) { $('#timeout_checkbox').prop('checked', true); } else { $('#timeout_checkbox').prop('checked', false); }
// Get timeout value
if (result.timeout) {
$('#timeout').val(result.timeout);
} else {
$('#timeout').val(1800000);
}
// Get fadeout_checkbox
if (result.fadeout_checkbox === true) { $('#fadeout_checkbox').prop('checked', true); } else { $('#fadeout_checkbox').prop('checked', false); }
// Get fadeout value
if (result.fadeout) { $('#fadeout').val(result.fadeout); } else { $('#fadeout').val(5000); }
});
// GET RID OF DIALOG, CHANGE TO NORMAL INPUT / SEND INFO
$('#login_submit').click(function() {
$(document).trigger('connect', {
jid: $('#username').val().toLowerCase(),
password: $('#password').val()
});
$('#username').val('');
$('#password').val('');
});
$('#disconnect').click(function () {
console.log('Disconnect button clicked.');
Whisper.connection.disconnect();
Whisper.connection = null;
});
////////////////////////////////////////////////////////////////
// CHANGE COOKIES TO chrome.storage (4/15/14)!!!!!!!!!!!!!!!!!!!
// Manually get JID/SID/RID from chrome.storage here ///////////
// [UPGRADE] Whisper.get_storage(); // Taking this out??? (4/16/14) // Make it work (4/24/14)
chrome.storage.sync.get([
'jid',
'sid',
'rid'
], function (result) {
Whisper.storage.jid = result.jid;
Whisper.storage.sid = result.sid;
Whisper.storage.rid = result.rid;
console.log('JID/SID/RID retrieved from chrome.storage');
console.log('Whisper.storage JID: '+Whisper.storage.jid+' and SID: '+Whisper.storage.sid+' and RID: '+Whisper.storage.rid);
if (Whisper.storage.jid && Whisper.storage.sid && Whisper.storage.rid) {
console.log('Chrome storage JID/SID/RID detected. Triggering attach...');
$(document).trigger('attach', Whisper.storage);
} else {
console.log('No storage detected. Action stopped.');
}
});
// FRIEND_LINK CLICKS
$('.friend_link').click(function() {
console.log('.friend_link click detected.');
// [ADDED] conditional to see if user is already logged in (5/6/14)
if (Whisper.currentStatus === 5 || Whisper.currentStatus === 8) {
$('#roster-area').show();
var currentElement = $(this)[0];
console.log(currentElement);
var clicked = $(this)[0].id;
console.log('Clicked ID: '+clicked);
switch(clicked) {
case 'first_link': Whisper.which_friend = 'first';
break;
case 'second_link': Whisper.which_friend = 'second';
break;
case 'third_link': Whisper.which_friend = 'third';
break;
case 'fourth_link': Whisper.which_friend = 'fourth';
break;
case 'fifth_link': Whisper.which_friend = 'fifth';
break;
}
console.log('Whisper.which_friend: '+Whisper.which_friend);
$(document).trigger('roster', function() {
console.log('Roster event triggered.');
});
} else {
$('#roster-alert').show(function() {
setTimeout(function() {
$('#roster-alert').fadeOut('slow');
}, 1000);
});
}
});
// SAVE EVENT \\
$('#save').on('click', function() {
console.log('"Save" clicked.');
Whisper.on_save_onoff('onoff_checkbox', 'onoff');
Whisper.on_save_friends('first_checkbox', 'first', 'first_jid', 'first_name');
Whisper.on_save_friends('second_checkbox', 'second', 'second_jid', 'second_name');
Whisper.on_save_friends('third_checkbox', 'third', 'third_jid', 'third_name');
Whisper.on_save_friends('fourth_checkbox', 'fourth', 'fourth_jid', 'fourth_name');
Whisper.on_save_friends('fifth_checkbox', 'fifth', 'fifth_jid', 'fifth_name');
Whisper.on_save_regular('timeout_checkbox', 'timeout');
Whisper.on_save_regular('fadeout_checkbox', 'fadeout');
Whisper.on_saved();
});
});
// CONNECT EVENT \\
$(document).bind('connect', function (ev, data) {
Whisper.connection = new Strophe.Connection(
'http://ec2-54-186-151-244.us-west-2.compute.amazonaws.com:5280/http-bind');
Whisper.connection.connect(data.jid+'@chat.facebook.com', data.password, Whisper.on_connect);
});
// CONNECTED EVENT \\
$(document).bind('connected', function () {
$('#login-status').html('[Connected!]');
console.log('Connected.');
console.log(Whisper.connection);
var iq = $iq({type: 'get'}).c('query', {xmlns: 'jabber:iq:roster'});
Whisper.connection.sendIQ(iq, Whisper.on_roster);
Whisper.connection.addHandler(Whisper.on_roster_changed, "jabber:iq:roster", "iq", "set");
//Whisper.connection.addHandler(Whisper.on_message, null, "message", "chat");
// Manually save JID_master and PASS to chrome.storage here \\
chrome.storage.sync.set({
jid_master: Whisper.connection.authzid,
pass_master: Whisper.connection.pass
}, function() {
console.log('JID/PASS saved to chrome.storage');
//console.log('Whisper.storage JID: '+Whisper.storage.jid_master+' and PASS: '+Whisper.storage.pass_master);
});
});
// ATTACH EVENT \\
$(document).bind('attach', function (ev, data) {
Whisper.connection = new Strophe.Connection(
'http://ec2-54-186-151-244.us-west-2.compute.amazonaws.com:5280/http-bind');
//data.rid = parseInt(data.rid,10)+1;
Whisper.connection.attach(data.jid, data.sid, data.rid, Whisper.on_connect);
});
////////////////////////////////////////////////////////////////
// CHANGE COOKIES TO chrome.storage!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
// DISCONNECTED EVENT \\
$(document).bind('disconnected', function () {
// Manually (or maybe I can keep this one in the Whisper object?)
Whisper.del_storage();
Whisper.connection = null;
Whisper.pending_subscriber = null;
Whisper.on_connect;
$('#roster-area ul').empty();
$('#chat-area ul').empty();
$('#chat-area div').remove();
});
////////////////////////////////////////////////////////////////
// ROSTER LOADED EVENT
$(document).bind('roster', function() {
$('.roster-contact').on('click', function() {
console.log('.roster-contact click detected.');
var jid = $(this).find(".roster-jid").text();
console.log('.roster-contact JID: '+jid);
var name = $(this).find(".roster-name").text();
console.log('.roster-contact NAME: '+name);
switch(Whisper.which_friend) {
case 'first':
Whisper.first_jid = jid;
Whisper.first_name = name;
console.log('Whisper.first_jid: '+Whisper.first_jid+' and name: '+Whisper.first_name);
$('#first_link').html(Whisper.first_name+' (click to change)');
break;
case 'second':
Whisper.second_jid = jid;
Whisper.second_name = name;
console.log('Whisper.second_jid: '+Whisper.second_jid+' and name: '+Whisper.second_name);
$('#second_link').html(Whisper.second_name+' (click to change)');
break;
case 'third':
Whisper.third_jid = jid;
Whisper.third_name = name;
console.log('Whisper.first_jid: '+Whisper.third_jid+' and name: '+Whisper.third_name);
$('#third_link').html(Whisper.third_name+' (click to change)');
break;
case 'fourth':
Whisper.fourth_jid = jid;
Whisper.fourth_name = name;
console.log('Whisper.fourth_jid: '+Whisper.fourth_jid+' and name: '+Whisper.fourth_name);
$('#fourth_link').html(Whisper.fourth_name+' (click to change)');
break;
case 'fifth':
Whisper.fifth_jid = jid;
Whisper.fifth_name = name;
console.log('Whisper.fifth_jid: '+Whisper.fifth_jid+' and name: '+Whisper.fifth_name);
$('#fifth_link').html(Whisper.fifth_name+' (click to change)');
break;
}
$('#roster-area').fadeOut('fast');
});
});
////////////////////////////////////////////////////////////////
// CHANGE COOKIES TO chrome.storage!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
// UNLOAD EVENT (Leaving page) \\
$(window).bind('unload', function () {
console.log('Unload triggered.');
if (Whisper.connection !== null) {
// Manually save JID/SID/RID to chrome.storage here \\
//Whisper.set_storage();
chrome.storage.sync.set({
jid: Whisper.connection.jid,
sid: Whisper.connection._proto.sid,
rid: Whisper.connection._proto.rid
}, function() {
console.log('JID/SID/RID saved to chrome.storage');
//console.log('Whisper.storage JID: '+Whisper.storage.jid+' and SID: '+Whisper.storage.sid+' and RID: '+Whisper.storage.rid);
});
} else {
// Manually delete JID/SID/RID to chrome.storage here (or maybe I can keep this one in the Whisper object?)\\
Whisper.del_storage();
}
});
////////////////////////////////////////////////////////////////