forked from alice0775/userChrome.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
multiSelection.uc.js
149 lines (141 loc) · 4.94 KB
/
multiSelection.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
// ==UserScript==
// @name multiSelection.uc.js
// @namespace http://space.geocities.yahoo.co.jp/gl/alice0775
// @include main
// @description alt+選択でテーブルでも複数の文字列を選択できるようにする。 既知の不具合:ちらつく
// @author Alice0775
// @version 2009/06/05 18:00 デザインモードなときは無視
// @version 2008/07/23 18:00
// @note selectLinkTextWithAltKey.uc.jsもあると尚良い
// ==/UserScript==
var multiSelection = {
kCARET: "accessibility.browsewithcaret",
selRange: [],
init: function(){
gBrowser.addEventListener('load', this, true);
window.addEventListener('unload', this, false);
gBrowser.mPanelContainer.addEventListener('mouseup', this, false);
//gBrowser.mPanelContainer.addEventListener('keyup', this, true);
},
uninit: function(){
gBrowser.removeEventListener('load', this, true);
gBrowser.mPanelContainer.removeEventListener('mouseup', this, false);
//gBrowser.mPanelContainer.removeEventListener('keyup', this, true);
},
handleEvent: function(event){
switch(event.type){
case "load":
this.detach();
break;
case "unload":
this.uninit();
break;
case "mouseup":
if (event.button == 0 &&
event.altKey && !event.ctrlKey && !event.shiftKey ){
this.getSelection(event);
} else {
this.detach();
}
break;
case "keyup":
var doc = event.originalTarget.ownerDocument;
if(doc.contentType != 'text/plain'
&& doc.contentType != 'text/html'
&& doc.contentType != 'application/xml'
&& doc.contentType != 'application/xhtml+xml')
return;
//designModeなら何もしない
if (Components.lookupMethod(doc, 'designMode').call(doc) == 'on')
return;
if (this.getPref(this.kCARET, 'bool', false))
return
if (event.altKey &&
(event.keyCode == KeyEvent.DOM_VK_LEFT ||
event.keyCode == KeyEvent.DOM_VK_RIGHT ||
event.keyCode == KeyEvent.DOM_VK_UP ||
event.keyCode == KeyEvent.DOM_VK_DOWN ||
event.keyCode == KeyEvent.DOM_VK_PAGE_DOWN ||
event.keyCode == KeyEvent.DOM_VK_PAGE_UP ) ){
this.getSelection(event);
} else {
this.detach();
}
break;
}
},
getFocusedWindow: function(){ //現在のウインドウを得る
var focusedWindow = document.commandDispatcher.focusedWindow;
if (!focusedWindow || focusedWindow == window)
return window.content;
else
return focusedWindow;
},
getSelection: function(event){
var doc, selectionRange, flg, selRange, selCon;
var targetWindow = this.getFocusedWindow();
var selection = Components.lookupMethod(targetWindow, 'getSelection').call(targetWindow);
for (var i = 0; i < selection.rangeCount; i++){
selectionRange = selection.getRangeAt(i);
flg = false;
for (var j = 0; j < this.selRange.length; j++){
if (this.selRange[j] == selectionRange){
flg = true;
break;
}
}
if (!flg)
this.selRange.push(selectionRange);
}
for (var i = 0; i < this.selRange.length; i++){
selRange = this.selRange[i];
doc = selRange.startContainer.ownerDocument
selCon = this.getSelconForDoc(doc);
if (!selCon)
continue;
selection = selCon.getSelection(selCon.SELECTION_NORMAL);
selection.addRange(selRange);
}
},
detach: function(){
this.selRange =[];
},
getSelconForDoc: function getSelconForDoc(doc){
var docShell = this.getDocShellForFrame(doc.defaultView);
try {
var selCon = docShell
.QueryInterface(Components.interfaces.nsIInterfaceRequestor)
.getInterface(Components.interfaces.nsISelectionDisplay)
.QueryInterface(Components.interfaces.nsISelectionController);
return selCon;
} catch (e) {
return null;
}
},
getDocShellForFrame: function getDocShellForFrame(aFrame){
return aFrame
.QueryInterface(Components.interfaces.nsIInterfaceRequestor)
.getInterface(Components.interfaces.nsIWebNavigation)
.QueryInterface(Components.interfaces.nsIDocShell);
},
getPref: function(aPrefString, aPrefType, aDefault){
var xpPref = Components.classes['@mozilla.org/preferences-service;1']
.getService(Components.interfaces.nsIPrefBranch2);
try{
switch (aPrefType){
case 'complex':
return xpPref.getComplexValue(aPrefString, Components.interfaces.nsILocalFile); break;
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;
}
}
multiSelection.init();