forked from ywzhaiqi/userChromeJS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
autoContextmenu.uc.xul
346 lines (320 loc) · 12.5 KB
/
autoContextmenu.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
339
340
341
342
343
344
345
346
<?xml version="1.0"?>
<overlay id="autoContextmenu"
xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul">
<!--
// ==UserScript==
// @name autoContextmenu
// @namespace http://space.geocities.yahoo.co.jp/gl/alice0775
// @description Auto context menu on selection
// @include main
// @compatibility Firefox 2.0 3.0 3.5 3.6 3.7a3pre
// @author Alice0775
// @version 2011/10/04 22:00 Mouse Gesture Redox があるとHTML5 contextmenuが動かないのを修正
// @version 2010/03/10 09:00 Mouse Gesture Redox があると動かないのを修正
// @version 2009/07/21 00:00 mousedownタイミング修正
// ==/UserScript==
// @version 2007/10/05 00:00 リンク上やbody上など長押し(msec)でcontext menu表示
// @version 2007/09/25 00:00
// @Note
-->
<script type="application/x-javascript" xmlns="http://www.w3.org/1999/xhtml"><![CDATA[
var autoContextmenu = {
xpPref: Components.classes['@mozilla.org/preferences-service;1'].
getService(Components.interfaces.nsIPrefBranch2),
prefstr: "userChrome.autoContextmenu",
kTIMER : 800, //リンク上やbody上など長押し(msec)でcontext menu表示
kTOLERANCE:10,
contextOpen:false,
timer: null,
cursorTimer: null,
prev: null,
prevcursor: null,
init: function() {
var menu = document.getElementById("autoContextmenu");
if (!menu){
setTimeout(function(){autoContextmenu.init();}, 200);
return;
} else {
this.setMenuCkeck();
this.addPrefListener(this.PrefListener); // 登録処理
window.addEventListener("unload", this, false);
}
},
uninit: function(){
gBrowser.mPanelContainer.removeEventListener("mousedown", this, true);
gBrowser.mPanelContainer.removeEventListener("mouseup", this, true);
gBrowser.mPanelContainer.removeEventListener("click", this, true);
this.removePrefListener(this.PrefListener); // 解除処理
window.removeEventListener("unload", this, false);
},
isEnable: function(){
if(!document.getElementById("autoContextmenu")
|| document.getElementById("autoContextmenu").getAttribute('checked') != "true")
return false
return true;
},
handleEvent: function(event){
switch (event.type) {
case "mousedown":
//window.userChrome_js.debug("mousedown " + event.button);
this.resetCursor();
if (event.button != 0) {
return;
}
this.timer = new Date().getTime();
var isClickable = this.isParentClickableNode(event.target);
var elem = isClickable || event.target;
if (isClickable)
this.cursorTimer = setTimeout(function(self, elem) {
self.prev = elem;
self.prevcursor = self.prev.style.getPropertyValue("cursor");
self.prev.style.setProperty("cursor", "help", "important");
self.cursorTimer2 = setTimeout(function(self) {
self.resetCursor();
},3000, self);
}, this.kTIMER, this, elem);
this.lastX = event.screenX;
this.lastY = event.screenY;
//this.exec(event);
break;
case "mouseup":
//window.userChrome_js.debug("mouseup " + event.button);
this.resetCursor();
if (event.button != 0) {
return;
}
this.exec(event);
break;
case "click":
//window.userChrome_js.debug("click " + event.button);
this.resetCursor();
if (event.button != 0) {
return;
}
if (this.contextOpen == true) {
event.stopPropagation();
event.preventDefault();
}
break;
case "unload":
this.uninit();
}
},
resetCursor: function() {
if (this.cursorTimer) {
clearTimeout(this.cursorTimer);
}
if (this.cursorTimer2) {
clearTimeout(this.cursorTimer2);
}
if (this.prev) {
//if (this.prevcursor)
// this.prev.style.setProperty("cursor", this.prevcursor, null);
//else
this.prev.style.removeProperty("cursor");
this.prev = null;
}
},
setMenuCkeck: function(){
try{
var check = this.xpPref.getBoolPref(this.prefstr);
} catch(e){
var check = true;
}
var menu = document.getElementById("autoContextmenu");
menu.setAttribute('checked', check);
if (check) {
gBrowser.mPanelContainer.addEventListener("mousedown", this, true);
gBrowser.mPanelContainer.addEventListener("mouseup", this, true);
gBrowser.mPanelContainer.addEventListener("click", this, true);
} else {
gBrowser.mPanelContainer.removeEventListener("mousedown", this, true);
gBrowser.mPanelContainer.removeEventListener("mouseup", this, true);
gBrowser.mPanelContainer.removeEventListener("click", this, true);
}
},
toggle: function(){
try{
var check = this.xpPref.getBoolPref(this.prefstr);
} catch(e){
var check = true;
}
this.xpPref.setBoolPref(this.prefstr, !check);
},
exec: function(event) {
this.contextOpen = false;
if (event.button != 0)
return;
if (event.type === "mousedown")
return;
var selection = document.commandDispatcher.focusedWindow.getSelection();
var flg = (new Date().getTime() - this.timer > this.kTIMER);
if ( Math.abs(this.lastX - event.screenX) + Math.abs(this.lastY - event.screenY) > this.kTOLERANCE)
var flg = false
if (!flg && selection.toString() == "")
return;
/*
if (event.type == "mousedown")
if ((selection.rangeCount > 0) && !selection.getRangeAt(0).collapsed)
if (event.target.tagName != "menuitem")
selection.removeAllRanges();
*/
var link = this.isParentClickableNode(event.target);
var within = link ? this.isSelectedIn(link) : false;
var context = false;
if (!link && selection.isCollapsed && flg)
context = true;
else if (link && !within && flg)
context = true;
else if(link && within || flg)
context = true;
else if (!link && !selection.isCollapsed || flg)
context = true;
if (context) {
//window.userChrome_js.debug("contextmenu");
if ('mgGestureState' in window) {
mgGestureState.allowContext = true;
var mouseEvent = document.createEvent("MouseEvents");
mouseEvent.initMouseEvent("contextmenu", true, true, event.view, 1,
event.screenX, event.screenY, event.clientX, event.clientY,
false, false, false, false, 2, null);
this.contextOpen = true;
event.target.dispatchEvent(mouseEvent);
/*
document.popupNode = event.target;
//document.getElementById("contentAreaContextMenu").
// openPopup(null, "after_pointer", event.clientX, event.clientY, true, true);
document.getElementById("contentAreaContextMenu").
showPopup( event.target, event.screenX, event.screenY, 'context');
*/
this.contextOpen = true;
} else {
// Mouse Gesture Redox があると動かない
var mouseEvent = document.createEvent("MouseEvents");
mouseEvent.initMouseEvent("contextmenu", true, true, event.view, 1,
event.screenX, event.screenY, event.clientX, event.clientY,
false, false, false, false, 2, null);
this.contextOpen = true;
event.target.dispatchEvent(mouseEvent);
}
}
},
//現在のウインドウを得る
get focusedWindow(){
var win = document.commandDispatcher.focusedWindow;
if (!win || win == window)
win = window.content;
return win;
},
//選択範囲の親要素を得る
getSelectedNodes: function(){
var targetWindow = this.focusedWindow;
var selection = Components.lookupMethod(targetWindow, 'getSelection').call(targetWindow);
try{
var selRange = selection.getRangeAt(0);
}catch(e){
var selRange = selection;
}
if(!selRange)return null;;
var sNode = selRange.startContainer;
var eNode = selRange.endContainer;
return [this.oyaNode(sNode),this.oyaNode(eNode)]
},
//選択範囲の要素が所属するa要素を得る, a要素でないならnull
oyaNode: function(aNode){
var pNode = aNode;
while(pNode && !(pNode instanceof HTMLAnchorElement || pNode instanceof HTMLAreaElement) ){
pNode = pNode.parentNode;
}
return pNode;
},
//選択テキストがリンク要素か
isSelectedIn: function(aNode){
aNode = this.oyaNode(aNode);
if(!aNode) return true;
var selNodes = this.getSelectedNodes();
if(!selNodes) return false;
if(aNode == selNodes[0] && aNode == selNodes[1]) return true;
return false;
},
isParentClickableNode : function(aNode) {
if (!aNode) return null;
var d = aNode.ownerDocument;
try {
var xpathResult = d.evaluate(
'ancestor-or-self::*[((local-name() = "a" or local-name() = "A") and @href) or local-name() = "button" or local-name() = "BUTTON" or ((local-name() = "input" or local-name() = "INPUT") and (@type = "SUBMIT" or @type = "submit" or @type = "BUTTON" or @type = "button" or @type = "IMAGE" or @type = "image"))]',
aNode,
this.NSResolver,
XPathResult.FIRST_ORDERED_NODE_TYPE,
null
);
}
catch(e) {
return null;
}
return xpathResult.singleNodeValue;
},
NSResolver : {
lookupNamespaceURI : function(aPrefix) {
switch (aPrefix) {
case 'xul':
return 'http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul';
case 'html':
case 'xhtml':
return 'http://www.w3.org/1999/xhtml';
case 'xlink':
return 'http://www.w3.org/1999/xlink';
default:
return '';
}
}
},
//Thanks Piro.
// 監視を開始する
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.autoContextmenu",
//'userChrome.XXX'という名前の設定が変更された場合全てで処理を行う
observe : function(aSubject, aTopic, aPrefstring) {
if (aTopic == 'nsPref:changed') {
// 設定が変更された時の処理
autoContextmenu.setMenuCkeck();
}
}
}
}
autoContextmenu.init();
]]></script>
<commandset id="mainCommandSet">
<comand id="cmd_autoContext_toggle"
oncommand="autoContextmenu.toggle();"/>
</commandset>
<keyset id="mainKeyset">
<key id="key_autoContext_toggle" key="a" command="cmd_autoContext_toggle" modifiers="accel,alt"/>
</keyset>
<!-- toolbarbuttonの時は autoCheck .checked, menuitem時は autocheck setAttribute('checked', )になる 変なの-->
<menupopup id="menu_ToolsPopup">
<menuitem id="autoContextmenu"
label="选中自动弹出右键菜单"
type="checkbox"
checked="false"
key="key_autoContext_toggle"
command="cmd_autoContext_toggle"
autocheck="false"
insertbefore="menu_preferences"/>
</menupopup>
</overlay>