FPS Counter and Site Optimization

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

目前為 2024-02-06 提交的版本,檢視 最新版本

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

})();