-
Notifications
You must be signed in to change notification settings - Fork 0
/
ScrollBarFixer.user.js
75 lines (73 loc) · 3.03 KB
/
ScrollBarFixer.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
// ==UserScript==
// @name Scrollbar Fixer
// @author github.com/richkmls
// @version 1.1
// @description A script that fixes scrollbars on any website and prevents them from being hidden, overridden or manipulated
// @match *://*/*
// @exclude https://github.com/*
// @grant none
// ==/UserScript==
// To prevent the script from running on additional websites, you can add more @exclude directives.
// Simply add a new line above with the format: // @exclude [URL or URL pattern]
// For example, to exclude the website http://example.net/, you would add the following line:
// @exclude http://example.net/*
(function() {
'use strict';
// Initialize counter and timeout variables
let counter = 0;
let timeout = 1000;
// Set the number of extra tries
let extratries = 1;
// Define the fixScrollbars function
function fixScrollbars() {
// Initialize the hasDisabledScrollbars variable
let hasDisabledScrollbars = false;
// Check if the overflow property is set to hidden for the documentElement or body
for (let el of [document.documentElement, document.body]) {
let style = window.getComputedStyle(el);
if (style.overflow === 'hidden' || style.overflowX === 'hidden' || style.overflowY === 'hidden') {
hasDisabledScrollbars = true;
break;
}
}
// If scrollbars are not disabled
if (!hasDisabledScrollbars) {
// If the counter is less than the number of extra tries
if (counter < extratries) {
// Increment the counter and try again.
counter++;
// Call the fixScrollbars function again
setTimeout(fixScrollbars, timeout);
} else {
// If the counter is equal to or greater than the number of extra tries, return and do not repeat the script
return;
}
}
// Retrieve the document and body elements from the DOM
let [doc, body] = [document.documentElement, document.body];
// Ensure that both elements display scrollbars only when necessary
for (let el of [doc, body]) {
el.style.overflow = 'auto';
}
// Override any CSS rules that modify scrollbars
Object.defineProperty(document, 'styleSheets', {
get: () => [],
configurable: false
});
// Return the current vertical scroll position of the document element
Object.defineProperty(window, 'scrollY', {
get: () => doc.scrollTop,
configurable: false
});
// Block any scripts that attempt to scroll the window or the elements
for (let prop of ['scrollTo', 'scrollTop']) {
for (let obj of [window, doc, body]) {
Object.defineProperty(obj, prop, {
set: () => {},
configurable: false
});
}
}
}
setTimeout(fixScrollbars, timeout);
})();