FPS Counter and Site Optimization

Displays FPS counter, optimizes site performance, and automatically unlocks FPS limit

当前为 2024-02-06 提交的版本,查看 最新版本

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==

// @name         FPS Counter and Site Optimization

// @namespace    http://tampermonkey.net/

// @version      0.9

// @description  Displays FPS counter, optimizes site performance, and automatically unlocks FPS limit

// @author       Kyu

// @match        *://*/*

// @license      MIT License

// @grant        none

// ==/UserScript==

(function() {

    'use strict';



    // FPS Counter

    const fpsElement = document.createElement('div');
    fpsElement.id = 'fpsCounter';
    document.body.insertBefore(fpsElement, document.body.firstChild);

    let frame = window.performance.now();

    function fpsMeasurement() {
        const now = window.performance.now();
        const frameTime = Math.min(1000 / 60, 1 / (now - frame));
        fpsElement.textContent = `FPS: ${Math.round(1 / frameTime)}`;
        frame = now;
        requestAnimationFrame(fpsMeasurement);
    }

    requestAnimationFrame(fpsMeasurement);



    // Site Optimization

    // Optimization message (temporary)
    const messageElement = document.createElement('div');
    messageElement.id = 'optimizationMessage';
    document.body.insertBefore(messageElement, document.body.firstChild);
    messageElement.textContent = 'Optimizing site...';

    // Clear the message after a short delay
    setTimeout(() => {
        document.body.removeChild(messageElement);
    }, 2000); // 2 seconds delay


    // Optimized image loading
    const images = document.querySelectorAll('img');
    images.forEach(image => {
        const observer = new IntersectionObserver((entries) => {
            if (entries[0].isIntersecting) {
                image.src = image.dataset.src; // Load actual image URL
                observer.disconnect();
            }
        }, { threshold: 0.1 }); // Trigger when 10% of the image is visible
        observer.observe(image);
    });


    // Additional optimization techniques

    // Minification of JavaScript and CSS
    const minifier = new Minifier();
    const scripts = document.querySelectorAll('script[type="text/javascript"]');
    scripts.forEach(script => {
        if (!script.src) {
            script.textContent = minifier.minify(script.textContent);
        }
    });
    const stylesheets = document.querySelectorAll('link[rel="stylesheet"]');
    stylesheets.forEach(stylesheet => {
        if (!stylesheet.href) {
            stylesheet.textContent = minifier.minify(stylesheet.textContent);
        }
    });

    // Deferral of non-critical scripts
    const deferredScripts = document.querySelectorAll('script[defer]');
    deferredScripts.forEach(script => {
        script.parentNode.removeChild(script);
        document.body.appendChild(script);
    });


    // FPS Unlocking

    const unlockFPS = () => {
        const requestAnimationFrameOriginal = window.requestAnimationFrame;
        window.requestAnimationFrame = (callback) => {
            return requestAnimationFrameOriginal(callback, {
                priority: 'low',
            });
        };

        const cancelAnimationFrameOriginal = window.cancelAnimationFrame;
        window.cancelAnimationFrame = (id) => {
            return cancelAnimationFrameOriginal(id);
        };
    };

    // Automatically unlock FPS
    unlockFPS();

})();