-
-
Notifications
You must be signed in to change notification settings - Fork 45
/
smoothScroll.js
976 lines (961 loc) · 29.6 KB
/
smoothScroll.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
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
import { BADGES } from "./helpers/badge.js";
export default {
icon: '<i class="fa-solid fa-computer-mouse fa-lg"></i>',
name: {
en: "Super smooth scroll",
vi: "Cuộn chuột siêu mượt",
},
description: {
en: `Scroll smoothly on all websites with your mouse and keyboard.<br/>
<ul>
<li>Suggested if you use mouse (turn off if use touchpad)</li>
<li>Click to Disable/Enable for current website</li>
<li>Support middle click to scroll</li>
</ul><br/>`,
vi: `Cuộn chuột siêu mượt cho mọi trang web.<br/>
<ul>
<li>Khuyên dùng với chuột (tắt nếu dùng touchpad)</li>
<li>Bấm để Tắt/Mở cho trang web hiện tại</li>
<li>Hỗ trợ bấm chuột giữa để cuộn trang</li>
</ul><br/>`,
video: "https://www.smoothscroll.net/mac/img/vid/Demo-Mac-720p.mp4",
},
badges: [BADGES.hot],
buttons: [
{
icon: '<i class="fa-solid fa-display"></i>',
name: {
vi: "Phần mềm cho windows/mac",
en: "App for windows/mac",
},
onClick: () => window.open("https://www.smoothscroll.net/", "_blank"),
},
],
changeLogs: {
"2024-07-11": "click to disable/enable for current site",
"2024-05-26": "init",
},
popupScript: {
onEnable: () => setEnableForAllTab(true),
onDisable: () => setEnableForAllTab(false),
},
contentScript: {
onDocumentStart_: enableSmoothScroll,
onClick_: () => {
if (typeof window.ufs_smoothScroll_disable === "function") {
window.ufs_smoothScroll_disable();
localStorage.setItem("ufs_smoothScroll_disable", 1);
if (window === window.top)
alert("DISABLED smooth scroll for this site: " + location.hostname);
} else {
localStorage.setItem("ufs_smoothScroll_disable", 0);
enableSmoothScroll();
if (window === window.top)
alert("ENABLED smooth scroll for this site: " + location.hostname);
}
},
},
};
async function setEnableForAllTab(enable) {
const { t } = await import("../popup/helpers/lang.js");
const { runScriptInTab, getAllTabs: getAllAvailableTabs } = await import(
"./helpers/utils.js"
);
const tabs = await getAllAvailableTabs();
let count = 0;
for (let tab of tabs) {
try {
runScriptInTab({
target: {
tabId: tab.id,
allFrames: true,
},
func: enable
? enableSmoothScroll
: () => {
window.ufs_smoothScroll_disable?.();
},
world: "ISOLATED",
});
count++;
} catch (e) {
console.error(e);
}
}
let text = enable
? { vi: "Bật", en: "Enabled" }
: { vi: "Tắt", en: "Disabled" };
if (count)
await Swal.fire({
icon: "success",
title: t({
vi: "Đã " + text.vi + " Cuộn chuột Siêu mượt",
en: "Super smooth scroll " + text.en,
}),
html: t({
vi:
`Đã tự ${text.vi} cho ${count} tab đang mở<br/><br/>` +
"Bạn có thể dùng ngay không cần tải lại trang.",
en:
`${text.en} smooth scroll for ${count} opening tabs<br/><br/>` +
"Dont need to reload websites.",
}),
});
}
// TODO:
// [ ] setting page
// [ ] horizontal scroll
// [x] fix window.scrollTo with behavior: "smooth"
// [x] check excluded
// https://chromewebstore.google.com/detail/smoothscroll/nbokbjkabcmbfdlbddjidfmibcpneigj
export function enableSmoothScroll() {
if (localStorage.getItem("ufs_smoothScroll_disable") == 1) return;
// =======================================================================
// ============================ sscr.js ==================================
// =======================================================================
// Scroll Variables (tweakable)
let defaultOptions = {
// Scrolling Core
frameRate: 150, // [Hz]
animationTime: 400, // [px]
stepSize: 100, // [px]
// Pulse (less tweakable)
// ratio of 'tail' to 'acceleration'
pulseAlgorithm: true,
pulseScale: 4,
pulseNormalize: 1,
// Acceleration
accelerationDelta: 50, // 20
accelerationMax: 3, // 1
// Keyboard Settings
keyboardSupport: true, // option
arrowScroll: 50, // [px]
// Other
touchpadSupport: true,
fixedBackground: true,
reverseDirection: false, // for linux users mostly
excluded: "",
};
let options = defaultOptions;
// Other Variables
let isExcluded = false;
let isFrame = false;
let direction = {
x: 0,
y: 0,
};
let initDone = false;
let root = document.documentElement;
let activeElement;
let observer;
let deltaBuffer = [];
let deltaBufferTimer;
let isMac = /^Mac/.test(navigator.platform);
let isWin = /Windows/i.test(navigator.userAgent);
let isLinux = /Linux/i.test(navigator.userAgent);
let key = {
left: 37,
up: 38,
right: 39,
down: 40,
spacebar: 32,
pageup: 33,
pagedown: 34,
end: 35,
home: 36,
};
let arrowKeys = {
37: 1,
38: 1,
39: 1,
40: 1,
};
/***********************************************
* SETTINGS
***********************************************/
// chrome.storage.sync.get(defaultOptions, function (syncedOptions) {
// options = syncedOptions;
// // it seems that sometimes settings come late
// // and we need to test again for excluded pages
// initTest();
// });
initTest();
/***********************************************
* INITIALIZE
***********************************************/
/**
* Tests if smooth scrolling is allowed. Shuts down everything if not.
*/
function initTest() {
// disable keyboard support if the user said so
if (!options.keyboardSupport) {
removeEvent("keydown", keydown);
}
// disable everything if the page is blacklisted
if (options.excluded) {
let domains = options.excluded.split(/[,\n] ?/);
domains.push("mail.google.com"); // exclude Gmail for now
domains.push("play.google.com/music"); // problem with Polymer elements
for (let i = domains.length; i--; ) {
if (document.URL.indexOf(domains[i]) > -1) {
isExcluded = true;
cleanup();
return;
}
}
}
}
/**
* Sets up scrolls array, determines if frames are involved.
*/
function init() {
if (initDone || isExcluded || !document.body) {
return;
}
initDone = true;
let body = document.body;
let html = document.documentElement;
let windowHeight = window.innerHeight;
let scrollHeight = body.scrollHeight;
// check compat mode for root element
root = document.compatMode.indexOf("CSS") >= 0 ? html : body;
activeElement = body;
// Checks if this script is running in a frame
if (top != self) {
isFrame = true;
}
// TODO: check if clearfix is still needed
else if (
scrollHeight > windowHeight &&
body.clientHeight + 1 < body.scrollHeight &&
html.clientHeight + 1 < html.scrollHeight
) {
if (root.offsetHeight <= windowHeight) {
let clearfix = document.createElement("div");
clearfix.style.clear = "both";
body.appendChild(clearfix);
}
}
// disable fixed background
if (!options.fixedBackground && !isExcluded) {
body.style.backgroundAttachment = "scroll";
html.style.backgroundAttachment = "scroll";
}
if (!isFrame) {
addEvent("message", function (e) {
if (e.data.SS == "SmoothScroll") {
let wheelEvent = e.data;
wheelEvent.target = getFrameByEvent(e);
wheel(wheelEvent);
}
});
}
}
function getFrameByEvent(event) {
let iframes = document.getElementsByTagName("iframe");
return [].filter.call(iframes, function (iframe) {
return iframe.contentWindow === event.source;
})[0];
}
/**
* Removes event listeners and other traces left on the page.
*/
function cleanup() {
observer && observer.disconnect();
removeEvent(wheelEvent, wheel);
removeEvent("mousedown", mousedown);
removeEvent("keydown", keydown);
}
/**
* Make sure we are the last listener on the page so special
* key event handlers (e.g for <video>) can come before us
*/
function loaded() {
setTimeout(function () {
init();
if (options.keyboardSupport) {
removeEvent("keydown", keydown);
addEvent("keydown", keydown);
}
}, 1);
}
const dateNow = (function () {
return window.performance && performance.now
? function () {
return performance.now();
}
: function () {
return Date.now();
};
})();
/************************************************
* SCROLLING
************************************************/
let que = [];
let pending = null;
let lastScroll = dateNow();
/**
* Pushes scroll actions to the scrolling queue.
*/
function scrollArray(elem, left, top) {
directionCheck(left, top);
if (options.accelerationMax != 1) {
let now = dateNow();
let elapsed = now - lastScroll;
if (elapsed < options.accelerationDelta) {
let factor = (1 + 50 / elapsed) / 2;
if (factor > 1) {
factor = Math.min(factor, options.accelerationMax);
left *= factor;
top *= factor;
}
}
lastScroll = dateNow();
}
// push a scroll command
que.push({
x: left,
y: top,
lastX: left < 0 ? 0.99 : -0.99,
lastY: top < 0 ? 0.99 : -0.99,
start: dateNow(),
});
// don't act if there's a pending frame loop
if (pending) {
return;
}
let scrollRoot = getScrollRoot();
let isWindowScroll = elem === scrollRoot || elem === document.body;
// if we haven't already fixed the behavior,
// and it needs fixing for this sesh
if (elem.$scrollBehavior == null && isScrollBehaviorSmooth(elem)) {
elem.$scrollBehavior = elem.style.scrollBehavior;
elem.style.scrollBehavior = "auto";
}
let step = function (time) {
let now = dateNow();
let scrollX = 0;
let scrollY = 0;
for (let i = 0; i < que.length; i++) {
let item = que[i];
let elapsed = now - item.start;
let finished = elapsed >= options.animationTime;
// scroll position: [0, 1]
let position = finished ? 1 : elapsed / options.animationTime;
// easing [optional]
if (options.pulseAlgorithm) {
position = pulse(position);
}
// only need the difference
let x = (item.x * position - item.lastX) >> 0;
let y = (item.y * position - item.lastY) >> 0;
// add this to the total scrolling
scrollX += x;
scrollY += y;
// update last values
item.lastX += x;
item.lastY += y;
// delete and step back if it's over
if (finished) {
que.splice(i, 1);
i--;
}
}
if (window.devicePixelRatio) {
//scrollX /= (window.devicePixelRatio;
//scrollY /= window.devicePixelRatio;
}
// scroll left and top
if (isWindowScroll) {
window.scrollBy(scrollX, scrollY);
} else {
if (scrollX) elem.scrollLeft += scrollX;
if (scrollY) elem.scrollTop += scrollY;
}
// clean up if there's nothing left to do
if (!left && !top) {
que = [];
}
if (que.length) {
pending = window.requestAnimationFrame(step);
} else {
pending = null;
// restore default behavior at the end of scrolling sesh
if (elem.$scrollBehavior != null) {
elem.style.scrollBehavior = elem.$scrollBehavior;
elem.$scrollBehavior = null;
}
}
};
// start a new queue of actions
pending = window.requestAnimationFrame(step);
}
/***********************************************
* EVENTS
***********************************************/
/**
* Mouse wheel handler.
* @param {Object} event
*/
function wheel(event) {
if (!initDone) {
init();
}
let target = event.target;
// leave early if default action is prevented
// or it's a zooming event with CTRL
if (event.defaultPrevented || event.ctrlKey) {
return true;
}
// leave embedded content alone (flash & pdf)
if (
isNodeName(activeElement, "embed") ||
(isNodeName(target, "embed") && /\.pdf/i.test(target.src)) ||
isNodeName(activeElement, "object") ||
target.shadowRoot
) {
return true;
}
let deltaX = -event.wheelDeltaX || event.deltaX || 0;
let deltaY = -event.wheelDeltaY || event.deltaY || 0;
if (isMac) {
if (event.wheelDeltaX && isDivisible(event.wheelDeltaX, 120)) {
deltaX = -120 * (event.wheelDeltaX / Math.abs(event.wheelDeltaX));
}
if (event.wheelDeltaY && isDivisible(event.wheelDeltaY, 120)) {
deltaY = -120 * (event.wheelDeltaY / Math.abs(event.wheelDeltaY));
}
}
if (isLinux) {
// issues #148 #176
let otherModifier = event.ctrlKey || event.altKey || event.metaKey;
if (event.shiftKey && !otherModifier) {
deltaX = deltaX || deltaY;
deltaY = 0;
}
}
// use wheelDelta if deltaX/Y is not available
if (!deltaX && !deltaY) {
deltaY = -event.wheelDelta || 0;
}
// line based scrolling
if (event.deltaMode === 1) {
deltaX *= 40;
deltaY *= 40;
}
// check if it's a touchpad scroll that should be ignored
if (!options.touchpadSupport && isTouchpad(deltaY)) {
return true;
}
let xOnly = deltaX && !deltaY;
let overflowing = overflowingAncestor(target, xOnly);
// nothing to do if there's no element that's scrollable
if (!overflowing) {
// Chrome iframes seem to eat wheel events, which we need to
// propagate up if the iframe has nothing overflowing to scroll
if (isFrame) {
event.preventDefault();
postScrollToParent(deltaX, deltaY);
// change target to iframe element itself for the parent frame
//Object.defineProperty(event, "target", {value: window.frameElement});
//return parent.wheel(event);
}
return true;
}
if (options.reverseDirection) {
deltaX *= -1;
deltaY *= -1;
}
// scale by step size
// delta is 120 most of the time
// synaptics seems to send 1 sometimes
if (Math.abs(deltaX) > 1.2) {
deltaX *= options.stepSize / 120;
}
if (Math.abs(deltaY) > 1.2) {
deltaY *= options.stepSize / 120;
}
scrollArray(overflowing, deltaX, deltaY);
if (event.preventDefault) event.preventDefault();
scheduleClearCache();
}
/**
* Keydown event handler.
* @param {Object} event
*/
function keydown(event) {
let target = event.target;
let modifier =
event.ctrlKey ||
event.altKey ||
event.metaKey ||
(event.shiftKey && event.keyCode !== key.spacebar);
// our own tracked active element could've been removed from the DOM
if (!document.contains(activeElement)) {
activeElement = document.activeElement;
}
// do nothing if user is editing text
// or using a modifier key (except shift)
// or in a dropdown
// or inside interactive elements
let inputNodeNames = /^(textarea|select|embed|object)$/i;
let buttonTypes = /^(button|submit|radio|checkbox|file|color|image)$/i;
if (
event.defaultPrevented ||
inputNodeNames.test(target.nodeName) ||
(isNodeName(target, "input") && !buttonTypes.test(target.type)) ||
isNodeName(activeElement, "video") ||
isInsideYoutubeVideo(event) ||
target.isContentEditable ||
modifier
) {
return true;
}
// [spacebar] should trigger button press, leave it alone
if (
(isNodeName(target, "button") ||
(isNodeName(target, "input") && buttonTypes.test(target.type))) &&
event.keyCode === key.spacebar
) {
return true;
}
// [arrwow keys] on radio buttons should be left alone
if (
isNodeName(target, "input") &&
target.type == "radio" &&
arrowKeys[event.keyCode]
) {
return true;
}
let xOnly = event.keyCode == key.left || event.keyCode == key.right;
let overflowing = overflowingAncestor(activeElement, xOnly);
if (!overflowing) {
// iframes seem to eat key events, which we need to propagate up
// if the iframe has nothing overflowing to scroll
return isFrame ? parent.keydown(event) : true;
}
let clientHeight = overflowing.clientHeight;
let shift,
x = 0,
y = 0;
if (overflowing == document.body) {
clientHeight = window.innerHeight;
}
switch (event.keyCode) {
case key.up:
y = -options.arrowScroll;
break;
case key.down:
y = options.arrowScroll;
break;
case key.spacebar: // (+ shift)
shift = event.shiftKey ? 1 : -1;
y = -shift * clientHeight * 0.9;
break;
case key.pageup:
y = -clientHeight * 0.9;
break;
case key.pagedown:
y = clientHeight * 0.9;
break;
case key.home:
if (overflowing == document.body && document.scrollingElement)
overflowing = document.scrollingElement;
y = -overflowing.scrollTop;
break;
case key.end:
let scroll = overflowing.scrollHeight - overflowing.scrollTop;
let scrollRemaining = scroll - clientHeight;
y = scrollRemaining > 0 ? scrollRemaining + 10 : 0;
break;
case key.left:
x = -options.arrowScroll;
break;
case key.right:
x = options.arrowScroll;
break;
default:
return true; // a key we don't care about
}
scrollArray(overflowing, x, y);
event.preventDefault();
scheduleClearCache();
}
/**
* Mousedown event only for updating activeElement
*/
function mousedown(event) {
activeElement = event.target;
}
/***********************************************
* OVERFLOW
***********************************************/
let uniqueID = (function () {
let i = 0;
return function (el) {
return el.uniqueID || (el.uniqueID = i++);
};
})();
let cacheX = {}; // cleared out after a scrolling session
let cacheY = {}; // cleared out after a scrolling session
let clearCacheTimer;
let smoothBehaviorForElement = {};
//setInterval(function () { cache = {}; }, 10 * 1000);
function scheduleClearCache() {
clearTimeout(clearCacheTimer);
clearCacheTimer = setInterval(function () {
cacheX = cacheY = smoothBehaviorForElement = {};
}, 1 * 1000);
}
function setCache(elems, overflowing, x) {
let cache = x ? cacheX : cacheY;
for (let i = elems.length; i--; ) cache[uniqueID(elems[i])] = overflowing;
return overflowing;
}
function getCache(el, x) {
return (x ? cacheX : cacheY)[uniqueID(el)];
}
// (body) (root)
// | hidden | visible | scroll | auto |
// hidden | no | no | YES | YES |
// visible | no | YES | YES | YES |
// scroll | no | YES | YES | YES |
// auto | no | YES | YES | YES |
function overflowingAncestor(el, x) {
let elems = [];
let body = document.body;
let rootScrollHeight = root.scrollHeight;
let rootScrollWidth = root.scrollWidth;
do {
let cached = getCache(el, x);
if (cached) {
return setCache(elems, cached, x);
}
elems.push(el);
if (
(x && rootScrollWidth === el.scrollWidth) ||
(!x && rootScrollHeight === el.scrollHeight)
) {
let topOverflowsNotHidden =
overflowNotHidden(root, x) && overflowNotHidden(body, x);
let isOverflowCSS =
topOverflowsNotHidden || overflowAutoOrScroll(root, x);
if (
(isFrame && isContentOverflowing(root, x)) ||
(!isFrame && isOverflowCSS)
) {
return setCache(elems, getScrollRoot(), x);
}
} else if (isContentOverflowing(el, x) && overflowAutoOrScroll(el, x)) {
return setCache(elems, el, x);
}
} while ((el = el.parentElement));
}
function isContentOverflowing(el, x) {
return x
? el.clientWidth + 10 < el.scrollWidth
: el.clientHeight + 10 < el.scrollHeight;
}
function computedOverflow(el, x) {
let property = x ? "overflow-x" : "overflow-y";
return getComputedStyle(el, "").getPropertyValue(property);
}
// typically for <body> and <html>
function overflowNotHidden(el, x) {
return computedOverflow(el, x) != "hidden";
}
// for all other elements
function overflowAutoOrScroll(el, x) {
return /^(scroll|auto)$/.test(computedOverflow(el, x));
}
// for all other elements
function isScrollBehaviorSmooth(el) {
let id = uniqueID(el);
if (smoothBehaviorForElement[id] == null) {
let scrollBehavior = getComputedStyle(el, "")["scroll-behavior"];
smoothBehaviorForElement[id] = "smooth" == scrollBehavior;
}
return smoothBehaviorForElement[id];
}
function postScrollToParent(deltaX, deltaY) {
parent.postMessage(
{
deltaX: deltaX,
deltaY: deltaY,
SS: "SmoothScroll",
},
"*"
);
}
/***********************************************
* HELPERS
***********************************************/
function addEvent(type, fn, arg) {
window.addEventListener(type, fn, arg || false);
}
function removeEvent(type, fn, arg) {
window.removeEventListener(type, fn, arg || false);
}
function isNodeName(el, tag) {
return el && (el.nodeName || "").toLowerCase() === tag.toLowerCase();
}
function directionCheck(x, y) {
x = x > 0 ? 1 : -1;
y = y > 0 ? 1 : -1;
if (direction.x !== x || direction.y !== y) {
direction.x = x;
direction.y = y;
que = [];
lastScroll = 0;
window.cancelAnimationFrame(pending);
pending = null;
}
}
function isTouchpad(deltaY) {
if (!deltaY) return;
if (!deltaBuffer.length) {
deltaBuffer = [deltaY, deltaY, deltaY];
}
deltaY = Math.abs(deltaY);
deltaBuffer.push(deltaY);
deltaBuffer.shift();
let dpiScaledWheelDelta = deltaY > 120 && allDeltasDivisableBy(deltaY); // win64
let tp =
!allDeltasDivisableBy(120) &&
!allDeltasDivisableBy(100) &&
!dpiScaledWheelDelta;
if (deltaY < 50) tp = true;
clearTimeout(deltaBufferTimer);
deltaBufferTimer = setTimeout(function () {
chrome?.storage?.local?.set?.({
deltaBuffer: deltaBuffer,
});
if (!tp)
chrome?.storage?.local?.set?.({
lastDiscreetWheel: dateNow(),
});
}, 1000);
return tp;
}
function isDivisible(n, divisor) {
return Math.floor(n / divisor) == n / divisor;
}
function allDeltasDivisableBy(divisor) {
return (
isDivisible(deltaBuffer[0], divisor) &&
isDivisible(deltaBuffer[1], divisor) &&
isDivisible(deltaBuffer[2], divisor)
);
}
chrome?.storage?.local?.get?.("deltaBuffer", function (stored) {
if (stored.deltaBuffer) {
deltaBuffer = stored.deltaBuffer;
}
});
function isInsideYoutubeVideo(event) {
let elem = event.target;
let isControl = false;
if (document.URL.indexOf("www.youtube.com/watch") != -1) {
do {
isControl =
elem.classList && elem.classList.contains("html5-video-controls");
if (isControl) break;
} while ((elem = elem.parentNode));
}
return isControl;
}
function getScrollRoot() {
return document.scrollingElement || document.body; // scrolling root in WebKit
}
/***********************************************
* PULSE (by Michael Herf)
***********************************************/
/**
* Viscous fluid with a pulse for part and decay for the rest.
* - Applies a fixed force over an interval (a damped acceleration), and
* - Lets the exponential bleed away the velocity over a longer interval
* - Michael Herf, http://stereopsis.com/stopping/
*/
function pulse_(x) {
let val, start, expx;
// test
x = x * options.pulseScale;
if (x < 1) {
// acceleartion
val = x - (1 - Math.exp(-x));
} else {
// tail
// the previous animation ended here:
start = Math.exp(-1);
// simple viscous drag
x -= 1;
expx = 1 - Math.exp(-x);
val = start + expx * (1 - start);
}
return val * options.pulseNormalize;
}
function pulse(x) {
if (x >= 1) return 1;
if (x <= 0) return 0;
if (options.pulseNormalize == 1) {
options.pulseNormalize /= pulse_(1);
}
return pulse_(x);
}
// new standard wheel event from Chrome 31+
let wheelEvent =
"onwheel" in document.createElement("div") ? "wheel" : "mousewheel";
addEvent(wheelEvent, wheel, {
passive: false,
});
addEvent("mousedown", mousedown);
addEvent("keydown", keydown);
addEvent("load", loaded);
// =======================================================================
// ============================ middlemouse.js ===========================
// =======================================================================
/**
* A module for middle mouse scrolling.
*/
const cleanupMiddlemouse = (function (window) {
let defaultOptions = {
middleMouse: true,
frameRate: 200,
};
let options = defaultOptions;
let img = document.createElement("div"); // img at the reference point
let scrolling = false; // guards one phase
// we check the OS for default middle mouse behavior only!
let isLinux = navigator.platform.indexOf("Linux") != -1;
// get global settings
// chrome.storage.sync.get(defaultOptions, function (syncedOptions) {
// options = syncedOptions;
// // leave time for the main script to check excluded pages
// setTimeout(function () {
// // if we shouldn't run, stop listening to events
// if (isExcluded && !options.middleMouse) {
// cleanup();
// }
// }, 10);
// });
/**
* Initializes the image at the reference point.
*/
function init() {
let url = chrome?.runtime?.getURL?.("/scripts/smoothscroll_cursor.png");
img.style.background = "url(" + url + ") no-repeat";
img.style.position = "fixed";
img.style.zIndex = "999999999";
img.style.width = "20px";
img.style.height = "20px";
new Image().src = url; // force download
}
/**
* Removes event listeners and other traces left on the page.
*/
function cleanup() {
removeEvent("mousedown", mousedown);
}
/**
* Shows the reference image, and binds event listeners for scrolling.
* It also manages the animation.
* @param {Object} event
*/
function mousedown(e) {
// use default action if we're disabled
// or it's not the midde mouse button
if (!options.middleMouse || e.button !== 1) {
return true;
}
let isLink = false;
let elem = e.target;
// linux middle mouse shouldn't be overwritten (paste)
let isLinuxInput =
isLinux &&
(/input|textarea/i.test(elem.nodeName) || elem.isContentEditable);
do {
isLink = isNodeName(elem, "a");
if (isLink) break;
} while ((elem = elem.parentNode));
elem = overflowingAncestor(e.target);
// if it's being used on an <a> element
// take the default action
if (!elem || isLink || isLinuxInput) {
return true;
}
// we don't want the default by now
e.preventDefault();
// quit if there's an ongoing scrolling
if (scrolling) {
return false;
}
// set up a new scrolling phase
scrolling = true;
// reference point
img.style.left = e.clientX - 10 + "px";
img.style.top = e.clientY - 10 + "px";
document.body.appendChild(img);
let refereceX = e.clientX;
let refereceY = e.clientY;
let speedX = 0;
let speedY = 0;
// animation loop
let last = dateNow();
let delay = 1000 / options.frameRate;
let finished = false;
window.requestAnimationFrame(
function step(time) {
let now = dateNow();
let elapsed = now - last;
// NOTE: later can also use document.scrollingElement
if (elem == document.body) {
window.scrollBy(speedX * elapsed, speedY * elapsed);
} else {
elem.scrollLeft += (speedX * elapsed) >> 0;
elem.scrollTop += (speedY * elapsed) >> 0;
}
last = now;
if (!finished) {
window.requestAnimationFrame(step);
}
},
elem,
delay
);
let firstMove = true;
function mousemove(e) {
let deltaX = Math.abs(refereceX - e.clientX);
let deltaY = Math.abs(refereceY - e.clientY);
let movedEnough = Math.max(deltaX, deltaY) > 10;
if (firstMove && movedEnough) {
addEvent("mouseup", remove);
firstMove = false;
}
speedX = ((e.clientX - refereceX) * 10) / 1000;
speedY = ((e.clientY - refereceY) * 10) / 1000;
}
function remove(e) {
removeEvent("mousemove", mousemove);
removeEvent("mousedown", remove);
removeEvent("mouseup", remove);
removeEvent("keydown", remove);
document.body.removeChild(img);
scrolling = false;
finished = true;
}
addEvent("mousemove", mousemove);
addEvent("mousedown", remove);
addEvent("keydown", remove);
}
addEvent("mousedown", mousedown);
if (document.readyState === "complete") init();
else addEvent("load", init);
return cleanup;
})(window);
window.ufs_smoothScroll_disable = () => {
cleanup();
cleanupMiddlemouse();
window.ufs_smoothScroll_disable = null;
};
return window.ufs_smoothScroll_disable;
}