-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
executable file
·2714 lines (2396 loc) · 93.3 KB
/
script.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
/*
Open-source, copyright free, non-commercial. Make it better!
tiny_meter.png & meter_sprite.png files are the property of Rotten Tomatoes.
TO-DO LIST:
* make it work for tv pages
needs an entirely separate ratings & critics database
season to season graph would be cool
* allow users to compare similarity with each other
* stats/graphs about similarity score
* e.g. bell-curve of similarity of all critics
*/
///////////////////////// //// //// //// //// ////
// // //// //// //// //// ////
// DATA STRUCTURE ///// //// //// //// //// ////
// VERSION 6 ///// //// //// //// //// ////
// // //// //// //// //// ////
///////////////////////// //// //// //// //// ////
/*
ratingsArray
============
Saved to local storage. Contains all critics this user has ever encountered.
Structure:
ratingsArray[0] = header row
ratingsArray[0][0] = first column: dataVersion
ratingsArray[0][1] = second column: user info
ratingsArray[0][j] = subsequent columns: critic info
ratingsArray[0][j][k] =
0 = critic name
1 = critic url path
2 = top critic flag
3 = (unused)
ratingsArray[i] = subsequent rows: movie ratings
ratingsArray[i][0] = first column: movie info
ratingsArray[i][0][k] =
0 = film name
1 = film path
2 = (unused)
ratingsArray[i][1] = second column: user's ratings
ratingsArray[i][j] = subsequent columns: critics' ratings
ratingsArray[i][j] = rating of this film
criticsArray
============
Temporary data for this page. Only contains critics of this film.
Structure:
criticsArray[y] = each row: a critic
0 = criticName;
1 = criticPath;
2 = criticIsTop;
3 = criticRating;
4 = reviewPath;
5 = reviewBlurb;
6 = films on common with user
7 = similarity scrore to user
8 = baysian sort score
*/
///////////////////////// //// //// //// //// ////
// // //// //// //// //// ////
// GLOBAL ///// //// //// //// //// ////
// VARIABLES ///// //// //// //// //// ////
// // //// //// //// //// ////
///////////////////////// //// //// //// //// ////
// open a messaging port to the event page
// this will prevent the eventPage from suspending
var messagePort = chrome.runtime.connect({name: 'readiness'});
var debug = '';
var storage = chrome.storage.local;
var appVersion = chrome.runtime.getManifest().version;
var userIDRT = '';
var pageFilmIndex = 0;
var pageFilmPath = '';
var pageFilmName = '';
var ratingsArray = [];
var criticsArray = [];
var favoritesArray = [];
var pagesScraped = 1;
var totalUserRatings = 0;
var dataReadyTimer = 0;
var updatingTimer = 0;
var starMatchTimer = 0;
var frTimer = 0;
var partialImportTimer = 0;
var loginTimer = 0;
var toolTipData = [];
var hasScrolledReviews = false;
// firstRun data
var firstRun = '';
var frPrivacy = 'NON';
var frMoviesFound = 0;
var frMoviesTried = 0;
var frMoviesImported = 0;
var frRatingsCount = 0;
var frRatingsImported = 0;
var frImportMessage = '';
var debuggingMessage = '';
var frContinueImport = true;
var frUserRatingsArray = [];
var frMoviesXhrs = [];
var frMoviesDeferred, frMoviesDeferreds = [];
var criticRatingsXhrs = [];
var criticRatingsDeferred, criticRatingsDeferreds = [];
var frPercent = 0;
var frStatusTitle = '';
// score panel info
var spAllScore = 0;
var spAllCount = 0;
var spTopScore = 0;
var spTopCount = 0;
var spAudienceScore = 0;
var spAudienceCount = '';
var spAudienceAverage = 0;
var spConsensus = '';
var spSynopsis = '';
// Rotten Tomatoes' element IDs that are subject to change
// if and when RT updates their code
var userLoginArea = '#navbar .header_links';
var userRatingsLink = '#headerUserSection .ratings a';
var elUserRatingRow = '.media-body';
var elUserRatingFilmLink = '.media-heading a';
var elUserRatingStars = '.glyphicon-star';
var userPrivacySetting = '.content_body input';
var userPrivacyAlert = '#headerUserSection .name a';
var freshPick = '#header-certified-fresh-picks a';
var loginLinkRT = '#header-top-bar-login';
var ratingWidgetTarget = '#topSection';
var starWidgetRT = '#rating_widget_desktop .rating_stars';
var starWidgetStarRT = 'data-rating-value';
var poster = '#movie-image-section div';
var elScorePanel = '.score-panel-wrap';
var reviewsPageCount = '.pageInfo';
var criticsCount = '#criticHeaders';
var elReviewsScrapeLink = '#criticHeaders a:nth-child(1)';
var elReviewsListRow = '.review_table_row';
var elReviewBlurb = '.the_review';
var elReviewPath = '.review_desc a';
var elReviewIcon = '.review_icon';
var elAnnoyingHeader1 = '.leaderboard_wrapper';
var elAnnoyingHeader2 = '#header-main';
var elCriticName = '.critic_name a:nth-child(1)';
var elCriticPub = '.critic_name em';
var elCriticIsTop = '.top_critic';
var elAllScore = '#all-critics-numbers .meter-value span';
var elAllCount = '#all-critics-numbers #scoreStats div:nth-child(2) span:nth-child(2)';
var elTopScore = '#top-critics-numbers .meter-value span';
var elTopCount = '#top-critics-numbers #scoreStats div:nth-child(2) span:nth-child(2)';
var elAudienceScore = '.audience-score .meter-value span';
var elAudienceCount = '.audience-info div:nth-child(2) span';
var elAudienceAverage = '.audience-info div:nth-child(1) span';
var elConsensus = '.critic_consensus';
var elConsensusJunk = '.superPageFontColor';
var elSynopsis = '#movieSynopsis';
var elPageFilmName = '.mop-ratings-wrap__title--top';
///////////////////////// //// //// //// //// ////
// // //// //// //// //// ////
// ACTION ///// //// //// //// //// ////
// ON LOAD ///// //// //// //// //// ////
// // //// //// //// //// ////
///////////////////////// //// //// //// //// ////
chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) {
show_about_modal();
});
messagePort.postMessage({status: 'sendFirstRun'});
messagePort.onMessage.addListener(function(msg) {
if(msg.firstRun) {
firstRun = msg.firstRun[0];
var type = '';
var previousVersion = msg.firstRun[1];
if(previousVersion != appVersion && $(elPageFilmName).length>0) {
if(parseInt(previousVersion.substr(0,1)) < 3) {
type = 'v3'
appUpdate_showModal(type);
}
if(parseInt(previousVersion.substr(2,1)) == 0 && parseInt(previousVersion.substr(4,previousVersion.length)) < 13) {
type = 'v3.0.13'
appUpdate_showModal(type);
}
}
messagePort.postMessage({status: 'sendRatingsArray'});
}
if(msg.data) {
ratingsArray = msg.data;
firstRun_check(true);
if($(elScorePanel).length>0 && location.pathname.indexOf('/tv/')<0 && $(elPageFilmName).length>0) {
// this is a movie listing
fix_annoying_header();
get_score_panel_data();
var fadeTime = 250;
$(elScorePanel).find('div').css('transition','opacity ' + fadeTime + 'ms');
$(elScorePanel).find('div').css('opacity','0');
var t1 = setTimeout(function(){
// wait for fadeout to finish
replace_score_panel();
insert_critics_widget();
insert_rating_widget();
var totalPages = find_total_pages();
show_update_status(totalPages);
ratingsArray_add_this_movie();
rating_widget_events_update(ratingsArray[pageFilmIndex][1]);
// each scrape call updates the ratingsArray & criticsArray
var scrapePath = $(elReviewsScrapeLink).attr('href');
if(scrapePath) {
scrapePath = 'https://www.rottentomatoes.com' + scrapePath;
scrapePath = scrapePath.replace('/reviews','');
} else {
scrapePath = pageFilmPath;
}
do_scrape_calls(totalPages,scrapePath,pageFilmIndex);
// execute when all criticRatingsDeferreds have resolved
$.when.apply(null, criticRatingsDeferreds)
.done(function() {
criticRatingsXhrs = [];
criticsArray_add_existing();
criticsArray_update();
match_RT_rating_widget();
update_critics_widget();
update_tomatometer();
add_score_panel_events();
add_critics_widget_events();
add_rating_widget_events();
add_extras_events();
$('#hrt_rating_widget UL').css('visibility','visible');
$('#hrt_updating').css('display','none');
clearInterval(updatingTimer);
criticRatingsDeferreds = [];
});
// fail state unneeded, nothing could possibli go wrong
},fadeTime);
}
}
});
///////////////////////// //// //// //// //// ////
// // //// //// //// //// ////
// DOM INSERTION ///// //// //// //// //// ////
// FUNCTIONS ///// //// //// //// //// ////
// // //// //// //// //// ////
///////////////////////// //// //// //// //// ////
function insert_critics_widget() {
var txt = '';
txt += '<div id="hrt_critics_widget">';
txt += '<div id="hrt_critics_title">';
txt += '<div>Critics reviews, ranked by similarity to you</div>';
txt += '</div>';
txt += '<a href="#" id="hrt_critics_filter"><span>show all critics</span></a>';
txt += '<div id="hrt_updating"></div>';
txt += '<div id="hrt_critics_rows" class="hrt_cr_empty">';
txt += '</div>'
txt += '</div>'
$('#hrt_score_panel').after(txt);
}
function get_score_panel_data() {
spAllScore = parseInt($(elAllScore).html());
spAllCount = parseInt($(elAllCount).html());
spTopScore = parseInt($(elTopScore).html());
spTopCount = parseInt($(elTopCount).html());
var txt = $(elAudienceScore).html();
spAudienceScore = parseInt(txt.substring(0,txt.length-1));
if($(elAudienceCount).length>0) {
// movie is released
if($(elAudienceCount)[0].nextSibling) {
spAudienceCount = $(elAudienceCount)[0].nextSibling.nodeValue;
}
spAudienceCount = spAudienceCount.replace(/\s/g,'');
if($(elAudienceAverage)[0].nextSibling) {
txt = $(elAudienceAverage)[0].nextSibling.nodeValue;
}
txt = txt.replace(/\s/g,'');
var fraction = txt.split('/');
if(fraction.length==2) {
var dividend = parseFloat(fraction[0]);
var divisor = parseFloat(fraction[1]);
if(divisor > 0) {
spAudienceAverage = (dividend/divisor)*100;
}
}
} else {
// movie not yet released ("want to see")
spAudienceAverage = '-1';
if($(elAudienceAverage)[0].nextSibling) {
spAudienceCount = $(elAudienceAverage)[0].nextSibling.nodeValue;
}
spAudienceCount = spAudienceCount.replace(/\s/g,'');
}
var el = $(elConsensus);
$(el).find(elConsensusJunk).remove();
spConsensus = $(el).html();
spSynopsis = $(elSynopsis).html();
}
function replace_score_panel() {
var audienceScoreIcon = 'popular';
if(spAudienceAverage<0) {
audienceScoreIcon = 'wanttosee';
} else {
if(spAudienceScore/100 < .6) {
audienceScoreIcon = 'unpopular';
}
}
var allScoreIcon = 'fresh';
if(spAllScore/100 < .6) {
allScoreIcon = 'rotten';
}
var topScoreIcon = 'fresh';
if(spTopScore/100 < .6) {
topScoreIcon = 'rotten';
}
var txt = '';
txt += '<div id="hrt_tooltip"><span></span></div>';
txt += '<div id="hrt_score_panel">';
txt += '<div id="hrt_your_critics">';
txt += '<div class="hrt_score_panel_title has_tip">All critics:</div>';
if(Number.isNaN(spAllCount) || Number.isNaN(spAllScore)) {
// tomatometer not available in all critics section
txt += '<div class="hrt_score_panel_box">';
txt += '<span>No consensus yet</span>';
txt += '</div>';
} else {
txt += '<div class="hrt_score_panel_box">';
txt += '<div class="hrt_score_panel_meter has_tip hrt_' + allScoreIcon + '">';
txt += '<div></div><div>' + spAllScore + '</div><div>%</div>';
txt += '</div>';
txt += '</div>';
txt += '<div class="hrt_score_panel_ratings">';
txt += '<div class="hrt_histogram has_tip">';
txt += '<div class="hrt_h0"><div></div></div>';
txt += '<div class="hrt_h1"><div></div></div>';
txt += '<div class="hrt_h2"><div></div></div>';
txt += '<div class="hrt_h3"><div></div></div>';
txt += '<div class="hrt_h4"><div></div></div>';
txt += '</div>';
txt += '</div>';
txt += '<div class="hrt_score_panel_count">(' + spAllCount + ' critics)</div>';
}
txt += '</div><!--';
txt += '--><div id="hrt_top_critics">';
txt += '<div class="hrt_score_panel_title has_tip">Top critics:</div>';
if(Number.isNaN(spTopCount) || Number.isNaN(spTopScore)) {
// tomatometer not availablle in top critics section
txt += '<div class="hrt_score_panel_box">';
txt += '<span>No consensus yet</span>';
txt += '</div>';
} else {
txt += '<div class="hrt_score_panel_box">';
txt += '<div class="hrt_score_panel_meter has_tip hrt_'+ topScoreIcon +'">';
txt += '<div></div><div>' + spTopScore + '</div><div>%</div>';
txt += '</div>';
txt += '</div>';
txt += '<div class="hrt_score_panel_ratings">';
txt += '<div class="hrt_histogram has_tip">';
txt += '<div class="hrt_h0"><div></div></div>';
txt += '<div class="hrt_h1"><div></div></div>';
txt += '<div class="hrt_h2"><div></div></div>';
txt += '<div class="hrt_h3"><div></div></div>';
txt += '<div class="hrt_h4"><div></div></div>';
txt += '</div>';
txt += '</div>';
txt += '<div class="hrt_score_panel_count">(' + spTopCount + ' critics)</div>';
}
txt += '</div><!--';
txt += '--><div id="hrt_audiences">';
txt += '<div class="hrt_score_panel_title has_tip">Audiences:</div>';
txt += '<div class="hrt_score_panel_box">';
txt += '<div class="hrt_score_panel_meter has_tip hrt_' + audienceScoreIcon + '">';
txt += '<div></div><div>' + spAudienceScore + '</div><div>%</div>';
txt += '</div>';
txt += '</div>';
txt += '<div class="hrt_score_panel_ratings">';
if(spAudienceAverage>-1) {
txt += '<div><div style="width:' + spAudienceAverage + '%;"></div></div>';
} else {
txt += '<span class="hrt_want">Want to see</span>'
}
txt += '</div>';
txt += '<div class="hrt_score_panel_count">(' + spAudienceCount + ' users)</div>';
txt += '</div><!--';
txt += '--><div id="hrt_rateit">';
txt += '<div class="hrt_score_panel_title has_tip">Rate this movie:</div>';
txt += '<div class="hrt_score_panel_box"></div>';
txt += '<div class="hrt_score_panel_count">Heirloom Installed!</div>';
txt += '<a id="hrt_aboutLink" href="#">Extras & Help</a>';
txt += '</div><!--';
txt += '--><div id="hrt_consensus">';
txt += '<span><span>Consensus: </span>' + spConsensus + '</span>';
txt += '</div><!--';
txt += '--><div id="hrt_movie_synopsis">';
txt += '<span><span>Synopsis: </span>' + spSynopsis + '</span>';
txt += '</div>';
txt += '</div>';
$(poster).css('height','750px');
$(poster).css('transition','background-color 250ms');
$(poster).css('background-color','rgb(232, 232, 229)');
$(elScorePanel).css({
'padding' : '0'
});
$(elScorePanel).html(txt);
var t = setTimeout(function(){
// delay needed for DOM change to complete
$('#hrt_score_panel').css('transition','opacity 250ms');
$('#hrt_score_panel').css('opacity','1');
},10);
}
function insert_rating_widget() {
var txt = '';
txt += '<div id="hrt_rating_widget">';
txt += '<ul>';
txt += '<li><a href="#" id="star_a_5"><span id="star_5">best</span></a></li>';
txt += '<li><a href="#" id="star_a_4"><span id="star_4">good</span></a></li>';
txt += '<li><a href="#" id="star_a_3"><span id="star_3">okay</span></a></li>';
txt += '<li><a href="#" id="star_a_2"><span id="star_2">bad</span></a></li>';
txt += '<li><a href="#" id="star_a_1"><span id="star_1">worst</span></a></li>';
txt += '<li><a href="#" id="star_a_0"><span id="star_0">not rated</span></a></li>';
txt += '</ul>';
txt += '</div>';
$('#hrt_rateit .hrt_score_panel_box').html(txt);
}
///////////////////////// //// //// //// //// ////
// // //// //// //// //// ////
// BIND EVENTS ///// //// //// //// //// ////
// TO INSERTIONS ///// //// //// //// //// ////
// // //// //// //// //// ////
///////////////////////// //// //// //// //// ////
function add_score_panel_events() {
toolTipData[0] = new Object;
toolTipData[0].target = '#hrt_your_critics .hrt_score_panel_title';
toolTipData[0].html = '<strong>Your critics:</strong> more weight is given to the opinions of those critics who are similar to you, based on your ratings of the same films. Unweighted score: ' + spAllScore + '%';
toolTipData[0].left = 0;
toolTipData[0].top = 15;
toolTipData[0].width = 110;
toolTipData[1] = new Object;
toolTipData[1].target = '#hrt_top_critics .hrt_score_panel_title';
toolTipData[1].html = '<strong>Top critics</strong> are chosen by Rotten Tomatoes, and their opinions may or may not be similar to yours.';
toolTipData[1].left = 0;
toolTipData[1].top = 15;
toolTipData[1].width = 110;
toolTipData[2] = new Object;
toolTipData[2].target = '#hrt_audiences .hrt_score_panel_title';
toolTipData[2].html = '<strong>Audiences</strong> are visitors to Rotten Tomatoes who have rated or reviewed this film.';
toolTipData[2].left = 0;
toolTipData[2].top = 15;
toolTipData[2].width = 110;
toolTipData[3] = new Object;
toolTipData[3].target = '#hrt_rateit .hrt_score_panel_title';
toolTipData[3].html = '<strong>Rate this film</strong> to improve the accuracy of the \'Your Critics\' meter & ranking.';
toolTipData[3].left = 0;
toolTipData[3].top = 15;
toolTipData[3].width = 110;
toolTipData[4] = new Object;
toolTipData[4].target = '.hrt_histogram';
toolTipData[4].html = 'Shows the distribution of critics\' ratings, from 1-start to 5-star. Each column shows the percentage of critics who gave that rating.';
toolTipData[4].left = 0;
toolTipData[4].top = 55;
toolTipData[4].width = 110;
toolTipData[5] = new Object;
toolTipData[5].target = '.hrt_score_panel_box .hrt_fresh';
toolTipData[5].html = '<strong>Fresh:</strong> most critics think this film is \'okay\' or better.';
toolTipData[5].left = 5;
toolTipData[5].top = 20;
toolTipData[5].width = 110;
toolTipData[6] = new Object;
toolTipData[6].target = '.hrt_score_panel_box .hrt_rotten';
toolTipData[6].html = '<strong>Rotten:</strong> few critics think this film is \'okay\' or better.';
toolTipData[6].left = 5;
toolTipData[6].top = 20;
toolTipData[6].width = 110;
toolTipData[7] = new Object;
toolTipData[7].target = '.hrt_score_panel_box .hrt_middling';
toolTipData[7].html = '<strong>Passable:</strong> while most critics think this film is at least \'okay\', very few think it is \'great\'.';
toolTipData[7].left = 6;
toolTipData[7].top = 20;
toolTipData[7].width = 110;
toolTipData[8] = new Object;
toolTipData[8].target = '.hrt_score_panel_box .hrt_controversial';
toolTipData[8].html = '<strong>Dividing:</strong> despite the % score, most critics have a strong opinion and disagree with each other.';
toolTipData[8].left = 6;
toolTipData[8].top = 20;
toolTipData[8].width = 110;
toolTipData[9] = new Object;
toolTipData[9].target = '.hrt_score_panel_box .hrt_amazing';
toolTipData[9].html = '<strong>Amazing:</strong> an exceptionally high percentage of critics think this film is great.';
toolTipData[9].left = 6;
toolTipData[9].top = 20;
toolTipData[9].width = 110;
toolTipData[10] = new Object;
toolTipData[10].target = '.hrt_score_panel_box .hrt_popular';
toolTipData[10].html = '<strong>Popular:</strong> at least 60% of RT voters think this film is \'okay\' or better.';
toolTipData[10].left = 6;
toolTipData[10].top = 20;
toolTipData[10].width = 110;
toolTipData[11] = new Object;
toolTipData[11].target = '.hrt_score_panel_box .hrt_unpopular';
toolTipData[11].html = '<strong>Unpopular:</strong> less than 60% of RT voters think this film is \'okay\' or better.';
toolTipData[11].left = 6;
toolTipData[11].top = 20;
toolTipData[11].width = 110;
toolTipData[12] = new Object;
toolTipData[12].target = '.hrt_score_panel_box .hrt_wanttosee';
toolTipData[12].html = '<strong>Want to see:</strong> the percentage of RT voters who say they want to see this film.';
toolTipData[12].left = 6;
toolTipData[12].top = 20;
toolTipData[12].width = 110;
toolTipData[13] = new Object;
toolTipData[13].target = '#hrt_critics_widget';
toolTipData[13].html = 'A critic\'s ranking is based on the number of films you\'ve both rated, and how the critic\'s your ratings of those films are to your ratings. Click on the heart icon to favorite critics so that you can easily find their reviews of other films.';
toolTipData[13].left = 170;
toolTipData[13].top = -140;
toolTipData[13].width = 160;
for(var i=0,il=toolTipData.length; i<il; i++) {
$(toolTipData[i].target).data('html',toolTipData[i].html);
$(toolTipData[i].target).data('left',toolTipData[i].left);
$(toolTipData[i].target).data('top',toolTipData[i].top);
$(toolTipData[i].target).data('width',toolTipData[i].width);
$(toolTipData[i].target).mouseenter(function(event) {
$('#hrt_tooltip span').html($(this).data('html'));
var el = $(this);
var left = $(el).data('left');
var top = $(el).data('top');
var width = $(el).data('width');
// we need to set this first because it changes the element's height
$('#hrt_tooltip').css('width',width);
$('#hrt_tooltip').css({
'left' : $(el).position().left - left,
'top' : $(el).position().top - top - $('#hrt_tooltip').height()
});
$('#hrt_tooltip').addClass('hrt_shown');
});
$(toolTipData[i].target).mouseleave(function(event) {
$('#hrt_tooltip').removeClass('hrt_shown');
});
}
}
function add_critics_widget_events() {
$('#hrt_critics_rows').scroll(function(event) {
// this allows the entire page to be scrolled
// if the mouse is over the reviews area
// by forcing the user to scroll twice
if(!hasScrolledReviews) {
$('#hrt_critics_rows').css('overflow-y','hidden');
hasScrolledReviews = true;
var t = setTimeout(function() {
$('#hrt_critics_rows').css('overflow-y','scroll');
},500);
}
});
$('#hrt_critics_rows').mouseleave(function(event) {
hasScrolledReviews = false;
});
$('#hrt_critics_filter').click(function(event) {
apply_critic_filter();
return false;
});
$('.hrt_critic_heart').click(function(event) {
var match = false;
var id = $(this).attr('id');
var id = id.substring(4,id.length);
$(this).toggleClass('favorite_critic');
for(x=0,xl=favoritesArray.length; x<xl; x++) {
if(favoritesArray[x]==id) {
favoritesArray.splice(x,1);
match = true;
break;
}
}
if(!match) {
favoritesArray[favoritesArray.length] = id;
}
storage.set({'favorites': favoritesArray}, function() {
});
// send data to the eventPage for gA
messagePort.postMessage({favorited: id});
return false;
});
$('#hrt_noratings_import').click(function(event) {
firstRun = 'firstRun';
messagePort.postMessage({ firstRun: [firstRun,null] });
firstRun_check(false);
return false;
});
}
function add_rating_widget_events() {
// add events for each star in the widget
for(var x=0, xl=6; x<xl; x++) {
$('#star_a_'+x).click({ x:x }, function(event) {
// save data and update
var num = event.data.x;
rating_widget_events_save(num);
rating_widget_events_update(num);
criticsArray_update();
update_critics_widget();
update_tomatometer();
save_to_storage();
// logged in on RT
// so simulate click on RT
simulate_rt_widget_click(num)
return false;
});
}
}
function match_RT_rating_widget() {
// RT doesn't pull in its user rating record
// until well after the page loads,
// so we have to listen for it
var observer = new MutationObserver(function(mutations, observer) {
if(ratingsArray[pageFilmIndex]) {
if($(starWidgetRT)[0] instanceof Node) {
var starWidgetTimer = setTimeout(function(){
// wait 250ms to let mutation finish loading
save_RT_ratings_click();
var rtWidget = $(starWidgetRT).eq(0);
$(rtWidget).click(function(event) {
// add new event to RT's native rating widget
save_RT_ratings_click();
});
observer.disconnect();
},250);
}
}
});
var observationTarget = $(ratingWidgetTarget)[0];
observer.observe(observationTarget,
{
childList: true,
subtree: true
});
}
function save_RT_ratings_click() {
var rtWidget = $(starWidgetRT).eq(0);
var rtStars = $(rtWidget)[0].style.width;
var ratingFromRT = Math.round(parseInt(rtStars)/20);
var ratingFromLocal = ratingsArray[pageFilmIndex][1];
// check if this function was called from
// clicking HRT rating widget (simulated)
var simulated = false;
var classList = $(rtWidget).attr('class').split(/\s+/);
$.each(classList, function(index, item) {
if(item.indexOf('simulated')>-1) {
simulated = true;
}
});
if(!simulated) {
if(ratingFromRT != ratingFromLocal) {
// ratings don't already match
if(ratingFromRT>0 && !simulated) {
// rating from RT exists and
// this mutation was not started
// by tapping the HRT widget
// so override local
rating_widget_events_save(ratingFromRT);
rating_widget_events_update(ratingFromRT);
criticsArray_update();
update_critics_widget();
update_tomatometer();
save_to_storage();
} else if(ratingFromLocal>0) {
// rating from local exists
// so update RT widget
simulate_rt_widget_click(ratingFromLocal);
}
}
}
$(rtWidget).removeClass('simulated');
}
function rating_widget_events_update(star) {
if('#user_rating') {
$('#user_rating').remove();
}
if(star>0) {
$('#star_' + star).after('<i id="user_rating"></i>');
$('#hrt_rateit .hrt_score_panel_title').html('Your rating:');
} else {
$('#hrt_rateit .hrt_score_panel_title').html('Rate this movie:');
}
}
function rating_widget_events_save(star) {
ratingsArray[pageFilmIndex][1] = parseInt(star);
}
function simulate_rt_widget_click(star) {
var rtWidget = $(starWidgetRT).eq(0);
// record that this is a simulated click
$(rtWidget).addClass('simulated');
var rtWidgetStar = rtWidget.find('[' + starWidgetStarRT + '="' + star + '"]');
// find rt widget position accounting for scroll
var clientX = rtWidgetStar[0];
if(typeof clientX != "undefined") {
clientX = rtWidgetStar[0].getBoundingClientRect().left + 2;
// clientX += ((star*26)-8);
// simulate click on RT's star rating tool
var event = new MouseEvent('click', {
'view': window,
'bubbles': true,
'cancelable': true,
'clientX': clientX,
'clientY': 2,
'button': 0,
'relatedTarget': null
});
rtWidgetStar[0].dispatchEvent(event);
}
}
function add_extras_events() {
// overlay box with extras
$('#hrt_aboutLink, .has_tip').click(function(event) {
show_about_modal();
return false;
});
}
function show_about_modal() {
// send data to the eventPage for gA
messagePort.postMessage({aboutModal: 'opened'});
$('BODY').append($('<div>',{ id: 'hrt_modalClickZone' }));
$('BODY').append($('<div>',{ id: 'hrt_modal' }));
$('#hrt_modal').append($('<div>', { id: 'hrt_modalInner', style: 'height: 80vh' }));
$('#hrt_modalInner').append($('<strong>', { text: 'Thanks for Using Heirloom Rotten Tomatoes - Version ' + appVersion + '!', style: 'text-align:center; padding-bottom:10px;' }));
$('#hrt_modalInner').append($('<span>', { text: 'To report an problem or to make a nice comment, tweet ' }));
$('#hrt_modalInner').find('span:last').append($('<a>', { href: 'https://twitter.com/messages/compose?recipient_id=3084491', target: '_blank', text: '@mattthew' }));
$('#hrt_modalInner').append($('<strong>', { text: 'Your reviews keep this project alive!' }));
$('#hrt_modalInner').append($('<span>', { text: 'This free app is an open-source, fan supported, labor of love. To show your support, please ' }));
$('#hrt_modalInner').find('span:last').append($('<a>', { href: 'https://chrome.google.com/webstore/detail/heirloom-rotten-tomatoes/ckmbpodfggiamhcmpilepdccpdnpfofd/reviews', target: '_blank', text: 'add a review' }));
$('#hrt_modalInner').find('span:last').append('. Thanks! ');
$('#hrt_modalInner').find('span:last').append(' Neither this browser app nor it\'s developer are affiliated with or supported by Rotten Tomatoes in any way.');
$('#hrt_modalInner').append($('<strong>', { text: 'Import ratings from your account:' }));
$('#hrt_modalInner').append($('<span>', { text: 'If you\'ve rated any movies on Rotten Tomatoes before installing this app, ' }));
$('#hrt_modalInner').find('span:last').append($('<a>', { href: '#', text: 'import your ratings now', id: 'hrt_import_ratings' }));
$('#hrt_modalInner').find('span:last').append(' to improve the accuracy of the app.');
$('#hrt_modalInner').append($('<strong>', { text: 'Information this app collects:' }));
$('#hrt_modalInner').append($('<span>', { text: 'This app only saves ratings data to your computer. Your movie ratings are 100% private to your computer and never transmitted. The app never accesses nor stores any login info. The app never transmits personally identifiable information about the pages that you visit. The app does use a Google Analytics cookie to send anonymous aggregated information to the developer. This anonymous information includes: how often the app is used and installed, movies with interesting Tomatometer scores, critics who have high similarity to many app users.' }));
$('#hrt_modalInner').append($('<div>', { id: 'hrt_rated_movies' }));
$('#hrt_modalInner').append($('<strong>'));
$('#hrt_modalInner').append($('<strong>', { text: 'Experimental features:' }));
$('#hrt_modalInner').append($('<a>', { href: '#', text: 'export list of critics with their similarity to you', id: 'export_critics' }));
$('#hrt_modalInner').append($('<strong>'));
$('#hrt_modalInner').append($('<a>', { href: '#', text: 'export raw data file', id: 'export_raw' }));
$('#hrt_modalInner').append($('<strong>'));
$('#hrt_modalInner').append($('<input>', { type: 'file', id: 'hrt_import_raw' }));
$('#hrt_modalInner').append($('<output>', { id: 'list_ratings' }));
$('#hrt_modalInner').append($('<input>', { type: 'button', id: 'hrt_fake_import', name: 'fake_import', value: 'import raw data file' }));
$('#hrt_modalInner').append($('<strong>'));
$('#hrt_modalInner').append($('<span>', { text: 'Export a comparison of every critic with every other critic, given the movies you\'ve rated so far. The report only contains critic-pairs who have rated at least 10 movies in common, so you need to have rate many movies to get useful results. WARNING: This may generate a very large file and Chrome will hang for one or more minutes. ' }));
$('#hrt_modalInner').find('span:last').append($('<a>', { href: '#', text: 'Try experimental report.', id: 'extras_events_compareAll' }));
$('#hrt_modalInner').append($('<strong>'));
$('#hrt_modalInner').append($('<span>', { text: 'Export a histogram of ratings for each critic, based on the movies you\'ve rated so far. For each critic, this lists the count of their ratings in each of the five star levels.' }));
$('#hrt_modalInner').find('span:last').append($('<a>', { href: '#', text: 'Try experimental report.', id: 'extras_events_histograms' }));
$('#hrt_modalInner').append($('<strong>'));
$('#hrt_modalInner').find('strong:last').append($('<a>', { href: '#', text: 'erase your data', id: 'hrt_erase', style: 'color:red;' }));
// insert summary and list of the user's ratings
var tempArray = ratingsArray.slice(0);
tempArray.sort(function(a, b) {
var aSort = a[1];
var bSort = b[1];
return bSort-aSort;
});
var count1 = 0;
var count2 = 0;
var count3 = 0;
var count4 = 0;
var count5 = 0;
var countTotal = 0;
var list = '';
list += '<span style="margin-bottom:0px;">Your ⭑ Rating, (Average Critic Rating), Movie Title</span>'
for(var i=1,il=tempArray.length; i<il; i++) {
if(tempArray[i][1]>0) {
// user rated this movie
switch(tempArray[i][1]) {
case 1:
count1++;
break
case 2:
count2++;
break
case 3:
count3++;
break
case 4:
count4++;
break
case 5:
count5++;
break
}
countTotal++;
var average = 0;
var total = 0;
for(var j=2, jl=tempArray[0].length; j<jl; j++) {
if(tempArray[i][j]>0) {
average += tempArray[i][j];
total++;
}
}
if(total>0) {
average = average/total
} else {
average = 0;
}
average = Math.round(average*10)/10;
var avT = average + '';
if(avT.indexOf('.')<0) { avT += '.0'; }
list += '<span style="margin-bottom:0px;">' + tempArray[i][1] + ', (' + avT + '), ';
list += '<a target="_blank" href="'+ tempArray[i][0][1] + '">' + tempArray[i][0][0] + '</a></span>';
}
}
$('#hrt_rated_movies').html(list);
var table = '';
var positivity = 0;
var positivity = ((count5/countTotal)*5) + ((count4/countTotal)*4) + ((count3/countTotal)*3) + ((count2/countTotal)*2) + ((count1/countTotal)*1) - (1);
positivity = Math.round((positivity/4)*100);
var variance = 0;
variance += Math.pow(((count5/countTotal)-0.2),2);
variance += Math.pow(((count4/countTotal)-0.2),2);
variance += Math.pow(((count3/countTotal)-0.2),2);
variance += Math.pow(((count2/countTotal)-0.2),2);
variance += Math.pow(((count1/countTotal)-0.2),2);
variance = variance/5;
var stdev = Math.sqrt(variance);
var variation = Math.round(((0.4472-stdev)/0.45)*100);
table += '<span>';
table += '⭑⭑⭑⭑⭑: ' + count5 + '<br>';
table += '⭑⭑⭑⭑: ' + count4 + '<br>';
table += '⭑⭑⭑: ' + count3 + '<br>';
table += '⭑⭑: ' + count2 + '<br>';
table += '⭑: ' + count1 + '<br>';
table += 'Your positivity: ' + positivity + '% <i>(most critics score 60-80%)</i><br>';
table += 'Your variation: ' + variation + '% <i>(most critics score 50-70%)</i><br>';
table += '</span>';
table += '<span>If you rated every movie as five stars, your ratings positivity would be 100% and your ratings variation would be 0%. If you rated every movie as one star, positivity would be 0% and variation 0%. If each of the star categories above had the same count of movies, your rating variation would be 100%.</span>';
$('#hrt_rated_movies').prepend(table);
$('#hrt_rated_movies').prepend('<strong>You\'ve rated ' + countTotal + ' movies:</strong>');
$('#hrt_modalInner').append($('<span>', { text: '' }));
$('#hrt_modalInner').append($('<span>', { text: '' }));
// bindings
$('#hrt_modalClickZone').click(function(event) {
$('#hrt_modal').remove();
$('#hrt_modalClickZone').remove();
$('BODY').off('keyup');
});
$('BODY').keyup(function(event) {
if(event.keyCode) {
$('#hrt_modal').remove();
$('#hrt_modalClickZone').remove();
$('BODY').off('keyup');
}
});
$('#hrt_erase').click(function(event) {
erase_data();
return false;
});
$('#hrt_import_ratings').click(function(event) {
$('#hrt_modal').remove();
$('#hrt_modalClickZone').remove();
$('BODY').off('keyup');