XDHx86 / Auto Scroll

// ==UserScript==
// @name         Auto Scroll
// @version      1.1
// @description  Auto Scroll anywhere on the internet (Supports Android and IOS)
// @copyright    2020, XDHx86 (https://openuserjs.org/users/XDHx86)
// @license      MIT
// @author       XDHx86
// @updateURL    https://openuserjs.org/meta/XDHx86/Auto_Scroll.meta.js
// @downloadURL  https://openuserjs.org/install/XDHx86/Auto_Scroll.user.js
// @namespace    https://openuserjs.org/users/XDHx86
// @match        *://*/*
// @grant        GM_addStyle
// @grant        GM_registerMenuCommand
// @grant        GM_setValue
// @grant        GM_getValue
// @noframes
// ==/UserScript==

(function() {
    'use strict';

    // Supports all browsers and Firefox Android too
    // Doesn't work in frames
    // If you want it to work in frames for some reason delete the "// @noframes" line above


    // Default scroll speed, to change the scroll speed just go to TamperMonkey popup menu
    var scroll = GM_getValue('scroll', 80);

    // Detect platform to assign scroll step
    // This affects your scroll speed significantly
    // Alternatively you can modify this value for higher scroll speed which increases it drastically
    // Beware that increasing this value by more than 0.25 can make the auto scroll speed too fast
    // FOR PC ONLY
    var nav = window.navigator.platform.toLowerCase();
    if (nav.includes('win') || nav.includes('ubuntu') || nav.includes('linux')) var scrolly = 1;
    // FOR ANDROID AND IOS ONLY
    else scrolly = 2;

    // Optional CSS for smooth scrolling.
    // If you don't want it or just feel like it, you're free to comment it out.
    GM_addStyle('* { scroll-behavior: smooth; }');

    // Website exclusion and inclusion.
    // Click on tampermonkey extension pop-up menu.
    // Click add/remove to/from blacklist/whitelist.
	// Or switch to blacklist/whitelist mode.
    // Please don't touch any of these - Unless you know what you're doing - otherwise the script may not work
    var blacklist = GM_getValue('bl', []),
        bl = false,
        whitelist = GM_getValue('wl', []),
        wl = false,
        mode = GM_getValue('mode', true),
        save = () => { scroll = parseInt(GM_getValue('scroll', 80)); };
    GM_registerMenuCommand('Change Auto Scroll Speed', () => {
        var choice = () => Number(prompt('Choose speed:\nVery Fast: 10-40, Fast: 40-70, Medium: 70-100, Slow: 100-130, Very Slow: 130+')),
            value = choice();
        if (isNaN(value)) choice();
        else {
            GM_setValue('scroll', value);
            save();
        }
    });
    if (!mode) blacklist.forEach((e) => { if (e == location.host) bl = true; });
    if (mode) whitelist.forEach((e) => { if (e == location.host) wl = true; });
    if (!mode) {
        GM_registerMenuCommand('Switch to whitelist mode', () => { GM_setValue('mode', true); location.reload(); });
        if (!bl) {
            GM_registerMenuCommand('Add to blacklist', () => {
                blacklist.push(location.host);
                GM_setValue('bl', blacklist);
                location.reload();
            });
        } else {
            GM_registerMenuCommand('Remove from blacklist', () => {
                blacklist.splice(blacklist.indexOf(location.host), 1);
                GM_setValue('bl', blacklist);
                location.reload();
            });
            return;
        }
    } else {
        GM_registerMenuCommand('Switch to blacklist mode', () => { GM_setValue('mode', false); location.reload(); });
        if (!wl) {
            GM_registerMenuCommand('Add to whitelist', () => {
                whitelist.push(location.host);
                GM_setValue('wl', whitelist);
                location.reload();
            });
            return;
        } else {
            GM_registerMenuCommand('Remove from whitelist', () => {
                whitelist.splice(whitelist.indexOf(location.host), 1);
                GM_setValue('wl', whitelist);
                location.reload();
            });
        }
    }

    var timer = setInterval(() => { window.scrollBy(0, scrolly); }, scroll),
        bool = true,
        cond = false;

    window.onclick = () => {
        if (bool) {
            clearInterval(timer);
            bool = false;
        } else {
            console.log(scroll);
            timer = setInterval(() => { window.scrollBy(0, scrolly); }, scroll);
            bool = true;
        }
    }
    window.onblur = () => {
		// This line right here.
		// Please don't delete it and instead just comment it out.
        cond = false;
        if (bool) {
            clearInterval(timer);
            bool = false;
            cond = true;
        }
    }
    // Continue auto scrolling when you're back (PC only)
    // Only if you left the page while auto scrolling. To disable comment out "cond = false;" above.
    window.onfocus = (e) => {
        if (!bool && cond) {
            console.log(scroll);
            timer = setInterval(() => { window.scrollBy(0, scrolly); }, scroll);
            bool = true;
        }
    }
    window.onauxclick = (e) => {
        if (e.button < 1) return;
        clearInterval(timer);
        bool = false;
    }
    window.ontouchmove = () => {
        clearInterval(timer);
        bool = false;
    }
    window.onkeydown = () => {
        clearInterval(timer);
        bool = false;
    }
})();