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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
|
/*
* 2008
* Copyright Steve McFred and Jeremy Gillick (author of SwitchProxy)
*/
/*
*
* ERROR CODES
* 100 - 149: mproxy_setProxy()
* 150 - 199: mproxy_removeProxy()
* 200 - 249: mproxy_clearCookies()
* 250 - 299: mproxy_editProxyDialog()
* 300 - 349: mproxy_manualUpgradCheck()
*
*/
var gSwitchP_List;
var gSwitchP_ListPopup;
var gSwitchP_ContextList;
var gSwitchP_StatusBar;
var gSwitchP_MenuList;
var gSwitchP_ManageList;
var gSwitchP_ElementList;
var gSwitchP_ElementButton;
var gSwitchP_LastItem;
var gSwitchP_ProxyCount = 0;
var gSwitchP_InManager = false;
var gSwitchP_Loaded = false;
var gSwitchP_Cycle = null;
var gSwitchP_CommandEnd = true;
var gSwitchP_Prefs = null;
var gSwitchP_NoneLabel = "None";
var gSwitchPTabMgr = null;
function mproxy_initProxy(event){
window.removeEventListener("load", mproxy_initProxy, true);
if(!gSwitchP_Loaded){
gSwitchP_List = document.getElementById('proxy-list')
gSwitchP_ListPopup = document.getElementById('proxy-list-popup');
gSwitchP_ManageList = document.getElementById('manage-proxy-list');
gSwitchP_ContextList = document.getElementById('context-proxy-list');
gSwitchP_StatusBar = document.getElementById('mproxy-status');
gSwitchP_MenuList = document.getElementById('mproxy-menu-list');
gSwitchP_ElementList = document.getElementById('mproxy-element-list');
gSwitchP_ElementButton = document.getElementById('mproxy-element-button');
gSwitchP_Prefs = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefBranch);
gSwitchP_NoneLabel = mproxy_getString("common.proxy.none");
//Is in Proxy Manager
if(gSwitchP_ManageList != null){
gSwitchP_InManager = true;
gSwitchP_List = gSwitchP_ManageList;
}
mproxy_showMenus(true);
//Cleanup RDF File
mproxy_cleanupRdf();
//Populate Proxy List
mproxy_populateList();
//Set Last Proxy Selected
if(gSwitchP_List != null)
gSwitchP_LastItem = gSwitchP_List.selectedItem;
//Add Preferences Listener
var oProxyObserver = {
observe : function(subject, topic, data){ mproxy_populateList(); }
};
var oMenuObserver = {
observe : function(subject, topic, data){ mproxy_showMenus(false); }
};
//var oPrefBranch = Components.classes["@mozilla.org/preferences-service;1"].createInstance(Components.interfaces.nsIPrefBranchInternal);
////oPrefBranch.addObserver("network.proxy", oPrefObserver, false);
//oPrefBranch.addObserver("mproxy.proxy.rdf.lastupdate", oProxyObserver, false);
//oPrefBranch.addObserver("mproxy.display", oMenuObserver, false);
//if(navigator.userAgent.search(/Thunderbird/gi) > -1){
// oPrefBranch = Components.classes["@mozilla.org/preferences-service;1"].createInstance(Components.interfaces.nsIPrefBranchInternal);
//}
//Else{
oPrefBranch = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefService);
//}
oPrefBranch.addObserver("mproxy.proxy.rdf.lastupdate", oProxyObserver, false);
//oPrefBranch.addObserver("mproxy.display", oMenuObserver, false);
//oPrefBranch.addObserver("network.proxy", oPrefObserver, false);
//Auto-Update Anonymous Proxy Lists (after 10 minutes)
//gAnonUpdateTimout = setTimeout("mproxy_anon_autoUpdateLists()", 600000);
// Start Anon Rotation
//setTimeout("mproxy_anon_nextProxy()", 1000);
gSwitchP_Loaded = true;
}
}
// Return Tab Manager
function mproxy_getTabManager(){
return gSwitchPTabMgr;
}
//Used for debugging removed
//Displays the context menu based on what
// the user selects in the options
function mproxy_showMenus(onStartup){
try{
//Show/Hide menus
var oPrefs = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefService).getBranch("");
// Status Bar
var isHidden = (oPrefs.prefHasUserValue("mproxy.display.statusbar") && !oPrefs.getBoolPref("mproxy.display.statusbar"))
document.getElementById("mproxy-status").setAttribute("collapsed", isHidden);
// If not thunderbird
if(navigator.userAgent.search(/Thunderbird/gi) < 0){
//Context
var isHidden = (oPrefs.prefHasUserValue("mproxy.display.context") && !oPrefs.getBoolPref("mproxy.display.context"))
document.getElementById("mproxy-context-menu").setAttribute("collapsed", isHidden);
document.getElementById("mproxy-context-separator").setAttribute("hidden", isHidden);
//Toolbar
if(onStartup){
oPrefs.setBoolPref("mproxy.display.toolbar", (!document.getElementById("proxy-toolbar").getAttribute("collapsed")));
}
else{
var isHidden = (oPrefs.prefHasUserValue("mproxy.display.toolbar") && !oPrefs.getBoolPref("mproxy.display.toolbar"))
document.getElementById("proxy-toolbar").setAttribute("collapsed", isHidden);
}
}
}catch(err){}
}
// Show Element List
function mproxy_showElementList(event){
var oContext = document.getElementById('context-proxy-list');
if(oContext == null){
return;
}
}
//Returns is the entered menu is currently showing
// values are 'context' or 'toolbar'
function mproxy_menuShowing(sMenu){
var oObj = null;
if(sMenu == "context"){
oObj = document.getElementById("mproxy-context-menu");
}
else if(sMenu == "toolbar"){
oObj = document.getElementById("proxy-toolbar");
}
if(oObj == null)
return false;
return !eval(oObj.getAttribute("collapsed"));
}
//Clears current Proxy List
function mproxy_clearList(){
gSwitchP_ProxyCount = 0;
//Toolbar List
if(!gSwitchP_InManager && gSwitchP_List != null && gSwitchP_List.removeAllItems && !gSwitchP_List.open){
gSwitchP_List.removeAllItems();
}
//Manage Proxy Box
if(gSwitchP_InManager && gSwitchP_List != null){
aNodes = gSwitchP_List.childNodes;
for(var i = aNodes.length-1; i >= 0; i--){
gSwitchP_List.removeChild(aNodes[i]);
}
}
//Context List
if(gSwitchP_ContextList != null){
aNodes = gSwitchP_ContextList.childNodes;
for(var i = aNodes.length-1; i >= 0; i--){
gSwitchP_ContextList.removeChild(aNodes[i]);
}
}
//Toolbar Element List
if(gSwitchP_ElementList != null){
aNodes = gSwitchP_ElementList.childNodes;
for(var i = aNodes.length-1; i >= 0; i--){
if(aNodes[i].getAttribute("class") == "proxy-menu-item")
gSwitchP_ElementList.removeChild(aNodes[i]);
}
}
// Tools Menu List
if(gSwitchP_MenuList != null){
aNodes = gSwitchP_MenuList.childNodes;
for(var i = aNodes.length-1; i >= 0; i--){
if(aNodes[i].getAttribute("class") == "proxy-menu-item")
gSwitchP_MenuList.removeChild(aNodes[i]);
}
}
}
//Add Item to lists
function mproxy_appendToList(sLabel, sValue, isSelected, iType){
var oItem = null;
gSwitchP_ProxyCount++;
try{
//Toolbar List
if(!gSwitchP_InManager && gSwitchP_List != null && !gSwitchP_List.open){
oItem = gSwitchP_List.appendItem(sLabel, sValue);
oItem.setAttribute("oncommand", "mproxy_selectProxy();"); //mproxy_anon_stopRotation();
oItem.setAttribute("proxyType", iType);
}
//Manage Proxy Box
if(gSwitchP_InManager && gSwitchP_List != null && sLabel != gSwitchP_NoneLabel){
oItem = gSwitchP_List.appendItem(sLabel, sValue);
oItem.setAttribute("proxyType", iType);
}
//Context List
if(gSwitchP_ContextList != null){
oItem = document.createElement("menuitem");
oItem.setAttribute("label", sLabel);
oItem.setAttribute("value", sValue);
oItem.setAttribute("type", "checkbox");
oItem.setAttribute("autocheck", "false");
oItem.setAttribute("proxyType", iType);
oItem.setAttribute("oncommand", "mproxy_queueSetProxy(this);");
gSwitchP_ContextList.appendChild(oItem);
}
//Toolbar Element List
if(gSwitchP_ElementList != null){
//Get separator, which is after proxy list
var oSeparator = document.getElementById("mproxy-element-list-separator");
oItem = document.createElement("menuitem");
oItem.setAttribute("id", sValue);
oItem.setAttribute("label", sLabel);
oItem.setAttribute("value", sValue);
oItem.setAttribute("type", "checkbox");
oItem.setAttribute("autocheck", "false");
oItem.setAttribute("proxyType", iType);
oItem.setAttribute("class", "proxy-menu-item");
oItem.setAttribute("oncommand", "mproxy_queueSetProxy(this);");
gSwitchP_ElementList.insertBefore(oItem, oSeparator);
}
// Tools Menu List
if(gSwitchP_MenuList != null){
//Get separator, which is after proxy list
var oSeparator = document.getElementById("mproxy-menu-list-separator");
oItem = document.createElement("menuitem");
oItem.setAttribute("id", sValue);
oItem.setAttribute("label", sLabel);
oItem.setAttribute("value", sValue);
oItem.setAttribute("type", "checkbox");
oItem.setAttribute("autocheck", "false");
oItem.setAttribute("proxyType", iType);
oItem.setAttribute("class", "proxy-menu-item");
oItem.setAttribute("oncommand", "mproxy_queueSetProxy(this);");
gSwitchP_MenuList.insertBefore(oItem, oSeparator);
}
//Select
if(isSelected)
mproxy_selectItem(sLabel);
}catch(err){}
}
//Sets List item with labe sLabel, to be selected/checked
function mproxy_selectItem(sLabel){
try{
//Toolbar List
if(!gSwitchP_InManager && gSwitchP_List != null){
oItem = gSwitchP_List.getElementsByAttribute("label", sLabel);
if(oItem != null){
oItem = oItem[0];
gSwitchP_List.selectedItem = oItem;
oItem.setAttribute("selected", true);
}
}
//Context
if(gSwitchP_ContextList != null){
oItem = gSwitchP_ContextList.getElementsByAttribute("label", sLabel);
if(oItem != null){
//Uncheck Other
var aChecked = gSwitchP_ContextList.getElementsByAttribute("checked", "true");
for(var c = 0; c < aChecked.length; c++){
aChecked[c].setAttribute("checked", false);
}
//Select
oItem = oItem[0];
gSwitchP_ContextList.selectedItem = oItem;
oItem.setAttribute("checked", true);
}
}
//Toolbar Element List
if(gSwitchP_ElementList != null){
oItem = gSwitchP_ElementList.getElementsByAttribute("label", sLabel);
if(oItem != null && oItem.length > 0){
//Uncheck Other
var aChecked = gSwitchP_ElementList.getElementsByAttribute("checked", "true");
for(var c = 0; c < aChecked.length; c++){
aChecked[c].setAttribute("checked", false);
}
//Check Selected
oItem = oItem[0];
gSwitchP_ElementList.selectedItem = oItem;
oItem.setAttribute("selected", true);
oItem.setAttribute("checked", true);
//Change Button Label
gSwitchP_ElementButton = document.getElementById('mproxy-element-button');
if(gSwitchP_ElementButton != null){
gSwitchP_ElementButton.setAttribute("label", sLabel);
}
}
}
// Tools Menu List
if(gSwitchP_MenuList != null){
oItem = gSwitchP_MenuList.getElementsByAttribute("label", sLabel);
if(oItem != null && oItem.length > 0){
//Uncheck Other
var aChecked = gSwitchP_MenuList.getElementsByAttribute("checked", "true");
for(var c = 0; c < aChecked.length; c++){
aChecked[c].setAttribute("checked", false);
}
//Check Selected
oItem = oItem[0];
gSwitchP_MenuList.selectedItem = oItem;
oItem.setAttribute("selected", true);
oItem.setAttribute("checked", true);
}
}
// Status Bar
if(gSwitchP_StatusBar != null){
gSwitchP_StatusBar.setAttribute("label", mproxy_getString("common.label.proxy")+" "+sLabel);
}
mproxy_clearTimeout(gSwitchP_Cycle);
}catch(err){
mproxy_debug(err);
}
}
//Return menuitem for the given sUri
function mproxy_getMenuItem(sUri){
var oItem = null;
//Toolbar
oItem = (gSwitchP_List != null) ? gSwitchP_List.getElementsByAttribute("value", sUri) : null;
if(oItem != null && oItem[0] != null)
return oItem[0];
//Context
oItem = (gSwitchP_List != null) ? gSwitchP_ContextList.getElementsByAttribute("value", sUri) : null;
if(oItem != null && oItem[0] != null)
return oItem[0];
//Toolbar Element
oItem = (gSwitchP_List != null) ? gSwitchP_ElementList.getElementsByAttribute("value", sUri) : null;
if(oItem != null && oItem[0] != null)
return oItem[0];
//Default
return null;
}
function mproxy_cycle(sAction){
if(gSwitchP_Cycle != null){
mproxy_clearTimeout(gSwitchP_Cycle);
gSwitchP_CommandEnd = true;
if(sAction == 'change' && gSwitchP_LastItem != null && gSwitchP_List != null && gSwitchP_List.selectedItem.label != gSwitchP_LastItem.label && gSwitchP_List.selectedIndex > 0){
gSwitchP_List.label = gSwitchP_LastItem.label;
gSwitchP_List.selectedItem = gSwitchP_LastItem;
gSwitchP_List.selectedItem.value = gSwitchP_LastItem.value;
mproxy_setProxy();
}
else if(sAction == 'select' && gSwitchP_List != null && gSwitchP_List.selectedIndex > -1){
mproxy_setStatus(mproxy_getString("toolbar.notApplied"), "#F00");
setTimeout("mproxy_setStatus(mproxy_getString('toolbar.loading'))", 3000);
setTimeout("mproxy_populateList('"+ gSwitchP_LastItem.label +"');", 4000);
setTimeout("mproxy_setProxy();", 4500);
}
}
}
//When user selects a Proxy from the menu list
function mproxy_selectProxy(){
mproxy_clearTimeout(gSwitchP_Cycle);
if(gSwitchP_List == null || gSwitchP_List.selectedItem == null || gSwitchP_InManager)
return;
//if(gSwitchP_List.selectedItem.label != gSwitchP_NoneLabel){
//document.getElementById('edit-button').disabled = false;
//document.getElementById('remove-button').disabled = false;
//document.getElementById('edit-button').image = "chrome://mproxy/content/icons/pencil.png";
//document.getElementById('remove-button').image = "chrome://mproxy/content/icons/cross.png";
//}
//else{
//document.getElementById('edit-button').disabled = true;
//document.getElementById('remove-button').disabled = true;
//document.getElementById('edit-button').image = "chrome://mproxy/content/icons/pencil_disabled.png";
//document.getElementById('remove-button').image = "chrome://mproxy/content/icons/cross_disabled.png";
//}
if(typeof(gSwitchP_List.selectedItem.value) != 'undefined'){
//If this item is not being used by the browser,
// show a helpful message and cycle
var oPrefs = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefService).getBranch("");
if((!oPrefs.prefHasUserValue("mproxy.proxy.current") || oPrefs.getCharPref("mproxy.proxy.current") != gSwitchP_List.selectedItem.value) && gSwitchP_LastItem != null && gSwitchP_LastItem != gSwitchP_List.selectedItem){
mproxy_setStatus(mproxy_getString("toolbar.clickApply"), "#060");
//Cycle if user doesn't click 'Apply'
//if(gSwitchP_Cycle == null)
// gSwitchP_Cycle = setTimeout("mproxy_cycle('select')", 10000);
}
//If it is being used by the browser
else if(oPrefs.prefHasUserValue("mproxy.proxy.current") && oPrefs.getCharPref("mproxy.proxy.current") == gSwitchP_List.selectedItem.value){
mproxy_clearTimeout(gSwitchP_Cycle);
//Anonomous
if(gSwitchP_List.selectedItem.getAttribute("proxyType") == "3"){
// if(mproxy_anon_getCurrent() == "unknown")
// mproxy_setProxy(false);
//
// mproxy_setStatus(mproxy_getString("toolbar.using") +" "+ gSwitchP_List.selectedItem.label +" ["+ mproxy_anon_getCurrent() +"] ("+ mproxy_anon_getCurrentCount() +")");
}
//Standard
else{
mproxy_setStatus(mproxy_getString("toolbar.using") +" "+ gSwitchP_List.selectedItem.label);
}
}
else{
mproxy_clearTimeout(gSwitchP_Cycle);
mproxy_setStatus(mproxy_getString("toolbar.using.none"));
}
}
return true;
}
//Queue Set Proxy
function mproxy_queueSetProxy(oMenuItem){
mproxy_clearTimeout(gSwitchP_Cycle);
setTimeout("mproxy_setProxy(true, true, null, '"+ oMenuItem.label +"', '"+ oMenuItem.getAttribute("value") +"', '"+ oMenuItem.getAttribute("proxyType") +"')", 10);
}
//Set Proxy
function mproxy_setProxy(bClean, fromContextMenu, oMenuItem, sLabel, sUri, sType){
//Start Command
gSwitchP_CommandEnd = false;
mproxy_clearTimeout(gSwitchP_Cycle);
//mproxy_anon_stopRotation();
//If in Proxy Manager, do nothing
if(gSwitchP_InManager){
gSwitchP_CommandEnd = true;
return;
}
var hasError = false;
//Set fromContextMenu
if(fromContextMenu == null)
fromContextMenu = false;
//Get Proxy URI & Label
var sProxyUri = "";
var sProxyLabel = "";
var sProxyType = 0;
if(sUri != null && sLabel != null){
sProxyUri = sUri;
sProxyLabel = sLabel;
if(sType) sProxyType = sType;
}
else if(oMenuItem != null){
sProxyUri = oMenuItem.getAttribute("value");
sProxyLabel = oMenuItem.getAttribute("label");
sProxyType = oMenuItem.getAttribute("proxyType");
}
else{
if(fromContextMenu == true){
sProxyUri = gSwitchP_ContextList.selectedItem.value;
sProxyLabel = gSwitchP_ContextList.selectedItem.label;
sProxyType = gSwitchP_ContextList.selectedItem.getAttribute("proxyType");
}
else if(gSwitchP_List != null){
sProxyUri = gSwitchP_List.selectedItem.value;
sProxyLabel = gSwitchP_List.selectedItem.label;
sProxyType = gSwitchP_List.selectedItem.getAttribute("proxyType");
}
}
//Branch if Anonomous
if(sProxyType == "3"){
//mproxy_setStatus(mproxy_getString("toolbar.loading"));
//mproxy_anon_loadProxy(sProxyUri, true);
}
else if(sProxyUri != ""){
mproxy_setStatus(mproxy_getString("toolbar.loading"));
try{
//Always clear proxy settings first - this may mess up with selection...
//gSwitchP_Prefs.setIntPref("network.proxy.type", 0);
//Change Browser Preferences
var aProps = mproxy_ds_getPropertyValuesFor(sProxyUri);
for(key in gmproxy_options){
//Get pref type and update preference
var sPrefVal = "";
if(typeof(gmproxy_options_defaults[key]) == "number"){
gSwitchP_Prefs.setIntPref(gmproxy_options[key], parseInt(aProps[gSProxyRdfNodeUriRoot +"#"+ key]));
}
else if(typeof(gmproxy_options_defaults[key]) == "boolean"){
gSwitchP_Prefs.setBoolPref(gmproxy_options[key], eval(aProps[gSProxyRdfNodeUriRoot +"#"+ key]));
}
else{ //String
gSwitchP_Prefs.setCharPref(gmproxy_options[key], aProps[gSProxyRdfNodeUriRoot +"#"+ key]);
}
}
//Get Proxy Label
sProxyLabel = aProps[gSProxyRdfNodeName];
//Update Status
mproxy_setStatus(mproxy_getString("toolbar.using") +" "+ sProxyLabel);
//Update Preference
gSwitchP_Prefs.setCharPref("mproxy.proxy.current", sProxyUri);
}
catch(err){
alert("100: "+ mproxy_getString("error.unknown")+ "\n("+ err +")");
hasError = true;
//Cycle back to 'None'
//if(gSwitchP_Cycle == null && !fromContextMenu)
// gSwitchP_Cycle = setTimeout("mproxy_cycle('change')", 500);
}
}
else{
//Turn off proxy pref
try{
gSwitchP_Prefs.setIntPref("network.proxy.type", 0);
//Update Status
mproxy_setStatus(mproxy_getString("toolbar.using.none"));
//Update Preference
gSwitchP_Prefs.setCharPref("mproxy.proxy.current", "");
}catch(e){
alert("101: "+ mproxy_getString("error.unknown"));
hasError = true;
}
}
// Finalize
if(hasError){
//Update Status
mproxy_setStatus(mproxy_getString("error.proxy.load") +" " + sProxyLabel, "#F00");
}
else{
//mproxy_tab_notify(sProxyUri);
//Select this proxy in all lists
mproxy_selectItem(sProxyLabel);
//Set Last Proxy
gSwitchP_LastItem = mproxy_getMenuItem(sProxyUri);
//Options
try{
if(bClean){
// Clear Cookies
if(typeof(gBrowser) != 'undefined' && gSwitchP_Prefs.prefHasUserValue("mproxy.clear.cookies") && gSwitchP_Prefs.getBoolPref("mproxy.clear.cookies")){
mproxy_clearCookies();
}
// Reload Page
if((typeof(gBrowser) != 'undefined' && (!gSwitchP_Prefs.prefHasUserValue("mproxy.reload.tab") || gSwitchP_Prefs.getBoolPref("mproxy.reload.tab")))){
try{
var oTab = gBrowser.mCurrentBrowser;
oTab.webNavigation.reload(nsIWebNavigation.LOAD_FLAGS_BYPASS_PROXY | nsIWebNavigation.LOAD_FLAGS_BYPASS_CACHE);
} catch(err) { }
}
// Set Update Pref
gSwitchP_Prefs.setIntPref("mproxy.proxy.rdf.lastupdate", (new Date()).getTime());
}
}catch(err){alert("102: "+ mproxy_getString("error.unknown") + "\n("+err+")");}
}
//End Command
gSwitchP_CommandEnd = true;
}
//Test if the RDF Element, oRef, is the current proxy being used
function mproxy_isSelected(oRef){
try{
var oPrefs = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefBranch);
var aProps = mproxy_ds_getPropertyValuesFor(oRef.Value);
var sCurrUri = "";
var iType = parseInt(aProps[gSProxyRdfNodeUriRoot + "#networkProxyType"]);
if(oPrefs.prefHasUserValue("mproxy.proxy.current") && (sCurrUri = oPrefs.getCharPref("mproxy.proxy.current")) == oRef.Value){
//Verify this is really what's being used in the browser
if(iType == oPrefs.getIntPref("network.proxy.type")){
if(iType == 1){
for(key in gmproxy_options){
//Skip These Keys
if(key == "networkProxyType" || key == "networkProxyAutoconfigURL" || key == "networkProxyHTTPShare" )
continue;
//Skip if RDF has Default Values
if( (key == "networkProxySOCKSVersion" && (aProps[gSProxyRdfNodeUriRoot +"#"+ key] == "5" || aProps[gSProxyRdfNodeUriRoot +"#"+ key] == ""))
|| (key == "networkProxyNone" && aProps[gSProxyRdfNodeUriRoot +"#"+ key] == "localhost, 127.0.0.1")){
continue;
}
//If Pref and RDF doesn't have a value for this
if(!oPrefs.prefHasUserValue(gmproxy_options[key])
&& (aProps[gSProxyRdfNodeUriRoot +"#"+ key] == "" || aProps[gSProxyRdfNodeUriRoot +"#"+ key] == "0")){
continue;
}
// If pref doesn't have this and the RDF record does
// return false
if(!oPrefs.prefHasUserValue(gmproxy_options[key])
&& (aProps[gSProxyRdfNodeUriRoot +"#"+ key] != "" && aProps[gSProxyRdfNodeUriRoot +"#"+ key] != "0")){
return false;
}
//Get pref and convert to string
var sPrefVal = "";
if(oPrefs.getPrefType(gmproxy_options[key]) == Components.interfaces.nsIPrefBranch.PREF_INT)
sPrefVal = oPrefs.getIntPref(gmproxy_options[key]) + "";
if(oPrefs.getPrefType(gmproxy_options[key]) == Components.interfaces.nsIPrefBranch.PREF_BOOL)
sPrefVal = oPrefs.getBoolPref(gmproxy_options[key]) + "";
if(oPrefs.getPrefType(gmproxy_options[key]) == Components.interfaces.nsIPrefBranch.PREF_STRING)
sPrefVal = oPrefs.getCharPref(gmproxy_options[key]);
//Return false if isn't equal
if(sPrefVal != aProps[gSProxyRdfNodeUriRoot +"#"+ key]){
return false;
}
}
return true;
}
//If type is 2 and the PAC url is the same
else if(iType == 2 && oPrefs.prefHasUserValue("mproxy.proxy.current")
&& oPrefs.getCharPref("network.proxy.autoconfig_url") == aProps[gSProxyRdfNodeUriRoot + "#networkProxyAutoconfigURL"]){
return true;
}
}
//Anonomous
else if(iType == 3 && oPrefs.prefHasUserValue("network.proxy.http") && oPrefs.prefHasUserValue("network.proxy.http_port")){
//var sProxy = oPrefs.getCharPref("network.proxy.http") +":"+ oPrefs.getIntPref("network.proxy.http_port");
//is sProxy in proxy list
//oTestRes = mproxy_ds_getElementForValue(gSProxyRdfNodeUriRoot +"#proxy", sProxy);
//if(oTestRes instanceof Components.interfaces.nsIRDFResource && oTestRes.Value == oRes.Value){
//mproxy_anon_loadProxy(oRef.Value, true);
//return true;
//}
return false;
}
}
}catch(err){}
return false;
}
function mproxy_removeProxy(){
mproxy_clearTimeout(gSwitchP_Cycle);
var oItem = gSwitchP_List.selectedItem;
//Can't delete 'None'
if((gSwitchP_List.selectedItem != null) &&
(gSwitchP_List.selectedItem.label == gSwitchP_NoneLabel) ){
alert("150: "+ mproxy_getString("error.remove.forbidden"));
return;
}
if(!confirm(mproxy_getString("confirm.remove")))
return;
try{
//RDF
//Remove
mproxy_ds_removeElement(oItem.value);
//Cleanup
mproxy_cleanupRdf();
//Update List
mproxy_populateList();
setTimeout("mproxy_setProxy();", 300);
}
catch(err){
alert("151: "+ mproxy_getString("error.remove") + "\n("+ err +")");
}
}
function mproxy_clearCookies(){
try{
var oCookies = Components.classes["@mozilla.org/cookiemanager;1"].createInstance(Components.interfaces.nsICookieManager);
oCookies.removeAll();
}catch(err){
alert("200: "+ mproxy_getString("error.cookie"));
}
return true;
}
function mproxy_add_proxy(sName, sPort, issocks, toset){
try{
var oTestProxy = mproxy_ds_getElementForValue(gSProxyRdfNodeName, sName);
if(oTestProxy != null){
return false;
}
var sProxyUri = mproxy_getUniqueProxyUri();
var oProxy = mproxy_ds_getResource(sProxyUri);
mproxy_ds_addElement(sProxyUri);
if (issocks) { //socks
mproxy_ds_addProperty(oProxy, mproxy_ds_getResource(gSProxyRdfNodeUriRoot+"#networkProxyType"), "1", true);
mproxy_ds_addProperty(oProxy, mproxy_ds_getResource(gSProxyRdfNodeUriRoot+"#networkProxyHTTP"), "", true);
mproxy_ds_addProperty(oProxy, mproxy_ds_getResource(gSProxyRdfNodeUriRoot+"#networkProxyHTTP_Port"), "", true);
mproxy_ds_addProperty(oProxy, mproxy_ds_getResource(gSProxyRdfNodeUriRoot+"#networkProxyHTTPShare"), false, true);
mproxy_ds_addProperty(oProxy, mproxy_ds_getResource(gSProxyRdfNodeUriRoot+"#networkProxySSL"), "", true);
mproxy_ds_addProperty(oProxy, mproxy_ds_getResource(gSProxyRdfNodeUriRoot+"#networkProxySSL_Port"), "", true);
mproxy_ds_addProperty(oProxy, mproxy_ds_getResource(gSProxyRdfNodeUriRoot+"#networkProxyFTP"), "", true);
mproxy_ds_addProperty(oProxy, mproxy_ds_getResource(gSProxyRdfNodeUriRoot+"#networkProxyFTP_Port"), "", true);
mproxy_ds_addProperty(oProxy, mproxy_ds_getResource(gSProxyRdfNodeUriRoot+"#networkProxyGopher"), "", true);
mproxy_ds_addProperty(oProxy, mproxy_ds_getResource(gSProxyRdfNodeUriRoot+"#networkProxyGopher_Port"), "", true);
mproxy_ds_addProperty(oProxy, mproxy_ds_getResource(gSProxyRdfNodeUriRoot+"#networkProxySOCKS"), "127.0.0.1", true);
mproxy_ds_addProperty(oProxy, mproxy_ds_getResource(gSProxyRdfNodeUriRoot+"#networkProxySOCKS_Port"), sPort, true);
mproxy_ds_addProperty(oProxy, mproxy_ds_getResource(gSProxyRdfNodeUriRoot+"#networkProxySOCKSVersion"), "5", true);
mproxy_ds_addProperty(oProxy, mproxy_ds_getResource(gSProxyRdfNodeUriRoot+"#networkProxyNone"), "localhost, 127.0.0.1", true);
mproxy_ds_addProperty(oProxy, mproxy_ds_getResource(gSProxyRdfNodeUriRoot+"#networkProxyAutoconfigURL"), "", true);
mproxy_ds_addProperty(oProxy, mproxy_ds_getResource(gSProxyRdfNodeUriRoot+"#name"), sName, true);
}
else { //regular http for all
mproxy_ds_addProperty(oProxy, mproxy_ds_getResource(gSProxyRdfNodeUriRoot+"#networkProxyType"), "1", true);
mproxy_ds_addProperty(oProxy, mproxy_ds_getResource(gSProxyRdfNodeUriRoot+"#networkProxyHTTP"), "127.0.0.1", true);
mproxy_ds_addProperty(oProxy, mproxy_ds_getResource(gSProxyRdfNodeUriRoot+"#networkProxyHTTP_Port"), sPort, true);
mproxy_ds_addProperty(oProxy, mproxy_ds_getResource(gSProxyRdfNodeUriRoot+"#networkProxyHTTPShare"), true, true);
mproxy_ds_addProperty(oProxy, mproxy_ds_getResource(gSProxyRdfNodeUriRoot+"#networkProxySSL"), "127.0.0.1", true);
mproxy_ds_addProperty(oProxy, mproxy_ds_getResource(gSProxyRdfNodeUriRoot+"#networkProxySSL_Port"), sPort, true);
mproxy_ds_addProperty(oProxy, mproxy_ds_getResource(gSProxyRdfNodeUriRoot+"#networkProxyFTP"), "127.0.0.1", true);
mproxy_ds_addProperty(oProxy, mproxy_ds_getResource(gSProxyRdfNodeUriRoot+"#networkProxyFTP_Port"), sPort, true);
mproxy_ds_addProperty(oProxy, mproxy_ds_getResource(gSProxyRdfNodeUriRoot+"#networkProxyGopher"), "127.0.0.1", true);
mproxy_ds_addProperty(oProxy, mproxy_ds_getResource(gSProxyRdfNodeUriRoot+"#networkProxyGopher_Port"), sPort, true);
mproxy_ds_addProperty(oProxy, mproxy_ds_getResource(gSProxyRdfNodeUriRoot+"#networkProxySOCKS"), "", true);
mproxy_ds_addProperty(oProxy, mproxy_ds_getResource(gSProxyRdfNodeUriRoot+"#networkProxySOCKS_Port"), "", true);
mproxy_ds_addProperty(oProxy, mproxy_ds_getResource(gSProxyRdfNodeUriRoot+"#networkProxySOCKSVersion"), "5", true);
mproxy_ds_addProperty(oProxy, mproxy_ds_getResource(gSProxyRdfNodeUriRoot+"#networkProxyNone"), "localhost, 127.0.0.1", true);
mproxy_ds_addProperty(oProxy, mproxy_ds_getResource(gSProxyRdfNodeUriRoot+"#networkProxyAutoconfigURL"), "", true);
mproxy_ds_addProperty(oProxy, mproxy_ds_getResource(gSProxyRdfNodeUriRoot+"#name"), sName, true);
}
if (toset) {
mproxy_setProxy(true, null, null, sName, sProxyUri, 1);
mproxy_selectItem(sName);
}
return true;
}catch(e){
alert(mproxy_getString("error.unknown") +"\n("+ e +")");
return false;
}
}
function mproxy_populateList(sSelectedLabel){
mproxy_clearTimeout(gSwitchP_Cycle);
var sProxyUri = "";
var iSelected = (gSwitchP_List != null) ? gSwitchP_List.selectedIndex : -1;
var aProxies = new Array();
//Don't populate if a local command has not finished or if list is open
if(!gSwitchP_CommandEnd){
gSwitchP_CommandEnd = true; //reset
return;
}
else{ //Start Command
gSwitchP_CommandEnd = false;
}
//Load Elements
try{
//Remove List Items & Add 'None'
mproxy_clearList();
mproxy_appendToList(gSwitchP_NoneLabel, "", true, 0);
mproxy_selectItem(gSwitchP_NoneLabel);
//Add Tor as default proxy, and set proxy to it
//mproxy_add_proxy(mproxy_getString("common.proxy.tor"), "9050", true, true);
//Get and Sort Elements
var aProxies = mproxy_ds_getAllElements();
aProxies.sort(mproxy_sortProxies);
//Add to list
for(i = 0; i < aProxies.length; i++){
if(typeof(aProxies[i]) == 'undefined')
continue;
try{
oRes = mproxy_ds_getResource(aProxies[i]);
sProxyName = mproxy_ds_getValueFor(oRes, mproxy_ds_getResource(gSProxyRdfNodeName));
sProxyType = mproxy_ds_getValueFor(oRes, mproxy_ds_getResource(gSProxyRdfNodeUriRoot+ "#networkProxyType"));
if(sProxyName != null){
oItem = mproxy_appendToList(sProxyName, aProxies[i], false, sProxyType);
//Select Item
if((sSelectedLabel != null && sSelectedLabel == sProxyName) || (sSelectedLabel == null && mproxy_isSelected(oRes))){
mproxy_selectItem(sProxyName);
}
}
} catch(err) {}
}
//Finish
gSwitchP_CommandEnd = true;
setTimeout("mproxy_selectProxy();", 300);
}catch(err){}
}
/*
* Sort Proxy List
* + oResA and oResB are RDF URIs
*/
function mproxy_sortProxies(oResA, oResB){
try{
//Get Proxy Names
sValA = mproxy_ds_getValueFor(mproxy_ds_getResource(oResA), mproxy_ds_getResource(gSProxyRdfNodeName));
sValB = mproxy_ds_getValueFor(mproxy_ds_getResource(oResB), mproxy_ds_getResource(gSProxyRdfNodeName));
if (sValA < sValB)
return -1;
if (sValA == sValB)
return 0;
if (sValA > sValB)
return 1;
}
catch(e){ return 0; }
}
/* Cleanup unused mproxy data in RDF
* + Remove resources that have not been applied or used
*/
function mproxy_cleanupRdf(){
try{
var aElements = mproxy_ds_getAllElements();
for(var e = 0; e < aElements.length; e++){
oRes = mproxy_ds_getResource(aElements[e]);
aProps = mproxy_ds_getPropertyValuesFor(aElements[e]);
//If does not have a name property,
// then it is not listed -- so it's junk
if(aProps[gSProxyRdfNodeName] == null){
mproxy_ds_removeElement(aElements[e]);
}
}
}catch(err){}
}
// Automatic Upgrade
/*
* From Version 0.4
* + Add preference 'mproxy.version'
* + Add preference 'mproxy.proxy.current'
* + Change proxy's URI to random number
* + Convert #proxy to networkProxyAutoconfigURL
* + Add elements for full manual proxy configuration
* + view the globals.js gmproxy_options array for full list
*/
//Generates random number for proxy URI
function mproxy_getUniqueProxyUri(){
var sUri = gSProxyRdfRoot + "/mproxy_" + Math.round((Math.random() * 200000));
if(mproxy_ds_doesProxyElementExist(sUri))
return mproxy_getUniqueProxyUri();
return sUri;
}
function mproxy_setStatus(sMsg, sColor){
if(!sColor)
sColor = "#000";
oStatus = document.getElementById('status-text');
if(oStatus != null){
document.getElementById('status-text').style.color = sColor;
document.getElementById('status-text').value = sMsg;
}
}
//Clear Timeout
function mproxy_clearTimeout(){
clearTimeout(gSwitchP_Cycle);
gSwitchP_Cycle = null;
}
//Execute a command when system is ready
// iCurrCount is for internal use, do not
// pass this argument
function mproxy_doCommand(sCommand, iLimitCount){
if(iLimitCount == null)
iLimitCount = 0;
//Wait for current command to end
if(!gSwitchP_CommandEnd && iLimitCount < 10){
iLimitCount++
setTimeout("mproxy_doCommand('"+ sCommand +"', "+ iCurrCount +")", 100);
}
else{
gSwitchP_CommandEnd = true;
setTimeout(sCommand, 100);
}
}
/*
* Launch Dialogs
*/
//Edit Proxy Dialog
function mproxy_editProxyDialog(isNew){
mproxy_clearTimeout(gSwitchP_Cycle);
var sAction = null;
//Nothing Selected
var oEditItem = null
if(!isNew && (oEditItem = gSwitchP_List.selectedItem) == null){
alert("250: "+ mproxy_getString("error.edit.select"));
return;
}
//Can't edit 'None'
if(!gSwitchP_InManager && !isNew && gSwitchP_List != null && gSwitchP_List.selectedItem != null && gSwitchP_List.selectedItem.label == gSwitchP_NoneLabel){
alert("251: "+ mproxy_getString("error.edit.forbidden"));
return;
}
//Edit
if(!isNew){
//Anon
//if(oEditItem.getAttribute("proxyType") == "3"){
// window.openDialog("chrome://mproxy/content/dialogs/editanon.xul","editproxy","centerscreen, chrome", "edit", oEditItem.value);
//}
//Standard
//else{
window.openDialog("chrome://mproxy/content/dialogs/editproxy.xul","editproxy","centerscreen, chrome", "edit", oEditItem.value);
//}
}
//Add
else{
//window.openDialog("chrome://mproxy/content/dialogs/addproxytype.xul","editproxy","centerscreen, chrome", "add");
window.openDialog("chrome://mproxy/content/dialogs/editproxy.xul","addproxy","centerscreen, chrome", "add", gSwitchP_InManager);
}
}
//Manage Proxy Dialog
function mproxy_openProxyManager(){
window.openDialog("chrome://mproxy/content/dialogs/manager.xul","manageproxies","centerscreen, chrome, resizable");
}
/*
* Options
*/
function mproxy_openmproxyPrefs(){
window.openDialog("chrome://mproxy/content/options/options.xul","mproxyPrefs","centerscreen, chrome");
}
function mproxy_goTomproxySite(fromDialog){
// If Thunderbird
if(navigator.userAgent.search(/Thunderbird/gi) > -1){
var messenger = Components.classes["@mozilla.org/messenger;1"].createInstance();
messenger = messenger.QueryInterface(Components.interfaces.nsIMessenger);
messenger.launchExternalURL(gmproxy_DownloadSite +"?r=mproxy");
}
else{
opener.gBrowser.selectedTab = opener.getBrowser().addTab(gmproxy_DownloadSite +"?r=mproxy");
}
self.close();
}
function mproxy_goTomproxyAbout(){
window.openDialog("chrome://mproxy/content/dialogs/about.xul","mproxyAbout","centerscreen, chrome, modal");
}
/*
* Automatic Updator
*/
//If there is a new version, returns the XPI url, else returns false
//The user selects to check for updates
/*
* UNINSTALL
*/
function mproxy_uninstall(oLogger){
try{
var oPrefs = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefBranch);
oPrefs.deleteBranch("mproxy");
oLogger.addLog(new window.ExtuninstallLogItem(oLogger.REMOVE_ACTION, oLogger.SUCCESS_STATUS, "Preferences Branch 'mproxy'", null, null));
}catch(err){
oLogger.addLog(new window.ExtuninstallLogItem(oLogger.REMOVE_ACTION, oLogger.WARN_STATUS, "Preferences Branch 'mproxy'", 1001, err));
throw 1001;
}
}
|