-
Notifications
You must be signed in to change notification settings - Fork 3
/
CenterImage.user.js
605 lines (563 loc) · 15.8 KB
/
CenterImage.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
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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
// ==UserScript==
// @name Center Image & Video
// @namespace CenterImage
// @author Owyn
// @version 2024.02.01
// @description Improved controls for images & videos opened directly with your browser - hotkeys & resizing & visuals
// @updateURL https://github.com/Owyn/Center_Image/raw/master/CenterImage.user.js
// @downloadURL https://github.com/Owyn/Center_Image/raw/master/CenterImage.user.js
// @supportURL https://github.com/Owyn/Center_Image/issues
// @homepage https://github.com/Owyn/Center_Image
// @run-at document-end
// @noframes
// @grant GM.getValue
// @grant GM.setValue
// @grant GM_addElement
// @grant GM_registerMenuCommand
// @sandbox JavaScript
// @match http://*/*
// @match https://*/*
// @match file:///*/*
// ==/UserScript==
"use strict";
let type = document.contentType.substring(0,document.contentType.indexOf("/"));
let i, is_video;
if (type === "image")
{
is_video = false;
i = document.images[0];
}
else if(type === "video")
{
is_video = true;
i = document.querySelector("video");
}
else
{
return false;
}
if (typeof GM_registerMenuCommand !== "undefined")
{
GM_registerMenuCommand("Center Image Configuration", cfg, "n");
}
const AddElementToPage = typeof GM_addElement === "function" ? GM_addElement : function GM_addElement(node, type, content)
{
let el = document.createElement(type);
el.textContent = content.textContent;
node.appendChild(el);
};
let rescaled = 0;
let iot = 0, iol = 0;
let skip_by = 5;
//let theStyle;
let title;
function makeimage()
{
if(typeof cfg_js !== "string") {setTimeout(function() { makeimage(); }, 11); return false;} // lets wait for async
title = document.title;
if(cfg_bgclr)
{
if(document.head){document.head.innerHTML = "";} // remove FireFox background
}
i.removeAttribute('class');
i.removeAttribute('style'); // chrome
document.body.removeAttribute('style');
if(!is_video)
{
i.addEventListener("click", rescale, true);
i.addEventListener("mousedown", onmousedown, true);
}
else
{
i.volume = cfg_vol;
i.addEventListener("volumechange", onvolumechange, true);
i.controls = true;
i.loop = true;
}
i.addEventListener("auxclick", rescale, true);
window.addEventListener("keydown", onkeydown, true);
window.addEventListener("scroll", onscroll, false);
window.addEventListener("resize", onresize, true);
onVisibilityChange();
}
function onvolumechange()
{
GM.setValue("vid_volume", (i.muted? "0" : i.volume));
}
function onVisibilityChange()
{
if (document.visibilityState === 'visible')
{
//console.log('visible');
window.removeEventListener('visibilitychange', onVisibilityChange);
autoresize();
}
}
window.addEventListener("visibilitychange", onVisibilityChange);
var scrolledY, scrolledX;
function onscroll()
{
scrolledY = window.pageYOffset;
scrolledX = window.pageXOffset;
}
var FireFox = ((navigator.userAgent.indexOf('Firefox') != -1) ? true : false);
function onresize() // doesn't let image change back to "fit to window" in chrome & in both helps keep scroll progress
{
if(rescaled != 1)
{
let wasFilled = (rescaled === 2 ? true : false);
rescaled = 1;
let chromeplsstopX, chromeplsstopY;
if(!FireFox){ chromeplsstopX = window.pageXOffset; chromeplsstopY = window.pageYOffset;}
rescale(null, wasFilled);
if(!FireFox){ window.scrollTo(chromeplsstopX,chromeplsstopY);}
}
}
function changeCursor()
{
if(i.scrollHeight > window.innerHeight) // image pushing out-of-screen at top fix
{
i.classList.add("center_H");
//theStyle.sheet.rules[0].style.margin = "0px auto";
}
else
{
i.classList.add("center");
//theStyle.sheet.rules[0].style.margin = "auto";
}
if(is_video) return;
//i.style.margin = "auto";
if(rescaled === 0) // original
{
if((orgImgWidth == window.innerWidth && orgImgHeight <= window.innerHeight) || (orgImgWidth <= window.innerWidth && orgImgHeight == window.innerHeight)) // perfect fit on one side, can't resize
{
i.style.cursor = "";
}
else if (orgImgWidth > window.innerWidth || orgImgHeight > window.innerHeight)
{
i.style.cursor = "zoom-out";
}
else
{
i.style.cursor = "zoom-in";
}
}
else if(rescaled === 2) // fill
{
if(orgImgWidth == window.innerWidth && orgImgHeight == window.innerHeight) // perfect fit, can't resize
{
i.style.cursor = "";
}
else if (orgImgWidth > i.width)
{
i.style.cursor = "zoom-in";
}
else
{
i.style.cursor = "zoom-out";
}
}
else // if(rescaled === 1) // fit
{
if((orgImgWidth == window.innerWidth && orgImgHeight <= window.innerHeight) || (orgImgWidth <= window.innerWidth && orgImgHeight == window.innerHeight)) // perfect fit on one side, can't resize
{
i.style.cursor = "";
}
else if (orgImgWidth > i.width)
{
i.style.cursor = "zoom-in";
}
else
{
i.style.cursor = "zoom-out";
}
}
}
function onmousedown(event)
{
if(i.offsetLeft > 0){iol = i.offsetLeft;}
if(i.offsetTop > 0){iot = i.offsetTop;}
if(event.which === 2) // middle mouse Chrome fix
{
event.preventDefault();
event.stopPropagation();
return;
}
}
function rescale(event, fill)
{
let ex,ey;
if(event) // mouse click
{
if (typeof event.y === "undefined") // Firefox
{
ex = event.clientX;
ey = event.clientY;
}
else
{
ex = event.x;
ey = event.y;
}
ex -= iol;
ey -= iot;
if(event.which === 2) // middle mouse
{
fill = true;
}
else if(event.which === 3) // right mouse
{
return;
}
event.preventDefault();
event.stopPropagation();
}
//document.body.style.overflowX = '';
//document.body.style.overflowY = '';
document.body.removeAttribute('style');
let scrollMax_Y = window.scrollMaxY || ((document.body.scrollHeight || document.documentElement.scrollHeight)- document.documentElement.clientHeight);
let scrollMax_X = window.scrollMaxX || ((document.body.scrollWidth || document.documentElement.scrollWidth)- document.documentElement.clientWidth);
let scrollProgressY = scrolledY / scrollMax_Y;
let scrollProgressX = scrolledX / scrollMax_X;
let unFilling = false;
let sidesCMP;
i.removeAttribute('class'); // clear classes
if(fill)
{
if(rescaled === 2) // to original
{
rescaled = 0;
i.classList.add("org");
//theStyle.sheet.rules[0].style.width = orgImgWidth + "px";
//theStyle.sheet.rules[0].style.height = orgImgHeight + "px";
}
else // fill
{
sidesCMP = (orgImgWidth / orgImgHeight) < (window.innerWidth / window.innerHeight);
rescaled = 2;
}
}
else
{
if(rescaled != 0) // to original
{
if(rescaled === 2) {unFilling = true;}
rescaled = 0;
i.classList.add("org");
//theStyle.sheet.rules[0].style.width = orgImgWidth + "px";
//theStyle.sheet.rules[0].style.height = orgImgHeight + "px";
}
else // fit
{
sidesCMP = (orgImgWidth / orgImgHeight) > (window.innerWidth / window.innerHeight);
rescaled = 1;
}
}
if(rescaled != 0)
{
if(sidesCMP)
{
i.classList.add("fill_H");
//theStyle.sheet.rules[0].style.width = "100%";
//theStyle.sheet.rules[0].style.height = "auto";
document.body.style.overflowX = 'hidden'; // we don't need unscrollable scrollbars if they appear
}
else
{
i.classList.add("fill_V");
//theStyle.sheet.rules[0].style.height = "100%";
//theStyle.sheet.rules[0].style.width = "auto";
document.body.style.overflowY = 'hidden'; // we don't need unscrollable scrollbars if they appear
}
}
changeCursor();
if(event && (!unFilling && (!fill || (fill && (scrollMax_Y <= 0 && scrollMax_X <= 0))))) // left mouse click (fill-click with no scrollbars and not left click after middle click - else preserve scroll percentage)
{
let scale = Math.min((window.innerWidth / i.width), (window.innerHeight / i.height));
window.scrollTo((ex / scale) - (window.innerWidth / 2), (ey / scale) - (window.innerHeight / 2));
}
else // keep percentage scroll progress for KB hotkeys
{
scrollMax_Y = window.scrollMaxY || ((document.body.scrollHeight || document.documentElement.scrollHeight)- document.documentElement.clientHeight);
scrollMax_X = window.scrollMaxX || ((document.body.scrollWidth || document.documentElement.scrollWidth)- document.documentElement.clientWidth);
window.scrollTo(Math.round(scrollProgressX * scrollMax_X), Math.round(scrollProgressY * scrollMax_Y));
scrolledY = window.pageYOffset;
scrolledX = window.pageXOffset;
}
}
var orgImgWidth;
var orgImgHeight;
var ARC = 0;
function autoresize()
{
if(!((!is_video && i.naturalHeight) || i.videoHeight))
{
ARC++;
if(ARC < 500)
{
setTimeout(autoresize, 10);
}
else
{
console.warn("Center Image: Gave up waiting for a working image, it is broken");
}
return;
}
if(is_video) // chrome & FF don't show video resolution, + chrome doesn't show video name
{
if(!FireFox)
{
title = i.src.substr(i.src.lastIndexOf("/")+1);
if(title.indexOf("?") != -1)
{
title = title.substr(0, title.indexOf("?"));
}
}
title = title + " (" + i.videoWidth + "x" + i.videoHeight + ")";
//title = title + " (" + i.naturalWidth + "x" + i.naturalHeight + ")"; // for images
//title = decodeURIComponent(title); // browser already decoded it
}
else if(FireFox)
{
let f = title.indexOf(" — Scaled (");
if(f !== -1)
{
title = title.substring(0,f);
}
}
document.title = title;
orgImgWidth = Math.round((is_video ? i.videoWidth : i.naturalWidth) / window.devicePixelRatio);
orgImgHeight = Math.round((is_video ? i.videoHeight : i.naturalHeight) / window.devicePixelRatio);
let css = (is_video? "video" : "img") +`{position: absolute; top: 0; right: 0; bottom: 0; left: 0; background-color: `+cfg_bgclr+` !important; outline: none; max-width: unset; max-height: unset;}
body {margin: 0px !important; background-color: `+cfg_bgclr+` !important;}
.center {margin: auto !important;}
.center_H {margin: 0px auto !important;}
.fill_H {width: 100% !important; height: auto !important;}
.fill_V {width: auto !important; height: 100% !important;}
.org {width: `+ orgImgWidth + `px !important; height: `+ orgImgHeight + `px !important; }`;
AddElementToPage(document.documentElement, 'style', {textContent: css});
let InitRescale = false; // directly opened image is already fit to window if it is bigger by the browser
if(cfg_fitWH && orgImgHeight > window.innerHeight && orgImgWidth > window.innerWidth) // both scrollbars
{
InitRescale = true;
}
else if(cfg_fitB && (orgImgHeight > window.innerHeight || orgImgWidth > window.innerWidth)) // one scrollbar
{
InitRescale = true;
}
else if(cfg_fitS && orgImgHeight <= window.innerHeight && orgImgWidth <= window.innerWidth) // no scrollbars
{
InitRescale = true;
}
if(InitRescale)
{
rescale(null, cfg_fill ? true : false);
}
else
{
i.classList.add("org");
changeCursor();
}
if(cfg_js){eval(cfg_js);}
}
// hotkeys
if (typeof KeyEvent === "undefined")
{
var KeyEvent = {
DOM_VK_SPACE: 32,
DOM_VK_LEFT: 37,
DOM_VK_UP: 38,
DOM_VK_RIGHT: 39,
DOM_VK_DOWN: 40,
DOM_VK_A: 65,
DOM_VK_D: 68,
DOM_VK_P: 80,
DOM_VK_Q: 81,
DOM_VK_S: 83,
DOM_VK_W: 87,
DOM_VK_NUMPAD2: 98,
DOM_VK_NUMPAD4: 100,
DOM_VK_NUMPAD5: 101,
DOM_VK_NUMPAD6: 102,
DOM_VK_NUMPAD8: 104,
DOM_VK_F5: 116,
DOM_VK_TAB: 9,
DOM_VK_ENTER: 13
};
}
function cancelEvent(a)
{
a = a ? a : window.event;
if (a.stopPropagation)
{
a.stopPropagation();
}
if (a.preventDefault)
{
a.preventDefault();
}
a.cancelBubble = true;
a.cancel = true;
a.returnValue = false;
return false;
}
function scroll_space(a, b)
{
var by = Math.round((b ? window.innerHeight : window.innerWidth) * 0.50 * (a ? -1 : 1));
if(!b)
{
window.scrollBy(0, by);
}
else
{
window.scrollBy(by, 0);
}
}
function onkeydown (b)
{
var a = (window.event) ? b.keyCode : b.which;
if (a != KeyEvent.DOM_VK_SPACE && (b.altKey || b.ctrlKey || b.metaKey))
{
return;
}
var by = Math.round(window.innerHeight * 0.10);
switch (a)
{
case KeyEvent.DOM_VK_RIGHT:
case KeyEvent.DOM_VK_D:
case KeyEvent.DOM_VK_NUMPAD6:
if(!is_video)
{
window.scrollBy(by, 0);
}
else
{
i.currentTime += skip_by;
}
cancelEvent(b);
break;
case KeyEvent.DOM_VK_LEFT:
case KeyEvent.DOM_VK_A:
case KeyEvent.DOM_VK_NUMPAD4:
if(!is_video)
{
window.scrollBy(by * -1, 0);
}
else
{
i.currentTime -= skip_by;
}
cancelEvent(b);
break;
case KeyEvent.DOM_VK_W:
case KeyEvent.DOM_VK_NUMPAD8:
window.scrollBy(0, by * -1);
cancelEvent(b);
break;
case KeyEvent.DOM_VK_S:
case KeyEvent.DOM_VK_NUMPAD2:
window.scrollBy(0, by);
cancelEvent(b);
break;
case KeyEvent.DOM_VK_SPACE:
if(!is_video)
{
scroll_space(b.shiftKey, b.ctrlKey);
}
else if(i.paused || i.ended)
{
i.play();
}
else
{
i.pause();
}
cancelEvent(b);
break;
case KeyEvent.DOM_VK_TAB:
case KeyEvent.DOM_VK_ENTER:
rescale(null, true);
cancelEvent(b);
break;
case KeyEvent.DOM_VK_Q:
case KeyEvent.DOM_VK_NUMPAD5:
rescale(null, false);
cancelEvent(b);
break;
case KeyEvent.DOM_VK_P:
cfg();
cancelEvent(b);
}
}
var cfg_bgclr = "grey";
var cfg_fitWH = true;
var cfg_fitB = false;
var cfg_fitS = true;
var cfg_fill = false;
var cfg_js;
var cfg_vol = "0.5";
function cfg(){}
if (typeof GM !== "undefined" && typeof GM.getValue !== "undefined")
{
async function loadCfg()
{
cfg_bgclr = await GM.getValue("bgColor", "grey");
cfg_fitWH = await GM.getValue("fitWH", true);
cfg_fitB = await GM.getValue("fitB", false);
cfg_fitS = await GM.getValue("fitS", true);
cfg_fill = await GM.getValue("fill", false);
cfg_js = await GM.getValue("js", "");
cfg_vol = await GM.getValue("vid_volume", "0.5");
}
loadCfg();
function $(id) {return document.getElementById(id);}
cfg = function ()
{
function saveCfg()
{
GM.setValue("bgColor", $("ci_cfg_2_bgclr").value);
GM.setValue("fitWH", $("ci_cfg_3_fitWH").checked);
GM.setValue("fitB", $("ci_cfg_4_fitB").checked);
GM.setValue("fitS", $("ci_cfg_5_fitS").checked);
GM.setValue("fill", $("ci_cfg_7_fill").checked);
GM.setValue("js", $("ci_cfg_6_js").value);
alert("Configuration Saved");
if($("ci_cfg_2_bgclr").value){document.body.bgColor = $("ci_cfg_2_bgclr").value;}else{document.body.removeAttribute("bgColor");}
}
if(i){
i.removeEventListener("click", rescale, true);
i.removeEventListener("auxclick", rescale, true);
i.removeEventListener("mousedown", onmousedown, true);
}
window.removeEventListener("keydown", onkeydown, true);
if(document.head){document.head.innerHTML = "";}
document.body.innerHTML = "";
var div = document.createElement("div");
div.style = "margin: auto; width: fit-content; height: fit-content; border: 1px solid black; color: black; background: silver; position: absolute; top: 0; right: 0; bottom: 0; left: 0;";
div.innerHTML = "<b><center>Configuration</center></b>"
+ "<br><input id='ci_cfg_2_bgclr' type='text' size='6'> Background color (empty = default)"
+ "<br><br>Fit to window images:" + " ( Fill to window instead <input id='ci_cfg_7_fill' type='checkbox'> )"
+ "<br><br><input id='ci_cfg_3_fitWH' type='checkbox'> Larger than window both vertically and horizontally"
+ "<br><br><input id='ci_cfg_4_fitB' type='checkbox'> Larger than window either vertically or horizontally"
+ "<br><br><input id='ci_cfg_5_fitS' type='checkbox'> Smaller than window"
+ "<br><br><center>Custom JS Action:<textarea id='ci_cfg_6_js' style='margin: 0px; width: 400px; height: 50px;'></textarea>"
+ "<br><input id='ci_cfg_save' type='button' value='Save configuration'></center>";
document.body.appendChild(div);
$("ci_cfg_2_bgclr").value = cfg_bgclr;
$("ci_cfg_3_fitWH").checked = cfg_fitWH;
$("ci_cfg_4_fitB").checked = cfg_fitB;
$("ci_cfg_5_fitS").checked = cfg_fitS;
$("ci_cfg_7_fill").checked = cfg_fill;
$("ci_cfg_6_js").value = cfg_js;
$("ci_cfg_save").addEventListener("click", saveCfg, true);
}
}
else
{
cfg_js = "";
cfg = function ()
{
alert("Sorry, Chrome userscripts in native mode can't have configurations! You need to install TamperMonkey userscript manager extension. (it's very good)");
}
}
makeimage();