-
Notifications
You must be signed in to change notification settings - Fork 190
/
smartproxy_可移动按钮.uc.js
2296 lines (2183 loc) · 89.5 KB
/
smartproxy_可移动按钮.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 SmartProxyForChina.uc.js
// @description 类似AutoProxy的UC脚本
// @author dannylee
// @namespace lidanny2012/[email protected]
// @include main
// @license MIT License
// @compatibility Firefox 13-21
// @charset UTF-8
// @version 1.2.1.9
// @note 2014/07/25 完善可移动按钮,不支持放在 panelUI 里面。
// @note 1.2.1 2014/04/26 findbar is back, bugs is fixed by ABP2.6
// @note 1.2.0 2014/04/11 Modify for FF29's AddonbarforUC.uc.js
// @note 1.1.0 2014/02/14 Fixed for FF27 ,cancel finderbar
// @note 1.0.9 2014/01/15 更新订阅后自动载入并保存,全局代理不再匹配规则
// @note 1.0.8.5 2013/9/16 修复bug
// @note 1.0.8 2013/9/7 修复个别含重定向地址的proxy bugs, 最小分辨率支持1024X768
// @note 1.0.8 2013/9/7 修改按钮动作, 左键点击弹出菜单,中键点击切换代理模式,右键点击打开系统规则和设置
// @note 1.0.7 2013/5/13 Fixed screen 1360X768 bugs
// @note 1.0.6 2013/5/09 Fixed treeview bug in FF22
// @note 1.0.5 2013/4/30 Fixed bug and change icon
// @note 1.0.4 2013/4/29 Fixed bug and url Unicode's view
// @note 1.0.3 2013/4/28 ADD gfwlist download and update
// @note 1.0.2 2013/4/26 Update For Use ABP 2.2.3 Matcher and Filter Class
// @note 1.0.1 2013/4/11 Remove E4X
// @note 1.0.0 2012/3/16 add UI for rule and import gfwlist file
// @note 0.0.5 2011/10/21 limit proxy server numbers
// @note 0.0.4 2011/8/19 update for nsIProtocolProxyService
// @note 0.0.3 2011/8/18 update filter json file
// @note 0.0.2 2011/8/15 update for abp filter class and match
// @note 0.0.1 2011/8/13 Create Project
// @note 与AutoProxy或其它代理脚本同时使用会冲突
// @note 不依赖ABP, 不安装ABP也可以使用, 也可以与ABP同时使用。
// @note Firefox代理设置为无,最好打开Firefox远程DNS解析
// @note 附加组件栏按钮, 左键点击切换代理模式,右键点击打开系统规则和设置。
// @note 下拉菜单切换代理模式和智能化的对当前站点的代理规则进行操作
// @note report bug and linkurl: https://g.mozest.com/thread-43513-1-1
// @updateURL https://j.mozest.com/ucscript/script/104.meta.js
// @screenshot http://j.mozest.com/images/uploads/previews/000/00/01/c8e11c00-f2fd-eb7f-cc32-cad6a55ae316.jpg http://j.mozest.com/images/uploads/previews/000/00/01/thumb-c8e11c00-f2fd-eb7f-cc32-cad6a55ae316.jpg
// ==/UserScript==
(function(css){
if (window.SmartProxy) {
window.SmartProxy.uninit();
delete window.SmartProxy;
}
const Cc = Components.classes;
const Ci = Components.interfaces;
const Cr = Components.results;
const Cu = Components.utils;
if (!window.Services)
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
const ProxySrv = Cc['@mozilla.org/network/protocol-proxy-service;1'].getService(Ci.nsIProtocolProxyService);
const dirService = Cc["@mozilla.org/file/directory_service;1"].getService(Ci.nsIProperties);
const ioService = Components.classes['@mozilla.org/network/io-service;1'].getService(Ci.nsIIOService);
const _eTLDService = Cc["@mozilla.org/network/effective-tld-service;1"].getService(Ci.nsIEffectiveTLDService);
const _idnService = Cc["@mozilla.org/network/idn-service;1"].getService(Ci.nsIIDNService);
const promptService = Cc["@mozilla.org/embedcomp/prompt-service;1"].getService(Ci.nsIPromptService);
const clipboard = Cc["@mozilla.org/widget/clipboard;1"].getService(Ci.nsIClipboard);
const clipboardHelper = Cc["@mozilla.org/widget/clipboardhelper;1"].getService(Ci.nsIClipboardHelper);
const FileInputStream = Components.Constructor("@mozilla.org/network/file-input-stream;1", "nsIFileInputStream", "init");
const ConverterInputStream = Components.Constructor("@mozilla.org/intl/converter-input-stream;1", "nsIConverterInputStream", "init");
const FileOutputStream = Components.Constructor("@mozilla.org/network/file-output-stream;1", "nsIFileOutputStream", "init");
const ConverterOutputStream = Components.Constructor("@mozilla.org/intl/converter-output-stream;1", "nsIConverterOutputStream", "init");
const IPREG = /^(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9])\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[0-9])$/;
const PORTREG = /^\+?[1-9][0-9]*$/;
const SUBSCRIPTION_UPDATE_SUCCESS = 'DOWNLOAD SUCCESS';
var windowMediator = Cc["@mozilla.org/appshell/window-mediator;1"].getService(Ci.nsIWindowMediator);
var EXPORTED_SYMBOLS = ["SmartProxy"];
window.SmartProxy = {
QueryInterface: function(uuid) {
if (!uuid.equals(Ci.nsISupports)) {
throw Cr.NS_ERROR_NO_INTERFACE;
}
return this;
},
_ps: Components.classes["@mozilla.org/preferences-service;1"].
getService(Components.interfaces.nsIPrefService).
getBranch("userChromeJS.SmartProxy.").
QueryInterface(Components.interfaces.nsIPrefBranch2),
Prefs: {},
SPfile: null,
SPfilters: [],
icon: null,
popupid: null,
MsgBox: null,
SVRROWS: null,
modebtn: null,
noproxy: null,
autoproxy: null,
globalproxy: null,
siteProxyOn: null,
reportgfwlist: null,
Treevisible: false,
SPfilterActionMenu: null,
_isready: false,
_FilterIsReady: false,
mode: ['disabled', 'auto', 'global'],
init: function(){
this._isready = false;
this._FilterIsReady = false;
this.loadSPoverloay();
},
uninit: function(){
if (SmartProxy.Proxy.isRegisted)
ProxySrv.uninit();
SPdefaultMatcher.clear();
this.SPfilters = null;
this._ps.removeObserver("", this, false);
this.popupid.parentNode.removeChild(this.popupid);
this.icon.parentNode.removeChild(this.icon);
var e = E("sp-mains");
if (e) e.parentNode.removeChild(e);
var e = E("SP-splitter");
if (e) e.parentNode.removeChild(e);
this.Prefs = {};
},
loadSPjson: function(){
this.SPfile = this._getSmartProxyfile(this.Prefs.patternsfile);
var afile = this._getSmartProxyfile("SPpatterns.json");
if (afile.exists())
this._importpatternsfile(afile);
else {
if (this.SPfile.exists()) {
var subrec = this._importSUBpatternsfile(this.SPfile);
if (subrec > 0)
SmartProxy.saveAllFilter();
} else {
this.showMsg("没有任何代理规则!");
this._ps.setCharPref("proxyMode", "disabled");
}
}
},
loadSPoverloay: function() {//urlbar-icons
// Search for widget toolbar by reading toolbar's currentset attribute
let container = null;
let toolbars = document.getElementsByTagName('toolbar');
let id = 'SmartProxy-button';
for (let i = 0; i < toolbars.length; i += 1) {
let toolbar = toolbars[i];
if (toolbar.getAttribute('currentset').indexOf(id) !== -1) {
container = toolbar;
}
}
if (!container) {
// console.log('111', '不存在 SmartProxy-button')
} else {
let nextNode = null;
let currentSet = container.getAttribute('currentset');
let ids = (currentSet === '__empty') ? [] : currentSet.split(',');
let idx = ids.indexOf(id);
if (idx !== -1) {
for (let i = idx; i < ids.length; i += 1) {
nextNode = document.getElementById(ids[i]);
if (nextNode) {
break;
}
}
}
// console.log('111', 'nextNode is', nextNode);
this.fixButton = function() {
container.insertItem(id, nextNode, null, false);
};
}
var overlay = '\
<overlay xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul" \
xmlns:html="http://www.w3.org/1999/xhtml"> \
<toolbarpalette id="addon-bar">\
<toolbarbutton id="SmartProxy-button" label="SmartProxy" \
class="toolbarbutton-1 chromeclass-toolbar-additional" type="menu" removable="true"\
image="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAACxElEQVQ4jY3RTUiTARgHcClCUwfT2d5NOygsETIPLTMonZvOTW225dT8wnSY22rzY9jM+fGaWqgZghCIhFF6CUqd+urUuVz5gS3nNqeFNDp0meQxqEj+HUSlw8r/+fn/eHgePz8fsdksYS6Xi/B4PITX6yG8Xi/h9XoJj8dDeFwuwrW8TJAkecxX3+/FQP92d2c7ujvb8aijDR0PWnCfrEe9vgbVlWo8bGuGxTQe6xN43Nm+XaVVoVKjxG1VOcoVJSgqzIVUKoFQmApFWTFmJkbO+QTaWxu3CwtyUJAvR17udUilEqSni8Dn83Gey4VMlvVvoMmg3xamCSEUpkIgEOBiQgLOxsaCw+Eg4nQEJFcz9gCrdYptX1lQO+zL6nWHTb31aV39eWtD3dPdsdPSXP9dX1v1Mzv7GlhsFpgEE2GnwsBghCIjQ7wHTFNGXq1OiyqtCto7FVApFSgrLUL+DTkkkkykpKagUqMEjUZDcHAwgoICERh4EmKx6BCo1CihUipQcasUirJiFBXmQibLgkiUhsSkJDQ36NFkuAtDnQ5kYx3Ixjr09nQdAmWlRcjOvgaZLAsSSSbEYhEEAgEuxMeDw+Gg4Z5uZ2ZydHdwoH/INPYqz2ZbjLFYTDF9fX0n/KYpI08ulyKZn4zLiVcQFxeH6OhoREZFgh3OBovNQitpwOCzfsxOGWE1U7sD/U8uHVx7mjLyxGIROGc4YIezwSSYYDBCERIaAjqdDjqdjnp99deh509vmqgReW9vV9Rf75qmjLwkHu+gQKPRDg4VEBAAf39/6Go0bp//njMNc62WSefK4hunc+29c9Nt//Jxw/Ft073mXLUtOhets06rmRr2CexndnaUmJ+huqxmyrG8YPllNo29nrdMlvy3eLjJONftWrWvfVj6PT8zsbv0bu6Hbent0JGB/bzMyTneR5KBR53/A0SneonKm1KCAAAAAElFTkSuQmCC" \
popup="sp-popup" tooltiptext="代理状态:" onclick="SmartProxy.iconclick(event);">\
<menupopup id="SmartProxy-popup" onpopupshowing="SmartProxy.setpopupenabled(event);">\
<menuitem id="siteProxyOn" label="对%s启用代理" hidden="true" onclick="SmartProxy.setUrlproxy(event);"/>\
<menuitem id="reportgfwlist" label="向\'哥扶我\'报告本站问题" onclick="SmartProxy.commandreport();"/>\
<menu label="默认代理服务器" id="DefaultProxyID">\
<menupopup id="sp-popup" />\
</menu>\
</menupopup>\
</toolbarbutton>\
</toolbarpalette>\
<vbox id="appcontent">\
<splitter id="SP-splitter" hidden="true"/>\
<vbox id="sp-mains" hidden="true">\
<hbox>\
<vbox id="sprulelist-details" height="300" width="60%" flex="1">\
<groupbox flex="1">\
<caption label="代理规则列表" />\
<hbox pack="end">\
<toolbarbutton id="SPupdateSubUrl" class="actiontoolbtn" label="订阅Url" width="60" tooltiptext="修改\'哥扶我\'规则下载地址" oncommand="SmartProxy.updatesubUrl();"/>\
<toolbarbutton id="SPupdateSubFilter" class="actiontoolbtn" label="更新订阅" width="70" tooltiptext="强制更新\'哥扶我\'规则" oncommand="SmartProxy.updatesubFilter();"/>\
<spacer flex="1" />\
<label value="规则主页" max-width="40" class="actionbtn" onclick="SmartProxy.loadGFWurl();"/>\
<spacer flex="1" />\
<toolbarbutton id="SPfindButton" class="actiontoolbtn" label="查找" width="40" oncommand="SmartProxy.EID(\'findbar\').startFind(SmartProxy.EID(\'findbar\').FIND_NORMAL)"/>\
<toolbarbutton id="SPfilterActionButton" type="menu" width="45" class="actiontoolbtn" label="动作">\
<menupopup id="SPfilterActionMenu" onpopupshowing="SmartProxy.FilterActions.fillActionsPopup();">\
<menuitem id="SPfilters-edit-command" label="编辑" oncommand="SmartProxy.FilterActions.startEditing();"/>\
<menuitem id="SPfilters-copy-command" label="复制" oncommand="SmartProxy.FilterActions.copySelected(true);"/>\
<menuitem id="SPfilters-delete-command" label="删除" oncommand="SmartProxy.FilterActions.deleteSelected();"/>\
<menuitem id="SPfilters-Unicode-command" label="查看解码" oncommand="SmartProxy.FilterActions.ConvertUnicode();"/>\
<menuitem id="SPfilters-Opentab-command" label="新建标签页打开" oncommand="SmartProxy.FilterActions.Openfilterintab();"/>\
</menupopup>\
</toolbarbutton>\
<toolbarbutton id="SPaddFilterButton" class="actiontoolbtn" label="添加" width="40" oncommand="SmartProxy.FilterActions.insertFilter();"/>\
<toolbarbutton id="SPsaveAllFilter" class="actiontoolbtn" label="保存" width="40" tooltiptext="保存全部规则" oncommand="SmartProxy.saveAllFilter();"/>\
</hbox>\
<tree id="SPfiltersTree" flex="1" editable="true" seltype="multiple" enableColumnDrag="false" hidecolumnpicker="true">\
<treecols>\
<treecol id="col-enabled" label="启用" cycler="true" flex="0" persist="width ordinal sortDirection hidden"/>\
<splitter class="tree-splitter"/>\
<treecol id="col-filter" label="规则" flex="10" persist="width ordinal sortDirection hidden"/>\
<splitter class="tree-splitter"/>\
<treecol id="col-slow" label="!" flex="0" width="16" persist="width ordinal sortDirection hidden"/>\
</treecols>\
<treechildren id="SPfiltersTreeChildren" oncontextmenu="SmartProxy.SPfilterActionMenu.openPopupAtScreen(event.screenX, event.screenY, true);"/>\
</tree>\
<findbar id="findbar"/>\
</groupbox>\
</vbox>\
<vbox id="sp-contents" width="40%">\
<hbox>\
<spacer flex="1" />\
<toolbarbutton id="closesliderbar" width="18" height="12" tooltiptext="关闭" oncommand="SmartProxy.closeslitterbar();"/>\
</hbox>\
<hbox id="sp-main">\
<groupbox id="sp-proxy" flex="2" hidden="false" height="200" width="100%"><caption id="SPPROXY" label="代理服务器设置" />\
<grid>\
<rows id="SROWS">\
<row id="discription">\
<label value="名称"/>\
<label value="主机"/>\
<label value="端口"/>\
<hbox>\
<label value="http"/>\
<label value="socks4"/>\
<label value="socks5"/>\
</hbox>\
<label value="默认"/>\
</row>\
</rows>\
</grid>\
<spacer flex="1" />\
<radiogroup id="proxymode" oncommand="SmartProxy.setMode();">\
<p><radio label="禁用代理" id="noproxy" value="disabled" checked="true"/>\
<radio label="自动代理" id="autoproxy" value="auto" />\
<radio label="全局代理" id="globalproxy" value="global" /><spacer flex="1" /><label value="保存服务器配置" class="actionbtn" onclick="SmartProxy.savePrefs();" /></p>\
</radiogroup>\
</groupbox>\
</hbox>\
<hbox id="sp-footer">\
<label id="SPMESSAGE" value="" />\
</hbox>\
</vbox>\
</hbox>\
</vbox>\
</vbox>\
</overlay>';
if (getFoxVer() >= 29) {
if (window.UCADDONBAR)
overlay.replace('<toolbarpalette id="addon-bar">', '<toolbarpalette id="UC-addon-bar">');
else
overlay.replace('<toolbarpalette id="addon-bar">', '<toolbarpalette id="nav-bar">');
}
overlay = "data:application/vnd.mozilla.xul+xml;charset=utf-8," + encodeURI(overlay);
window.userChrome_js.loadOverlay(overlay, SmartProxy);
SmartProxy.style = addStyle(css);
},
observe: function(aSubject, aTopic, aData){
if (aTopic == "xul-overlay-merged") {
if (!this._isready) {
if (this.fixButton) {
this.fixButton();
delete this.fixButton;
}
this.refreshPrefs();
this._ps.addObserver("", this, false);
Application.console.log("SmartProxy界面加载完毕!");
this.icon = E("SmartProxy-button");
this.popupid = E("sp-popup");
this.MsgBox = E("SPMESSAGE");
this._isready = true;
this.updateUI();
this._addSVRlist();
this.loadSPjson();
SmartProxy.FilterActions.init();
SmartProxy.FilterSearch.init();
if (!SmartProxy.Proxy.isRegisted)
SmartProxy.Proxy.init();
}
}
if (aTopic == "nsPref:changed") {
switch (aData) {
case 'default_proxy':
this.Prefs.default_proxy = this._ps.getIntPref("default_proxy");
this.updatetips();
SmartProxy.Proxy.setdefaultProxy();
break;
case 'knownProxy':
this.Prefs.knownProxy = this._ps.getCharPref("knownProxy");
if (SmartProxy.Proxy.isRegisted)
SmartProxy.Proxy.uninit();
this._setproxyConfig();
this._addSVRlist();
this.updatetips();
if (!SmartProxy.Proxy.isRegisted)
SmartProxy.Proxy.init();
break;
case 'proxyMode':
this.Prefs.proxyMode = this._ps.getCharPref("proxyMode");
this.updateSHOWMODE();
this.updatetips();
break;
}
}
},
updateUI: function() {
this.SVRROWS = E("SROWS");
this.modebtn = E("proxymode");
this.noproxy = E("noproxy");
this.autoproxy = E("autoproxy");
this.globalproxy = E("globalproxy");
this.siteProxyOn = E("siteProxyOn");
this.reportgfwlist = E("reportgfwlist");
this.SPfilterActionMenu = E("SPfilterActionMenu");
this.updateSHOWMODE();
this.updatetips();
},
updateSHOWMODE: function() {
this.modebtn.value = this.Prefs.proxyMode;
if (this.Prefs.proxyMode == this.mode[0])
this.modebtn.selectedItem = this.noproxy;
else if (this.Prefs.proxyMode == this.mode[1])
this.modebtn.selectedItem = this.autoproxy;
else if (this.Prefs.proxyMode == this.mode[2])
this.modebtn.selectedItem = this.globalproxy;
},
_addSVRlist: function(){
var pmenu = null;
while (this.SVRROWS.lastChild) {
if (this.SVRROWS.lastChild.id != "discription")
this.SVRROWS.removeChild(this.SVRROWS.lastChild);
else
break;
}
while (this.popupid.firstChild) {
this.popupid.removeChild(this.popupid.firstChild);
}
for (var i=0; i < this.proxyConfigs.length; i++) {
this._createServerList(i, this.proxyConfigs[i]);
pmenu = document.createElement("menuitem");
pmenu.setAttribute("id", "POPUP_" + i.toString());
pmenu.setAttribute("type", "radio");
pmenu.setAttribute("name", "SVRDEF");
pmenu.setAttribute("label", this.proxyConfigs[i].name + " " + this.proxyConfigs[i].host + ":" + this.proxyConfigs[i].port);
if (i == this.Prefs.default_proxy)
pmenu.setAttribute("checked", true);
pmenu.setAttribute("oncommand", "SmartProxy.popdefaultproxy(event);");
this.popupid.appendChild(pmenu);
}
},
updatetips: function(){
this.updateicon(this.Prefs.proxyMode);
if (this.Prefs.proxyMode == this.mode[0])
this.icon.setAttribute("tooltiptext", "代理状态: 无");
else if (this.Prefs.proxyMode == this.mode[1])
this.icon.setAttribute("tooltiptext", "代理状态: 自动代理 \n " + this.formattiptext());
else if (this.Prefs.proxyMode == this.mode[2])
this.icon.setAttribute("tooltiptext", "代理状态: 全局代理 \n " + this.formattiptext());
//this.showMsg(this.icon.getAttribute("tooltiptext") + " " + this.formattiptext());
this.showMsg(this.icon.getAttribute("tooltiptext"));
},
updateicon: function(proxyMode){
switch (proxyMode) {
case "disabled" :
this.icon.setAttribute("image", "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAACxElEQVQ4jY3RTUiTARgHcClCUwfT2d5NOygsETIPLTMonZvOTW225dT8wnSY22rzY9jM+fGaWqgZghCIhFF6CUqd+urUuVz5gS3nNqeFNDp0meQxqEj+HUSlw8r/+fn/eHgePz8fsdksYS6Xi/B4PITX6yG8Xi/h9XoJj8dDeFwuwrW8TJAkecxX3+/FQP92d2c7ujvb8aijDR0PWnCfrEe9vgbVlWo8bGuGxTQe6xN43Nm+XaVVoVKjxG1VOcoVJSgqzIVUKoFQmApFWTFmJkbO+QTaWxu3CwtyUJAvR17udUilEqSni8Dn83Gey4VMlvVvoMmg3xamCSEUpkIgEOBiQgLOxsaCw+Eg4nQEJFcz9gCrdYptX1lQO+zL6nWHTb31aV39eWtD3dPdsdPSXP9dX1v1Mzv7GlhsFpgEE2GnwsBghCIjQ7wHTFNGXq1OiyqtCto7FVApFSgrLUL+DTkkkkykpKagUqMEjUZDcHAwgoICERh4EmKx6BCo1CihUipQcasUirJiFBXmQibLgkiUhsSkJDQ36NFkuAtDnQ5kYx3Ixjr09nQdAmWlRcjOvgaZLAsSSSbEYhEEAgEuxMeDw+Gg4Z5uZ2ZydHdwoH/INPYqz2ZbjLFYTDF9fX0n/KYpI08ulyKZn4zLiVcQFxeH6OhoREZFgh3OBovNQitpwOCzfsxOGWE1U7sD/U8uHVx7mjLyxGIROGc4YIezwSSYYDBCERIaAjqdDjqdjnp99deh509vmqgReW9vV9Rf75qmjLwkHu+gQKPRDg4VEBAAf39/6Go0bp//njMNc62WSefK4hunc+29c9Nt//Jxw/Ft073mXLUtOhets06rmRr2CexndnaUmJ+huqxmyrG8YPllNo29nrdMlvy3eLjJONftWrWvfVj6PT8zsbv0bu6Hbent0JGB/bzMyTneR5KBR53/A0SneonKm1KCAAAAAElFTkSuQmCC");
break;
case "auto" :
this.icon.setAttribute("image", "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAACwElEQVQ4jY3RW0iTcRgG8D9FaIowNfycXaSxxIvyIhOK0plWE2KWqB2NUjG3pVPTiixxZSW1DoJgmckqlCLyNO1z8zSdOmVO3b45XR6+tlrJt6iLrrpIni5MpYuVz/X7/Hh5X0I8xGTSbbJarRTLshTHsRTHcRTHcRTLshRrtVLWkRFKoVCs89Qnj9uM7uLGDyhucqCw0Ym8tx+R/cqBUy9YJD2dQUG9GTpt+3aPQEmD0S1SLeCgagHCZwvYWf0FWytdIHc/gSic2PPQgq53LTs8AgWqYfeWShc2P3KBPPhTLHeClDpAiucRcnPs34C0dshNbjhBFE6QMgfI5XkQ+RyIZAYk0w5SZloC9HoNf8I4JLNMjMgmLSbZ7PtJ2fzslKzi9ch38RM7xNU2hN5hQLJnQaRzIBfmQeQOkHvMEtBJq4WXiuQokEshz82BVJKFzIx0nDyRCrH4MBIOJECiUIKUfAZRLIDc4kAqvoLUza0C+XkSSCVZyDmfgazMM0g/fQzJyUkQiQ4hJjYWpeW3kdvCQNpsRn7HFPI1dtzXjq4CmRnpSEk5guTkJIjFh5GYKEJ8fDx2RUdDIBDg+tWib10drYv1qtoGbVvjcZPJEKHTaSNqamo2kE5aLUxNPYq4/XHYG7MPkZGRCA8PR2hYKPghfATzg1GuuIb657Xo1qih76EXVbXVu1eu3UmrhYmJIgi2CcAP4SOICkJgYAD8A/zB4/HA4/FQcqXQ1fCy7pyWbkmtqlKG/fWuTlotjBUKVwp+fn7w9fWBj89GeHt7w8vLC0UX82we/92rbY7S6zoYo6GPYcyjzLRtwmG3WZzTNjMzbjIwBn03o++hmz0Cy+nubqX6u2ilvoe2GAZ7f/Ro25r6dR1n/1tc3aQ9ymYdnzCPDf8a6NMsDg/2/jQNDzSsGVjOm7S09Uql0net878BWaGbdwSdS90AAAAASUVORK5CYII=");
break;
case "global" :
this.icon.setAttribute("image", "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAACk0lEQVQ4jY2RW0hTcRzHfxQ99RodtIeoJx+SICGMgr0UQkEvJkEvhWa5ZV5AQ0bltFB0eQFLXUvXxVFE3qZOt+nWZm1jHnM7x8uuZ2dubpyV1WMPybeHadLDqu/z//Phz+dHlGUsazvA8zwjCAIjSQIjSRIjSRIjCAIj8DzDu92MSqXak42nvglPum44iroREbXDMdx+t47y1yKuvBBw8WkQNUNe2EyTx7IKlHpPukiXwjldCrJnKZzoTeJodwLUFgepYjjV6YNlaiw/q6BG50of7k7gUFcC1LENPoiB7omgughymxf/LpBrP6apKQZSxUCNIuhOBFQVBlUEQaV+UCObETgcMzks61Es+ZYUy8vLikAopIiIoqLpjWezeCCA831ryG3hQOUhkDwMuhUBVYmgdi4jMBsNsuLBCIp0KZwdTOGMJon8xxs42JkAta6DVCKK+1mQcgOkSoEeSqDWz6CB8K7gQn8Ap7VJFPYncfzJRiZWexzUHAMpo5APulA5xkE+6kX19CqqZ/x4ZFrYFZzsWgWp4xmodX03Vn0EVBHEjV7rpmV6fGtIp9WbJoYvs6wzz2Yz5Wk0mn1kNhpkOS086K4IUkZBNWGQPAS67geV+UFlQdQMODD0XIvZGQMcc8Ytnba38Hdts9Ego/tcBioPZkJVRkG1MVB9HNSQwM1XroT+5cA1k3HsUk+P+sgf5zIbDTJqWssADQlQ43aoti+gjq+g7u8oHfGuZL231TRaYLfbOPeCh/PxHLcS8ouBUCCxFg5wrM/LOd1O7r3NMppVsLPZ2XHGbjGqHXNGn3Pe+m3ONDFit01f/Se4+5PJghX+05J30fXTbpnacn2w/mBd8/r/FuzsbUnJXrVavf9/3/8CMPjCugYKVA0AAAAASUVORK5CYII=");
break;
default:
this.icon.setAttribute("image", "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAACvElEQVQ4jY3RW0iTARQH8EPRQyoGaX6KvRQ+CClBggoFori8lDHzRloYmqXLayYOr/MyRZcmmKIMNVJ7CEqdOt2mTmc40ZVun6bzMqdO5ZOMnuol+fcwTHpY+X8+/x+Hc4hsRKdTO7Msy5hMJobjTAzHcQzHcYzJZGJMLMuw09OMSCQ6ZatPnR3S/bpaMepqxXhRU4maqjKUiwpQkP8MOVkCVFeWQq0Y8LIJ1NeK97Mz05CVkYqnaSlISU7E/YRY8PkR4PGCkZz0AKrBXm+bgLiieD8hPgbx96IRF3sXfH4EwsJCEBgYiGs+PoiMvPNvoKQwf593kwceLxhBQUHw9fPDFS8veHh4wP2iOyJuh1sBjWbYTaebEczp5wQLCwsC4+qqYN1sFjTUSw6e52YjM0OAqCg+XN1c4cK4wPmCM5ycziM8PNQKKOWygKj2dYR07CG4fQ83Wnfh/WoHLvUWUPUWSGRGYtVbOJ5zhIODA+zt7WBndxahoSHHwK0WI65Ld+HfsourTTu43GAB1W6DyjdBBRtIbxpGaVE+CoW5EBULISoWorFBcgz4vvwCkmxbS9VboIpNULEZlLcOSl3B4+axA9VQ32FXh7Rb0f8+Tqeb8lSrFZ6tra1nSCmXBbhVsaAiM6hgA5S9BkpbBT1aBiUvg5JXkN2mQddrKUaGZdCMyg87pM3+f66tlMsCqMRgLaWsgNLWQOkboJxNUN42SGjBk06tpftN20OFvDe6sVFy6a93KeWyACpbshaEFlDpHqiSA9V8BdV9AzV8R9KH+UWb/x5T9PhMTKgN07MzBj1rMCyuLpuX14y7S2tGg04/b5ianjKMq1U9NoGjjIz0MRMquUQzKtdPfRz7MabsH5xQDyX+t3i8yYDPIvt5bv6T9tfk+PDhrFbzU6ed7D4xcJR3MTGnJRKJ/UnnfwPOTqHPv5QmxQAAAABJRU5ErkJggg==");
}
},
formattiptext: function(){
var tips = this.proxyConfigs[this.Prefs.default_proxy].name + " " + this.proxyConfigs[this.Prefs.default_proxy].host + ":" + this.proxyConfigs[this.Prefs.default_proxy].port;
return tips;
},
savePrefs: function() {
var svrstr = "";
for (let row=this.SVRROWS.firstChild.nextSibling; row; row=row.nextSibling) {
var setstr = "";
var aELM = row.firstChild;
if (aELM.value.trim() == "")
setstr = "未命名";
else
setstr = aELM.value.trim();
aELM = aELM.nextSibling;
if (aELM.value.trim() == "")
setstr = setstr + ";" + "127.0.0.1";
else if (IPREG.test(aELM.value.trim())) {
setstr = setstr + ";" + aELM.value.trim();
} else {
alert("你输入的是神马主机?");
return;
}
aELM = aELM.nextSibling;
if (aELM.value.trim() == "")
setstr = setstr + ";" + "80";
else if (PORTREG.test(aELM.value.trim())) {
setstr = setstr + ";" + aELM.value.trim();
} else {
alert("你输入的是神马端口号?");
return;
}
aELM = aELM.nextSibling;
setstr = setstr + ";" + aELM.value;
if (svrstr == "")
svrstr = setstr;
else
svrstr = svrstr+ "$" + setstr;
}
this._ps.setCharPref("knownProxy", svrstr);
this.showMsg("服务器配置保存成功!");
},
refreshPrefs: function() {
if (!this._ps.prefHasUserValue("knownProxy")) {
this._ps.setCharPref("knownProxy","Free Gate;;8580;$Puff;;1984;$ssh -D;;7070;socks$Toonel;;8080;$Tor;;9050;socks$Wu Jie;;9666;$Your Freedom;;8080;");
}
if (!this._ps.prefHasUserValue("default_proxy")) {
this._ps.setIntPref("default_proxy", 5);
}
if (!this._ps.prefHasUserValue("proxyMode")) {
this._ps.setCharPref("proxyMode", "auto");
}
if (!this._ps.prefHasUserValue("curVersion")) {
this._ps.setCharPref("curVersion", "0.4b2.2011041023");
}
if (!this._ps.prefHasUserValue("patternsfile")) {
this._ps.setCharPref("patternsfile", "SPpatterns.ini");
}
if (!this._ps.prefHasUserValue("url")) {
this._ps.setCharPref("url", "https://autoproxy-gfwlist.googlecode.com/svn/trunk/gfwlist.txt");
}
this.Prefs = {
default_proxy : this._ps.getIntPref("default_proxy"),
knownProxy : this._ps.getCharPref("knownProxy"),
proxyMode : this._ps.getCharPref("proxyMode"),
curVersion : this._ps.getCharPref("curVersion"),
patternsfile : this._ps.getCharPref("patternsfile"),
url : this._ps.getCharPref("url")
}
this._setproxyConfig();
},
_setproxyConfig: function(){
this.proxyConfigs = [];
this.proxyConfigs = this.Prefs.knownProxy.split("$").map(
function(str) {
var entry = str.split(";");
var SRV = {};
SRV.name = entry[0];
SRV.host = entry[1] == '' ? '127.0.0.1' : entry[1];
SRV.port = entry[2];
SRV.type = /^socks4?$/i.test(entry[3]) ? entry[3] : 'http';
return(SRV);
}
);
},
_createServerList: function(aid, aProxy) {
var proxyRow = document.createElement("row");
proxyRow.setAttribute("id", "PROXY_" + aid.toString());
var proxyName = document.createElement("textbox");
var proxyHost = document.createElement("textbox");
var proxyPort = document.createElement("textbox");
var proxyType = document.createElement("radiogroup");
var proxySel = document.createElement("checkbox");
proxySel.setAttribute("checked", (this.Prefs.default_proxy == aid) ? true : false);
proxySel.setAttribute("oncommand", "SmartProxy.setdefaultproxy(event);");
proxyName.setAttribute("class", "proxyName");
proxyHost.setAttribute("class", "proxyHost");
proxyPort.setAttribute("class", "proxyPort");
proxySel.setAttribute("class", "selBox");
proxyType.setAttribute("orient", "horizontal");
var http = document.createElement("radio");
var socks = document.createElement("radio");
var socks4 = document.createElement("radio");
http.setAttribute("class", "proxyHttp");
http.setAttribute("value", "http");
socks.setAttribute("class", "proxySocks5");
socks.setAttribute("value", "socks");
socks4.setAttribute("class", "proxySocks4");
socks4.setAttribute("value", "socks4");
http.setAttribute("selected", aProxy && aProxy.type == "http" );
socks.setAttribute("selected", aProxy && aProxy.type == "socks" );
socks4.setAttribute("selected", aProxy && aProxy.type == "socks4");
if (aProxy) {
proxyName.setAttribute("value", aProxy.name);
proxyHost.setAttribute("value", aProxy.host);
proxyPort.setAttribute("value", aProxy.port);
}
proxyType.appendChild(http);
proxyType.appendChild(socks4);
proxyType.appendChild(socks);
proxyRow.appendChild(proxyName);
proxyRow.appendChild(proxyHost);
proxyRow.appendChild(proxyPort);
proxyRow.appendChild(proxyType);
proxyRow.appendChild(proxySel);
this.SVRROWS.appendChild(proxyRow);
},
setdefaultproxy: function(evt) {
if (!evt.target.checked)
evt.target.setAttribute("checked", true);
else {
var evp = evt.target.parentNode;
var oevp;
for (var i=0; i < this.proxyConfigs.length; i++){
oevp = E("PROXY_" + i.toString());
if (oevp != evp) {
oevp.getElementsByTagName("checkbox")[0].setAttribute("checked", false);
}else {
this._ps.setIntPref("default_proxy", i);
E("POPUP_" + i.toString()).setAttribute("checked", true);
}
}
}
},
popdefaultproxy: function(evt) {
var evp = evt.target.id;
evp = evp.substring(6);
var oevp;
for (var i=0; i < this.proxyConfigs.length; i++){
oevp = E("PROXY_" + i.toString());
if (i.toString() != evp) {
oevp.getElementsByTagName("checkbox")[0].setAttribute("checked", false);
}else {
this._ps.setIntPref("default_proxy", i);
oevp.getElementsByTagName("checkbox")[0].setAttribute("checked", true);
}
}
},
setMode: function() {
this._ps.setCharPref("proxyMode", this.modebtn.value);
},
iconclick: function(evt) {
if (evt.target != this.icon) return;
if (evt.button == 1){
if (this.Prefs.proxyMode == this.mode[0]) {
this._ps.setCharPref("proxyMode", this.mode[1]);
return;
}else if (this.Prefs.proxyMode == this.mode[1]){
this._ps.setCharPref("proxyMode", this.mode[2]);
return;
}else if (this.Prefs.proxyMode == this.mode[2]) {
this._ps.setCharPref("proxyMode", this.mode[0]);
return;
}
}else if (evt.button == 2) {
evt.preventDefault();
var QW1 = E("sp-mains");
var QW2 = E("SP-splitter");
if (QW1.hidden) {
QW1.hidden = QW2.hidden = false;
this.Treevisible = true;
//SmartProxy.FilterView.updateData();
} else {
QW1.hidden = QW2.hidden = true;
this.Treevisible = false;
}
}
},
showMsg: function(Msg){
this.MsgBox.value = Msg;
},
closeslitterbar: function(){
var QW1 = E("sp-mains");
var QW2 = E("SP-splitter");
QW1.hidden = QW2.hidden = true;
this.Treevisible = false;
},
commandreport: function() {
var crurl= getFocusedWindow().content.document.location;
crurl = makeURI(crurl);
if (crurl.scheme != "http" && crurl.scheme != "https" && crurl.scheme != "ftp") return;
crurl = "https://gfwlist.autoproxy.org/report/?url=" + crurl.spec;
let protocolService = Cc["@mozilla.org/uriloader/external-protocol-service;1"].getService(Ci.nsIExternalProtocolService);
protocolService.loadURI(makeURI(crurl), null);
},
setpopupenabled: function(evt) {
var crurl= getFocusedWindow().content.document.location.href;
//Application.console.log(crurl);
var srurl = makeURI(crurl);
if ((srurl.scheme != "http" && srurl.scheme != "https" && srurl.scheme != "ftp") || this.Prefs.proxyMode == "disabled") {
this.siteProxyOn.setAttribute("hidden", true);
this.reportgfwlist.setAttribute("hidden", true);
return;
} else {
//var docDomain = getHostname(crurl);
var topDomain = getDomain(crurl);
var matchs = SPdefaultMatcher.matchesAny(crurl, topDomain);
if (matchs && matchs instanceof SPWhitelistFilter) {
this.siteProxyOn.setAttribute("hidden", true);
this.reportgfwlist.setAttribute("hidden", true);
return;
} else if (matchs && matchs instanceof SPBlockingFilter) {
this.siteProxyOn.setAttribute("value", true);
this.siteProxyOn.filter = matchs;
this.siteProxyOn.setAttribute("label", "禁用" + topDomain + "的代理规则");
return;
}
this.siteProxyOn.setAttribute("hidden", false);
this.siteProxyOn.setAttribute("value", "blocked");
this.siteProxyOn.newfilter = [crurl, topDomain];
this.siteProxyOn.setAttribute("label", "对" + topDomain + "使用代理");
this.reportgfwlist.setAttribute("hidden", false);
}
},
setUrlproxy: function(evt){
if (evt.button != 0) return;
if (evt.target.value != "blocked") {
if (evt.target.filter) {
for (let i = 0; i < this.SPfilters.length; i++) {
if (this.SPfilters[i] == evt.target.filter) {
this.SPfilters[i].disabled = evt.target.value;
SmartProxy.FilterView.updateFilter(this.SPfilters[i]);
if (this.SPfilters[i].disabled && this.SPfilters[i] instanceof SPRegExpFilter && SPdefaultMatcher.hasFilter(this.SPfilters[i]))
SPdefaultMatcher.remove(this.SPfilters[i]);
var afile = this._getSmartProxyfile("SPpatterns.json");
this._exportpatternsfile(afile);
break;
}
}
}
} else {
if (evt.target.value == "blocked" && evt.target.newfilter && evt.target.newfilter.length == 2) {
var val = [];
val = evt.target.newfilter;
var ishave = false;
for (let i = 0; i < this.SPfilters.length; i++) {
if (this.SPfilters[i].matches(val[0], val[1])) {
if (this.SPfilters[i] instanceof SPWhitelistFilter) {
ishave = true;
break;
}else if (this.SPfilters[i].disabled) {
this.SPfilters[i].disabled = false;
SmartProxy.FilterView.updateFilter(this.SPfilters[i]);
if (this.SPfilters[i] instanceof SPRegExpFilter && !SPdefaultMatcher.hasFilter(this.SPfilters[i]))
SPdefaultMatcher.add(this.SPfilters[i]);
ishave = true;
SmartProxy.FilterView.updateData();
var afile = this._getSmartProxyfile("SPpatterns.json");
this._exportpatternsfile(afile);
break;
}
}
}
if (!ishave) {
let filter = SPFilter.fromText(SPFilter.normalize(val[1]));
if (filter && filter instanceof SPRegExpFilter) {
this.SPfilters.push(filter);
var position = SmartProxy.FilterView.data.length;
SmartProxy.FilterView.addFilterAt(position++, filter);
if (!SPdefaultMatcher.hasFilter(filter))
SPdefaultMatcher.add(filter);
var afile = this._getSmartProxyfile("SPpatterns.json");
this._exportpatternsfile(afile);
}
}
}
}
},
loadGFWurl: function() {
window.openNewTabWith("https://autoproxy.org/zh-CN/Subscriptions", null, null, null, false);
},
_getSmartProxyfile : function(afile) {
var profileDir = dirService.get("UChrm", Ci.nsIFile);
var filedir = profileDir.clone().QueryInterface(Ci.nsILocalFile);
filedir.appendRelativePath("UCProfileDir");
filedir.appendRelativePath("SmartProxy");
if(!filedir.exists()) {
filedir.create(Ci.nsIFile.DIRECTORY_TYPE, 0700);
}
filedir.appendRelativePath(afile);
return filedir;
},
stringToFile : function(str, file) {
var stream = new FileOutputStream(file, 0x02 | 0x08 | 0x10 | 0x20, -1, 0);
var cos = new ConverterOutputStream(stream, "UTF-8", 16384, Ci.nsIConverterInputStream.DEFAULT_REPLACEMENT_CHARACTER);
cos.writeString(str);
cos.close();
stream.close();
},
_exportpatternsfile : function(file) {
const PR_WRONLY = 0x02;
const PR_CREATE_FILE = 0x08;
const PR_TRUNCATE = 0x20;
var rjson = { createdBy : 'SmartProxy', createdAt : new Date(), SPfilters : [], DisbledFilter : [] };
for (var i = 0; i < this.SPfilters.length; i++) {
rjson.SPfilters.push(this.SPfilters[i].text);
if (this.SPfilters[i].disabled)
rjson.DisbledFilter.push(this.SPfilters[i].text);
}
var fileStream = new FileOutputStream(file, PR_WRONLY | PR_CREATE_FILE | PR_TRUNCATE, 0644, 0);
var stream = new ConverterOutputStream(fileStream, "UTF-8", 16384, Ci.nsIConverterInputStream.DEFAULT_REPLACEMENT_CHARACTER);
stream.writeString(JSON.stringify(rjson, null, 4));
stream.close();
},
_importpatternsfile : function(file) {
var fileStream = new FileInputStream(file, 0x01, 0444, 0);
var stream = new ConverterInputStream(fileStream, "UTF-8", 16384, Ci.nsIConverterInputStream.DEFAULT_REPLACEMENT_CHARACTER);
var str = {};
var rjson = '';
while (stream.readString(0xffffffff, str) != 0) {
rjson += str.value;
}
stream.close();
rjson = JSON.parse(rjson);
this._FilterIsReady = false;
this.SPfilters = [];
SPdefaultMatcher.clear();
for (var i = 0; i < rjson.SPfilters.length; i++) {
let filter = SPFilter.fromText(SPFilter.normalize(rjson.SPfilters[i]));
if (filter && filter instanceof SPRegExpFilter && !SPdefaultMatcher.hasFilter(filter)) {
SPdefaultMatcher.add(filter);
this.SPfilters.push(filter);
}
}
for (var i = 0; i < rjson.DisbledFilter.length; i++) {
let filter = SPFilter.fromObject(SPFilter.normalize(rjson.DisbledFilter[i]));
continue;
}
this._FilterIsReady = true;
this.showMsg("共:" + this.SPfilters.length +"个规则!");
},
_importSUBpatternsfile : function(file) {
var fileStream = new FileInputStream(file, 0x01, 0444, 0);
var stream = new ConverterInputStream(fileStream, "UTF-8", 16384, Ci.nsIConverterInputStream.DEFAULT_REPLACEMENT_CHARACTER);
stream = stream.QueryInterface(Ci.nsIUnicharLineInputStream);
var line = {};
this._FilterIsReady = false;
var val = "";
var cont;
var newcow = 0;
do {
cont = stream.readLine(line);
val = line.value;
val = val.trim();
if (val != "" && val[0] != "[" && val[0] != "!") {
let filter = SPFilter.fromText(SPFilter.normalize(val));
if (filter && filter instanceof SPRegExpFilter && !SPdefaultMatcher.hasFilter(filter)) {
SPdefaultMatcher.add(filter);
this.SPfilters.push(filter);
if (this.Treevisible) {
var position = SmartProxy.FilterView.data.length;
SmartProxy.FilterView.addFilterAt(position++, filter);
}
newcow = newcow + 1;
}
}
} while (cont);
stream.close();
this._FilterIsReady = true;
this.showMsg("共新增了:" + newcow.toString() +"个规则!");
return newcow;
},
updatesubUrl: function(){
var nUrl = window.prompt('设置新的下载地址:',this.Prefs.url);
if(!nUrl) return;
nUrl = nUrl.trim();
var anew = makeURI(nUrl);
if (anew && (anew.scheme == "http" || anew.scheme == "https")) {
this._ps.setCharPref("url", nUrl);
this.Prefs.url = nUrl;
} else
alert("非法的地址!");
},
updatesubFilter: function(){
function errorCallback(result) {
SmartProxy.showMsg(result);
if (result == SUBSCRIPTION_UPDATE_SUCCESS) {
SmartProxy.showMsg("订阅规则更新成功!");
SmartProxy.addsubFilter();
}
}
SmartProxy.showMsg("请等待,正在更新...");
let url = this.Prefs.url;
var request = Components.classes["@mozilla.org/xmlextras/xmlhttprequest;1"]
.createInstance(Components.interfaces.nsIXMLHttpRequest);
let me = this;
request.onerror = function (event) {
setTimeout(function () { errorCallback(request.statusText); }, 0);
var error = request.statusText;
return error;
};
request.onload = function (event) {
try {
self._rawData = request.responseText;
if (!self._rawData) {
var error = '返回了一个空文件!';
setTimeout(function () { errorCallback(error); }, 0);
return;
}
self._rawData = self._rawData.replace(/[\r\n]/g, '');
self._rawData = windowMediator.getMostRecentWindow("").atob(self._rawData);
SmartProxy.stringToFile(self._rawData, SmartProxy.SPfile);
setTimeout(function () {
errorCallback(SUBSCRIPTION_UPDATE_SUCCESS);
}, 0);
} catch (e) {
setTimeout(function () { errorCallback(e.toString()); }, 0);
}
};
request.open("GET", url);
request.send(null);
},
addsubFilter: function() {
if (this.SPfile.exists()) {
var newrec = this._importSUBpatternsfile(this.SPfile);
if (!SmartProxy.Proxy.isRegisted)
SmartProxy.Proxy.init();
if (newrec > 0)
SmartProxy.saveAllFilter();
} else {
this.showMsg("没有找到订阅规则文件!");
}
},
saveAllFilter: function() {
var afile = this._getSmartProxyfile("SPpatterns.json");
this._exportpatternsfile(afile);
this.showMsg("规则文件保存成功!");
},
EID: function(id){
return document.getElementById(id);
},
};
SmartProxy.Proxy = {
proxyserver: null,
defaultProxy: null,
proxyConfigs: [],
DirectProxy: null,
isRegisted: false,
init: function() {
if (!SmartProxy._FilterIsReady) return;
SmartProxy.Proxy.proxyserver = [];
SmartProxy.Proxy.DirectProxy = null;
SmartProxy.Proxy.defaultProxy = null;
for (let aproxy of SmartProxy.proxyConfigs) {
SmartProxy.Proxy.proxyserver.push(ProxySrv.newProxyInfo(aproxy.type, aproxy.host, aproxy.port, 1, 0, null));
}
SmartProxy.Proxy.DirectProxy = ProxySrv.newProxyInfo('direct', '', -1, 0, 0, null);
SmartProxy.Proxy.defaultProxy = SmartProxy.Proxy.proxyserver[SmartProxy.Prefs.default_proxy];
if (!SmartProxy.Proxy.defaultProxy) {
SmartProxy.showMsg("无效的默认代理!");
return;
}
ProxySrv.registerFilter(SmartProxy.Proxy, 0);
SmartProxy.Proxy.isRegisted = true;
},
uninit: function() {
ProxySrv.unregisterFilter(SmartProxy.Proxy);
SmartProxy.Proxy.isRegisted = false;
},
setdefaultProxy: function() {
SmartProxy.Proxy.defaultProxy = SmartProxy.Proxy.proxyserver[SmartProxy.Prefs.default_proxy];
},
applyFilter: function(ProxySrv, uri, aProxy) {
if (SmartProxy.Prefs.proxyMode == 'disabled' || uri.schemeIs('feed'))
return SmartProxy.Proxy.DirectProxy;
if (SmartProxy.Prefs.proxyMode == 'global') {
return SmartProxy.Proxy.defaultProxy;
}
if (SmartProxy.Prefs.proxyMode == 'auto')
SmartProxy.updateicon(SmartProxy.Prefs.proxyMode);
var splttag = uri.spec.indexOf("?");
if (splttag > 20)
var match = SPdefaultMatcher.matchesAny(uri.spec.substring(0, splttag), uri.host);
else {
splttag = uri.spec.indexOf("=");
if (splttag > 20)
var match = SPdefaultMatcher.matchesAny(uri.spec.substring(0, splttag), uri.host);
else
var match = SPdefaultMatcher.matchesAny(uri.spec, uri.host);
}
if (match) {
if (SmartProxy.Proxy.CanNotProxy(match))
return SmartProxy.Proxy.DirectProxy;
else {
if (SmartProxy.Prefs.proxyMode == 'auto')
SmartProxy.updateicon("auto-on");
return SmartProxy.Proxy.defaultProxy;
}
}else{
if (SmartProxy.Prefs.proxyMode == 'auto')
return SmartProxy.Proxy.DirectProxy;
else
return SmartProxy.Proxy.defaultProxy;
}
},
CanNotProxy: function(amatch) {
if (amatch.disabled)
return true;
else if (amatch instanceof SPWhitelistFilter)
return true;
else
return false;
},
};
SmartProxy.FilterActions = {
init: function() {
let me = this;
this.treeElement.parentNode.addEventListener("keypress", function(event) {
me.keyPress(event);
}, true);
SmartProxy.FilterView.filters = SmartProxy.SPfilters;
this.treeElement.view = SmartProxy.FilterView;
this.treeElement.inputField.addEventListener("keypress", function(event) {
if (event.keyCode >= event.DOM_VK_PAGE_UP && event.keyCode <= event.DOM_VK_DOWN)
event.stopPropagation();
}, false);
},
get treeElement() E("SPfiltersTree"),
get visible() {
return !this.treeElement.parentNode.collapsed;
},
get focused() {
let focused = document.commandDispatcher.focusedElement;
while (focused)
{
if ("treeBoxObject" in focused && focused.treeBoxObject == SmartProxy.FilterView.boxObject)
return true;
focused = focused.parentNode;
}
return false;
},
setSortOrder: function(sortOrder) {
let col = (SmartProxy.FilterView.sortColumn ? SmartProxy.FilterView.sortColumn.id : "col-filter");
SmartProxy.FilterView.sortBy(col, sortOrder);
},
selectionToggleDisabled: function() {
if (this.treeElement.editingColumn)
return;
let items = SmartProxy.FilterView.selectedItems.filter(function(i) i.filter instanceof SPActiveFilter);