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.

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

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

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey,才能安装此脚本。

您需要先安装用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==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 = '';
    }
})();