-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathdrag_to_resize.user.js
301 lines (243 loc) · 8.62 KB
/
drag_to_resize.user.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
// ==UserScript==
// @name Drag to Resize
// @namespace http://kylej.name/
// @description Drag to resize images, based on code in the RES.
// @author Kabaka
// @version 1.3
// @source https://github.com/lenethx/drag-to-resize
// @include *
// @exclude http://www.chess.com/*
// @exclude http://chess.com/*
// @exclude https://neocities.org/*
// ==/UserScript==
/*
* Drag to Resize - Drag images to resize them no matter where you are.
*
* The image resizing code was extracted from honestbleeps's
* ([email protected]) Reddit Enhancement Suite, a GPL
* Greasemonkey script. The idea was, as far as I know, all his. What
* I've done is duplicated that feature in this script and started
* adding on things to make it useful in different contexts.
*
* Because it now runs everywhere, it will likely break some web
* sites. And it definitely opens up doors for some silliness such as
* making images hilariously gigantic. If this script causes you to
* lose data, money, or time, don't hold me responsible!
*
*
* Instructions:
*
* To resize an image, hold the left mouse button and drag. Down and to the
* right will expand. Up and to the left will shrink. Images aligned to the
* right will expand in an unusual way. Sorry.
*
* To reset an image to original size, right-click it.
*
* To make an image fit the screen (by height), double-click.
*
* To drag an image without resizing (as if the script were not installed),
* hold control (or command on Mac) and drag.
*
*
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
var imageData = Array();
/*
* Find all img elements on the page and feed them to makeImageZoomable().
* Also, record the image's original width in imageData[] in case the user
* wants to restore size later.
*/
function findAllImages() {
var imgs = document.getElementsByTagName('img');
for (let i = 0; i < imgs.length; ++i) {
feedSingleImage(imgs[i], i);
}
}
function feedSingleImage(image, i=imageData.length) {
// We will populate this as the user interacts with the image, if they
// do at all.
imageData[i] = {};
imageData[i].resized = false;
image.dragToResizeId = i;
makeImageZoomable(image);
}
function findNewImages() {
//Add observer to feed new images to makeImageZoomable
var mutationObserver = new MutationObserver(function(mutations) {
mutations.forEach(function(mutation) {
if (mutation.type=='childList'){
mutation.addedNodes.forEach(function(newnode){
if (newnode.nodeName=='IMG'){
feedSingleImage(newnode);
} else if (newnode.firstChild){
newnode.querySelectorAll('img').forEach(function(childimg){
feedSingleImage(childimg);
});
}
});
}
});
});
mutationObserver.observe(document.documentElement, {
attributes: true,
characterData: true,
childList: true,
subtree: true,
attributeOldValue: true,
characterDataOldValue: true
});
}
/*
* Calculate the drag size for the event. This is taken directly from
* honestbleeps's Reddit Enhancement Suite.
*
* @param mousedown e or mousemove event.
* @return Size for image resizing.
*/
function getDragSize(e) {
return (p = Math.pow)(p(e.clientX - (rc = e.target.getBoundingClientRect()).left, 2) + p(e.clientY - rc.top, 2), .5);
}
/*
* Get the viewport's vertical size. This should work in most browsers. We'll
* use this when making images fit the screen by height.
*
* @return int Viewport size.
*/
function getHeight() {
return window.innerHeight ||
document.documentElement.clientHeight ||
document.body.clientHeight;
}
/*
* Get the imageData entry for the given event's target.
*
* @return Object
*/
function getImageData(e) {
return imageData[e.target.dragToResizeId];
}
/*
* Try to stop propagation of an event.
*
* @param event e Event to stop.
* @return false
*/
function stopEvent(e) {
e.preventDefault();
e.returnValue = false;
e.stopPropagation();
return false;
}
/*
* Set up events for the given img element to make it zoomable via
* drag to zoom. Most of this is taken directly from honestbleeps's
* Reddit Enhancement Suite. Event functions are currently written
* inline. For readability, I may move them. But the code is small
* enough that I don't yet care.
*
* @param Object imgElement Image element.
*/
function makeImageZoomable(imgElement) {
dragTargetData = {};
imgElement.addEventListener('mousedown', function(e) {
/*
* This is so we can support the command key on Mac. The combination of OS
* and browser changes how the key is passed to JavaScript. So we're just
* going to catch all of them. This means we'll also be catching meta keys
* for other systems. Oh well! Patches are welcome.
*/
if (e.ctrlKey != 0 || (e.metaKey != null && e.metaKey != 0))
return true;
if (e.button !== 0)
return false;
// Store some data about the image in case we want to restore size later.
var myImageData = getImageData(e);
if (myImageData.position == null) {
myImageData.zIndex = e.target.style.zIndex;
myImageData.width = e.target.style.width;
myImageData.height = e.target.style.height;
myImageData.position = e.target.style.position;
}
dragTargetData.image_width = e.target.width;
dragTargetData.dragSize = getDragSize(e);
e.preventDefault();
}, true);
// Reset image to original size and unlock for future events.
imgElement.addEventListener('contextmenu', function(e) {
var myImageData = getImageData(e);
if (!myImageData.resized)
return true;
myImageData.resized = false;
e.target.style.zIndex = myImageData.zIndex;
e.target.style.maxWidth = e.target.style.width = myImageData.width;
e.target.style.maxHeight = e.target.style.height = myImageData.height;
e.target.style.position = myImageData.position;
return stopEvent(e);
}, true);
// Expand image to fill screen.
imgElement.addEventListener('dblclick', function(e) {
if (e.ctrlKey != 0 || (e.metaKey != null && e.metaKey != 0))
return true;
var myImageData = getImageData(e);
if (myImageData.resized) {
// If we've already resized it, we have to set this back to the
// original value. Otherwise, the max size image will keep the
// original width. Dunno why!
e.target.style.maxWidth = e.target.style.width = myImageData.width;
}
e.target.style.position = "fixed";
e.target.style.zIndex = 1000;
e.target.style.top = 0;
e.target.style.left = 0;
e.target.style.maxWidth = e.target.style.width = "auto";
e.target.style.maxHeight = e.target.style.height = getHeight() + "px";
myImageData.resized = true;
// Most browsers will want to save the image or something. Prevent that.
return stopEvent(e);
}, true);
imgElement.addEventListener('mousemove', function(e) {
if (!dragTargetData.dragSize)
return true;
e.target.style.maxWidth =
e.target.style.width =
((getDragSize(e)) * dragTargetData.image_width / dragTargetData.dragSize) + "px";
e.target.style.maxHeight = '';
e.target.style.height = 'auto';
e.target.style.zIndex = 1000; // Make sure the image is on top.
if (e.target.style.position == '') {
e.target.style.position = 'relative';
}
getImageData(e).resized = true;
}, false);
imgElement.addEventListener('mouseout', function(e) {
dragTargetData.dragSize = false;
return !getImageData(e).resized;
}, false);
imgElement.addEventListener('mouseup', function(e) {
dragTargetData.dragSize = false;
return !getImageData(e).resized;
}, true);
imgElement.addEventListener('click', function(e) {
if (e.ctrlKey != 0 || (e.metaKey != null && e.metaKey != 0))
return true;
dragTargetData.dragSize = false;
if (getImageData(e).resized === false)
return true;
return stopEvent(e);
}, false);
}
findAllImages();
findNewImages();
document.addEventListener('dragstart', function() {return false}, false);