Useless Things Series: Mouse and Keyboard Event Counters

Counts mouse movement in inches, scroll events, key presses, mouse clicks (left and right), and displays the top 5 keys pressed along with their counts.

目前為 2023-12-21 提交的版本,檢視 最新版本

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

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

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         Useless Things Series: Mouse and Keyboard Event Counters
// @version      1.2
// @description  Counts mouse movement in inches, scroll events, key presses, mouse clicks (left and right), and displays the top 5 keys pressed along with their counts.
// @match        *://*/*
// @grant        none
// @license      MIT
// @namespace https://greasyfork.org/users/1126616
// ==/UserScript==

(function() {
    var mouseInches = 0;
    var scrollCount = 0;
    var letterCount = 0;
    var leftClickCount = 0;
    var rightClickCount = 0;
    var keyPressCount = {};

    var countersDiv = document.createElement('div');
    countersDiv.style.position = 'fixed';
    countersDiv.style.left = '10px';
    countersDiv.style.top = '10px';
    countersDiv.style.padding = '10px';
    countersDiv.style.border = '2px solid #fff';
    countersDiv.style.borderRadius = '5px';
    countersDiv.style.transition = 'background-color 0.3s, opacity 0.3s';
    countersDiv.style.backgroundColor = 'rgba(0, 0, 0, 0.7)';
    countersDiv.style.color = '#fff';
    countersDiv.style.cursor = 'move';
    countersDiv.style.userSelect = 'none';
    countersDiv.style.display = 'none';
    countersDiv.style.opacity = '0';
    document.body.appendChild(countersDiv);

    var mouseCounter = createCounter('Mouse Inches');
    var scrollCounter = createCounter('Scrolls');
    var letterCounter = createCounter('Letters Pressed');
    var leftClickCounter = createCounter('Left Clicks');
    var rightClickCounter = createCounter('Right Clicks');
    var topKeysCounter = createCounter('Top 5 Keys');

    countersDiv.appendChild(mouseCounter);
    countersDiv.appendChild(scrollCounter);
    countersDiv.appendChild(letterCounter);
    countersDiv.appendChild(leftClickCounter);
    countersDiv.appendChild(rightClickCounter);
    countersDiv.appendChild(topKeysCounter);

    var timeout;
    var lastScrollTime = new Date().getTime();
    var isDragging = false;
    var offsetX, offsetY;

    countersDiv.addEventListener('mousedown', function(e) {
        isDragging = true;
        offsetX = e.clientX - countersDiv.getBoundingClientRect().left;
        offsetY = e.clientY - countersDiv.getBoundingClientRect().top;
    });

    document.addEventListener('mousemove', function(e) {
        if (isDragging) {
            countersDiv.style.left = e.clientX - offsetX + 'px';
            countersDiv.style.top = e.clientY - offsetY + 'px';
        }
    });

    document.addEventListener('mouseup', function() {
        isDragging = false;
    });

    document.addEventListener('mousemove', function(event) {
        clearTimeout(timeout);

        var deltaX = event.movementX || 0;
        var deltaY = event.movementY || 0;
        mouseInches += Math.sqrt(deltaX * deltaX + deltaY * deltaY) / 96;
        updateCounters();

        timeout = setTimeout(function() {
            countersDiv.style.opacity = '0';
            setTimeout(function() {
                countersDiv.style.display = 'none';
            }, 300);
        }, 3000);
    });

    document.addEventListener('wheel', function(event) {
        var currentTime = new Date().getTime();
        if (currentTime - lastScrollTime > 100) {
            scrollCount++;
            lastScrollTime = currentTime;
        }

        updateCounters();
    });

    document.addEventListener('keydown', function(event) {
        letterCount++;
        var key = event.key.toUpperCase();
        keyPressCount[key] = (keyPressCount[key] || 0) + 1;

        updateCounters();
    });

    document.addEventListener('mousedown', function(event) {
        if (event.button === 0) {
            leftClickCount++;
        } else if (event.button === 2) {
            rightClickCount++;
        }

        updateCounters();
    });

    function updateCounters() {
        mouseCounter.textContent = 'Mouse Inches: ' + mouseInches.toFixed(2) + ' in';
        scrollCounter.textContent = 'Scrolls: ' + scrollCount;
        letterCounter.textContent = 'Letters Pressed: ' + letterCount;
        leftClickCounter.textContent = 'Left Clicks: ' + leftClickCount;
        rightClickCounter.textContent = 'Right Clicks: ' + rightClickCount;

        // Display top 5 keys pressed in a column
        var sortedKeys = Object.keys(keyPressCount).sort(function(a, b) {
            return keyPressCount[b] - keyPressCount[a];
        });

        var topKeysText = 'Top 5 Keys:<br>';
        for (var i = 0; i < Math.min(5, sortedKeys.length); i++) {
            var key = sortedKeys[i];
            topKeysText += key + ': ' + keyPressCount[key] + '<br>';
        }

        topKeysCounter.innerHTML = topKeysText;

        countersDiv.style.display = 'block';
        countersDiv.style.opacity = '1';
    }

    function createCounter(label) {
        var counter = document.createElement('div');
        counter.style.marginBottom = '8px';
        counter.textContent = label + ': 0';
        counter.style.color = '#fff';
        return counter;
    }
})();