forked from alice0775/userChrome.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlinkInNewTabForSpecifiedPage.uc.xul
338 lines (311 loc) · 11.3 KB
/
linkInNewTabForSpecifiedPage.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
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
<?xml version="1.0" encoding="UTF-8"?>
<overlay xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">
<!--
// ==UserScript==
// @name linkInNewTabForSpecifiedPage
// @namespace http://space.geocities.yahoo.co.jp/gl/alice0775
// @description 特定のサイトのリンクは新規タブで開く
// @include main
// @compatibility Firefox 2.0 3.0
// @author Alice0775
// @version 2008/09/20 19:30 javascriptは現在のタブ
// @Note リンク上の左クリックを中クリックに置き換えている
// ==/UserScript==
// @version 2008/03/22 01:00 例外処理
// @version 2007/10/11 22:30
-->
<script language="JavaScript"><![CDATA[
var linkInNewTabForSpecifiedPage = {
prefstr:'userChrome.linkInNewTabForSpecifiedPage.url',
prefbool:'userChrome.linkInNewTabForSpecifiedPage.enable',
include:[],
exclude:[],
exec:false,
init: function(){
// 初期化中に
this._window = gBrowser.mPanelContainer;
this._window.addEventListener("click", this, true);
var that = this;
//window.addEventListener("focus", that.tabSelected, false);
//gBrowser.tabContainer.addEventListener("TabSelect", that.tabSelected, false);
linkInNewTabForSpecifiedPage.addPrefListener(that.PrefListener); // 登録処理
gBrowser.addProgressListener(that);
this.initPrefs();
},
uninit: function(){
// 必要なくなれば
this._window.removeEventListener("click", this, true);
var that = this;
//window.removeEventListener("focus", that.tabSelected, false);
//gBrowser.tabContainer.removeEventListener("TabSelect", that.tabSelected, false);
linkInNewTabForSpecifiedPage.removePrefListener(that.PrefListener); // 解除処理
gBrowser.removeProgressListener(that);
},
onStatusChange: function(){},
onProgressChange: function(){},
onLocationChange: function(aWebProgress, aRequest, aLocation){
linkInNewTabForSpecifiedPage.tabSelected();
},
onStateChange: function(){},
onSecurityChange: function(){},
initPrefs:function(){
this.include = [];
this.exclude = [];
var Urls = this.getPref(this.prefstr,"str",'').split(' ');
for(var i=0,len=Urls.length;i<len;i++){
if(Urls[i]){
if(Urls[i].match(/^!/)){
this.exclude.push(this.convert2RegExp(Urls[i]));
this.debug(this.convert2RegExp(Urls[i]));
}else{
this.include.push(this.convert2RegExp(Urls[i]));
this.debug(this.convert2RegExp(Urls[i]));
}
}
}
},
modPrefs: function(){
var text0, objArray=[], selected = {}, result, text1, text;
var promptService = Cc["@mozilla.org/embedcomp/prompt-service;1"]
.getService(Ci.nsIPromptService);
while(true){
text0 = this.getPref(this.prefstr, 'str', '').replace(/^ +| +$/g,'');
objArray = text0.split(' ');
//objArray.sort();
objArray.sort(function(a,b) {
a=a.toLowerCase();
b=b.toLowerCase();
if (a < b) return -1;
if (a > b) return 1;
return 0;
})
text0 = ' ' + text0 + ' ';
objArray = ['サイトを追加'].concat(objArray);
selected = {};
result = promptService.select(null, '常にリンクは新規タブで閲覧するサイト',
'追加:"サイトを追加"を選択, 修正/削除:サイトを選択, 終了:キャンセル',
objArray.length, objArray, selected);
if (!result) return;
if(selected.value == 0){//追加
this.addSite(window.content.document.location.href);
}else{
text = objArray[selected.value];
text1 = window.prompt('常にリンクは新規タブで閲覧するサイト(完全一致*,!)/ ブランクで削除', text);
if(text1 ==''){//削除
this.setPref(this.prefstr, 'str', text0.replace(' '+text+' ', ' ').replace(/^ +| +$/g,''));
}else if(text1){//修正
text1 = text1.replace(/^ +| +$/g,'');
this.setPref(this.prefstr, 'str', text0.replace(' '+text+' ', ' '+text1+' ').replace(/^ +| +$/g,''));
}
}
}
},
addSite:function(href){
var text = window.prompt('常にリンクは新規タブで閲覧するサイト(完全一致*,!)',href);
this.debug(text);
if(!text) return;
if(text == '') return;
text = text.replace(/^ +| +$/g,'');
text = this.getPref(this.prefstr, 'str', '').replace(/^ +| +$/g,'') + ' ' + text;
this.setPref(this.prefstr, 'str', text)
},
toggle: function(){
var flg = this.getPref(this.prefbool, 'bool', true);
this.setPref(this.prefbool, 'bool', !flg);
},
tabSelected: function(event){
var that = linkInNewTabForSpecifiedPage;
//var browser = gBrowser.selectedTab;
// browser は新たに選択されたタブです
try{
var doc = gBrowser.selectedBrowser.contentDocument;
var url = doc.location.href;
for(var i=0,len=that.exclude.length;i<len;i++){
if( that.exclude[i].test(url)){
that.exec = false;
return;
}
}
for(var i=0,len=that.include.length;i<len;i++){
if( that.include[i].test(url)){
that.exec = that.getPref(that.prefbool, 'bool', true);
return;
}
}
}catch(e){}
that.exec = false;
},
handleEvent: function(event){
var target = event.originalTarget;
switch (event.type) {
case "click":
if (this.exec && event.button == 0 && this.isLink(target) ) {
this.debug("click");
event.preventDefault();
event.stopPropagation();
var elem = event.originalTarget;
var MouseEvents = document.createEvent("MouseEvents");
/*initMouseEvent(type, canBubble, cancelable, view,
detail, screenX, screenY, clientX, clientY,
ctrlKey, altKey, shiftKey, metaKey,
button, relatedTarget);
*/
MouseEvents.initMouseEvent("click", true, true, this._getFocusedWindow(),
event.detail, event.screenX, event.screenY, event.clientX, event.clientY,
event.ctrlKey, event.altKey, event.shiftKey, event.metaKey,
1, event.relatedTarget);
target.dispatchEvent(MouseEvents);
}
break;
}
},
_getFocusedWindow: function(){ //現在のウインドウを得る
var focusedWindow = document.commandDispatcher.focusedWindow;
if (!focusedWindow || focusedWindow == window)
return window._content;
else
return focusedWindow;
},
isLink: function(node) {
while (node) {
if((node instanceof HTMLAnchorElement || node instanceof HTMLAreaElement)
&& node.hasAttribute("href") ) {
if (/^\s*javascript:/.test(node.getAttribute("href")))
return false;
return node;
}
node = node.parentNode;
}
return false;
},
debug: function(aMsg){
const Cc = Components.classes;
const Ci = Components.interfaces;
Cc["@mozilla.org/consoleservice;1"]
.getService(Ci.nsIConsoleService)
.logStringMessage(aMsg);
},
onToolMenupopup: function(){
var flg = linkInNewTabForSpecifiedPage.getPref(linkInNewTabForSpecifiedPage.prefbool, 'bool', true);
var menuitem = document.getElementById("linkInNewTabForSpecifiedPageCheckbox");
menuitem.setAttribute('checked', flg);
},
// Converts a pattern in this programs simple notation to a regular expression.
// thanks AdBlock! http://www.mozdev.org/source/browse/adblock/adblock/
convert2RegExp: function( pattern ) {
var s = new String(pattern);
if(s.match(/^!/)){
s = s.substr(1);
}
var res = new String("^");
for (var k = 0 ; k < s.length ; k++) {
switch(s[k]) {
case '*' :
res += ".*";
break;
case '.' :
case '?' :
case '^' :
case '$' :
case '+' :
case '{' :
case '[' :
case '|' :
case '(' :
case ')' :
case ']' :
res += "\\" + s[k];
break;
case '\\' :
res += "\\\\";
break;
case ' ' :
// Remove spaces from URLs.
break;
default :
res += s[k];
break;
}
}
// fortunately, we don't need .tld in chrome :)
return new RegExp(res + '$', "i");
},
getPref: function(aPrefString, aPrefType, aDefault){
var xpPref = Components.classes["@mozilla.org/preferences-service;1"]
.getService(Components.interfaces.nsIPrefBranch2);
try{
switch (aPrefType){
case "str":
return xpPref.getCharPref(aPrefString).toString(); break;
case "int":
return xpPref.getIntPref(aPrefString); break;
case "bool":
default:
return xpPref.getBoolPref(aPrefString); break;
}
}catch(e){}
return aDefault;
},
setPref: function(aPrefString, aPrefType, aValue){
var xpPref = Components.classes["@mozilla.org/preferences-service;1"]
.getService(Components.interfaces.nsIPrefBranch2);
try{
switch (aPrefType){
case "str":
return xpPref.setCharPref(aPrefString, aValue); break;
case "int":
aValue = parseInt(aValue);
return xpPref.setIntPref(aPrefString, aValue); break;
case "bool":
default:
return xpPref.setBoolPref(aPrefString, aValue); break;
}
}catch(e){}
return null;
},
// 監視を開始する
addPrefListener: function(aObserver) {
try {
var pbi = Components.classes['@mozilla.org/preferences;1'].getService(Components.interfaces.nsIPrefBranch2);
pbi.addObserver(aObserver.domain, aObserver, false);
} catch(e) {}
},
// 監視を終了する
removePrefListener: function(aObserver) {
try {
var pbi = Components.classes['@mozilla.org/preferences;1'].getService(Components.interfaces.nsIPrefBranch2);
pbi.removeObserver(aObserver.domain, aObserver);
} catch(e) {}
},
PrefListener:{
domain : 'userChrome.linkInNewTabForSpecifiedPage',
//"XX"という名前の設定が変更された場合全てで処理を行う
observe : function(aSubject, aTopic, aPrefstring) {
if (aTopic == 'nsPref:changed') {
// 設定が変更された時の処理
linkInNewTabForSpecifiedPage.initPrefs();
}
}
}
};
// エントリポイント
linkInNewTabForSpecifiedPage.init();
window.addEventListener("unload", function(){ linkInNewTabForSpecifiedPage.uninit(); }, false);
]]></script>
<menupopup id="menu_ToolsPopup">
<menu id="linkInNewTabForSpecifiedPageToolMenu"
label="linkInNewTabForSpecifiedPageの設定" accesskey="l"
insertbefore="menu_preferences">
<menupopup onpopupshowing="event.stopPropagation();linkInNewTabForSpecifiedPage.onToolMenupopup();">
<menuitem label="常にリンクは新規タブで有効/無効"
id="linkInNewTabForSpecifiedPageCheckbox"
accesskey="a"
type="checkbox"
oncommand="linkInNewTabForSpecifiedPage.toggle()"/>
<menuitem label="常にリンクは新規タブで閲覧するサイトの登録"
accesskey="s"
oncommand="linkInNewTabForSpecifiedPage.modPrefs()"/>
</menupopup>
</menu>
</menupopup>
</overlay>