内存监控控制台

11/16/2023, 5:24:31 PM

当前为 2023-11-20 提交的版本,查看 最新版本

// ==UserScript==
// @name        内存监控控制台
// @namespace   Violentmonkey Scripts
// @match        *://*/*
// @grant       none
// @version     1.2
// @author      K
// @description 11/16/2023, 5:24:31 PM
// @license MIT
// ==/UserScript==

(function() {
    let timer = null;
    const memoryInfoDiv = document.createElement('div');

    // 样式设置
    memoryInfoDiv.style.position = 'fixed';
    memoryInfoDiv.style.bottom = '10px';
    memoryInfoDiv.style.right = '10px';
    memoryInfoDiv.style.border = '1px solid #0f0'; // 绿色边框
    memoryInfoDiv.style.backgroundColor = '#000'; // 黑色背景
    memoryInfoDiv.style.color = '#0f0'; // 绿色文字
    memoryInfoDiv.style.padding = '10px';
    memoryInfoDiv.style.zIndex = '100000000';
    memoryInfoDiv.style.opacity = '0.7'; // 透明度
    memoryInfoDiv.style.fontFamily = 'Consolas, "Courier New", monospace'; // 控制台字体
    memoryInfoDiv.style.userSelect = 'none'; // 防止文本被选择
    document.body.appendChild(memoryInfoDiv);

    // 拖动功能
    let isDragging = false;
    let offsetX, offsetY;

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

    document.addEventListener('mouseup', function() {
        isDragging = false;
        memoryInfoDiv.style.cursor = 'grab';
    });

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

            // 保存位置
            localStorage.setItem('memoryInfoDivPosition', JSON.stringify({ left: memoryInfoDiv.style.left, top: memoryInfoDiv.style.top }));
        }
    });

    // 更新内存信息的函数
    function updateMemoryInfo() {
        const memory = window.performance.memory;
        if (!memory) {
            memoryInfoDiv.textContent = "Performance API is not supported in this browser.";
            return;
        }

        const toGB = bytes => (bytes / 1024 / 1024 / 1024).toFixed(2) + ' GB';
        const totalMemory = toGB(memory.jsHeapSizeLimit);
        const usedMemory = toGB(memory.usedJSHeapSize);
        const freeMemory = toGB(memory.jsHeapSizeLimit - memory.usedJSHeapSize);
        const usageRate = ((memory.usedJSHeapSize / memory.jsHeapSizeLimit) * 100).toFixed(2) + '%';
        const freeRate = ((1 - memory.usedJSHeapSize / memory.jsHeapSizeLimit) * 100).toFixed(2) + '%';

        memoryInfoDiv.innerHTML = `
            <strong>Memory Usage:</strong><br>
            Total: ${totalMemory}<br>
            Used: ${usedMemory}<br>
            Free: ${freeMemory}<br>
            Usage Rate: ${usageRate}<br>
            Free Rate: ${freeRate}
        `;
    }

    function init() {
        clearInterval(timer);
        timer = setInterval(updateMemoryInfo, 300);
        memoryInfoDiv.style.display = 'block';
        localStorage.setItem('memoryInfoDivVisible', 'true');
    }

    function destroy() {
        clearInterval(timer);
        memoryInfoDiv.style.display = 'none';
        localStorage.setItem('memoryInfoDivVisible', 'false');
    }

    // 从localStorage加载状态和位置
    const savedVisible = localStorage.getItem('memoryInfoDivVisible');
    const savedPosition = JSON.parse(localStorage.getItem('memoryInfoDivPosition'));

    if (savedVisible === 'true') {
        init();
    } else {
        destroy();
    }

    if (savedPosition) {
        memoryInfoDiv.style.left = savedPosition.left;
        memoryInfoDiv.style.top = savedPosition.top;
        memoryInfoDiv.style.right = 'auto';
        memoryInfoDiv.style.bottom = 'auto';
    }

    // 键盘事件监听器,用于切换浮窗显示状态
    document.addEventListener('keydown', function(event) {
        if (event.key === 'F10') {
            event.preventDefault(); // 防止F10默认行为
            if (memoryInfoDiv.style.display === 'none') {
                init();
            } else {
                destroy();
            }
        }
    });
})();