forked from sinamt/SGR
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sgr.js
1762 lines (1478 loc) · 66.4 KB
/
sgr.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
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-18940431-1']);
_gaq.push(['_trackPageview']);
(function($) {
// $.sgr namespace constructor
//
$.sgr = function(func_name) {
$.isFunction(func_name) ? func_name.call() : null
}
////////////
// CONFIG
////////////
// Minimum allowed height for the preview iframe
//
$.sgr.minimum_iframe_height = 700;
$.sgr.minimum_iframe_height_str = $.sgr.minimum_iframe_height.toString() + 'px';
////////////
// End CONFIG
////////////
// Settings cache
//
$.sgr._settings = {};
// Container to store when an entry has been closed. Used to prevent entry 'flicker'
// when entry is closed/opened/closed too quickly
//
$.sgr.entry_closed_at_time = {};
// Stored original entry content for entry being replaced by readability.
//
$.sgr.entry_original_content = {};
// Google Reader _USER_ID value
//
$.sgr.USER_ID = null;
// Regexp setup for detecting Google Reader URLs. Needs to allow country tlds.
//
$.sgr.start_url_str = '^http(?:s|)\:\/\/(?:www\.|)';
$.sgr.gr_url_base = $.sgr.start_url_str + 'google(\.co(m|)|)(\.[A-Za-z]{2}|)\/reader\/';
$.sgr.gr_main_window_re = new RegExp($.sgr.gr_url_base);
$.sgr.gr_settings_window_re = new RegExp($.sgr.gr_url_base + 'settings');
// Youtube API URLs
//
$.sgr.youtube_api = {
video: "http://gdata.youtube.com/feeds/api/videos/[video_id]?v=2&alt=jsonc"
}
// Vimeo API URLs
//
$.sgr.vimeo_api = {
video: "http://vimeo.com/api/v2/video/[video_id].json"
}
// Entry tab HTML snippet
//
$.sgr.entry_tabs_html = '<div class="sgr-entry-tabs"><div class="sgr-tab-readable sgr-entry-tab">Readable</div><div class="sgr-tab-link sgr-entry-tab">Link</div><div class="sgr-tab-feed sgr-entry-tab">Feed</div></div>';
// Settings feedback messages
//
$.sgr.setting_feedback_messages = {
url_in_subject: {'true': 'Default to include entry hostname in subject.', 'false': 'Default to <em>not</em> include entry hostname in subject.'},
hide_likers: {'true': "Hide 'Liked by users' for each entry.", 'false': "Show 'Liked by users' for each entry."},
entry_tabs: {'true': "Display 'Content Type' tabs for each entry ('Readable', 'Link', 'Feed').", 'false': "<em>Do not</em> display 'Content Type' tabs for each entry ('Readable', 'Link', 'Feed')."},
use_iframes: {'true': 'Default to open all entries as previews (iframes).', 'false': 'Default to <em>not</em> open all entries as previews (iframes).'},
use_readability: {'true': 'Default to open all entries as readable content.', 'false': 'Default to <em>not</em> open all entries as readable content.'},
readability_pre_fetch: {'true': 'If readability enabled for feed/folder, default to pre-fetch all non-read entries as readable content.', 'false': 'If readability enabled for feed/folder, <em>do not</em> default to pre-fetch all non-read entries as readable content.'},
readability_more_images: {'true': 'For readable content, try to include more images in the content.', 'false': 'Use normal settings for fetching readable content.'}
}
// Load default global settings.
//
$.sgr.initSettings = function() {
$.sgr.initUserId();
// Set the defaults for global settings
//
var default_settings = {use_iframes: false, use_readability: false, readability_pre_fetch: false, url_in_subject: false, hide_likers: false, entry_tabs: true, readability_more_images: false};
$.each(default_settings, function(key,value) {
var stored_setting = $.sgr.getGlobalSetting(key);
if (stored_setting === null) {
$.sgr.setGlobalSetting(key,value);
}
});
// Sanity check for use_iframes and use_readability to ensure both are not true simultaneously
//
if ($.sgr.getGlobalSetting('use_iframes') == true && $.sgr.getGlobalSetting('use_readability') == true) {
$.sgr.setGlobalSetting('use_iframes',false);
$.sgr.setGlobalSetting('use_readability',false);
$.sgr.togglePreFetchReadableContentMenuOption();
}
// Register the setting 'readability_more_images' with the background window
//
$.sgr.sendRequest({action: 'global_setting_background', setting_name: 'readability_more_images', setting_value: $.sgr.getSetting('readability_more_images')});
}
// Initialise the USER_ID. This is found within the javascript itself on the google reader page.
//
$.sgr.initUserId = function() {
$("head script").each(function(){
var user_id_matches = this.innerHTML.match(/_USER_ID = "(.*?)"/);
if (user_id_matches != null) {
$.sgr.USER_ID = user_id_matches[1];
// Register the USER_ID with the background window
//
$.sgr.sendRequest({action: 'regsiter_user_id', user_id: $.sgr.USER_ID});
return;
}
});
}
// Helper function to add global CSS styles to the page head
//
$.sgr.addStyles = function(css) {
var head=document.getElementsByTagName('head')[0];
if (head)
{
var style=document.createElement('style');
style.type='text/css';
style.innerHTML=css;
head.appendChild(style);
}
}
// Initialise global page CSS styles
//
$.sgr.initStyles = function() {
var global_styles = ' div.preview .entry-container { display: none; } .entry .entry-container-preview { padding: 0.5em 0; margin: 0 10px 0 0; color: #000; max-width: 98%; display: block; left: -10000px; } .entry .entry-container-preview .entry-title { max-width: 98%; } .entry .entry-container-preview .entry-main .entry-date { display: none; } .entry .entry-container-preview-hidden { position: absolute; } #setting-enhanced .enhanced { border-bottom:1px solid #FFCC66; margin:0; padding:0.6em 0; } #setting-enhanced .enhanced-header { font-weight: bold; margin-bottom: 1em; } div.preview iframe.preview { display: block; overflow-y: hidden; } .entry .sgr-hostname { font-weight: normal; } .entry .entry-main .sgr-hostname { font-size: 90%; } .sgr-entry-tabs {position: absolute; left: 500px; padding: 0px 10px; top: -6px; z-index: 100; } .sgr-entry-tab {background-color: whiteSmoke; padding: 5px 6px 3px; margin: 2px 1px 0; border: 2px solid #DDD; border-bottom: none; border-top-left-radius: 3px; border-top-right-radius: 3px; float: left; } .sgr-entry-tabs .selected {background-color: white; border: 2px solid #999; border-bottom: none;} .sgr-entry-tab:hover {cursor: pointer; background-color: #FFFFCC;} .cards .entry {padding: 21px 0 0;} #sgr-prefs-menu-menu {display: none; overflow-y: auto} .goog-menuitem-disabled .goog-menuitem-checkbox {opacity: 0.5;} .sgr-wikipedia-content .tright {float: right; clear: right; margin: 0.5em 0px 0.8em 1.4em;} .sgr-wikipedia-content .tleft {float: left; clear: left; margin: 0.5em 1.4em 0.8em 0px;} .sgr-wikipedia-content .thumbinner { background-color: #F9F9F9; border: 1px solid #CCC; font-size: 94%; overflow: hidden; padding: 3px !important; text-align: center; min-width: 100px; } .sgr-wikipedia-content #toc, .sgr-wikipedia-content .toc, .sgr-wikipedia-content .mw-warning {background-color: #F9F9F9; border: 1px solid #AAA; font-size: 95%; padding: 5px;} .sgr-wikipedia-content #toc ul, .sgr-wikipedia-content .toc ul {list-style-image: none; list-style-type: none; margin-left: 0px; padding-left: 0px; text-align: left;} .sgr-wikipedia-content .infobox { background-color: #F9F9F9; border: 1px solid #AAA; clear: right; color: black; float: right; margin: 0.5em 0px 0.5em 1em; padding: 0.2em; }';
// Check if 'Hide likers' is enabled and add appropriate CSS
//
if ($.sgr.getSetting('hide_likers')) {
global_styles += ' .entry-likers { display: none; }';
}
$.sgr.addStyles(global_styles);
}
// Set a setting value per feed or folder. Store it in localStorage.
//
$.sgr.setLocalSetting = function(setting_name,value) {
var key = $.sgr.getSettingName(setting_name, 'local');
if (key == false) {
return false;
}
//debug("setLocalSetting() : " + key + " = " + value);
$.stor.set(key, value);
$.sgr._settings[key] = value;
}
// Set a global setting value. Store it in localStorage.
//
$.sgr.setGlobalSetting = function(setting_name,value) {
var key = $.sgr.getSettingName(setting_name, 'global');
//debug("setGlobalSetting() : " + key + " = " + value);
$.stor.set(key, value);
$.sgr._settings[key] = value;
}
// Fetch a per feed or folder setting value from localStorage.
//
$.sgr.getLocalSetting = function(setting_name) {
var key = $.sgr.getSettingName(setting_name, 'local');
if (key == null) {
return null;
}
var value = $.sgr._settings[key];
if (value == null) {
//debug("no $.sgr._settings[key] found for " + key);
value = $.stor.get(key);
$.sgr._settings[key] = value;
}
//debug("getLocalSetting() : " + key + " = " + value);
return value;
}
// Fetch a global setting value from localStorage.
//
$.sgr.getGlobalSetting = function(setting_name) {
var key = $.sgr.getSettingName(setting_name, 'global');
var value = $.sgr._settings[key];
if (value == null) {
//debug("no $.sgr._settings[key] found for " + key);
value = $.stor.get(key);
$.sgr._settings[key] = value;
}
//debug("getGlobalSetting() : " + key + " = " + value);
return value;
}
// Get a setting value. Look for a locally set value first (for this user), otherwise use
// the default global setting value.
//
$.sgr.getSetting = function(setting_name) {
var local_setting = $.sgr.getLocalSetting(setting_name);
return local_setting == null ? $.sgr.getGlobalSetting(setting_name) : local_setting;
}
// Get a setting name, namespaced to the currently selected feed or folder
//
$.sgr.getLocalSettingName = function(setting_name) {
var feed = $.sgr.getCurrentFeedName();
if (typeof feed == 'undefined') {
return false;
}
return "setting_" + setting_name + "_" + feed;
}
// Get a setting key name. Can be either a local or global name depending on setting_type.
// Namespace the setting name to the USER_ID.
//
$.sgr.getSettingName = function(setting_name, setting_type) {
var _setting_name = false;
if (setting_type == 'local') {
_setting_name = $.sgr.getLocalSettingName(setting_name);
} else if (setting_type == 'global') {
_setting_name = 'global_' + setting_name;
}
if (_setting_name == false || $.sgr.USER_ID == null) {
return null;
}
return $.sgr.USER_ID + "_" + _setting_name;
}
// Find and return the currently selected feed href or folder name
//
$.sgr.getCurrentFeedName = function() {
// First look for a selected feed or folder, then try for a left-hand nav selection
//
var selected_href = $("a.tree-link-selected, #lhn-selectors .selected .link").first().attr('href');
if (typeof selected_href != 'undefined') {
return unescape(selected_href);
}
}
// Helper function to get the CSS xpath for a specific HTML element
//
$.sgr.getXPath = function(elt) {
if (elt instanceof jQuery) {
elt = elt.get(0);
}
var path = '';
for (; elt && elt.nodeType == 1; elt = elt.parentNode)
{
var idx = 0;
$(elt.parentNode).children(elt.tagName).each(function(index) {
if (this == elt) {
idx = index + 1;
return;
}
});
//var idx = $(elt.parentNode).children(elt.tagName).index($(elt)) + 1;
idx > 1 ? (idx='[' + idx + ']') : (idx='');
path = '/' + elt.tagName.toLowerCase() + idx + path;
}
return path;
}
// Google reader main window content script handler for receiving a request from the background window.
// Processes the following requests:
// - set_window_height : adjusts an entry's iframe window to the requested height
// - global_setting_change : processes a global setting change from the google reader settings iframe
//
$.sgr.receiveRequest = function(request, sender, sendResponse) {
//debug("reader.js: receiveRequest() called. request.action: " + request.action);
// Iframe window height
//
if (request.action == 'set_window_height') {
//debug("set_window_height: " + request.window_height + ", " + request.iframe_id);
if (typeof request.iframe_id == 'undefined' || request.iframe_id == null || request.iframe_id == '' || request.iframe_id.match(/^[A-Za-z0-9]{8}$/) == null) {
return;
}
$.sgr.setIframeWindowHeight($('#sgr_preview_' + request.iframe_id), request.window_height);
// Global setting change from settings iframe
//
} else if (request.action == 'global_setting_change') {
$.sgr.globalSettingChange(request);
}
sendResponse({});
}
// Set the specified iframe element to the specified height. Height should be specified
// as the integer value of pixels to be set.
//
$.sgr.setIframeWindowHeight = function(iframe, raw_height) {
var height = parseInt(raw_height);
//debug('height = ' + height);
if (iframe.size() < 1) {
return;
}
// Make sure the requested height is above our minimum
//
if (height < parseInt($.sgr.minimum_iframe_height)) {
return;
}
// Set the iframe height
//
iframe.attr('height', height.toString() + 'px');
}
// Scroll viewing pane to the top of a specific entry
//
$.sgr.scrollTo = function(entry) {
$("#entries").scrollTop(entry.attr("offsetTop"));
}
// Toggle the showing / hiding of an entry preview iframe.
//
$.sgr.togglePreview = function(entry) {
//debug("togglePreview");
// If this entry is already open in an iframe, close it
//
if (entry.hasClass("preview")) {
$.sgr.removePreview(entry);
// Else show the entry in an iframe
//
} else {
if ($.sgr.isExpandedView() == false) {
$.sgr.scrollTo(entry);
}
$.sgr.showPreview(entry);
}
}
// Show an entry preview iframe.
//
$.sgr.showPreview = function(entry) {
//debug("showPreview");
entry.removeClass("readable").addClass("preview");
$.sgr.updateSelectedEntryTab(entry);
// If there is already a hidden preview container for this entry, show it
//
if (entry.find(".entry-container-preview-hidden").size() > 0) {
$.sgr.restorePreview(entry);
} else { //if ($(".entry-body iframe.preview").size() <= 0) {
$.sgr.createPreviewIframe(entry, false);
}
}
// Show an entry preview iframe only if one isn't already being shown.
//
$.sgr.checkAndShowPreview = function(entry, scroll_to) {
if (typeof scroll_to == 'undefined') {
scroll_to = false;
}
if (!entry.hasClass("preview")) {
if (scroll_to) {
$.sgr.scrollTo(entry);
}
$.sgr.showPreview(entry);
}
}
// Create a new iframe element for previewing an entry.
//
$.sgr.createPreviewIframe = function(entry, hidden) {
if (typeof hidden == 'undefined') {
hidden = false;
}
// Create a new div.entry-container-preview for our iframe.
//
entry.find(".entry-container").after('<div class="entry-container-preview' + (hidden ? ' entry-container-preview-hidden' : '') + '"></div>');
// Add the entry header to our iframe container
//
$.sgr.populateIframeHeading(entry);
// Add the iframe
//
entry.find(".entry-container-preview .entry-main").append('<iframe id="sgr_preview_' + $.sgr.generateRandomString(8) +'" scrolling="no" width="100%" height="' + $.sgr.minimum_iframe_height_str + '" src="' + $.sgr.getEntryUrl(entry) + '" class="preview"></iframe>');
}
// Completely remove the iframe preview container from the DOM.
//
$.sgr.removePreview = function(entry) {
//debug("removePreview");
$(entry).removeClass("preview").find(".entry-container-preview").remove();
}
// Save the preview iframe container (effectively hiding it) for possible re-use later.
//
$.sgr.savePreview = function(entry) {
//debug("savePreview");
$(entry).removeClass("preview").find(".entry-container-preview").addClass("entry-container-preview-hidden");
}
// Restore a previously saved/hidden preview iframe.
//
$.sgr.restorePreview = function(entry) {
//debug("restorePreview");
entry.find(".entry-container-preview-hidden").removeClass("entry-container-preview-hidden");
$.sgr.populateIframeHeading(entry);
}
// Generate a random alphanumeric string of a given length
//
$.sgr.generateRandomString = function(str_len) {
var text = "";
var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
for( var i=0; i < parseInt(str_len); i++ )
text += possible.charAt(Math.floor(Math.random() * possible.length));
return text;
}
// When adding a preview iframe, we need to rebuild the entry header elements to prepend
// to our iframe container.
//
$.sgr.populateIframeHeading = function(entry) {
// If the preview container is not in this entry, or it already has
// an .entry-main added, return
//
if (entry.find(".entry-container-preview").size() <= 0
|| entry.find(".entry-container-preview .entry-main").size() > 0)
{
return;
}
// Grab the relevant header components from the existing .entry-container and
// prepend them to our new preview container.
//
var preview_header = entry.find(".entry-container .entry-main").clone();
if (preview_header.size() > 0) {
preview_header.find(".entry-body").remove();
entry.find(".entry-container-preview").prepend(preview_header);
$.sgr.addHostnameToSubject(entry, '.entry-container-preview .entry-main .entry-title');
}
}
// Display an entry containing readable content
//
$.sgr.showReadableEntry = function(entry) {
entry.addClass("readable").addClass($.sgr.generateReadableEntryClass($.sgr.getEntryUrl(entry)));
$.sgr.updateSelectedEntryTab(entry);
$.sgr.addHostnameToSubject(entry, '.entry-title');
var entry_body = entry.find(".entry-body");
entry_body.html("<p>Loading...</p>");
$.sgr.sendReadabilityFetchRequest(entry);
}
// Wrapper for displaying an entry containing readable content that
// checks if it's already showing a readable entry, and if not also trys to
// save any iframe being shown for the entry.
//
$.sgr.checkAndShowReadableEntry = function(entry, scroll_to) {
if (typeof scroll_to == 'undefined') {
scroll_to = false;
}
if (!entry.hasClass("readable")) {
$.sgr.savePreview(entry);
if (scroll_to) {
$.sgr.scrollTo(entry);
}
$.sgr.showReadableEntry(entry);
}
}
// Setup the Settings window. Google Reader settings are handled via a seperate iframe.
// When a user starts the Settings iframe, we execute this function to inject our 'Enhanced'
// settings tab content into the DOM.
//
$.sgr.initSettingsNavigation = function() {
$('#settings .settings-list').append(' <li id="setting-enhanced" class="setting-group"> <div id="setting-enhanced-body" class="setting-body"><div class="enhanced"> <div class="enhanced-header">Entry</div> <label> <input type="checkbox" id="setting-global-entry-tabs"> Display \'Content Type\' tabs for each entry (\'Readable\', \'Link\', \'Feed\'). </label> <br /> <label> <input type="checkbox" id="setting-global-hide-likers"> Hide \'Liked by users\' for each entry. </label> </div> <div class="enhanced"> <div class="enhanced-header">Opening entries</div> <label> <input type="radio" name="global_open_entry_default" id="setting-global-use-iframes"> Default to open all entries as previews (iframes). </label> <br /> <label> <input type="radio" name="global_open_entry_default" id="setting-global-use-readability"> Default to open all entries as readable content. </label> </div> <div class="enhanced"> <div class="enhanced-header">Entry subject</div> <label> <input type="checkbox" id="setting-global-url-in-subject"> Default to include entry hostname in subject. </label> </div> <div class="enhanced"> <div class="enhanced-header">Readable content</div> <label><input type="checkbox" name="global_readability_pre_fetch" id="setting-global-readability-pre-fetch"> If readability enabled for feed/folder, default to pre-fetch all non-read entries as readable content.</label> <br /> <label><input type="checkbox" name="global_readability_more_images" id="setting-global-readability-more-images"> Try to fetch more images along with readable content. Sometimes this may result in too much clutter. This functionality is experimental. </label> </div> </div> </li>');
// Inject the Enhanced tab heading html
//
$("#settings-navigation").append('<h3 id="setting-header-enhanced" class="setting-group-title"><span class="link setting-group-link">Super</span></h3>');
// Click event for Enhanced tab. Add "selected" state for our enhanced tab and remove it from other tabs.
//
$("#setting-header-enhanced").click(function(){
$("#settings .setting-group-title, #settings .setting-group").removeClass("selected");
$("#setting-header-enhanced, #setting-enhanced").addClass("selected");
});
// Click event for non-Enhanced tabs. We need to remove the "selected" state from our enhanced tab.
//
$(".setting-group-title:not(#setting-header-enhanced)").click(function(){
$("#setting-header-enhanced, #setting-enhanced").removeClass("selected");
});
var global_settings = ['use_iframes', 'use_readability', 'url_in_subject', 'hide_likers', 'readability_pre_fetch', 'entry_tabs', 'readability_more_images'];
// Loop the possible global settings and set the checkboxs to appropriate initial values
// based on the user's current global setting values. Also initialise a click event
// handler for each setting.
//
$(global_settings).each(function(){
var gs_name = this;
var alt_gs_name = gs_name.replace(/_/g, '-');
// Set the setting checkbox state based on the user's global setting value
//
if ($.sgr.getGlobalSetting(gs_name) == true) {
$("#setting-global-" + alt_gs_name).attr('checked','checked');
}
// Initialise a click event handler for this setting checkbox
//
$("#setting-global-" + alt_gs_name).click(function() {
$.sgr.globalSettingClickEventHandler(gs_name, false, 'setting-global-' + alt_gs_name);
});
});
// Initialise the settings feedback message area
//
$("#message-area-outer").addClass("hidden").addClass("info-message");
}
// Settings checkbox click event handler. Handles a user changing a setting.
//
$.sgr.globalSettingClickEventHandler = function(gs_name, gs_default, gs_id) {
gs_name = gs_name.toString();
var gs_value = !$.sgr.getGlobalSetting(gs_name);
$.sgr.setGlobalSetting(gs_name, gs_value);
if (gs_value) {
$("#" + gs_id).attr('checked','checked');
} else {
$("#" + gs_id).removeAttr('checked');
}
$.sgr.showSettingChangeFeedbackMesssage(gs_name, gs_value);
// Special case for use_iframes / use_readability. If either is being enabled, make sure the opposite is disabled
//
if (gs_value) {
if (gs_name == 'use_iframes') {
$.sgr.setGlobalSetting('use_readability',false);
$.sgr.sendRequest({action: 'global_setting_change', setting_name: 'use_readability', setting_value: false});
$.sgr.togglePreFetchReadableContentMenuOption();
} else if (gs_name == 'use_readability') {
$.sgr.setGlobalSetting('use_iframes',false);
$.sgr.sendRequest({action: 'global_setting_change', setting_name: 'use_iframes', setting_value: false});
$.sgr.togglePreFetchReadableContentMenuOption();
}
}
$.sgr.sendRequest({action: 'global_setting_change', setting_name: gs_name, setting_value: gs_value, set_in_background: (gs_name == 'readability_more_images' ? true : false)});
}
// Act on a global setting change in the main Google Reader window
//
$.sgr.globalSettingChange = function(data) {
// Set the changed setting value to reflect changes done in the settings iframe
//
$.sgr.setGlobalSetting(data.setting_name, data.setting_value);
//debug("data.setting_name = " + data.setting_name + ", data.setting_value = " + data.setting_value);
if (data.setting_name == 'url_in_subject') {
$.sgr.toggleHostnameInSubjects();
} else if (data.setting_name == 'hide_likers') {
$.sgr.toggleEntryLikers();
} else if (data.setting_name == 'entry_tabs') {
$.sgr.toggleEntryTabs();
} else if (data.setting_name == 'readability_more_images') {
// Tell the background page to clear it's sessionStore of readable content
//
$.sgr.sendRequest({action: 'clear_store', store_type: 'session'});
}
}
// Display a feedback 'flash' message on screen after a global setting is changed
//
$.sgr.showSettingChangeFeedbackMesssage = function(gs_name, gs_value) {
var fb_value = gs_value;
if (gs_value === true) {
fb_value = 'true';
} else if (gs_value === false) {
fb_value = 'false';
}
var msg = $.sgr.setting_feedback_messages[gs_name][fb_value];
if (msg != null) {
$("#message-area-inner").html(msg);
$("#message-area-outer").removeClass("hidden").width($("#message-area-inner").width() + 15).css('margin-left', '-' + ($("#message-area-outer").width() / 2) + 'px');
// Timeout handling for removing the flash message
//
clearTimeout($.sgr.setting_feedback_timer);
$.sgr.setting_feedback_timer = setTimeout(function(){
$("#message-area-outer").addClass("hidden");
}, 6000);
}
}
// Main setup for Google Reader window. Sets up appropriate listeners.
//
$.sgr.initMainWindowEvents = function() {
// Do not execute this for the settings iframe
//
if (window.location.href.match($.sgr.gr_settings_window_re)) {
//debug("returning, reader settings");
return;
}
// Add the entry hostname to entry subjects (if enabled)
//
$.sgr.toggleHostnameInSubjects();
// Inject our 'Super settings...' button
//
$.sgr.initSgrSettingsButton();
// Note: We try to setup live events on the entire div#entries area where possible.
// This keeps the amount of live events to a minimum.
//
// Any keydown event
//
$(document).keydown(function(ev){
// keydown 8, 9 or 0 : switch between readable, iframe and original
// entry views
//
if (jQuery.inArray(parseInt(ev.keyCode), [48,56,57]) > -1) {
// Don't fire in text-accepting inputs that we didn't directly bind to
// from jquery.hotkeys.js
//
if ( this !== ev.target && (/textarea|select/i.test( ev.target.nodeName ) ||
ev.target.type === "text") ) {
return;
}
var entry = $("#current-entry");
if (entry.hasClass("expanded") || $.sgr.isExpandedView()) {
// Keydown 0 - show original content
//
if (ev.keyCode == 48) {
$.sgr.checkAndShowEntryOriginalContent(entry, true);
$.sgr.sendRequest({action: 'ga_track_event', ga_category: 'OriginalEntry', ga_action: 'keydown'});
// Keydown 8 - show readable content
//
} else if (ev.keyCode == 56) {
$.sgr.checkAndShowReadableEntry(entry, true);
$.sgr.sendRequest({action: 'ga_track_event', ga_category: 'ReadableEntry', ga_action: 'keydown'});
// Keydown 9 - show iframe content
//
} else if (ev.keyCode == 57) {
$.sgr.checkAndShowPreview(entry, true);
$.sgr.sendRequest({action: 'ga_track_event', ga_category: 'IframeEntry', ga_action: 'keydown'});
}
}
}
});
// Any click event
//
$(document).click(function(ev) {
//debug("document click");
var ev_target = $(ev.target);
// If the user is clicking the 'Super settings..' button
//
if (ev_target.hasClass('sgr-prefs-menu-item')) {
var sgr_prefs_menu = ev_target.closest("#sgr-prefs-menu");
// Remove settings menu
//
if (sgr_prefs_menu.hasClass("goog-button-base-open")) {
$.sgr.removeSgrSettingsMenu();
// Add settings menu
//
} else {
sgr_prefs_menu.addClass("goog-button-base-open");
// Inject our settings options
//
$("body").append(
'<div class="goog-menu goog-menu-vertical" role="menu" aria-haspopup="true" tabindex="-1" id="sgr-prefs-menu-menu" aria-activedescendant="">' +
$.sgr.getGoogMenuitemHtml('menu_entry_tabs', 'Show entry content tabs', $.sgr.getSetting('entry_tabs')) +
$.sgr.getGoogMenuseparatorHtml() +
$.sgr.getGoogMenuitemHtml('menu_use_iframes', 'Full entry content', $.sgr.getSetting('use_iframes')) +
$.sgr.getGoogMenuitemHtml('menu_use_readability', 'Readable content', $.sgr.getSetting('use_readability')) +
$.sgr.getGoogMenuseparatorHtml() +
$.sgr.getGoogMenuitemHtml('menu_readability_pre_fetch', 'Pre-fetch readable content', $.sgr.getSetting('readability_pre_fetch')) +
$.sgr.getGoogMenuitemHtml('menu_url_in_subject', 'Show host in subject', $.sgr.getSetting('url_in_subject')) +
'</div>'
);
var offset = sgr_prefs_menu.offset();
$.sgr.togglePreFetchReadableContentMenuOption();
$("#sgr-prefs-menu-menu").css('left', offset.left).css('top', offset.top + sgr_prefs_menu.height()).show();
// Initialise a hover event for hovering over our settings menu options
//
$(".sgr-menuitem").hover(
function(ev) {
if (!$(this).hasClass("goog-menuitem-disabled")) {
$(this).addClass("goog-menuitem-highlight");
}
if ($(this).hasClass("goog-menuseparator")) {
$(this).removeClass("goog-menuitem-highlight");
}
},
function(ev) {
$(this).removeClass("goog-menuitem-highlight");
}
);
}
// Else if a user is clicking the menu itself, we can remove the super menu
//
} else if (ev_target.hasClass('sgr-menuitem-item')) {
if (!ev_target.hasClass('goog-menuitem-disabled') && !ev_target.parent().hasClass('goog-menuitem-disabled')) {
$.sgr.removeSgrSettingsMenu();
}
// Else a click anywhere else not on the 'Super settings...' button, remove the super menu
//
} else {
$.sgr.removeSgrSettingsMenu();
}
});
// div#entries live DOMNodeInserted event
//
$("#entries").live('DOMNodeInserted', function(ev){
var ev_target = $(ev.target);
// If an entry is having it's content inserted (e.g. being opened), take appropriate action to
// inject our own tabs or replace the content as necessary.
//
if (ev_target.hasClass("entry-container")) {
var entry = ev_target.closest(".entry");
//debug("#entries DOMNodeInserted .entry-container");
$.sgr.removePreview($(".preview"));
$.sgr.setEntryOriginalContent(entry);
$.sgr.handleEntryOpen(entry);
}
// If this is an .entry node being inserted
//
if (ev_target.hasClass("entry")) {
var entry = ev_target;
$.sgr.setEntryOriginalContent(entry);
// If we are in expanded view
//
if ($.sgr.isExpandedView()) {
$.sgr.handleEntryOpen(entry);
} else {
// Add hostname to subject
//
$.sgr.addHostnameToSubject(entry, '.entry-title');
// Pre fetch readable content
//
if (!entry.hasClass("read") && $.sgr.getSetting("use_readability") && $.sgr.getSetting("readability_pre_fetch")) {
$.sgr.sendReadabilityFetchRequest(entry, {pre_fetch: true});
}
}
}
});
// #entries live DOMNodeRemoved event
//
$("#entries").live('DOMNodeRemoved', function(ev){
var ev_target = $(ev.target);
// If the entry is being closed (.entry-container is being removed) note the time and remove
// any DOM components we have previously added for this entry.
//
if (ev_target.hasClass("entry-container")) {
var entry = ev_target.closest(".entry");
// Store the time that this particular entry is being closed
//
var entry_closed_at_time = new Date();
var entry_xpath = $.sgr.getXPath(entry);
$.sgr.entry_closed_at_time[entry_xpath] = entry_closed_at_time.getTime();
//debug("setting entry_closed_at_time for " + entry_xpath + " : " + $.sgr.entry_closed_at_time[entry_xpath]);
// Cleanup any iframes and entry tabs we have previously injected for this entry
//
$.sgr.removePreview(entry);
entry.removeClass("readable");
$.sgr.removeEntryTabs(entry);
}
});
// Feed/folder header DOMNodeInserted
//
$("#viewer-top-controls").live('DOMNodeInserted', function(ev){
var ev_target = $(ev.target);
// After a "Settings..." button has been injected, add our own "Super settings.." button
//
if (ev_target.attr('id') == "stream-prefs-menu") {
$.sgr.initSgrSettingsButton();
}
});
// Feed/folder setting menu option live click event
//
$(".sgr-menuitem").live('click', function(ev) {
var setting_name = $(this).attr('id').match(/^menu_(.*)/)[1];
// Return if this menu item is disabled
//
if ($(this).hasClass("goog-menuitem-disabled")) {
return false;
}
// If this feed/folder doesn't already have a setting for this, use
// the global setting to determine what to set the feed/folder setting to
//
var setting_value = !$.sgr.getSetting(setting_name);
$.sgr.setLocalSetting(setting_name, setting_value);
if (setting_value) {
// Set the setting in the menu to display our new value (a tick beside the setting).
//
$(this).addClass("goog-option-selected");
// Special case for mutually exclusive use_iframes / use_readability
//
if (setting_name == 'use_iframes') {
$.sgr.setLocalSetting('use_readability', !setting_value);
if (setting_value) {
$("#menu_use_readability").removeClass("goog-option-selected");
}
$.sgr.togglePreFetchReadableContentMenuOption();
$.sgr.switchAllEntriesToPreview();
} else if (setting_name == 'use_readability') {
$.sgr.setLocalSetting('use_iframes', !setting_value);
if (setting_value) {
$("#menu_use_iframes").removeClass("goog-option-selected");
}
$.sgr.togglePreFetchReadableContentMenuOption();
$.sgr.switchAllEntriesToReadable();
}
} else {
$(this).removeClass("goog-option-selected");
}
// Entry tabs toggle
//
if (setting_name == 'entry_tabs') {
$.sgr.toggleEntryTabs();
// Show hostname in subject toggle
//
} else if (setting_name == 'url_in_subject') {
$.sgr.toggleHostnameInSubjects();
// Pre-fetch readable content if this is being enabled
//
} else if (setting_name == 'readability_pre_fetch' && setting_value && $.sgr.getSetting("use_readability")) {
$.sgr.preFetchAllUnreadEntries();
}
});
// Entry tab live click
//
$(".sgr-entry-tab").live('click', function(ev) {
var tab = $(ev.target);
var entry = tab.closest(".entry");
// Readable
//
if (tab.hasClass("sgr-tab-readable")) {
$.sgr.checkAndShowReadableEntry(entry);
$.sgr.sendRequest({action: 'ga_track_event', ga_category: 'ReadableEntry', ga_action: 'click'});
// Link
//
} else if (tab.hasClass("sgr-tab-link")) {
debug(".sgr-entry-tab live click for sgr-tab-link");
$.sgr.checkAndShowPreview(entry);
$.sgr.sendRequest({action: 'ga_track_event', ga_category: 'IframeEntry', ga_action: 'click'});
// Feed
//
} else if (tab.hasClass("sgr-tab-feed")) {
$.sgr.checkAndShowEntryOriginalContent(entry);
$.sgr.sendRequest({action: 'ga_track_event', ga_category: 'OriginalEntry', ga_action: 'click'});
}
});
// Sign out link click
//
$("#guser").live("click",function(ev) {
var ev_target = $(ev.target);
if (ev_target.attr('href') == "https://www.google.com/accounts/Logout?service=reader") {
$.sgr.runReaderLogout();
}
});
// Keyboard shortcut help - DOMNodeInserted live event
//
$(".keyboard-help-banner .secondary-message").live('DOMNodeInserted',function(ev){
var ev_target = $(ev.target);
if (ev_target.attr('id') == 'keyboard-help-container') {
var start_tr = ev_target.find("#keyboard-help tr:eq(9)");
start_tr.find("td:eq(0)").remove();
start_tr.find("td:eq(0)").replaceWith('<th colspan="2">Super - acting on items</th>');
start_tr.next().find("td:eq(0)").addClass("key").html("8:");
start_tr.next().find("td:eq(1)").addClass("desc").html("view readable content");
start_tr.next().next().find("td:eq(0)").addClass("key").html("9:");
start_tr.next().next().find("td:eq(1)").addClass("desc").html("view link in iframe");
start_tr.next().next().next().find("td:eq(0)").addClass("key").html("0:");
start_tr.next().next().next().find("td:eq(1)").addClass("desc").html("view original entry");
}
});
if (chrome) {
// Chrome listener for background messages
//
chrome.extension.onRequest.addListener($.sgr.receiveRequest);
}
$.sgr.sendRequest({action: 'ga_track_pageview', track_url: self.location.pathname});
}
// Remove our "Super settings.." menu from the DOM
//
$.sgr.removeSgrSettingsMenu = function() {
$("#sgr-prefs-menu").removeClass("goog-button-base-open");
$("#sgr-prefs-menu-menu").remove();
}
// Add our "Super settings..." menu to the DOM
//
$.sgr.initSgrSettingsButton = function() {
$("#stream-prefs-menu").after($.sgr.getSgrSettingsButtonHtml());
}
// Update the selected Entry Tab based on the entry content being shown
//
$.sgr.updateSelectedEntryTab = function(entry) {
//debug("$.sgr.updateSelectedEntryTab()");
var tab = null;
if (entry.hasClass("preview")) {
tab = entry.find(".sgr-tab-link");
} else if (entry.hasClass("readable")) {
tab = entry.find(".sgr-tab-readable");