forked from alice0775/userChrome.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
contextProxySwitch.uc.xul
258 lines (236 loc) · 9.68 KB
/
contextProxySwitch.uc.xul
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
<?xml version="1.0"?>
<overlay id="context_ProxySwitch"
xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">
<!--
// ==UserScript==
// @name contextProxySwitch
// @namespace http://space.geocities.yahoo.co.jp/gl/alice0775
// @description 一発プロキシ設定
// @include main
// @compatibility Firefox 2.0 3.0 3.1
// @author Alice0775
// @Note quickProxyModoki3.0.uc.xulも合わせてどうぞ
// @version 2013/09/13 00:00 Bug 856437 Remove Components.lookupMethod
// @version 2009/09/06 00:00 httpsも設定するかどうか USE_PROXY_SSL
// ==/UserScript==
// @version 2009/03/16 00:00 初期化時チェック
// @version 2008/12/04 00:00 Cleanup
// @version 2008/11/03 00:00 PACファイル使用中の表示
// @version 2007/06/17 00:00 http://www.cybersyndrome.net/ のproxyを選択して一括登録
// @version 2007/06/15
-->
<script type="application/x-javascript" xmlns="http://www.w3.org/1999/xhtml"><![CDATA[
var ucjsProxy ={
// --- config ---
USE_PROXY_SSL:false, //httpsも設定 true:する, [false]しない
// --- config ---
_Prefs: Components.classes["@mozilla.org/preferences-service;1"].
getService(Components.interfaces.nsIPrefBranch),
_exp:/([a-z0-9_\-]+(\.[a-z0-9_\-]+)+):(\d+)/i,
_Switch: function _Switch() {
var first = true;
var text = this._getselection();
var texts = text.split("\n");
for (var i = 0; i < texts.length; i++){
var xx = texts[i].match(this._exp);
var proxyhttp = RegExp.$1;
var proxyhttp_port = parseInt(RegExp.$3, 10);
if( !xx || text =='' || proxyhttp_port <= 0 || proxyhttp_port > 65535 || proxyhttp == '') {
//串じゃない
this.deselectProxy();
} else {
//たぶん串なので串をセットする
this.addProxy(proxyhttp + ":" + proxyhttp_port);
if (first){
this.selectProxy(proxyhttp + ":" + proxyhttp_port);
first = false;
}
}
}
},
addProxy: function(proxy){
try{
var proxys = this._Prefs.getCharPref("userChrome_js.network.proxys");
}catch(e){
var proxys = "";
}
proxys = proxys.replace(proxy, "");
proxys = (proxys + "," + proxy).replace(/,,/, ',').replace(/^,/, '').replace(/,$/, '');
this._Prefs.setCharPref("userChrome_js.network.proxys", proxys);
},
delProxy: function(proxy){
try{
var proxys = this._Prefs.getCharPref("userChrome_js.network.proxys");
proxys = proxys.replace(proxy, "").replace(/,,/, ',').replace(/^,/, '').replace(/,$/, '');
this._Prefs.setCharPref("userChrome_js.network.proxys", proxys);
}catch(e){}
},
deselectProxy: function(){
this._Prefs.setIntPref("network.proxy.type", 0);
document.getElementById('nonProxy').setAttribute('checked', true);
},
PACProxy: function(){
this._Prefs.setIntPref("network.proxy.type", 2);
document.getElementById('PACProxy').setAttribute('checked', true);
},
allDelete: function(){
this._Prefs.setIntPref("network.proxy.type", 0);
this._Prefs.setCharPref("userChrome_js.network.proxys", '');
},
selectProxy: function(proxy){
var xx = proxy.match(this._exp);
var proxyhttp = RegExp.$1;
var proxyhttp_port = parseInt(RegExp.$3, 10);
this._Prefs.setCharPref("network.proxy.http", proxyhttp);
this._Prefs.setIntPref("network.proxy.http_port", proxyhttp_port);
this._Prefs.setCharPref("network.proxy.ftp", proxyhttp);
this._Prefs.setIntPref("network.proxy.ftp_port", proxyhttp_port);
if (this.USE_PROXY_SSL) {
this._Prefs.setCharPref("network.proxy.ssl", proxyhttp);
this._Prefs.setIntPref("network.proxy.ssl_port", proxyhttp_port);
}
this._Prefs.setIntPref("network.proxy.type", 1);
},
_getFocusedWindow: function(){
var focusedWindow = document.commandDispatcher.focusedWindow;
if (!focusedWindow || focusedWindow == window)
return window._content;
else
return focusedWindow;
},
_getselection: function() {
var targetWindow = this._getFocusedWindow();
var sel = targetWindow.getSelection();
// for textfields
if (sel && !sel.toString()) {
var node = document.commandDispatcher.focusedElement;
if (node &&
(node.type == "text" || node.type == "textarea") &&
'selectionStart' in node &&
node.selectionStart != node.selectionEnd) {
var offsetStart = Math.min(node.selectionStart, node.selectionEnd);
var offsetEnd = Math.max(node.selectionStart, node.selectionEnd);
return node.value.substr(offsetStart, offsetEnd-offsetStart);
}
}
return sel ? sel.toString() : "";
},
addMenuItem: function(proxy){
var menuitem = document.createElement('menuitem');
var menupopup = document.getElementById('contextProxySwitchMenupopup');
menupopup.appendChild(menuitem);
menuitem.setAttribute('label', proxy);
menuitem.setAttribute('type', 'radio');
menuitem.setAttribute('checked', false);
menuitem.setAttribute('name', 'contextPproxy');
menuitem.setAttribute('tooltiptext', 'プロキシに設定, 中クリックで削除');
menuitem.setAttribute('oncommand', 'ucjsProxy.selectProxy(this.getAttribute("label"));');
menuitem.setAttribute('onclick', 'if(event.button==1){ucjsProxy.deleteMenuItem(event);}');
return menuitem;
},
deleteMenuItem: function(event){
var target = event.target;
if (target.getAttribute('checked') == 'true'){
this.deselectProxy();
}
this.delProxy(target.getAttribute('label'));
target.parentNode.removeChild(target)
},
popupOnshowing: function(menupopup){
for (var j = 0; j < menupopup.childNodes.length; j++){
if (menupopup.childNodes[j].getAttribute('id') == 'contextProxySwitchMenuSeparator'){
break;
}
}
for (var i = menupopup.childNodes.length - 1; i > j; i--){
menupopup.removeChild(menupopup.lastChild);
}
try {
var proxys = this._Prefs.getCharPref("userChrome_js.network.proxys").split(",");
} catch(e) {
var proxys = [];
}
var enable = this._Prefs.getIntPref("network.proxy.type");
var proxyhttp = this._Prefs.getCharPref("network.proxy.http");
var proxyhttp_port = this._Prefs.getIntPref("network.proxy.http_port");
if (this._getselection() == '')
document.getElementById('contextProxySwitchSelect').setAttribute('disabled', true);
else
document.getElementById('contextProxySwitchSelect').removeAttribute('disabled');
var menuitem;
var flg = false;
for (var i=0; i<proxys.length; i++){
if (!proxys[i])
continue;
menuitem = this.addMenuItem(proxys[i]);
if(enable == 1 && proxys[i] == (proxyhttp + ":" + proxyhttp_port)) {
flg = true;
menuitem.setAttribute('checked', true);
}
}
if (!flg && enable == 1) {
this.addProxy(proxyhttp + ":" + proxyhttp_port);
/*
enable = 0;
this._Prefs.setIntPref("network.proxy.type", enable);
*/
}
document.getElementById('nonProxy').setAttribute('checked', (enable == 0));
document.getElementById('PacProxy').setAttribute('checked', (enable == 2));
},
init: function(){
try {
var proxys = this._Prefs.getCharPref("userChrome_js.network.proxys").split(",");
} catch(e) {
var proxys = [];
}
var enable = this._Prefs.getIntPref("network.proxy.type");
var proxyhttp = this._Prefs.getCharPref("network.proxy.http");
var proxyhttp_port = this._Prefs.getIntPref("network.proxy.http_port");
var flg = false;
for (var i=0; i<proxys.length; i++){
if (!proxys[i])
continue;
if(enable == 1 && proxys[i] == (proxyhttp + ":" + proxyhttp_port)) {
flg = true;
}
}
if (!flg && enable == 1) {
this.addProxy(proxyhttp + ":" + proxyhttp_port);
/*
enable = 0;
this._Prefs.setIntPref("network.proxy.type", enable);
*/
}
}
}
ucjsProxy.init();
]]></script>
// This is for the right click menu.
<popup id="contentAreaContextMenu">
<menu label="一発プロキシ設定">
<menupopup id="contextProxySwitchMenupopup"
onpopupshowing="ucjsProxy.popupOnshowing(this);">
<menuitem id="contextProxySwitchSelect"
label="選択文字列をプロキシに設定"
oncommand="ucjsProxy._Switch();"/>
<menuitem label="全部削除"
oncommand="ucjsProxy.allDelete();"/>
<menuseparator/>
<menuitem id="nonProxy"
type="radio"
name="contextPproxy"
checked="true"
label="プロキシなし"
oncommand="ucjsProxy.deselectProxy();"/>
<menuitem id="PacProxy"
type="radio"
name="contextPproxy"
checked="true"
label="PACファイル使用"
oncommand="ucjsProxy.PACProxy();"/>
<menuseparator id="contextProxySwitchMenuSeparator"/>
</menupopup>
</menu>
</popup>
</overlay>