Scroll mode for touchpad/mouse without/missing scroll/middle button

Enable scroll mode for touchpad/mouse without/missing scroll/middle button when Ctrl + Space or Ctrl + Shift and touchpad/left touchpad button/left mouse button are pressed. Text selection is enabled for Ctrl + Shift.

Από την 26/05/2024. Δείτε την τελευταία έκδοση.

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey, το Greasemonkey ή το Violentmonkey για να εγκαταστήσετε αυτόν τον κώδικα.

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey ή το Violentmonkey για να εγκαταστήσετε αυτόν τον κώδικα.

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey ή το Violentmonkey για να εγκαταστήσετε αυτόν τον κώδικα.

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey ή το Userscripts για να εγκαταστήσετε αυτόν τον κώδικα.

You will need to install an extension such as Tampermonkey to install this script.

Θα χρειαστεί να εγκαταστήσετε μια επέκταση διαχείρισης κώδικα χρήστη για να εγκαταστήσετε αυτόν τον κώδικα.

(Έχω ήδη έναν διαχειριστή κώδικα χρήστη, επιτρέψτε μου να τον εγκαταστήσω!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(Έχω ήδη έναν διαχειριστή στυλ χρήστη, επιτρέψτε μου να τον εγκαταστήσω!)

// ==UserScript==
// @name         Scroll mode for touchpad/mouse without/missing scroll/middle button
// @author       NWP
// @description  Enable scroll mode for touchpad/mouse without/missing scroll/middle button when Ctrl + Space or Ctrl + Shift and touchpad/left touchpad button/left mouse button are pressed. Text selection is enabled for Ctrl + Shift.
// @namespace    https://greasyfork.org/users/877912
// @version      0.1
// @license      MIT
// @match        *://*/*
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    // Sensitivity factor determines the scroll speed relative to mouse movement.
    // A higher value results in faster scrolling, while a lower value makes the scrolling slower.
    // Example:
    // - If sensitivity = 1.0, moving the mouse 100 pixels will scroll 100 pixels.
    // - If sensitivity = 0.5, moving the mouse 100 pixels will scroll 50 pixels.
    const sensitivity = 0.5;


    let scrollModeEnabled = false;
    let selectModeEnabled = false;
    let initialMouseY = 0;
    let scrollSpeed = 0;
    let scrollInterval = null;

    document.addEventListener('keydown', function(event) {
        if (event.ctrlKey && event.code === 'Space' && !scrollModeEnabled) {
            scrollModeEnabled = true;
            document.body.style.cursor = 'ns-resize'; // Indicate scroll mode is active
            disableTextSelection();
        }
        if (event.ctrlKey && event.shiftKey && !selectModeEnabled) {
            selectModeEnabled = true;
            document.body.style.cursor = 'ns-resize'; // Indicate select mode is active
            enableTextSelection();
        }
    });

    document.addEventListener('keyup', function(event) {
        // Check specific keyup events to exit scroll mode
        if (event.code === 'Space' || event.code === 'ControlLeft' || event.code === 'ControlRight') {
            exitScrollMode();
        }
        // Check specific keyup events to exit select mode
        if (event.code === 'ShiftLeft' || event.code === 'ShiftRight' || event.code === 'ControlLeft' || event.code === 'ControlRight') {
            exitSelectMode();
        }
    });

    document.addEventListener('mousedown', function(event) {
        if (scrollModeEnabled && event.button === 0) {
            initialMouseY = event.clientY; // Set initial position for scroll calculation
            startAutoScroll();
        }
        if (selectModeEnabled && event.button === 0) {
            initialMouseY = event.clientY; // Set initial position for scroll calculation
            startAutoScroll();
        }
    });

    document.addEventListener('mouseup', function(event) {
        if (event.button === 0) {
            exitScrollMode();
            exitSelectMode();
        }
    });

    document.addEventListener('mousemove', function(event) {
        if ((scrollModeEnabled || selectModeEnabled) && scrollInterval) {
            let currentMouseY = event.clientY;
            scrollSpeed = (currentMouseY - initialMouseY) * sensitivity;
        }
    });

    function startAutoScroll() {
        if (!scrollInterval) {
            scrollInterval = setInterval(function() {
                window.scrollBy(0, scrollSpeed); // Scroll based on current calculated speed
            }, 50);
        }
    }

    function stopAutoScroll() {
        if (scrollInterval) {
            clearInterval(scrollInterval);
            scrollInterval = null;
        }
    }

    function exitScrollMode() {
        if (scrollModeEnabled) {
            scrollModeEnabled = false;
            scrollSpeed = 0;
            document.body.style.cursor = 'default';
            enableTextSelection(); // Ensure text selection is enabled again
            stopAutoScroll();
        }
    }

    function exitSelectMode() {
        if (selectModeEnabled) {
            selectModeEnabled = false;
            scrollSpeed = 0;
            document.body.style.cursor = 'default';
            stopAutoScroll();
        }
    }

    function disableTextSelection() {
        document.body.style.userSelect = 'none';
        document.body.style.webkitUserSelect = 'none';
        document.body.style.msUserSelect = 'none';
        document.body.style.mozUserSelect = 'none';
    }

    function enableTextSelection() {
        document.body.style.userSelect = '';
        document.body.style.webkitUserSelect = '';
        document.body.style.msUserSelect = '';
        document.body.style.mozUserSelect = '';
    }
})();