-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathproductivityowl.js
More file actions
1195 lines (1044 loc) · 41.2 KB
/
productivityowl.js
File metadata and controls
1195 lines (1044 loc) · 41.2 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
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
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/************************************************
This code is licensed under the Apache v2.0 License
================================================
If you're expecting an organized, modern, easily extensible codebase
then you're looking at the wrong project. The reason the project is open-source
is so everyone can be confident the Owl has no trackers or data-harvesting.
If you're going to read the code I hope you like jQuery.
Copyright Mark Rieck, 2010 - 2020
Github: https://github.com/mrieck/productivityowl
Twitter: https://twitter.com/productivityowl
*************************************************/
chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) {
console.log("chrome runtime request is ");
console.log(request);
if(request.method == 'get_all_options'){
var allowedWebsites = localStorage['allowed_websites'];
var blockedWebsites = localStorage['blocked_websites'];
var autocloseOptions = localStorage['autoclose_options'];
var tasks = localStorage['tasks'];
var vacationTime = localStorage['vacation_time'];
var timeLimitDomains = localStorage['time_limit_domains'];
var interventionsDisabled = localStorage['turn_off_interventions'];
var statsDisabled = localStorage['turn_off_stats'];
var owlSide = localStorage['owl_side'];
//if empty
if(!interventionsDisabled){
interventionsDisabled = "no";
}
if(!statsDisabled){
statsDisabled = "no";
}
if(!owlSide){
owlSide = "right";
}
if(!timeLimitDomains){
timeLimitDomains = new Array();
}else{
timeLimitDomains = $.makeArray($.parseJSON(timeLimitDomains));
}
if(!allowedWebsites){
allowedWebsites = new Array();
}
if(!blockedWebsites){
blockedWebsites = new Array();
}
if(!tasks){
tasks = new Array();
}
if(!vacationTime){
vacationTime = 0;
}
var optionsObj = {
allowed_websites: allowedWebsites,
blocked_websites: blockedWebsites,
autoclose_options: autocloseOptions,
tasks: tasks,
vacation_time: vacationTime,
time_limit_domains: timeLimitDomains,
interventions_disabled: interventionsDisabled,
stats_disabled: statsDisabled,
owl_side: owlSide
};
//var theResponse = JSON.stringify(optionsObj);
console.log("sending get_all_options response");
console.log(optionsObj);
sendResponse({data: optionsObj});
}
else if(request.method == 'get_tasks'){
var theTasks = localStorage['tasks'];
sendResponse({data: theTasks});
}
else if(request.method == 'save_tasks'){
var theTasks = request.tasks;
if(theTasks){
var jsonEncoded = JSON.stringify(theTasks);
//console.log(jsonEncoded);
localStorage['tasks'] = jsonEncoded;
}
}
else if(request.method == 'save_owl_side'){
var theSide = request.side;
console.log("the side " + theSide);
if(theSide != 'right' && theSide != "left"){
theSide = "right";
}
console.log("now set to " + theSide);
localStorage['owl_side'] = theSide;
}
else if(request.method == "saveLinkClick")
{
chrome.tabs.create({
url: request.url
});
}
else if(request.method == 'test_respect_calc'){
var processArr = respectCalc();
sendResponse({data: processArr});
}
else if(request.method == 'get_domain_current_time'){
//get amount of time spent on current domain
var theDomain = request.domain;
var unixTime = new Date().getTime();
unixTime = unixTime - 60*60*4*1000;
//minus 4 hours... so yesterday's day lasts until 4 am today
var d = new Date(unixTime);
var dateString = getDateStorageString("bstats", d);
var allPastUrls = localStorage[dateString];
if(!allPastUrls){
allPastUrls = {};
}else{
allPastUrls = JSON.parse(allPastUrls);
}
var currCount = 0;
if(theDomain in allPastUrls){
currCount = allPastUrls[theDomain];
}
/*
var lastThreeUrls = localStorage['last_visited_urls'];
if(!lastThreeUrls){
lastThreeUrls = new Array();
}else{
lastThreeUrls = JSON.parse(lastThreeUrls);
}
console.log("lastThreeUrls ");
console.log(lastThreeUrls);
var currDate = new Date().toString("F");
lastThreeUrls.unshift({url: theDomain, visit_date: currDate});
if(lastThreeUrls.length > 3){
lastThreeUrls = lastThreeUrls.slice(0, 3);
}
localStorage['last_visited_urls'] = JSON.stringify(lastThreeUrls);
*/
//also record latest domain and time visited
//use that to check if someone uninstalled the extension
localStorage['lasturl_visited_time'] = currDate;
sendResponse({data: currCount});
}
else if(request.method == "isFreeTime")
{
var currDate = Date.today().setTimeToNow();
//request.url
//get local storage urls which each have a time that they occured
//clean out any urls that are older than 1 hour
//
var currDomain = "";
if(request.url)
{
currDomain = request.url;
}
//We store all urls so we can calcuate visited count.
//If Anti-Desperation mode is active we use this, different than browser stats
var allPastUrls = localStorage['url_history'];
var allPastUrlsCopy = new Array();
if(!allPastUrls)
{
allPastUrls = new Array();
}
else
{
allPastUrls = $.makeArray($.parseJSON(allPastUrls));
}
//very old code - maybe rewrite...
var visitedCount = 0;
for(var u = 0; u < allPastUrls.length; u++)
{
var urlObj = allPastUrls[u];
var pastDate = Date.parse(urlObj.date);
var pastDomain = urlObj.url;
var withinLastHour = true;
if(pastDate == null)
{
withinLastHour = false;
}
else if((currDate.getHours() - pastDate.getHours()) > 1)
{
withinLastHour = false;
}
if(withinLastHour)
{
allPastUrlsCopy.push({url: urlObj.url, date: pastDate.toString("M/d/yyyy HH:mm:ss")});
}
if(pastDomain == currDomain)
{
visitedCount++;
}
}
console.log("This page: " + currDomain + " was already visited: " + visitedCount);
if(currDomain != "")
{
//console.log("Pushing url: " + request.url);
//toString() produces this: 2014-01-28T20:18:28.369Z ...
//which cannot be parsed by Date.js? wtf... so currDate.toString("M/d/yyyy HH:mm:ss")
allPastUrlsCopy.push({url: request.url, date: currDate.toString("M/d/yyyy HH:mm:ss")});
localStorage['url_history'] = JSON.stringify(allPastUrlsCopy);
}
if(visitedCount > 50)
{
localStorage['url_history'] = JSON.stringify(new Array());
localStorage['allowed_history'] = JSON.stringify(new Array());
}
//returns negative number if in current freetime... number of hours and minutes left
//returns positive number if not in freetime... hours and minutes until next freetime
var dayIndex = -1;
var dayName = currDate.toString("ddd");
var dayArr = ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"];
for(var d = 0; d < dayArr.length; d++)
{
if(dayName == dayArr[d])
{
dayIndex = d;
}
}
if(dayIndex == -1) //Should never happen
{
console.log("Should not happen. Day not found");
return -99999999999999;
}
var todayTime = currDate.getHours() + (currDate.getMinutes() * 0.01);
var scheduleData = localStorage['scheduler_data'];
var workScheduleData = localStorage['scheduler_work_data'];
//console.log("schedule data");
//console.log(scheduleData);
var timespans = new Array();
var timespansWork = new Array();
if(scheduleData){
console.log("has some");
timespans = jQuery.makeArray(jQuery.parseJSON(scheduleData));
}
if(workScheduleData){
console.log("has some");
timespansWork = jQuery.makeArray(jQuery.parseJSON(workScheduleData));
}
for(var i = 0; i < timespans.length; i++)
{
var freetimeObj = timespans[i];
//Turn back into dates
freetimeObj.sTime = Date.parse(freetimeObj.sTime);
freetimeObj.eTime = Date.parse(freetimeObj.eTime);
}
for(var j = 0; j < timespansWork.length; j++)
{
var freetimeObj = timespansWork[j];
//Turn back into dates
freetimeObj.sTime = Date.parse(freetimeObj.sTime);
freetimeObj.eTime = Date.parse(freetimeObj.eTime);
}
var nextFreeTime = -1;
var response = -1;
var freeTimeType = "none";
for(var y = 0; y < timespans.length; y++)
{
var anObj = timespans[y];
var freeDayArr = anObj.dayArr;
for(var z = 0; z < freeDayArr.length; z++)
{
var currDayIndex = freeDayArr[z];
if(dayIndex == currDayIndex)
{
//On the same day in scheduler, check the times
var currStart = anObj.sTime.getHours() + (anObj.sTime.getMinutes() * 0.01);
var currEnd = anObj.eTime.getHours() + (anObj.eTime.getMinutes() * 0.01);
console.log("----Is " + todayTime + " inbetween " + currStart + " and " + currEnd);
if(todayTime >= currStart && todayTime <= currEnd) //inside FREE TIME
{
console.log("WE ARE INSIDE FREETIME");
//inbetween... calculate it
response = 1;
freeTimeType = "scheduler";
break;
}
}
}
}
for(var e = 0; e < timespansWork.length; e++)
{
var anObj = timespansWork[e];
var workDayArr = anObj.dayArr;
for(var f = 0; f < workDayArr.length; f++)
{
var currDayIndex = workDayArr[f];
if(dayIndex == currDayIndex)
{
//On the same day in scheduler, check the times
var currStart = anObj.sTime.getHours() + (anObj.sTime.getMinutes() * 0.01);
var currEnd = anObj.eTime.getHours() + (anObj.eTime.getMinutes() * 0.01);
console.log("----Is " + todayTime + " inbetween " + currStart + " and " + currEnd);
if(todayTime >= currStart && todayTime <= currEnd) //inside FREE TIME
{
console.log("WE ARE INSIDE WORKTIME");
freeTimeType = "worktime";
break;
}
}
}
}
var secLeft = 0;
var vacationStart = localStorage['start_vacation_time'];
var vacationDuration = localStorage['start_vacation_duration'];
//vacationDuration in minutes
var vacationMinutes = vacationDuration;
if(vacationStart)
{
vacationDuration = parseInt(vacationDuration);
console.log("VacationStart is: " + vacationStart);
var vacationDate = Date.parse(vacationStart);
//var vacationEnd = vacationStart.clone().addMinutes(vacationMinutes);
var now = new Date();
//now = Date.parse(now.toString("F"));
var span = new TimeSpan(now - vacationDate);
//console.log("SPan hours min sec:")
//console.log(span.getHours());
//console.log(span.getMinutes());
//console.log(span.getSeconds());
var totalSec = span.getTotalMilliseconds() / 1000;
//console.log("totalSec is: " + totalSec);
if(totalSec < (vacationDuration * 60))
{
//We are in vacation_time
response = 1;
freeTimeType = "vacation";
secLeft = (vacationMinutes * 60) - totalSec;
}
}
var vacationTimeLeft = parseInt(localStorage['vacation_time']);
vacationTimeLeft = vacationTimeLeft + Math.floor(secLeft / 60);
sendResponse({data: response, freetime_type: freeTimeType, visited_count: visitedCount, sec_left: secLeft, vacation_time: vacationTimeLeft});
}
else if(request.method == "resetVacationTime")
{
localStorage['vacation_time'] = 0;
sendResponse({data: 0});
}
else if(request.method == "addVacationTime")
{
var vacationTime = localStorage['vacation_time'];
if(!vacationTime)
{
vacationTime = 0;
}
else
{
vacationTime = parseInt(vacationTime);
if(vacationTime > 10000)
vacationTime = 0;
}
var timeAmount = parseInt(request.time);
vacationTime = vacationTime + timeAmount;
localStorage['vacation_time'] = vacationTime;
//localStorage[]
//minus 4 hours... so yesterday's day lasts until 4 am today
var unixTime = new Date().getTime();
var origTime = unixTime;
unixTime = unixTime - 60*60*4*1000;
var d = new Date(unixTime);
var taskDateString = getDateStorageString("taskcomplete", d);
console.log("date string");
console.log(taskDateString);
var allTodayTasksDone = localStorage[taskDateString];
if(!allTodayTasksDone){
console.log("create new today tasks done");
allTodayTasksDone = new Array();
}else{
allTodayTasksDone = JSON.parse(allTodayTasksDone);
}
console.log("all tasks before ");
console.log(allTodayTasksDone);
allTodayTasksDone.push({task_text: request.task_text, credits_earned: vacationTime, complete_time: origTime, subtask: request.subtask});
if(request.subtask == 'no'){
if(request.subtask_count){
for(var s = 0; s < request.subtask_count; s++){
allTodayTasksDone.push({task_text: "subtask", credits_earned: 0, complete_time: origTime, subtask: "yes"});
}
}
}
console.log("all tasks after ");
console.log(JSON.stringify(allTodayTasksDone));
localStorage[taskDateString] = JSON.stringify(allTodayTasksDone);
var responseTime = vacationTime;
console.log("response time: " + responseTime);
sendResponse({data: responseTime});
}
else if(request.method == "useVacationTime")
{
var response = 0;
var vacationTime = localStorage['vacation_time'];
if(!vacationTime)
{
vacationTime = 0;
}
else
{
vacationTime = parseInt(vacationTime);
}
var timeAmount = 1;
if(request.amount == 'all'){
timeAmount = vacationTime;
}else{
timeAmount = parseInt(request.amount);
}
console.log("Comparing: " + timeAmount + " and " + vacationTime);
if(vacationTime >= timeAmount)
{
response = 1;
var currDate = new Date().toString("F");
console.log("saving curr date: " + currDate);
localStorage['start_vacation_time'] = currDate;
localStorage['start_vacation_duration'] = request.amount;
localStorage['vacation_time'] = vacationTime - request.amount;
}
else
{
response = 0;
}
chrome.tabs.query({},function(tabs){
tabs.forEach(function(tab){
chrome.tabs.sendMessage(tab.id, {method: "vacation_started", vacation_min: timeAmount}, null);
});
});
sendResponse({data: response});
}
else if(request.method == "stopVacationTime")
{
var vacationTime = localStorage['vacation_time'];
if(!vacationTime)
{
vacationTime = 0;
}
else
{
vacationTime = parseInt(vacationTime);
}
var newVacationTime = vacationTime;
var startVacationTime = localStorage['start_vacation_time'];
var startVacationDuration = localStorage['start_vacation_duration']; //in minutes
localStorage.removeItem("start_vacation_time");
localStorage.removeItem("start_vacation_duration");
var vacationSecLeft = 0;
if(startVacationTime)
{
var vacationDate = Date.parse(startVacationTime);
var now = new Date();
var span = new TimeSpan(now - vacationDate);
var totalSec = span.getTotalMilliseconds() / 1000;
var vacationDurationSec = startVacationDuration * 60;
vacationSecLeft = vacationDurationSec - totalSec;
var minLeft = 0;
minLeft = Math.floor(vacationSecLeft / 60);
if(vacationSecLeft < 0){
minLeft = 0;
}
newVacationTime = vacationTime + minLeft;
localStorage['vacation_time'] = newVacationTime;
}
sendResponse({data: 1, new_vacation_time: newVacationTime});
}
else if(request.method == "optionsLinkClick")
{
console.log("options link click");
var navLink = request.navlink;
var extraHash = "";
if(navLink){
extraHash = navLink;
}
var fullUrl = chrome.extension.getURL("options.html" + extraHash);
chrome.tabs.create({
url: fullUrl
});
}
else if(request.method == "projectUrlClick")
{
console.log("project url click with " + request.project);
var fullUrl = "";
if(request.project == 'snipcss'){
fullUrl = "https://www.snipcss.com";
}
if(request.project == 'superanimo'){
fullUrl = "https://www.superanimo.com";
}
if(request.project == 'scrapehawk'){
fullUrl = "https://www.scrapehawk.com";
}
if(fullUrl != ""){
chrome.tabs.create({
url: fullUrl
});
}
}
else if(request.method == 'start_intervention'){
var currTime = new Date().getTime();
var iType = request.intervention_type;
var retSec = 180;
if(iType == 'intention'){
retSec = 240;
}
else if(iType == 'feelings'){
retSec = 240;
}
else if(iType == 'costbenefit'){
retSec = 240;
}
localStorage['intervention_active'] = 'yes';
localStorage['intervention_start'] = currTime;
localStorage['intervention_sec'] = retSec;
localStorage['intervention_type'] = iType;
//var d = new Date(currTime);
//var dateString = getDateStorageString("interventions", d);
//subtasks,costbenefit,feelings,intention
sendResponse({data: 1, time_left: retSec});
}
else if(request.method == 'complete_intervention'){
var activeIntervention = localStorage['intervention_active'];
if(!activeIntervention || activeIntervention != 'yes'){
//another process already completed or expired this intervention...
sendResponse({data: 0});
}
var iData = request['intervention_data'];
var iType = localStorage['intervention_type'];
var iStart = localStorage['intervention_start'];
var iSec = localStorage['intervention_sec'];
var unixTime = new Date().getTime();
var origTime = unixTime;
unixTime = unixTime - 60*60*4*1000;
var d = new Date(unixTime);
var dateString = getDateStorageString("interventions", d);
console.log("date string");
console.log(dateString);
var allTodayInterventions = localStorage[dateString];
if(!allTodayInterventions){
console.log("create new today tasks done");
allTodayInterventions = new Array();
}else{
allTodayInterventions = JSON.parse(allTodayInterventions);
}
console.log("all interventions before ");
console.log(allTodayInterventions);
allTodayInterventions.push({intervention_type: iType, intervention_start: iStart, intervention_sec: iSec,
intervention_data: iData, status: 'completed'});
console.log("all interventions after ");
console.log(allTodayInterventions);
localStorage[dateString] = JSON.stringify(allTodayInterventions);
localStorage['intervention_last_completed'] = origTime;
localStorage.removeItem("intervention_start");
localStorage.removeItem("intervention_sec");
localStorage.removeItem("intervention_type");
localStorage.removeItem("intervention_active");
chrome.tabs.query({},function(tabs){
tabs.forEach(function(tab){
//intervention could either be completed or expired
chrome.tabs.sendMessage(tab.id, {method: "intervention_status_change", status: "completed"}, null);
});
});
sendResponse({data: 1});
}
else if(request.method == 'expired_intervention'){
//just let the minute alarm do this...
}
else if(request.method == 'get_current_intervention'){
var activeIntervention = localStorage['intervention_active'];
var intLast = localStorage['intervention_last_completed'];
if(!activeIntervention || activeIntervention != 'yes'){
sendResponse({data: 0, intervention_last: intLast});
}
var intStart = localStorage['intervention_start'];
var intSec = localStorage['intervention_sec'];
var intType = localStorage['intervention_type'];
sendResponse({data: 1, intervention_start: intStart, intervention_sec: intSec, intervention_type: intType, intervention_last : intLast});
}
else if(request.method == 'get_intervention_stats'){
var unixTime = new Date().getTime();
var dayArr = new Array();
var allStats = new Array();
for(var x = 0; x < 21; x++){
var currdateTime = unixTime - (x * 86400000);
var currdate = new Date(currdateTime);
var storageString = getDateStorageString("interventions", currdate);
//dayArr.push(storageString);
var dateInterventions = localStorage[storageString];
if(!dateInterventions){
console.log("create new today tasks done");
dateInterventions = new Array();
}else{
dateInterventions = JSON.parse(dateInterventions);
}
var completeCount = 0;
var expiredCount = 0;
var totalCount = 0;
for(var m = 0; m < dateInterventions.length; m++){
var dInter = dateInterventions[m];
var theStatus = dInter['status'];
if(theStatus == "complete" || theStatus == "completed"){
completeCount++;
}else if(theStatus == 'expired'){
expiredCount++;
}
totalCount++;
}
var displayDate = currdate.toString("M/d/yyyy")
var aStat = {display_date: displayDate, completed: completeCount, expired: expiredCount, total: totalCount, data: dateInterventions};
allStats.push(aStat);
}
sendResponse({data: allStats});
}
else if(request.method == "closeMyTab")
{
var tabId = sender.tab.id;
var windowId = sender.tab.windowId;
//console.log("window id: " + windowId);
//We only remove if there is more than 1 tab in the window...
//We dont want to close chrome completely... so if there is only one tab, we go to google
//Thats why we need getAllInWindow
chrome.tabs.getAllInWindow(windowId, function(tabs)
{
//console.log("There are a total # of tabs: " + tabs.length);
if(tabs.length > 1) //Remove if the more than one tab...
{
chrome.tabs.remove(sender.tab.id);
}
else //Redirect if the only tab left (we dont want to close window)
{
var theUrl = localStorage['redirect_url'];
if(!theUrl)
{
/*REMOVE REMOVE REMOVE*/
theUrl = chrome.extension.getURL("options.html");
}
//I think this gets called repeatedly freezing the browser... try to only call it once
//by saving previous redirected url
var oldUrl = localStorage['redirected_url'];
if(!oldUrl || (oldUrl != sender.tab.url))
{
localStorage['redirected_url'] = sender.tab.url;
console.log("REDIRECTING2: " + theUrl);
chrome.tabs.update(sender.tab.id, {url: theUrl});
}
}
});
}
});
/******** CHROME HANDLERS ************/
chrome.extension.onRequest.addListener(function(request, sender, sendResponse)
{
console.log("DEPREACATED EVENT CALLED");
console.log("fix this - fix this - fix this - fix this - fix this");
console.log(request.method);
if (request.method == "getLocalStorage"){
sendResponse({data: localStorage[request.key]});
}
else if(request.method == "inactiveTabWillClose")
{
/******** CHANGE FAVICON FOR CLOSING INACTIVE??? ********/
}
else if(request.method == "optionsLinkClick")
{
var fullUrl = chrome.extension.getURL("options.html");
chrome.tabs.create({
url: fullUrl
})
//gotoOptPage(windowId, tabId);
}
else if(request.method == "visitedAllowedPage")
{
//Not sure what this was used for before...
//But I think I'll use it to calculate some respect if visiting allowed page
var currDate = Date.today().setTimeToNow();
var currUrl = "";
if(request.url)
{
currUrl = request.url;
}
//We store all urls in the past hour...
//This is needed for reducing the owl time on pages that user refreshes on
var allAllowedUrls = localStorage['allowed_history'];
if(!allAllowedUrls)
{
allAllowedUrls = new Array();
}
else
{
allAllowedUrls = jQuery.makeArray(jQuery.parseJSON(allAllowedUrls));
}
allAllowedUrls.push({url: currUrl, date: currDate.toString("M/d/yyyy HH:mm:ss")});
console.log("allowed url visited count: " + allAllowedUrls.length);
localStorage['allowed_history'] = JSON.stringify(allAllowedUrls);
}
else if(request.method == "saveForLater")
{
var saveUrl = request.the_url;
var saveObj =
{
saveUrl: request.the_url,
saveTime: request.the_time
};
var savedWebsites = localStorage["saved_websites"];
if(!savedWebsites)
{
console.log("NO WEBSITES SAVED, SAVING CURRENT: " + saveUrl);
savedWebsites = "";
var saveArr = new Array();
saveArr.push(saveObj);
localStorage['saved_websites'] = JSON.stringify(saveArr);
}
else
{
var websiteArr = jQuery.makeArray(jQuery.parseJSON(savedWebsites));
var alreadySavedIndex = -1;
for(var i = 0; i < websiteArr.length; i++)
{
var mySaveObj = websiteArr[i];
var aWebsite = mySaveObj.saveUrl;
if(mySaveObj.saveTime == undefined)
{
mySaveObj.saveTime = "Old Date";
aWebsite = websiteArr[i];
}
if(aWebsite == saveUrl)
{
alreadySavedIndex = i;
}
}
if(alreadySavedIndex >= 0)
{
websiteArr.splice(alreadySavedIndex, 1);
websiteArr.push(saveObj);
localStorage['saved_websites'] = JSON.stringify(websiteArr);
sendResponse({data: "page_already_saved"});
}
else //not found
{
sendResponse({data: "saved"});
websiteArr.push(saveObj);
localStorage['saved_websites'] = JSON.stringify(websiteArr);
}
}
//The array has the latest at the end
}
else{
sendResponse({data: "unknown request"});
}
return true;
});
chrome.tabs.onActivated.addListener(function(info)
{
var tab = chrome.tabs.get(info.tabId, function(tab)
{
chrome.tabs.sendRequest(tab.id, {tab_message: "now_active"});
});
});
function respectCalc(){
var unixTime = new Date().getTime();
var currdate = new Date(unixTime);
var hours = currdate.getHours();
var theHours = parseInt(hours);
//calculate after 9am
if(theHours < 9){
return;
}
var processArr = new Array();
for(var x = 1; x < 4; x++){
var currdateTime = unixTime - (x * 86400000);
var processDate = new Date(currdateTime);
var storageString = getDateStorageString("respectcalc", processDate);
var processData = localStorage[storageString];
if(!processData){
var respectObject = RESPECT.Calculator.calculateRespectScore(processDate);
processArr.push(respectObject);
var oldScore = parseFloat(respectObject['old_score']);
var newScore = parseFloat(respectObject['new_score']);
//shouldn't ever change that much
if(Math.abs(newScore - oldScore) < 3.0 && newScore > 0.0){
localStorage['respect_score'] = newScore;
}
localStorage[storageString] = JSON.stringify(respectObject);
}
}
return processArr;
}
chrome.alarms.onAlarm.addListener(function(alarm)
{
//console.log("alarm info");
//console.log(alarm);
if (alarm.name == 'hourly_alarm')
{
console.log("======================================");
console.log("HOURLY ALARM HAS FIRED AT");
//if time is after 7am... look back at if day respect processed or not
respectCalc();
}
else if (alarm.name == 'daily_alarm')
{
console.log("======================================");
console.log("DAILY ALARM HAS FIRED AT");
var currDate = Date.today().setTimeToNow();
console.log(currDate);
var allAllowedUrls = localStorage['allowed_history'];
var allAllowedUrlsCopy = new Array();
//Filter them based on time since visited
/*
if(!allAllowedUrls)
{
allAllowedUrls = new Array();
}
else
{
allAllowedUrls = jQuery.makeArray(jQuery.parseJSON(allAllowedUrls));
}
for(var u = 0; u < allAllowedUrls.length; u++)
{
var urlObj = allAllowedUrls[u];
var pastDate = Date.parse(urlObj.date);
var pastDomain = urlObj.url;
if(pastDate == null)
{
continue;
}
else if((currDate.getHours() - pastDate.getHours()) <= 1)
{
accrueVacation = true;
}
else
{
allAllowedUrlsCopy.push({url: urlObj.url, date: pastDate.toString("M/d/yyyy HH:mm:ss")} );
}
}
*/
//localStorage['allowed_history'] = allAllowedUrlsCopy;
//localStorage['vacation_time'] = 0;
console.log("======================================");
}
else if(alarm.name == 'minute_alarm'){
console.log("======================================");
console.log("MINUTE ALARM FIRED");
var turnOffStats = localStorage['turn_off_stats'];
if(!turnOffStats || turnOffStats == 'no'){
chrome.tabs.query({
active: true,
currentWindow: true // In the current window
}, function(activeTabs) {
// Since there can only be one active tab in one active window,
// the array has only one element
if(activeTabs.length <= 0){
console.log("no active tabs?");
return;
}
var tab = activeTabs[0];
console.log("tab");
console.log(tab);
chrome.tabs.sendMessage(tab.id, {method: "record_domain"}, function(response){
//console.log("minute recorddomain alarm response");
//console.log(response);
if(response.data == 'active'){
var recordDomain = response.domain;
var unixTime = new Date().getTime();
unixTime = unixTime - 60*60*4*1000;
//minus 4 hours... so yesterday's day lasts until 4 am today
var d = new Date(unixTime);
var dateString = getDateStorageString("bstats", d);
//console.log("storage stats");
//console.log(dateString);
var allPastUrls = localStorage[dateString];
//console.log(allPastUrls);
if(!allPastUrls){
//console.log("create new");
allPastUrls = {};
}else{
allPastUrls = JSON.parse(allPastUrls);
}
//console.log("all past urls before ");
//console.log(allPastUrls);
if(recordDomain in allPastUrls){
var currCount = allPastUrls[recordDomain];
currCount++;
allPastUrls[recordDomain] = currCount;
}else{
allPastUrls[recordDomain] = 1;
}
//console.log("all past urls after ");
//console.log(JSON.stringify(allPastUrls));
localStorage[dateString] = JSON.stringify(allPastUrls);
}
});
});
}
//console.log("checking interventions");
var activeIntervention = localStorage['intervention_active'];