-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimage_zoom.user.js
More file actions
219 lines (201 loc) · 8.13 KB
/
Copy pathimage_zoom.user.js
File metadata and controls
219 lines (201 loc) · 8.13 KB
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
// ==UserScript==
// @name image zoom
// @version 0.1.2
// @namespace roger21.free.fr
// @description Basic image zoom functionality as a user-script for your web browser.
// @icon data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAAgAQAAAABbAUdZAAAALElEQVR42mP4DwQMaMQHg8P8EOLz%2F%2F%2FnIcR%2FOPEZpARGUEH2w2EefgiBxS0ARNpzyS9f0t0AAAAASUVORK5CYII%3D
// @include *
// @updateURL https://raw.githubusercontent.com/roger21/image-zoom/master/image_zoom.user.js
// @installURL https://raw.githubusercontent.com/roger21/image-zoom/master/image_zoom.user.js
// @downloadURL https://raw.githubusercontent.com/roger21/image-zoom/master/image_zoom.user.js
// @supportURL https://github.com/roger21/image-zoom
// @homepageURL https://github.com/roger21/image-zoom
// @author roger21
// @grant none
// ==/UserScript==
/*
Copyright © 2020-2021, 2023 roger21@free.fr
This program is free software: you can redistribute it and/or modify it under the
terms of the GNU Affero 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 Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License along
with this program. If not, see <https://www.gnu.org/licenses/agpl.txt>.
*/
// historique :
// 0.1.2 (12/06/2023) :
// - added a mutation observer to catch dynamic images
// 0.1.1 (28/01/2021) :
// - updated page height detection for ff 85
// 0.1.0 (18/06/2020) :
// - seems good enougth for publication
var zoom_factor = 1.25;
var zoom_direction = "down";
var zoom_follows_cursor_x = true;
var zoom_follows_cursor_y = true;
let image_observer = new MutationObserver(image_add_events);
image_observer.observe(document, {
attributes: false,
childList: true,
characterData: false,
subtree: true,
});
function image_add_events() {
var images = document.querySelectorAll("img:not([image_zoom_events_added])");
for(let l_image of images) {
l_image.addEventListener("mousedown", image_zoom_down, true);
l_image.addEventListener("wheel", image_zoom_wheel, true);
l_image.setAttribute("image_zoom_events_added", "image_zoom_events_added");
}
}
image_add_events();
function get_offset(p_element) {
let _x = 0;
let _y = 0;
while(p_element && !isNaN(p_element.offsetLeft) && !isNaN(p_element.offsetTop)) {
_x += p_element.offsetLeft - p_element.scrollLeft;
_y += p_element.offsetTop - p_element.scrollTop;
p_element = p_element.offsetParent;
}
return {
top: _y,
left: _x
};
}
function disable_contextmenu(p_event) {
p_event.preventDefault();
p_event.stopImmediatePropagation();
p_event.stopPropagation();
this.removeEventListener("contextmenu", disable_contextmenu, true);
}
function disable_click(p_event) {
p_event.preventDefault();
p_event.stopImmediatePropagation();
p_event.stopPropagation();
this.removeEventListener("click", disable_click, true);
}
function disable_mouseup(p_event) {
p_event.preventDefault();
p_event.stopImmediatePropagation();
p_event.stopPropagation();
this.removeEventListener("mouseup", disable_mouseup, true);
}
function image_zoom_down(p_event) {
if(p_event.buttons === 3) {
document.addEventListener("contextmenu", disable_contextmenu, true);
document.addEventListener("click", disable_click, true);
document.addEventListener("mouseup", disable_mouseup, true);
p_event.preventDefault();
p_event.stopImmediatePropagation();
p_event.stopPropagation();
if(this.dataset.imagezoom === "true") {
this.width = this.dataset.width;
this.height = this.dataset.height;
delete this.dataset.imagezoom;
window.scrollTo(this.dataset.x, this.dataset.y);
if(this.dataset.margintop === "true") {
this.style.marginTop = "";
delete this.dataset.margintop;
}
} else {
let width = this.width;
let height = this.height;
this.dataset.width = width;
this.dataset.height = height;
this.dataset.x = parseInt(window.scrollX, 10);
this.dataset.y = parseInt(window.scrollY, 10);
this.style.maxWidth = "none";
this.style.maxHeight = "none";
this.style.width = "";
this.style.height = "";
this.dataset.imagezoom = "true";
let ratio = width / height;
let window_width = document.documentElement.clientWidth;
let window_height = window.innerHeight;
if(document.documentElement.clientHeight && document.documentElement.clientHeight !== 16) {
window_height = document.documentElement.clientHeight;
}
let window_ratio = window_width / window_height;
if(window_ratio < ratio) {
this.width = window_width;
this.height = parseInt(window_width / ratio + .5, 10);
} else {
this.width = parseInt(window_height * ratio + .5, 10);
this.height = window_height;
}
this.scrollIntoView();
}
}
}
function image_zoom_wheel(p_event) {
if(p_event.buttons === 2 && p_event.deltaY !== 0) {
document.addEventListener("contextmenu", disable_contextmenu, true);
document.addEventListener("click", disable_click, true);
document.addEventListener("mouseup", disable_mouseup, true);
p_event.preventDefault();
p_event.stopImmediatePropagation();
p_event.stopPropagation();
let width = this.width;
let height = this.height;
let window_width = document.documentElement.clientWidth;
let window_height = window.innerHeight;
if(document.documentElement.clientHeight && document.documentElement.clientHeight !== 16) {
window_height = document.documentElement.clientHeight;
}
let window_scroll_x = window.scrollX;
let window_scroll_y = window.scrollY;
let offset = get_offset(this);
let offset_x = p_event.offsetX;
let offset_y = p_event.offsetY;
let new_offset_x;
let new_offset_y;
if(typeof this.dataset.imagezoom === "undefined") {
this.dataset.width = width;
this.dataset.height = height;
this.dataset.x = parseInt(window.scrollX, 10);
this.dataset.y = parseInt(window.scrollY, 10);
if(this.classList.contains("overflowingVertical")) {
let ratio = width / height;
this.dataset.width = parseInt(window_height * ratio + .5, 10);
this.dataset.height = window_height;
this.dataset.x = 0;
this.dataset.y = 0;
}
this.style.maxWidth = "none";
this.style.maxHeight = "none";
this.style.width = "";
this.style.height = "";
this.dataset.imagezoom = "true";
}
if((zoom_direction === "down" && p_event.deltaY < 0) || (zoom_direction !== "down" && p_event.deltaY > 0)) {
this.width = parseInt(width / zoom_factor + .5, 10);
this.height = parseInt(height / zoom_factor + .5, 10);
new_offset_x = parseInt(offset_x / zoom_factor + .5, 10);
new_offset_y = parseInt(offset_y / zoom_factor + .5, 10);
} else {
this.width = parseInt(width * zoom_factor + .5, 10);
this.height = parseInt(height * zoom_factor + .5, 10);
new_offset_x = parseInt(offset_x * zoom_factor + .5, 10);
new_offset_y = parseInt(offset_y * zoom_factor + .5, 10);
}
if(this.offsetTop < 0) {
this.style.marginTop = "0";
this.dataset.margintop = "true";
} else if(this.height < window_height &&
(this.dataset.margintop === "true" || this.classList.contains("overflowingVertical"))) {
this.style.marginTop = "";
this.classList.remove("overflowingVertical");
delete this.dataset.margintop;
}
if(zoom_follows_cursor_x || zoom_follows_cursor_y) {
let new_offset = get_offset(this);
let variation_x = new_offset.left + new_offset_x - (offset.left + offset_x);
variation_x = zoom_follows_cursor_x ? variation_x : 0;
let variation_y = new_offset.top + new_offset_y - (offset.top + offset_y);
variation_y = zoom_follows_cursor_y ? variation_y : 0;
window.scrollTo(window.scrollX + variation_x, window.scrollY + variation_y);
}
}
}