forked from ywzhaiqi/userChromeJS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
uAutoPagerize.uc.js
2714 lines (2446 loc) · 99 KB
/
uAutoPagerize.uc.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
// ==UserScript==
// @name uAutoPagerize 中文规则增强版
// @namespace http://d.hatena.ne.jp/Griever/
// @description 在同一个页面自动加载下一页
// @include main
// @modified ywzhaiqi
// @compatibility Firefox 17
// @charset UTF-8
// @version 0.3.0
// @homepageURL https://github.com/ywzhaiqi/userChromeJS/tree/master/uAutoPagerize
// @reviewURL http://bbs.kafan.cn/thread-1555846-1-1.html
// @optionsURL about:config?filter=uAutopagerize
// @note 大幅修改 By ywzhaiqi
// @note 0.3.0 本家に倣って Cookie の処理を変更した
// @note 0.2.9 remove E4X
// @note 0.2.8 履歴に入れる機能を廃止
// @note 0.2.7 Firefox 14 でとりあえず動くように修正
// @note 0.2.6 組み込みの SITEINFO を修正
// @note 0.2.5 MICROFORMAT も設定ファイルから追加・無効化できるようにした
// @note 0.2.5 スペースアルクで動かなくなってたのを修正
// @note 0.2.4 SITEINFO のソートをやめてチェックの仕方を変えた(一度SITEINFOを更新した方がいいかも)
// @note 0.2.4 naver まとめ、kakaku.com 修正
// @note 0.2.3 kakaku.com のスペック検索に対応
// @note 0.2.3 Fx7 くらいから xml で動かなくなってたのを修正
// @note 0.2.3 ver 0.2.2 で nextLink に form 要素が指定されている場合などに動かなくなってたのを修正
// @note 0.2.2 コンテキストメニューが意外と邪魔だったので葬った
// @note 0.2.1 コンテキストメニューに次のページを開くメニューを追加
// @note 0.2.1 区切りのアイコンをクリックしても色が変わらなかったのを修正
// @note 0.2.0 INCLUDE を設定できるようにした
// @note 0.2.0 INCLUDE, EXCLUDE をワイルドカード式にした
// @note 0.2.0 アイコンに右クリックメニューを付けた
// @note 0.2.0 スクロールするまでは次を読み込まないオプションをつけた
// ==/UserScript==
// this script based on
// AutoPagerize version: 0.0.41 2009-09-05T16:02:17+09:00 (http://autopagerize.net/)
// oAutoPagerize (http://d.hatena.ne.jp/os0x/searchdiary?word=%2a%5boAutoPagerize%5d)
//
// Released under the GPL license
// http://www.gnu.org/copyleft/gpl.html
(function(css) {
// 放置的位置,true为地址栏,false为附加组件栏(可移动按钮)
var isUrlbar = false;
var ORIGINAL_SITEINFO = false; // 原版规则是否启用?略大,但很多站点都可以翻页
var SIMPLE_SEPARATOR = false; // true 原版的分隔条,false 多功能分隔条
var DB_FILENAME_CN = "uSuper_preloader.db.js"; // 中文数据库的位置
var DB_FILENAME_JSON = "uAutoPagerize.json"; // 原版数据库位置
var FORCE_TARGET_WINDOW = true;
var BASE_REMAIN_HEIGHT = 600;
var DEBUG = false;
var AUTO_START = true;
var SCROLL_ONLY = false;
var CACHE_EXPIRE = 24 * 60 * 60 * 1000;
var XHR_TIMEOUT = 30 * 1000;
// 新增的
var MAX_PAGER_NUM = -1; // 默认最大翻页数, -1表示无限制
var IMMEDIATELY_PAGER_NUM = 3; // 立即加载的初始页数
var USE_IFRAME = true; // 是否启用 iframe 加载下一页(浏览器级,默认只允许JavaScript,在 createIframe 中可设置其它允许)
var USE_FORCE_NEXT_PAGE = true; // 是否启用强制拼接?
var PRE_REQUEST_NEXT_PAGE = true; // 提前预读下一页..就是翻完第1页,立马预读第2页,翻完第2页,立马预读第3页..(大幅加快翻页快感-_-!!)
var SEPARATOR_RELATIVELY = true; //分隔符.在使用上滚一页或下滚一页的时候是否保持相对位置..
// 出在自动翻页信息附加显示真实相对页面信息,一般能智能识别出来。如果还有站点不能识别,可以把地址的特征字符串加到下面
// 最好不要乱加,一些不规律的站点显示出来的数字也没有意义
var REALPAGE_SITE_PATTERN = ['search?', 'search_', 'forum', 'thread'];
var SITEINFO_CN_IMPORT_URLS = [
//Super_preloader 的翻页规则更新地址
// "http://simpleu.googlecode.com/svn/trunk/scripts/Super_preloader.db.js",
"https://userscripts.org/scripts/source/142198.user.js",
// ywzhaiqi 的规则。github 速度略慢,用 googlecode
"http://autopagerize-userchrome.googlecode.com/git/_uAutoPagerize.js",
];
var SITEINFO_IMPORT_URLS = ORIGINAL_SITEINFO ? [
'http://wedata.net/databases/AutoPagerize/items.json',
] : [];
// ワイルドカード(*)で記述する
var INCLUDE = [
"*"
];
var EXCLUDE = [
'https://mail.google.com/*',
'https://maps.google.*/*',
'https://www.google.com/maps/*',
'https://www.google.com/calendar*',
'http://www.google.*/reader/*',
'*://app.yinxiang.com/*',
'*://www.dropbox.com/*',
'*://www.toodledo.com/*',
'*://www.wumii.com/*',
'*/archives/*'
];
var MY_SITEINFO = [
{
url : '^https?://mobile\\.twitter\\.com/'
,nextLink : '//div[contains(concat(" ",normalize-space(@class)," "), " w-button-more ")]/a[@href]'
,pageElement : '//div[@class="timeline"]/table[@class="tweet"] | //div[@class="user-list"]/table[@class="user-item"]'
,exampleUrl : 'https://mobile.twitter.com/ https://mobile.twitter.com/search?q=css'
},
];
var MICROFORMAT = [
{
url : '^https?://.*',
nextLink : '//a[@rel="next"] | //link[@rel="next"]',
pageElement : '//*[contains(@class, "autopagerize_page_element")]',
insertBefore: '//*[contains(@class, "autopagerize_insert_before")]'
}
];
var FORCE_SITEINFO = {
url : '^https?://.*',
nextLink : 'auto;',
pageElement: "//body/*"
};
// 分页导航图标的颜色
var COLOR = {
on: '#0f0',
off: '#ccc',
enable: '#0f0',
disable: '#ccc',
loading: '#0ff',
terminated: '#00f',
error: '#f0f'
};
// 分页导航上面的横线,背景在下面的 background 修改
var SEPARATOR_HR = true;
// 分页导航的4个图标
var sep_icons={
top: 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsMAAA7DAcdvqGQAAAEnSURBVDhPY6AKkFlt+J8cDNVOBQOwAZACwbPa/wXPaf+XWquHXzE6kFql/1/wvPZ/oZu6YCx4Qfu/+Bpd4gwRX6X3nx9os8B13f8CN6AYyOYHukRktTZ+Q8RW6fznPaP1n++qzn++a2gYKAaSE1qthd0QwZka/zm3qPznOqT+n/uc1n+eK9ooGCQGkuPcpvKfb5YqqiFaDTb/mcvF/jPXSvxn7ZP5z7FD9T/XJaAGJAwSA8mB1DBXiP+XqcATJmwLFf5zXtBCwSAxqDRhwDJD7j/7OU0UDBKDShMGTNOATj2ljoJBYlBpwoBxssx/puNATSegGMhmnEKKAc2S/xnXKf5n3KQEweuB7FZJ4g2w6vT9z1Am9p+hUhyCgWyTVjfiDSAeMDAAAJhj4ilBCDLiAAAAAElFTkSuQmCC'
,bottom: 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsMAAA7DAcdvqGQAAAEqSURBVDhPY6AJMGl1+89QJvafoVIcgoFsq07f/1BpwoCxVfI/43rF/4yblCB4HZDdLEmCAVNk/jMdV//PdAKKgWzGyTLEG8A0TeY/8yl1FAwSg0oTBiwz5P6zn9NEwSAxqDRhwLZQ4T/nBS0UDBKDSmMCmQrd/8wV4v+ZayX+s/bJ/OfYofqf65IWCgaJgeRAapjLxf5rNdigGsg3S/U/5zaV/1yH1P9zn9P6z3NFGwWDxEBynFtU/gvO1MDuGqHVWv95z2j957uq85/vGhoGioHkxFbp4A8LkdXa//nPaf8XuK77X+AGFAPZ/Ge1/4uv0iMuIMXX6P4XvKD9X+imLhgLntf+L7VKnzjNMCC1Vu+/INAlgkCbZVYb4tcMUkAOhmqnggHkAwYGAFvC4in4UgSOAAAAAElFTkSuQmCC'
,up: 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsMAAA7DAcdvqGQAAAEqSURBVDhPY6Ap0Fpo919zoc1/KJc0ILPa8L/gWe3/gue0/0ut1SPNEKlV+v8Fz2v/F7qpC8aCF7T/i6/RJc4Q8VV6//mBNgtc1/0vcAOKgWx+oEtEVmvjN0Rslc5/3jNa//mu6vznu4aGgWIgOaHVWtgNEZyp8Z9zi8p/rkPq/7nPaf3nuaKNgkFiIDnObSr/+Wapohqi1WDzn7lc7D9zrcR/1j6Z/xw7VP9zXQJqQMIgMZAcSA1zhfh/mQo8YcK2UOE/5wUtFAwSg0oTBiwz5P6zn9NEwSAxqDRhQLEBTNOAfj2ljoJBYlBpwoBxssx/puNATSegGMhmnEKKAc2S/xnXKf5n3KQEweuB7FZJ4g2w6vT9z1Am9p+hUhyCgWyTVjfiDSAeMDAAAG+OupggV3NXAAAAAElFTkSuQmCC'
,down: 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsMAAA7DAcdvqGQAAAEvSURBVDhPtZBLS8NAFIXz8JWqaVMksWUQlZbYSVMRXElx6c6/o3UhCNKFGzeKaHfZJj5ARFy7UURjEZ+/5jhjp4tRManigQ8u58y9c7nKv2i+uQRlxYay5nRg9cLWMkScLLVZgHoyBfV0usMxqzcLPQzYJdCuXGjXAlarOyT9AG2PQL9xJbgn4mT17U9gMK5IcE/EyfrzgIFgEkabSnBPxF9FGj70hgN9fRz92wRDF2VkHqgE93jG3+irNuhGXR5otsowzkvIXLoYjilGHj0J7vHMOCvBOpj5fpt8RDF6S2E+VWE+f4J5PLPD6s+3GIs8ZGMPuRcfuVcBq7N3Hpywlu6QzqEPq+0h/+Z/YN17KIaz6Zq7Kh7VYLFNLPYzieZ6a+6qEtRBg8XfNaeTorwD6Qm6mCukyg8AAAAASUVORK5CYII='
};
// 分页导航的样式
var sep_style = [
'.autopagerize_page_info {',
'clear: both;',
// 'background: none repeat scroll 0% 0% rgb(230, 230, 230);',
'margin: 10px 0;',
'text-align: center;',
'}',
'.autopagerize_page_info img {',
'width: 16px;',
'height: 16px;',
'padding-top: 4px;',
'padding-bottom: 4px;',
'margin-left: 6px;',
'margin-right: 6px;',
'display: inline-block;',
'vertical-align: middle;',
'}',
'.autopagerize_link {',
'padding: 0px;',
'margin-left: 5px;',
'vertical-align: middle;',
'}',
'.autopagerize_page_info input {',
'padding: 0px;',
'text-align: center;',
'vertical-align: middle;',
'}',
].join('\n');
let { classes: Cc, interfaces: Ci, utils: Cu, results: Cr } = Components;
if (!window.Services) Cu.import("resource://gre/modules/Services.jsm");
if (typeof window.uAutoPagerize != 'undefined') {
window.uAutoPagerize.destroy();
}
var ns = window.uAutoPagerize = {
INCLUDE_REGEXP : /./,
EXCLUDE_REGEXP : [],
MICROFORMAT : MICROFORMAT.slice(),
MY_SITEINFO : MY_SITEINFO.slice(),
SITEINFO_CN : [],
SITEINFO : [],
FORCE_SITEINFO : FORCE_SITEINFO,
get prefs() {
delete this.prefs;
return this.prefs = Services.prefs.getBranch("uAutoPagerize.");
},
get file() {
var aFile = Services.dirsvc.get('UChrm', Ci.nsILocalFile);
aFile.appendRelativePath('_uAutoPagerize.js');
delete this.file;
return this.file = aFile;
},
get file_cn() {
var aFile = Services.dirsvc.get('UChrm', Ci.nsILocalFile);
aFile.appendRelativePath(DB_FILENAME_CN);
delete this.file_cn;
return this.file_cn = aFile;
},
get INCLUDE() INCLUDE,
set INCLUDE(arr) {
try {
this.INCLUDE_REGEXP = arr.length > 0 ?
new RegExp(arr.map(wildcardToRegExpStr).join("|")) :
/./;
INCLUDE = arr;
} catch (e) {
log("INCLUDE 不正确");
}
return arr;
},
get EXCLUDE() EXCLUDE,
set EXCLUDE(arr) {
try {
this.EXCLUDE_REGEXP = arr.map(function(s){
return new RegExp(wildcardToRegExpStr(s));
});
EXCLUDE = arr;
} catch (e) {
log("EXCLUDE 不正确");
}
return arr;
},
get AUTO_START() AUTO_START,
set AUTO_START(bool) {
updateIcon();
return AUTO_START = !!bool;
},
get BASE_REMAIN_HEIGHT() BASE_REMAIN_HEIGHT,
set BASE_REMAIN_HEIGHT(num) {
num = parseInt(num, 10);
if (!num) return num;
let m = $("uAutoPagerize-BASE_REMAIN_HEIGHT");
if (m) m.setAttribute("tooltiptext", BASE_REMAIN_HEIGHT = num);
return num;
},
get MAX_PAGER_NUM() MAX_PAGER_NUM,
set MAX_PAGER_NUM(num) {
num = parseInt(num, 10);
if (!num) return num;
let m = $("uAutoPagerize-MAX_PAGER_NUM");
if (m) m.setAttribute("tooltiptext", MAX_PAGER_NUM = num);
return num;
},
get IMMEDIATELY_PAGER_NUM() IMMEDIATELY_PAGER_NUM,
set IMMEDIATELY_PAGER_NUM(num){
num = parseInt(num, 10);
if (!num && (num != 0)) return num;
let m = $("uAutoPagerize-immedialate-pages");
if (m) m.value = num;
return num;
},
get DEBUG() DEBUG,
set DEBUG(bool) {
let m = $("uAutoPagerize-DEBUG");
if (m) m.setAttribute("checked", DEBUG = !!bool);
return bool;
},
get FORCE_TARGET_WINDOW() FORCE_TARGET_WINDOW,
set FORCE_TARGET_WINDOW(bool) {
let m = $("uAutoPagerize-FORCE_TARGET_WINDOW");
if (m) m.setAttribute("checked", FORCE_TARGET_WINDOW = !!bool);
return bool;
},
get SCROLL_ONLY() SCROLL_ONLY,
set SCROLL_ONLY(bool) {
let m = $("uAutoPagerize-SCROLL_ONLY");
if (m) m.setAttribute("checked", SCROLL_ONLY = !!bool);
return bool;
},
get PRE_REQUEST_NEXT_PAGE() PRE_REQUEST_NEXT_PAGE,
set PRE_REQUEST_NEXT_PAGE(bool) {
let m = $("uAutoPagerize-PRE_REQUEST_NEXT_PAGE");
if (m) m.setAttribute("checked", PRE_REQUEST_NEXT_PAGE = !!bool);
return bool;
},
init: function() {
ns.style = addStyle(css);
ns.icon = ns.addButton();
ns.popupMenu = ns.addPopupMenu();
if(USE_FORCE_NEXT_PAGE)
ns.popupMenu.addEventListener("popupshowing", this.showForcePageMenu, false);
["DEBUG", "AUTO_START", "FORCE_TARGET_WINDOW", "SCROLL_ONLY", "PRE_REQUEST_NEXT_PAGE"].forEach(function(name) {
try {
ns[name] = ns.prefs.getBoolPref(name);
} catch (e) {}
}, ns);
["BASE_REMAIN_HEIGHT","MAX_PAGER_NUM", "IMMEDIATELY_PAGER_NUM"].forEach(function(name) {
try {
ns[name] = ns.prefs.getIntPref(name);
} catch (e) {}
}, ns);
if (!getCache())
requestSITEINFO();
if(!ns.loadSetting_CN())
ns.requestSITEINFO_CN();
ns.INCLUDE = INCLUDE;
try{
let str = ns.prefs.getCharPref("EXCLUDE");
ns.EXCLUDE = str.split(",");
}catch(e){}
if(!ns.EXCLUDE){
ns.EXCLUDE = EXCLUDE;
}
ns.addListener();
ns.loadSetting();
updateIcon();
},
uninit: function() {
ns.removeListener();
["DEBUG", "AUTO_START", "FORCE_TARGET_WINDOW", "SCROLL_ONLY", "PRE_REQUEST_NEXT_PAGE"].forEach(function(name) {
try {
ns.prefs.setBoolPref(name, ns[name]);
} catch (e) {}
}, ns);
["BASE_REMAIN_HEIGHT", "MAX_PAGER_NUM", "IMMEDIATELY_PAGER_NUM"].forEach(function(name) {
try {
ns.prefs.setIntPref(name, ns[name]);
} catch (e) {}
}, ns);
ns.prefs.setCharPref("EXCLUDE", ns.EXCLUDE.join(","));
},
theEnd: function() {
ns.IMMEDIATELY_PAGER_NUM = $("uAutoPagerize-immedialate-pages").value;
var ids = ["uAutoPagerize-icon", "uAutoPagerize-popup"];
for (let [, id] in Iterator(ids)) {
let e = document.getElementById(id);
if (e) e.parentNode.removeChild(e);
}
ns.style.parentNode.removeChild(ns.style);
ns.removeListener();
},
destroy: function() {
ns.theEnd();
ns.uninit();
delete window.uAutoPagerize;
},
addListener: function() {
gBrowser.mPanelContainer.addEventListener('DOMContentLoaded', this, true);
gBrowser.mTabContainer.addEventListener('TabSelect', this, false);
gBrowser.mTabContainer.addEventListener('TabClose', this, false);
window.addEventListener('uAutoPagerize_destroy', this, false);
window.addEventListener('unload', this, false);
},
removeListener: function() {
gBrowser.mPanelContainer.removeEventListener('DOMContentLoaded', this, true);
gBrowser.mTabContainer.removeEventListener('TabSelect', this, false);
gBrowser.mTabContainer.removeEventListener('TabClose', this, false);
window.removeEventListener('uAutoPagerize_destroy', this, false);
window.removeEventListener('unload', this, false);
},
handleEvent: function(event) {
switch(event.type) {
case "DOMContentLoaded":
if (this.AUTO_START)
this.launch(event.target.defaultView);
break;
case "TabSelect":
if (this.AUTO_START)
updateIcon();
break;
case "TabClose":
// 如果有 iframe 则移除
let browser = gBrowser.getBrowserForTab(event.target);
if(browser.uAutoPagerizeIframe){
var parent = browser.uAutoPagerizeIframe.parentNode;
parent.removeChild(browser.uAutoPagerizeIframe);
browser.uAutoPagerizeIframe = null;
}
break;
case "uAutoPagerize_destroy":
this.destroy(event);
break;
case "unload":
this.uninit(event);
break;
}
},
addButton: function(){
var icon;
if(isUrlbar){
icon = $('urlbar-icons').appendChild($C("image", {
id: "uAutoPagerize-icon",
state: "disable",
tooltiptext: "disable",
onclick: "if (event.button != 2) uAutoPagerize.iconClick(event);",
context: "uAutoPagerize-popup",
style: "padding: 0px 2px;",
}));
}else{
icon = $('addon-bar').appendChild($C("toolbarbutton", {
id: "uAutoPagerize-icon",
class: "toolbarbutton-1",
type: "context",
removable: "true",
state: "disable",
tooltiptext: "disable",
onclick: "if (event.button != 2) uAutoPagerize.iconClick(event);",
context: "uAutoPagerize-popup",
image: "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAACXBIWXMAAC4jAAAuIwF4pT92AAAAQElEQVR42mNgoAbg/sb9HxmDxNTuq/1HxiAx64PW/5HxcDKAYkCsbbaLbf8j4+FkAMWAWNuMe4z/I+PhZAAlAADWnzKwMlmELQAAAABJRU5ErkJggg=="
}));
setTimeout(function(icon){
icon.removeAttribute("image");
}, 500, icon);
var updateToolbar = {
runOnce: function(){
var toolbars = document.querySelectorAll("toolbar");
Array.slice(toolbars).forEach(function(toolbar) {
var currentset = toolbar.getAttribute("currentset");
if (currentset.split(",").indexOf("uAutoPagerize-icon") < 0) return;
toolbar.currentSet = currentset;
try {
BrowserToolboxCustomizeDone(true);
} catch (ex) {}
});
}
};
updateToolbar.runOnce();
}
return icon;
},
addPopupMenu: function(){
var xml = '\
<menupopup id="uAutoPagerize-popup"\
position="after_start"\
ignorekeys="true"\
onpopupshowing="uAutoPagerize.onPopupShowing(event);" >\
<menuitem label="启用自动翻页"\
id="uAutoPagerize-AUTOSTART"\
type="checkbox"\
autoCheck="true"\
checked="'+ AUTO_START +'"\
oncommand="uAutoPagerize.toggle(event);"/>\
<menuitem label="载入配置"\
tooltiptext="左键载入配置,右键编辑配置文件"\
oncommand="uAutoPagerize.loadSetting(true);"\
onclick="if(event.button == 2) { uAutoPagerize.edit(uAutoPagerize.file); return false; }"/>\
<menuitem label="更新中文规则" \
tooltiptext="包含Super_preloader规则和本人维护的规则"\
oncommand="uAutoPagerize.resetSITEINFO_CN();"/>\
<menuitem label="更新原版规则" hidden="' + !ORIGINAL_SITEINFO + '" \
oncommand="uAutoPagerize.resetSITEINFO();"/>\
<menuseparator/>\
<hbox>\
<textbox id="uAutoPagerize-blacklist-textbox" oninput="uAutoPagerize.checkUrl(event);"/>\
<toolbarbutton label="添加" id="uAutoPagerize-blacklist-icon" \
tooltiptext="添加到黑名单" onclick="uAutoPagerize.blacklistBtnClick(event);"/>\
</hbox>\
<hbox style="padding-left:32px;">\
立即翻<textbox type="number" value="' + IMMEDIATELY_PAGER_NUM + '" tooltiptext="连续翻页的数量" \
id="uAutoPagerize-immedialate-pages" style="width:35px" />页\
<toolbarbutton label="开始" tooltiptext="现在立即开始连续翻页" \
id="uAutoPagerize-immedialate-start" oncommand="uAutoPagerize.immediatelyStart();"/>\
</hbox>\
<menuitem label="强制拼接" type="checkbox" disabled="true" \
id="uAutoPagerize-force_nextpage" hidden="' + !USE_FORCE_NEXT_PAGE + '" \
oncommand="uAutoPagerize.forcePageToggle();" />\
<menuseparator/>\
<menuitem label="提前预读下一页"\
tooltiptext="翻完第1页,立马预读第2页,翻完第2页,立马预读第3页..(大幅加快翻页快感-_-!!)"\
id="uAutoPagerize-PRE_REQUEST_NEXT_PAGE"\
type="checkbox"\
autoCheck="false"\
checked="'+ PRE_REQUEST_NEXT_PAGE +'"\
oncommand="uAutoPagerize.PRE_REQUEST_NEXT_PAGE = !uAutoPagerize.PRE_REQUEST_NEXT_PAGE;"/>\
<menuitem label="新标签打开链接"\
id="uAutoPagerize-FORCE_TARGET_WINDOW"\
type="checkbox"\
autoCheck="false"\
checked="'+ FORCE_TARGET_WINDOW +'"\
oncommand="uAutoPagerize.FORCE_TARGET_WINDOW = !uAutoPagerize.FORCE_TARGET_WINDOW;"/>\
<menuitem label="设置翻页高度"\
id="uAutoPagerize-BASE_REMAIN_HEIGHT"\
tooltiptext="'+ BASE_REMAIN_HEIGHT +'"\
oncommand="uAutoPagerize.BASE_REMAIN_HEIGHT = prompt(\'\', uAutoPagerize.BASE_REMAIN_HEIGHT);"/>\
<menuitem label="设置最大自动翻页数"\
id="uAutoPagerize-MAX_PAGER_NUM"\
tooltiptext="'+ MAX_PAGER_NUM +'"\
oncommand="uAutoPagerize.MAX_PAGER_NUM = prompt(\'\', uAutoPagerize.MAX_PAGER_NUM);"/>\
<menuitem label="滚动时才翻页"\
id="uAutoPagerize-SCROLL_ONLY"\
type="checkbox"\
autoCheck="false"\
checked="'+ SCROLL_ONLY +'"\
oncommand="uAutoPagerize.SCROLL_ONLY = !uAutoPagerize.SCROLL_ONLY;"/>\
<menuitem label="调试模式"\
id="uAutoPagerize-DEBUG"\
type="checkbox"\
autoCheck="false"\
checked="'+ DEBUG +'"\
oncommand="uAutoPagerize.DEBUG = !uAutoPagerize.DEBUG;"/>\
<menuseparator/>\
<menuitem label="在线搜索翻页规则"\
id="uAutoPagerize-search"\
oncommand="uAutoPagerize.search()"/>\
</menupopup>\
';
var range = document.createRange();
range.selectNodeContents($('mainPopupSet'));
range.collapse(false);
range.insertNode(range.createContextualFragment(xml.replace(/\n|\t/g, '')));
range.detach();
return $("uAutoPagerize-popup");
},
onPopupShowing: function(event){
var excludeStr;
var locationHref = content.location.href;
var blacklistBtn = $("uAutoPagerize-blacklist-icon");
var blacklistText = $("uAutoPagerize-blacklist-textbox");
for(let [index, reg] in Iterator(ns.EXCLUDE_REGEXP)){
if(reg.test(locationHref)){
excludeStr = ns.EXCLUDE[index];
break;
}
}
if(excludeStr){
blacklistBtn.setAttribute("label", "修改");
blacklistText.setAttribute("oldvalue", excludeStr);
blacklistText.setAttribute("tooltiptext", "修改黑名单或从中删除,空白为删除");
blacklistText.style.color = "green";
}else{
excludeStr = "*" + content.location.host + "*";
blacklistBtn.setAttribute("label", "添加");
blacklistText.removeAttribute("oldvalue");
blacklistText.setAttribute("tooltiptext", "添加到黑名单中");
blacklistText.style.color = "";
}
blacklistText.value = excludeStr;
},
loadSetting: function(isAlert) {
var data = loadText(this.file);
if (!data) return false;
var sandbox = new Cu.Sandbox( new XPCNativeWrapper(window) );
sandbox.INCLUDE = null;
// sandbox.EXCLUDE = null;
sandbox.MY_SITEINFO = [];
sandbox.MICROFORMAT = [];
sandbox.USE_MY_SITEINFO = true;
sandbox.USE_MICROFORMAT = true;
try {
Cu.evalInSandbox(data, sandbox, '1.8');
} catch (e) {
log('load error.', e);
return alerts('uAutoPagerize', '配置文件有错误');
}
ns.MY_SITEINFO = sandbox.USE_MY_SITEINFO ? sandbox.MY_SITEINFO.concat(MY_SITEINFO): sandbox.MY_SITEINFO;
ns.MICROFORMAT = sandbox.USE_MICROFORMAT ? sandbox.MICROFORMAT.concat(MICROFORMAT): sandbox.MICROFORMAT;
if (sandbox.INCLUDE)
ns.INCLUDE = sandbox.INCLUDE;
// if (sandbox.EXCLUDE)
// ns.EXCLUDE = sandbox.EXCLUDE;
ns.MY_SITEINFO.forEach(function(i){
i.type = 'my';
});
if (isAlert)
alerts('uAutoPagerize', '配置文件已经重新载入');
return true;
},
launch: function(win, timer){
var doc = win.document;
var locationHref = win.location.href;
if (locationHref.indexOf('http') !== 0 ||
!ns.INCLUDE_REGEXP.test(locationHref)){
return updateIcon("不包含的页面");
}
for(let [index, reg] in Iterator(ns.EXCLUDE_REGEXP)){
if(reg.test(locationHref)){
return updateIcon("排除列表, " + ns.EXCLUDE[index]);
}
}
if (!/html|xml/i.test(doc.contentType) ||
doc.body instanceof HTMLFrameSetElement ||
win.frameElement && !(win.frameElement instanceof HTMLFrameElement) ||
doc.querySelector('meta[http-equiv="refresh"]'))
return updateIcon();
if (typeof win.AutoPagerize == 'undefined') {
win.filters = [];
win.documentFilters = [];
win.requestFilters = [];
win.responseFilters = [];
win.AutoPagerize = {
addFilter : function(f) { win.filters.push(f) },
addDocumentFilter : function(f) { win.documentFilters.push(f); },
addResponseFilter : function(f) { win.responseFilters.push(f); },
addRequestFilter : function(f) { win.requestFilters.push(f); },
launchAutoPager : function(l) { launchAutoPager_org(l, win); }
}
// uAutoPagerize original
win.fragmentFilters = [];
}
var ev = doc.createEvent('Event');
ev.initEvent('GM_AutoPagerizeLoaded', true, false);
doc.dispatchEvent(ev);
var miscellaneous = [];
// 新标签打开链接。
win.fragmentFilters.push(function(df){
if (!ns.FORCE_TARGET_WINDOW) return;
var arr = Array.slice(df.querySelectorAll('a[href]:not([href^="mailto:"]):not([href^="javascript:"]):not([href^="#"])'));
arr.forEach(function (elem){
elem.setAttribute('target', '_blank');
});
});
// 还得加上nextLink,不加上的话会查找下一页链接 2 次,特别是加了自动查找功能后
var index = -1, info, nextLink;
var hashchange = false;
// 已经移到外置规则,便于更新
if (/\bgoogle\.(?:com|co\.jp|com\.hk)$/.test(win.location.host)) {
if (!timer || timer < 400) timer = 400;
hashchange = true;
}else if (/^https?:\/\/(?:images|www)\.google(?:\.[^.\/]{2,3}){1,2}\/(images\?|search\?.*tbm=isch)/.test(locationHref)) {
// Google Image
[, info] = ns.getInfo(ns.MY_SITEINFO, win);
if (info) {
if (!timer || timer < 1000) timer = 1000;
win.requestFilters.push(function(opt) {
opt.url = opt.url
.replace(/&?(gbv=\d|sout=\d)/g, "")
.replace("?", '?gbv=1&sout=1&')
});
}
}else if (win.location.host === 'www.dailymotion.com') {
win.fragmentFilters.push(function(df) {
Array.slice(df.querySelectorAll('img[data-src]')).forEach(function(img) {
img.src = img.getAttribute('data-src');
img.removeAttribute('data-src');
});
});
}else if (win.location.host === 'www.newsmth.net') { // 水木清华社区延迟加载及下一页加载的修复
timer = 1000;
hashchange = true;
}
if(hashchange){
win.addEventListener("hashchange", function(event) {
debug("Add Hashchang Event listener" + win.location.href);
if (!win.ap) {
win.setTimeout(function(){
let [index, info] = [-1, null];
if (!info) [, info] = ns.getInfo(ns.MY_SITEINFO, win);
if (!info) [, info] = ns.getInfo(null, win);
if (info) win.ap = new AutoPager(win.document, info);
updateIcon();
}, timer);
return;
}
let info = win.ap.info;
win.ap.destroy(true);
win.setTimeout(function(){
win.ap = new AutoPager(win.document, info);
updateIcon();
}, timer);
}, false);
}
win.setTimeout(function(){
win.ap = null;
miscellaneous.forEach(function(func){ func(doc, locationHref); });
var index = -1;
var startTime = new Date().getTime();
debug("---------------------------------------------------------");
debug("开始查找规则:" + locationHref);
if (!info) [, info, nextLink] = ns.getInfo(ns.MY_SITEINFO, win);
// 第二检测 Super_preloader.db 的数据库
if(!info) [index, info, nextLink] = ns.getInfo(ns.SITEINFO_CN, win);
if (info) {
if (info.requestFilter)
win.requestFilters.push(info.requestFilter.bind(win));
if (info.responseFilter)
win.responseFilters.push(info.responseFilter.bind(win));
if (info.documentFilter)
win.documentFilters.push(info.documentFilter.bind(win));
if (info.filter)
win.filters.push(info.filter.bind(win));
if (info.fragmentFilter)
win.fragmentFilters.push(info.fragmentFilter.bind(win));
if (info.startFilter)
info.startFilter.call(win, win.document);
if (info.stylish) {
let style = doc.createElement("style");
style.setAttribute("id", "uAutoPagerize-style");
style.setAttribute("type", "text/css");
style.appendChild(doc.createTextNode(info.stylish));
doc.getElementsByTagName("head")[0].appendChild(style);
}
}
if (!info) [index, info, nextLink] = ns.getInfo(ns.SITEINFO, win);
if (!info) [, info, nextLink] = ns.getInfo(ns.MICROFORMAT, win);
// 强制拼接
if(!info && win.localStorage.ap_force_nextpage){
nextLink = autoGetLink(doc);
if(nextLink)
info = FORCE_SITEINFO;
}
let spenTime = "耗时: "+ (new Date().getTime() - startTime) + 'ms. ';
if (info) {
debug(" 找到匹配当前站点的规则: 是[" + info.type + ']第' + (index + 1) + '规则,' + spenTime +
"开始运行 Autopager");
win.ap = new AutoPager(win.document, info, nextLink);
}else{
let infoLength = ns.MY_SITEINFO.length + ns.SITEINFO_CN.length + ns.MICROFORMAT.length + ns.SITEINFO.length;
debug(" 所有规则都没法找到下一页或内容," + "共查找规则" + infoLength + "个," + spenTime +
"结束运行");
}
updateIcon();
}, timer||0);
},
iconClick: function(event){
if (!event || !event.button) {
ns.toggle();
} else if (event.button == 1) {
ns.loadSetting(true);
}
},
resetSITEINFO: function() {
if (confirm('reset SITEINFO?'))
requestSITEINFO();
},
toggle: function() {
if (ns.AUTO_START) {
ns.AUTO_START = false;
updateIcon();
} else {
ns.AUTO_START = true;
if (!content.ap){
ns.launch(content);
content.ap.scroll();
}else{
content.ap.scroll();
updateIcon();
}
}
},
checkUrl: function(event){
var blacklistText = event.target;
var url = content.location.href;
if (!url)
blacklistText.style.color = "";
var urlValue = wildcardToRegExpStr(blacklistText.value);
try {
var regexp = new RegExp(urlValue);
if (regexp.test(url))
blacklistText.style.color = "green";
else
blacklistText.style.color = "red";
} catch (e) {
blacklistText.style.color = "red";
}
},
blacklistBtnClick: function(event){
var textbox = $("uAutoPagerize-blacklist-textbox");
var oldvalue = textbox.getAttribute("oldvalue");
var newValue = textbox.value.trim();
if(oldvalue){
var index = ns.EXCLUDE.indexOf(oldvalue);
if(index == -1) return;
if(!newValue || newValue == "**"){ // remove
ns.EXCLUDE.splice(index, 1);
}else{ // modified
ns.EXCLUDE[index] = newValue;
}
ns.EXCLUDE = ns.EXCLUDE; // 重新赋值,刷新 EXCLUDE_REGEXP
ns.launch(content);
}else{ // add
if(!newValue || newValue == "**") return;
ns.EXCLUDE.push(newValue);
ns.EXCLUDE = ns.EXCLUDE;
if(content.ap){
content.ap.destroy(true);
updateIcon("排除列表, " + newValue);
}
}
$('uAutoPagerize-popup').hidePopup();
},
getInfo: function (list, win) {
if (!list) list = ns.MY_SITEINFO.concat(ns.SITEINFO_CN, ns.SITEINFO);
if (!win) win = content;
var doc = win.document;
var locationHref = doc.location.href;
for (let [index, info] in Iterator(list)) {
try {
var exp = info.url_regexp || Object.defineProperty(info, "url_regexp", {
enumerable: false,
value: toRE(info.url)
}).url_regexp;
if ( !exp.test(locationHref) ) continue;
if (info.url.length > 12)
debug(" 匹配地址的规则:[" + (info.type || "") + "]" + "第" + (index + 1) + "个");
var nextLink = getElementMix(info.nextLink, doc);
if (!nextLink) {
// FIXME microformats case detection.
// limiting greater than 12 to filter microformats like SITEINFOs.
if (info.url.length > 12)
debug(' nextLink not found.', String(info.nextLink));
continue;
}
var pageElement = getElementMix(info.pageElement, doc);
if (!pageElement) {
if (info.url.length > 12 || (typeof(info.url) !='string'))
debug(' pageElement not found.', info.pageElement);
continue;
}
return [index, info, nextLink, pageElement];
} catch(e) {
log('error at launchAutoPager() : ' + e);
}
}
return [-1, null];
},
getInfoFromURL: function (url, list) {
if (!url) url = content.location.href;
var list = ns.MY_SITEINFO.concat(ns.SITEINFO_CN, ns.SITEINFO);
return list.filter(function(info, index, array) {
try {
var exp = info.url_regexp || Object.defineProperty(info, "url_regexp", {
enumerable: false,
value: toRE(info.url)
}).url_regexp;
return exp.test(url);
} catch(e){ }
});
},
edit: function(aFile) {
if (!aFile || !aFile.exists() || !aFile.isFile()) return;
var editor = Services.prefs.getCharPref("view_source.editor.path");
if (!editor) {
alert("编辑器的路径未设定。\n 请设置 view_source.editor.path");
return;
}
try {
var UI = Cc["@mozilla.org/intl/scriptableunicodeconverter"].createInstance(Ci.nsIScriptableUnicodeConverter);
UI.charset = window.navigator.platform.toLowerCase().indexOf("win") >= 0? "gbk": "UTF-8";
var path = UI.ConvertFromUnicode(aFile.path);
exec(editor, path);
} catch (e) {}
},
immediatelyStart: function(){
var pages = $("uAutoPagerize-immedialate-pages").value;
pages = pages || ns.IMMEDIATELY_PAGER_NUM;
if(content.ap){
content.ap.loadImmediately(pages);
}
},
showForcePageMenu: function(event){
var menu = $("uAutoPagerize-force_nextpage");
var reset = function(){
menu.setAttribute("disabled", "true");
menu.setAttribute("checked", "false");
};
if(!content.location.href.startsWith("http"))
return reset();
var localStorage = content.localStorage;
if(!localStorage)
return reset();
var isForce = localStorage.ap_force_nextpage;
if(isForce){
menu.setAttribute("disabled", "false");
menu.setAttribute("checked", "true");
}else{
if(content.ap)
return reset();
// 从未检测过
if(typeof content.ap_nextLink == 'undefined'){
content.ap_nextLink = autoGetLink(content.document);
}
if(content.ap_nextLink){
menu.setAttribute("disabled", "false");
menu.setAttribute("checked", "false");
}else{
reset();
}
}
},
forcePageToggle: function(event){
var forcePageMenu = $("uAutoPagerize-force_nextpage");
if(content.localStorage.ap_force_nextpage){
content.localStorage.removeItem("ap_force_nextpage");
forcePageMenu.setAttribute("checked", "false");
if(content.ap)
content.ap.destroy(true);
}else{
content.localStorage.ap_force_nextpage = true;
forcePageMenu.setAttribute("checked", "true");
if(content.ap)
content.ap.destroy(true);
if (content.AutoPagerize && content.AutoPagerize.launchAutoPager)
content.AutoPagerize.launchAutoPager([FORCE_SITEINFO]);
}
},
search: function(){
var keyword = encodeURIComponent(content.location.href);
gBrowser.selectedTab = gBrowser.addTab('http://ap.teesoft.info/?exp=0&url=' + keyword);
},
gototop: function(win){
if(!win) win = content;
win.scroll(win.scrollX, 0);
},
gotobottom: function(win){
if(!win) win = content;
var doc = win.document;
win.scroll(win.scrollX, Math.max(doc.documentElement.scrollHeight, doc.body.scrollHeight));
},
gotoprev: function(win, sepSelector, insertPoint){
if(!win)
win = content;
if(!sepSelector)
sepSelector = ".autopagerize_link";
if(!insertPoint && win.ap)
insertPoint = win.ap.insertPoint.parentNode;
var [preDS, , divS] = ns.getSeparators(win, sepSelector, insertPoint);
divS = divS || 0;
if(SEPARATOR_RELATIVELY){
preDS = win.scrollY - (divS - preDS);
}else{
preDS += win.scrollY - 6;
}
win.scroll(win.scrollY, preDS);
},
gotonext: function(win, sepSelector, insertPoint){
if(!win)
win = content;
if(!sepSelector)
sepSelector = ".autopagerize_link";
if(!insertPoint && win.ap)