forked from AllMangasReader-dev/AMR
/
background.js
2258 lines (2152 loc) · 65.1 KB
/
background.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
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 file is part of All Mangas Reader.
All Mangas Reader is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
All Mangas Reader is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with All Mangas Reader. If not, see <http://www.gnu.org/licenses/>.
*/
//jQuery.noConflict();
var mangaList;
var mirrors;
var ctxIds = [];
var bookmarks;
var actMirrors;
var timeoutChap;
var timeoutMg;
var timeoutWs;
var updatews = 86400 * 1000;
var canvas;
var canvasContext;
var animationFrames = 20;
var animationSpeed = 30;
var rotation = 0;
var sharinganImage;
pstat.init();
wssql.init();
amrcsql.init();
/**
* Returns the week number for this date. dowOffset is the day of week the week
* "starts" on for your locale - it can be from 0 to 6. If dowOffset is 1 (Monday),
* the week returned is the ISO 8601 week number.
* @param int dowOffset
* @return int
*/
Date.prototype.getWeek = function (dowOffset) {
/*getWeek() was developed by Nick Baicoianu at MeanFreePath: http://www.meanfreepath.com */
dowOffset = typeof(dowOffset) == 'int' ? dowOffset : 0; //default dowOffset to zero
var newYear = new Date(this.getFullYear(), 0, 1);
var day = newYear.getDay() - dowOffset; //the day of week the year begins on
day = (day >= 0 ? day : day + 7);
var daynum = Math.floor((this.getTime() - newYear.getTime() -
(this.getTimezoneOffset() - newYear.getTimezoneOffset()) * 60000) / 86400000) + 1;
var weeknum;
//if the year starts before the middle of a week
if (day < 4) {
weeknum = Math.floor((daynum + day - 1) / 7) + 1;
if (weeknum > 52) {
nYear = new Date(this.getFullYear() + 1, 0, 1);
nday = nYear.getDay() - dowOffset;
nday = nday >= 0 ? nday : nday + 7;
/*if the next year starts before the middle of
the week, it is week #1 of that year*/
weeknum = nday < 4 ? 1 : 53;
}
} else {
weeknum = Math.floor((daynum + day - 1) / 7);
}
return weeknum;
};
// Here we shouldn't be using this...
var sync = new BSync({
getUpdate : function () {
//console.log('GET UPDATE');
var params = getParameters();
return params.updated;
},
onRead : function (json, bookmark) {
console.log('Reading incoming synchronisation');
if (!(json == undefined || json == null || json == "null")) {
console.log(' - Updating incoming entries');
// var lstTmp = $A(eval('(' + json.mangas + ')')); --> remove prototype usage
lstTmp = JSON.parse(json.mangas);
for (var i = 0; i < lstTmp.length; i++) {
var tmpManga = new MangaElt(lstTmp[i]);
console.log("\t - Reading manga entry : " + tmpManga.name + " in mirror : " + tmpManga.mirror);
var mangaExist = isInMangaList(tmpManga.url);
if (mangaExist == null) {
console.log("\t --> Manga not found in current list, adding manga... ");
if (!isMirrorActivated(tmpManga.mirror)) {
activateMirror(tmpManga.mirror);
}
var last = mangaList.length;
mangaList[last] = tmpManga;
mangaList[last].refreshLast();
try {
_gaq.push(['_trackEvent', 'AddManga', tmpManga.mirror, tmpManga.name]);
//pageTracker._trackEvent('AddManga', newManga.name);
} catch (e) {}
} else {
//Verify chapter last
console.log("\t --> Manga found in current list, verify last chapter read. incoming : " + tmpManga.lastChapterReadURL + "; current : " + mangaExist.lastChapterReadURL);
mangaExist.consult(tmpManga);
saveList();
}
}
console.log(' - Deleting mangas not in incoming list');
var deleteAr = [];
for (var i = 0; i < mangaList.length; i++) {
var found = false;
for (var j = 0; j < lstTmp.length; j++) {
var tmpManga = new MangaElt(lstTmp[j]);
if (mangaList[i].url == tmpManga.url) {
found = true;
break;
}
}
if (!found) {
console.log("\t - Deleting manga entry in current list : " + mangaList[i].name + " in mirror : " + mangaList[i].mirror);
deleteAr[deleteAr.length] = i;
}
}
for (var i = deleteAr.length - 1; i >= 0; i--) {
mangaList.remove(deleteAr[i], deleteAr[i]);
}
}
saveList();
refreshUpdateWith(this.syncedAt);
refreshSync();
},
onWrite : function () {
console.log('Writing current configuration to synchronise');
var params = getParameters();
this.write({
'mangas' : getJSONListToSync(),
'updated' : params.updated,
});
refreshSync();
}
});
function replaceInUrls(url, find, rep) {
var res = url;
if (url != undefined && url != null && url.indexOf(find) != -1) {
res = url.replace(find, rep);
}
return res;
}
/* Initialise the list of followed mangas */
function init() {
getMirrors(function (mirrorsT) {
mirrors = mirrorsT;
//Do not store mangas in mirrors.
// Braiam: why the use of localStorage?
var doDeleteMirs = false;
for (var i = 0; i < mirrors.length; i++) {
if (localStorage[mirrors[i].mirrorName]) {
doDeleteMirs = true;
localStorage.removeItem(mirrors[i].mirrorName);
}
}
if (doDeleteMirs) {
//Do refresh manga lists...
localStorage.removeItem("lastMangasUpdate");
}
initMirrorState();
actMirrors = [];
for (var i = 0; i < mirrors.length; i++) {
if (isMirrorActivated(mirrors[i].mirrorName)) {
actMirrors[actMirrors.length] = mirrors[i];
}
}
list = localStorage["mangas"];
mangaList = new Array();
if (!(list == undefined || list == null || list == "null")) {
var lstTmp = JSON.parse(list);
for (var i = 0; i < lstTmp.length; i++) {
// ########### CONVERSION submanga.me en submanga.com
lstTmp[i].url = replaceInUrls(lstTmp[i].url, "submanga.me", "submanga.com");
lstTmp[i].lastChapterReadURL = replaceInUrls(lstTmp[i].lastChapterReadURL, "submanga.me", "submanga.com");
lstTmp[i].url = replaceInUrls(lstTmp[i].url, "www.mangafox.com", "mangafox.me");
lstTmp[i].lastChapterReadURL = replaceInUrls(lstTmp[i].lastChapterReadURL, "www.mangafox.com", "mangafox.me");
mangaList[i] = new MangaElt(lstTmp[i]);
}
}
//Load bookmarks
var bms = localStorage["bookmarks"];
//bookmarks = $A(eval('(' + bms + ')')); --> remove eval function
// If there are no bookmarks just skip it.
if (bms !== undefined) {
bookmarks = JSON.parse(bms);
} else {
bookmarks = [];
}
var pars = getParameters();
//console.log("Determine version ");
var ancVersion = localStorage["version"];
if (ancVersion == undefined || ancVersion == null || ancVersion == "null") {
ancVersion = null;
}
var curVersion = chrome.extension.getVersion();
if (ancVersion === null || curVersion !== ancVersion) {
localStorage.version = curVersion;
if (pars.openupdate === 1) {
if (chrome.extension.isBeta()) {
chrome.tabs.create({
"url" : "https://github.com/AllMangasReader-dev/AMR/commits/develop"
});
} else {
chrome.tabs.create({
"url" : "http://wiki.allmangasreader.com/changelog"
});
}
}
try {
if (chrome.extension.isBeta()) {
_gaq.push(['_trackEvent', 'Install', 'Beta Channel', curVersion]);
} else {
_gaq.push(['_trackEvent', 'Install', curVersion]);
}
//pageTracker._trackEvent('Install', curVersion);
} catch (e) {}
localStorage.versionViewRelease = localStorage.version;
}
//IsBeta returns true if homepage_url (in manifest) contains github.com. homepage_url is changed by auto release tool... if it's beta channel, contains the link to github latest commit.
if (chrome.extension.isBeta()) {
chrome.browserAction.setTitle({
title : "All Mangas Reader Beta Channel " + curVersion
});
} else {
chrome.browserAction.setTitle({
title : "All Mangas Reader " + curVersion
});
}
//Log daily users
var dailyStr = getDailyStr();
var weeklyStr = getWeeklyStr();
var monthlyStr = getMonthlyStr();
if (localStorage["dailyStr"] == undefined || localStorage["dailyStr"] != dailyStr) {
try {
_gaq.push(['_trackEvent', 'Active', 'Day', dailyStr]);
localStorage["dailyStr"] = dailyStr;
} catch (e) {}
}
if (localStorage["weeklyStr"] == undefined || localStorage["weeklyStr"] != weeklyStr) {
try {
_gaq.push(['_trackEvent', 'Active', 'Week', weeklyStr]);
localStorage["weeklyStr"] = weeklyStr;
} catch (e) {}
}
if (localStorage["monthlyStr"] == undefined || localStorage["monthlyStr"] != monthlyStr) {
try {
_gaq.push(['_trackEvent', 'Active', 'Month', monthlyStr]);
localStorage["monthlyStr"] = monthlyStr;
} catch (e) {}
}
//console.log("Save current localSto ");
saveList();
if (pars.sync == 1) {
console.log("synchronization started");
sync.start();
}
//console.log("Refresh last chapters ");
if (!localStorage["lastChaptersUpdate"]) {
refreshAllLasts();
} else {
if (pars.checkmgstart == 1) {
refreshAllLasts();
} else {
var nextTime = new Date().getTime() - localStorage["lastChaptersUpdate"] - pars.updatechap;
if (nextTime > 0) {
refreshAllLasts();
} else {
console.log("Next time to refresh chapters list : " + (-nextTime));
timeoutChap = setTimeout(function () {
refreshAllLasts();
}, -nextTime);
}
}
}
if (!localStorage["lastMangasUpdate"]) {
refreshMangaLists();
} else {
var nextTime = new Date().getTime() - localStorage["lastMangasUpdate"] - pars.updatemg;
if (nextTime > 0) {
refreshMangaLists();
} else {
console.log("Next time to refresh manga list : " + (-nextTime));
timeoutMg = setTimeout(function () {
refreshMangaLists();
}, -nextTime);
//Test if there is new mirrors
refreshNewMirrorsMangaLists();
}
}
if (!localStorage["lastWsUpdate"]) {
refreshWebsites();
} else {
var nextTime = new Date().getTime() - localStorage["lastWsUpdate"] - updatews;
if (nextTime > 0) {
refreshWebsites();
} else {
console.log("Next time to refresh websites : " + (-nextTime));
timeoutWs = setTimeout(function () {
refreshWebsites();
}, -nextTime);
}
}
});
}
function getDailyStr() {
var d = new Date();
var m = d.getMonth() + 1;
var mstr = "" + m;
if (m < 10) {
mstr = "0" + mstr;
}
var j = d.getDate();
var jstr = "" + j;
if (j < 10) {
jstr = "0" + jstr;
}
return d.getFullYear() + "/" + mstr + "/" + jstr;
}
function getWeeklyStr() {
var d = new Date();
var w = d.getWeek();
var wstr = "" + w;
if (w < 10) {
wstr = "0" + wstr;
}
return d.getFullYear() + "/" + wstr;
}
function getMonthlyStr() {
var d = new Date();
var m = d.getMonth() + 1;
var mstr = "" + m;
if (m < 10) {
mstr = "0" + mstr;
}
return d.getFullYear() + "/" + mstr;
}
function formatMgName(name) {
if (name == undefined || name == null || name == "null")
return "";
return name.trim().replace(/[^0-9A-Za-z]/g, '').toUpperCase();
}
function getSimilarMangaTitles(mg) {
var res = [];
var titMg = formatMgName(mg.name);
for (var i = 0; i < mangaList.length; i++) {
if (formatMgName(mangaList[i].name) == titMg) {
res[res.length] = mangaList[i];
}
}
return res;
}
function readManga(request, callback, doSave) {
//console.log("READ MANGA : " + $H(request).toJSON());
var mangaExist = isInMangaList(request.url);
if (mangaExist == null) {
//Add the new Manga
var newManga = new MangaElt(request);
//Search for similar manga names to mark read top
var simMgs = getSimilarMangaTitles(newManga);
if (simMgs.length > 0) {
for (var i = 0; i < simMgs.length; i++) {
if (simMgs[i].read == 1) {
newManga.read = 1;
break;
}
if (simMgs[i].update == 0) {
newManga.update = 0;
break;
}
}
}
//Add new manga
mangaList[mangaList.length] = newManga;
//Refresh chapters
newManga.refreshLast();
refreshUpdate();
saveList();
try {
_gaq.push(['_trackEvent', 'AddManga', newManga.mirror, newManga.name]);
//pageTracker._trackEvent('AddManga', newManga.name);
} catch (e) {}
} else {
var ancLstChap = mangaExist.lastChapterReadURL;
if (ancLstChap != undefined) {
//Refresh manga item
mangaExist.consult(request);
if (doSave) {
saveList();
}
if (mangaExist.lastChapterReadURL != ancLstChap) {
refreshUpdate();
}
try {
_gaq.push(['_trackEvent', 'ReadManga', request.mirror, request.name]);
_gaq.push(['_trackEvent', 'ReadMangaChapter', request.name, request.lastChapterReadName]);
//pageTracker._trackEvent('ReadManga', request.name, request.lastChapterReadName);
} catch (e) {}
}
}
callback();
}
function killManga(request, callback, doSave) {
var mangaExist = isInMangaListId(request.url);
if (mangaExist != -1) {
var mirName = mangaList[mangaExist].mirror;
var mgName = mangaList[mangaExist].name;
mangaList.remove(mangaExist, mangaExist);
if (doSave) {
saveList();
refreshUpdate();
}
try {
_gaq.push(['_trackEvent', 'DeleteManga', mirName, mgName]);
//pageTracker._trackEvent('DeleteManga', request.name);
} catch (e) {}
}
callback();
}
function resetManga(request, callback, doSave) {
var mangaExist = isInMangaList(request.url);
if (mangaExist != null) {
if (mangaExist.listChaps.length > 0) {
mangaExist.lastChapterReadURL = mangaExist.listChaps[mangaExist.listChaps.length - 1][1];
mangaExist.lastChapterReadName = mangaExist.listChaps[mangaExist.listChaps.length - 1][0];
if (doSave) {
saveList();
refreshUpdate();
}
try {
_gaq.push(['_trackEvent', 'ResetManga', mangaExist.mirror, mangaExist.name]);
//pageTracker._trackEvent('DeleteManga', request.name);
} catch (e) {}
}
}
callback();
}
function markReadTop(request, callback, doSave) {
var mangaExist = isInMangaList(request.url);
if (mangaExist != null) {
mangaExist.read = request.read;
if (doSave) {
saveList();
refreshUpdate();
}
try {
if (request.read == 1) {
_gaq.push(['_trackEvent', 'SetReadState', mangaExist.mirror, mangaExist.name]);
} else {
_gaq.push(['_trackEvent', 'ReleaseReadState', mangaExist.mirror, mangaExist.name]);
}
//pageTracker._trackEvent('DeleteManga', request.name);
} catch (e) {}
}
callback();
}
function markUpdateTop(request, callback, doSave) {
var mangaExist = isInMangaList(request.url);
if (mangaExist != null) {
mangaExist.update = request.update;
if (doSave) {
saveList();
refreshUpdate();
}
try {
if (request.update == 1) {
_gaq.push(['_trackEvent', 'SetUpdateState', mangaExist.mirror, mangaExist.name]);
} else {
_gaq.push(['_trackEvent', 'ReleaseUpdateState', mangaExist.mirror, mangaExist.name]);
}
//pageTracker._trackEvent('DeleteManga', request.name);
} catch (e) {}
}
callback();
}
function addCategory(request, callback, doSave) {
var mangaExist = isInMangaList(request.url);
if (mangaExist != null) {
var isFound = false;
if (mangaExist.cats.length > 0) {
for (var i = 0; i < mangaExist.cats.length; i++) {
if (mangaExist.cats[i] == request.cat) {
isFound = true;
break;
}
}
}
if (!isFound) {
mangaExist.cats[mangaExist.cats.length] = request.cat;
}
if (doSave) {
saveList();
refreshUpdate();
}
}
callback();
}
function removeCatManga(request, callback, doSave) {
var mangaExist = isInMangaList(request.url);
if (mangaExist != null) {
var isFound = -1;
if (mangaExist.cats.length > 0) {
for (var i = 0; i < mangaExist.cats.length; i++) {
if (mangaExist.cats[i] == request.cat) {
isFound = i;
break;
}
}
}
if (isFound != -1) {
mangaExist.cats.remove(isFound, isFound);
}
if (doSave) {
saveList();
refreshUpdate();
}
}
callback();
}
function removeCategory(request, callback) {
if (mangaList.length > 0) {
for (var i = 0; i < mangaList.length; i++) {
if (mangaList[i].cats.length > 0) {
var toRem = [];
for (var j = 0; j < mangaList[i].cats.length; j++) {
if (mangaList[i].cats[j] == request.cat) {
toRem[toRem.length] = j;
}
}
for (var j = toRem.length - 1; j >= 0; j--) {
mangaList[i].cats.remove(toRem[j], toRem[j]);
}
}
}
}
saveList();
refreshUpdate();
callback();
}
function editCategory(request, callback) {
if (mangaList.length > 0) {
for (var i = 0; i < mangaList.length; i++) {
if (mangaList[i].cats.length > 0) {
var toRem = [];
for (var j = 0; j < mangaList[i].cats.length; j++) {
if (mangaList[i].cats[j] == request.cat) {
mangaList[i].cats[j] = request.newcat;
}
}
}
}
}
saveList();
refreshUpdate();
callback();
}
chrome.extension.onRequest.addListener(
function (request, sender, sendResponse) {
if (request.action == "readManga") {
readManga(request, function () {
sendResponse({});
}, true);
}
if (request.action == "readMangas") {
$.each(request.list, function (index, val) {
readManga(val, function () {}, false);
});
saveList();
refreshUpdate();
sendResponse({});
}
if (request.action == "killManga") {
killManga(request, function () {
sendResponse({});
}, true);
}
if (request.action == "killMangas") {
$.each(request.list, function (index, val) {
killManga(val, function () {}, false);
});
saveList();
refreshUpdate();
sendResponse({});
}
if (request.action == "resetManga") {
resetManga(request, function () {
sendResponse({});
}, true);
}
if (request.action == "resetMangas") {
$.each(request.list, function (index, val) {
resetManga(val, function () {}, false);
});
saveList();
refreshUpdate();
sendResponse({});
}
if (request.action == "setMangaChapter") {
resetManga(request, function () {
readManga(request, function () {
sendResponse({});
}, true);
}, false);
}
if (request.action == "markReadTop") {
markReadTop(request, function () {
if (request.updatesamemangas) {
//If group mode
if (getParameters().groupmgs == 1) {
//Look for mangas with same name...
var mangaExist = isInMangaList(request.url);
if (mangaExist != null) {
var othMg = getSimilarMangaTitles(mangaExist);
if (othMg.length > 0) {
for (var i = 0; i < othMg.length; i++) {
othMg.read = request.read;
}
saveList();
refreshUpdate();
}
}
}
sendResponse({});
} else {
sendResponse({});
}
}, true);
}
if (request.action == "markReadTops") {
$.each(request.list, function (index, val) {
markReadTop(val, function () {}, false);
});
saveList();
refreshUpdate();
sendResponse({});
}
if (request.action == "markUpdateTop") {
markUpdateTop(request, function () {
if (request.updatesamemangas) {
//If group mode
if (getParameters().groupmgs == 1) {
//Look for mangas with same name...
var mangaExist = isInMangaList(request.url);
if (mangaExist != null) {
var othMg = getSimilarMangaTitles(mangaExist);
if (othMg.length > 0) {
for (var i = 0; i < othMg.length; i++) {
othMg.update = request.update;
}
saveList();
refreshUpdate();
}
}
}
sendResponse({});
} else {
sendResponse({});
}
}, true);
}
if (request.action == "markUpdateTops") {
$.each(request.list, function (index, val) {
markUpdateTop(val, function () {}, false);
});
saveList();
refreshUpdate();
sendResponse({});
}
if (request.action == "addCategory") {
addCategory(request, function () {
sendResponse({});
}, true);
}
if (request.action == "addCategories") {
$.each(request.list, function (index, val) {
addCategory(val, function () {}, false);
});
saveList();
refreshUpdate();
sendResponse({});
}
if (request.action == "removeCategory") {
removeCategory(request, function () {
sendResponse({});
});
}
if (request.action == "removeCatManga") {
removeCatManga(request, function () {
sendResponse({});
}, true);
}
if (request.action == "removeCatMangas") {
$.each(request.list, function (index, val) {
removeCatManga(val, function () {}, false);
});
saveList();
refreshUpdate();
sendResponse({});
}
if (request.action == "editCategory") {
editCategory(request, function () {
sendResponse({});
});
}
if (request.action == "mangaInfos") {
var mangaExist = isInMangaList(request.url);
if (mangaExist != null) {
sendResponse({
read : mangaExist.read,
display : mangaExist.display
});
} else {
sendResponse(null);
}
}
if (request.action == "setDisplayMode") {
var mangaExist = isInMangaList(request.url);
if (mangaExist != null) {
mangaExist.display = request.display;
saveList();
refreshUpdate();
sendResponse({});
} else {
sendResponse({});
}
}
if (request.action == "parameters") {
sendResponse(getParameters());
}
if (request.action == "saveparameters") {
var obj = {};
obj.displayAdds = request.displayAdds;
obj.displayChapters = request.displayChapters;
obj.displayMode = request.displayMode;
obj.popupMode = request.popupMode;
obj.omSite = request.omSite;
obj.newTab = request.newTab;
obj.sync = request.sync;
obj.displayzero = request.displayzero;
obj.pub = request.pub;
obj.dev = request.dev;
obj.load = request.load;
obj.resize = request.resize;
obj.color = request.color;
obj.imgorder = request.imgorder;
obj.groupmgs = request.groupmgs;
obj.openupdate = request.openupdate;
obj.updatechap = request.updatechap;
obj.updatemg = request.updatemg;
obj.newbar = request.newbar;
obj.addauto = request.addauto;
obj.lrkeys = request.lrkeys;
obj.size = request.size;
obj.autobm = request.autobm;
obj.prefetch = request.prefetch;
obj.shownotifications = request.shownotifications;
obj.notificationtimer = request.notificationtimer;
obj.rightnext = request.rightnext;
obj.refreshspin = request.refreshspin;
obj.savebandwidth = request.savebandwidth;
obj.checkmgstart = request.checkmgstart;
obj.nocount = request.nocount;
obj.displastup = request.displastup;
obj.markwhendownload = request.markwhendownload;
obj.sendstats = request.sendstats;
obj.shownotifws = request.shownotifws;
var ancParams = getParameters();
obj.updated = ancParams.updated;
obj.syncAMR = ancParams.syncAMR;
// start sync or stop sync if different
if (ancParams.sync != obj.sync) {
if (obj.sync == 1) {
console.log("synchronization started (parameter update)");
sync.start();
try {
_gaq.push(['_trackEvent', 'Sync', 'activate']);
//pageTracker._trackEvent('DeleteManga', request.name);
} catch (e) {}
} else {
console.log("synchronization stopped (parameter update)");
sync.stop();
try {
_gaq.push(['_trackEvent', 'Sync', 'desactivate']);
//pageTracker._trackEvent('DeleteManga', request.name);
} catch (e) {}
}
}
// localStorage["parameters"] = $H(obj).toJSON(); --> remove prototype usage
localStorage["parameters"] = JSON.stringify(obj);
if (ancParams.displayzero != obj.displayzero) {
refreshTag();
}
if (ancParams.nocount != obj.nocount) {
drawIcon(false);
refreshTag();
}
if (ancParams.groupmgs != obj.groupmgs) {
refreshTag();
}
if (ancParams.updatechap != obj.updatechap) {
refreshAllLasts(true, false);
}
if (ancParams.updatemg != obj.updatemg) {
refreshMangaLists(true, false);
}
sendResponse({});
}
if (request.action == "opentab") {
chrome.tabs.create({
"url" : request.url
});
sendResponse({});
}
if (request.action == "openExtensionMainPage") {
//TO_CHANGE
chrome.tabs.create({
"url" : "http://www.allmangasreader.com/"
});
try {
//_gaq.push(['_trackEvent', 'ClickPub', 'OneMangaBanner']);
//pageTracker._trackEvent('ClickPub', 'OneMangaBanner');
} catch (e) {}
sendResponse({});
}
if (request.action == "save") {
saveList();
sendResponse({});
}
if (request.action == "mirrors") {
getFilledMirrorsDesc(activatedMirrors(), function (mirrorsDesc) {
sendResponse(mirrorsDesc);
});
}
if (request.action == "actmirrors") {
getActivatedMirrorsWithList({
list : activatedMirrors()
}, function (mirrorsDesc) {
sendResponse(mirrorsDesc);
});
}
if (request.action == "searchManga") {
if (getMangaMirror(request.mirrorName) != null) {
getMangaMirror(request.mirrorName).getMangaList(request.search, function (mirror, lst) {
request.list = lst;
sendResponse(request);
});
} else {
request.list = [];
sendResponse(request);
}
}
if (request.action == "searchMirrorsState") {
sendResponse({
res : localStorage["searchMirrorsState"]
});
}
if (request.action == "pagematchurls") {
doesCurrentPageMatchManga(request.url, activatedMirrors(), function (_isOk, _mirrorName, _implementationURL) {
//Load remote script implementation corresponding to current website
var docache = true;
// if the url isn't ok send false and exit function
if(!_isOk) {
sendResponse({ isOk: false});
return;
}
if (_implementationURL !== null && _implementationURL.indexOf(".php") != -1) {
docache = false;
}
$.loadScript(_implementationURL, docache, function (sScriptBody, textStatus, jsXHR) {
//Execute it in the concerned tab context
chrome.tabs.executeScript(sender.tab.id, {
code : sScriptBody
}, function () {
//Return to content script...
sendResponse({
isOk : _isOk,
mirrorName : _mirrorName,
implURL : _implementationURL
});
});
}, function() {
// error managing
console.log("Script " + _mirrorName + " failed to be loaded in page...");
sendResponse({ isOk : false });
}, 'text');
});
}
if (request.action == "deletepub") {
var params = getParameters();
params.pub = 0;
localStorage["parameters"] = JSON.stringify(params);
sendResponse({});
}
if (request.action == "getListManga") {
wssql.webdb.getMangaList(request.mirror, function (list) {
var mangas = list;
if (mangas != undefined && mangas != null && mangas.length > 0) {
sendResponse(mangas);
} else {
if (getMangaMirror(request.mirror) != null) {
getMangaMirror(request.mirror).getMangaList("", function (name, lst) {
sendResponse(lst);
});
} else {
sendResponse([]);
}
}
});
}
if (request.action == "getListChap") {
var mangaExist = isInMangaList(request.url);
if (mangaExist == null) {
if (getMangaMirror(request.mirror) != null) {
getMangaMirror(request.mirror).getListChaps(request.mangaUrl, request.mangaName, null, function (lst, obj) {
sendResponse(lst);
});
} else {
sendResponse([]);
}
} else {
sendResponse(mangaExist.listChaps);
}
}
if (request.action == "nbMangaInMirror") {
var nb = 0;
for (var i = 0; i < mangaList.length; i++) {
if (mangaList[i].mirror == request.mirror) {
nb++;
}
}
sendResponse({
number : nb,
mirror : request.mirror
});
}
if (request.action == "activateMirror") {
activateMirror(request.mirror);
try {
_gaq.push(['_trackEvent', 'ActivateMirror', request.mirror]);
} catch (e) {}
sendResponse({});
}
if (request.action == "desactivateMirror") {
desactivateMirror(request.mirror);
try {
_gaq.push(['_trackEvent', 'DesactivateMirror', request.mirror]);
} catch (e) {}
sendResponse({});
}
if (request.action == "isMirrorActivated") {
sendResponse({
mirror : request.mirror,
activated : isMirrorActivated(request.mirror)
});