-
Notifications
You must be signed in to change notification settings - Fork 123
/
ImageZoomINcontentAreaContextMenu.uc.js
309 lines (275 loc) · 11.1 KB
/
ImageZoomINcontentAreaContextMenu.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
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
// ==UserScript==
// @name ImageZoomINcontentAreaContextMenu.uc.js
// @namespace http://space.geocities.yahoo.co.jp/gl/alice0775
// @description コンテンツエリアコンテキストメニューにImageZoomを追加する
// @include main
// @compatibility Firefox 3.0
// @author Alice0775
// @version 2016/03/30 00:00 style
// @version 2013/04/14 21:00 checking element using Ci.nsIImageLoadingContent instead of HTMLImageElement
// @version 2012/12/08 22:30 Bug 788290 Bug 788293 Remove E4X
// @version LastMod 2009/02/18 14:00 スタイル幅高さが指定されていても実行できるように
// @Note Sub-Script/Overlay Loader v3.0.20mod
// @Note コンテンツエリアのコンテキストメニューにImageZoomを表示. ホイール回転でZoom
// @Note マウスジェスチャ等から使う場合は, ImageZoomINcontentAreaContextMenu.showHotMenu(event.target, event.screenX, event.screenY);
// ==/UserScript==
// @version LastMod 2009/01/10 18:20
var ImageZoomINcontentAreaContextMenu = {
// -- config --
ZOOMFACTOR_IN_MENU : [4.0, 2.0, 1.5, 1.25, 1.0, 0.75, 0.50, 0.25, 0.10],
//ホイール回転後メニューを閉じる msec
DELAY:2000,
// -- config --
ImageZoomMenu : null,
ImageZoomMenuPopup: null,
ImageZoomHotMenuPopup: null,
contexttimer: null,
init: function() {
var overlay = ' \
<overlay xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul" \
xmlns:html="http://www.w3.org/1999/xhtml"> \
<popup id="contentAreaContextMenu"> \
<menu id="context-ImageZoomMenu" \
label="Image Zoom" \
accesskey="I" \
insertbefore="context-sep-copyimage" \
hidden="true"> \
<menupopup id="context-ImageZoomMenupopup" \
onpopupshowing="event.stopPropagation();"> \
<menuitem label="Zoom In" \
accesskey="I;" \
oncommand="ImageZoomUtil.zoomBy(1.25);"/> \
<menuitem label="Zoom Out" \
accesskey="O" \
oncommand="ImageZoomUtil.zoomBy(0.80);"/> \
<menuitem label="Reset" \
accesskey="R" \
oncommand="ImageZoomUtil.reset();"/> \
<menuseparator/> \
</menupopup> \
</menu> \
</popup> \
</overlay>';
overlay = "data:application/vnd.mozilla.xul+xml;charset=utf-8," + encodeURI(overlay);
window.userChrome_js.loadOverlay(overlay, ImageZoomINcontentAreaContextMenu);
},
observe: function(){
window.addEventListener("unload", this, false);
document.getElementById("contentAreaContextMenu").addEventListener("popupshowing", this, false);
this.ImageZoomMenu = document.getElementById("context-ImageZoomMenu");
this.ImageZoomMenu.addEventListener("DOMMouseScroll", this, false);
this.ImageZoomMenu.addEventListener("click", this, false);
this.ImageZoomMenuPopup = document.getElementById("context-ImageZoomMenupopup");
this.ImageZoomMenuPopup.addEventListener("DOMMouseScroll", this, true);
},
uninit: function() {
if (this.contexttimer)
clearTimeout(this.contexttimer);
window.removeEventListener("unload", this, false);
document.getElementById("contentAreaContextMenu").removeEventListener("popupshowing", this, false);
this.ImageZoomMenu.removeEventListener("DOMMouseScroll", this, false);
this.ImageZoomMenu.removeEventListener("click", this, false);
this.ImageZoomMenuPopup.removeEventListener("DOMMouseScroll", this, true);
if (this.ImageZoomHotMenuPopup) {
this.ImageZoomHotMenuPopup.removeEventListener("DOMMouseScroll", this, true);
this.ImageZoomHotMenuPopup.removeEventListener("popuphidden", this, false);
}
},
handleEvent: function(aEvent) {
switch(aEvent.type) {
case 'unload':
this.uninit();
break;
case 'popupshowing':
this.popupshowing();
break;
case 'popuphidden':
this.popuphidden();
break;
case 'DOMMouseScroll':
if (aEvent.originalTarget == this.ImageZoomMenu){
var popup = this.ImageZoomMenu.firstChild;
popup.showPopup();
} else {
var popup = aEvent.originalTarget.parentNode;
}
this.wheelOnImageZoomMenu(popup, aEvent);
break;
case 'click':
this.clickMenu(aEvent);
break;
}
},
popupshowing: function(){
if (this.contexttimer)
clearTimeout(this.contexttimer);
var popup = this.ImageZoomMenuPopup;
if (gContextMenu && gContextMenu.onImage) {
popup.parentNode.removeAttribute('hidden');
this.createImageZoomMenu(popup);
} else {
popup.parentNode.setAttribute('hidden', true);
}
},
popuphidden: function(){
//Reset for hotmenu
ImageZoomUtil._target = null;
ImageZoomUtil._onImage = false;
},
clickMenu: function(event){
if (event.button == 1){
ImageZoomUtil.reset();
if (this.contexttimer)
clearTimeout(this.contexttimer);
document.getElementById("contentAreaContextMenu").hidePopup();
}
},
createImageZoomMenu: function(popup) {
for (var i = popup.childNodes.length - 1; i >= 4; i--) {
popup.removeChild(popup.lastChild);
}
var currentZoom = ImageZoomUtil.getZoomFactor();
var arr =[], flg = false;
for (var i = 0; i < this.ZOOMFACTOR_IN_MENU.length; i++) {
if (currentZoom == this.ZOOMFACTOR_IN_MENU[i])
flg = true;
if (!flg && currentZoom > this.ZOOMFACTOR_IN_MENU[i]) {
arr.push(currentZoom);
flg = true;
}
arr.push(this.ZOOMFACTOR_IN_MENU[i]);
}
if (!flg)
arr.push(currentZoom);
var zoom;
for (var i = 0; i < arr.length; i++) {
zoom = arr[i];
menuitem = document.createElement("menuitem");
menuitem.setAttribute('type', 'checkbox');
menuitem.setAttribute('label', zoom * 100 + '%');
menuitem.setAttribute('class', 'menu-iconic');
menuitem.setAttribute('oncommand', 'ImageZoomUtil.zoomTo(' + zoom + ');');
if (currentZoom == zoom)
menuitem.setAttribute('checked', true);
popup.appendChild(menuitem);
}
return popup;
},
showHotMenu: function(node, x, y){
if (!(node instanceof Ci.nsIImageLoadingContent || node.nodeName.match(/img/i)))
return;
ImageZoomUtil._target = node;
ImageZoomUtil._onImage = true;
var popupset = document.getElementById("mainPopupSet");
if (this.ImageZoomHotMenuPopup) {
this.ImageZoomHotMenuPopup.removeEventListener("DOMMouseScroll", this, true);
this.ImageZoomHotMenuPopup.removeEventListener("popuphidden", this, false);
popupset.removeChild(this.ImageZoomHotMenuPopup);
}
var popup = this.createImageZoomMenu(this.ImageZoomMenuPopup);
popup = popup.cloneNode(true);
popup.removeAttribute("id");
this.ImageZoomHotMenuPopup = popupset.appendChild(popup);
this.ImageZoomHotMenuPopup.addEventListener("DOMMouseScroll", this, true);
this.ImageZoomHotMenuPopup.addEventListener("popuphidden", this, false);
this.ImageZoomHotMenuPopup.showPopup(getBrowser(), x, y, "context","bottomleft","topleft");
},
wheelOnImageZoomMenu: function(popup, aEvent) {
var j =4;
for (var i = 4; i < popup.childNodes.length; i++) {
if (popup.childNodes[i].localName == 'menuseparator')
continue;
if (popup.childNodes[i].hasAttribute("checked") &&
popup.childNodes[i].getAttribute("checked")) {
var j = i;
}
}
var num = ( aEvent.detail > 0) ? j + 1 : j - 1;
/* ループする
if (num >= popup.childNodes.length)
num = 4;
else if (num < 4)
num = popup.childNodes.length - 1;
*/
/* ループしない */
if (num >= popup.childNodes.length ||
num < 4)
return;
if (popup.childNodes[num].localName == 'menuseparator')
num = ( aEvent.detail > 0) ? ++num : --num;
popup.childNodes[j].removeAttribute("checked")
popup.childNodes[num].setAttribute("checked", true)
eval(popup.childNodes[num].getAttribute("oncommand"));
if (this.contexttimer)
clearTimeout(this.contexttimer);
this.contexttimer = setTimeout(function(self){
document.getElementById("contentAreaContextMenu").hidePopup();
}, this.DELAY , this);
}
};
ImageZoomINcontentAreaContextMenu.init();
var ImageZoomUtil = {
_target: null,
_onImage: false,
get target() {
return (gContextMenu) ? gContextMenu.target : this._target;
},
get onImage() {
return (gContextMenu) ? gContextMenu.onImage : this._onImage;
},
getZoomFactor: function() {
var node = this.target;
if (!(node instanceof Ci.nsIImageLoadingContent || node.nodeName.match(/img/i)))
return 1;
if (!node.hasAttribute("originalWidth")) {
node.setAttribute("originalWidth", node.width);
node.setAttribute("originalHeight", node.height);
node.style.removeProperty("width");
node.style.removeProperty("height");
//node.style.width = "";
//node.style.height = "";
}
var w0 = node.getAttribute("originalWidth");
var h0 = node.getAttribute("originalHeight");
var w = node.width;
var h = node.height;
var zoomW = Math.ceil(((w0 > 0) ? w / w0 : 1) * 100) / 100;
//var zoomH = Math.ceil(((h0 > 0) ? h / h0 : 1) * 100) / 100;
return zoomW;
},
zoomTo: function(zoom, node) {
node = node || this.target;
if (!(node instanceof Ci.nsIImageLoadingContent || node.nodeName.match(/img/i)))
return;
if (!node.hasAttribute("originalWidth")) {
node.setAttribute("originalWidth", node.width);
node.setAttribute("originalHeight", node.height);
}
var w = node.getAttribute("originalWidth");
var h = node.getAttribute("originalHeight");
node.style.setProperty("width", w * zoom + "px", "");
node.style.setProperty("height", h * zoom + "px", "");
},
zoomBy: function(zoom, node) {
node = node || this.target;
if (!(node instanceof Ci.nsIImageLoadingContent || node.nodeName.match(/img/i)))
return;
if (!node.hasAttribute("originalWidth")) {
node.setAttribute("originalWidth", node.width);
node.setAttribute("originalHeight", node.height);
}
var w = node.width;
var h = node.height;
node.style.setProperty("width", w * zoom + "px", "");
node.style.setProperty("height", h * zoom + "px", "");
},
reset: function(node) {
node = node || this.target;
if (!(node instanceof Ci.nsIImageLoadingContent || node.nodeName.match(/img/i)))
return;
if (node.hasAttribute("originalWidth")) {
node.style.setProperty("width", node.getAttribute("originalWidth") + "px", "");
node.style.setProperty("height", node.getAttribute("originalHeight") + "px", "");
}
}
}