Site Optimization with FPS Counter

Optimize site performance and display FPS counter

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         Site Optimization with FPS Counter
// @namespace    http://tampermonkey.net/
// @version      0.3
// @description  Optimize site performance and display FPS counter
// @author       Kyu
// @match        *://*/*
// @license      MIT License
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    // Display optimization message
    const messageElement = document.createElement('div');
    messageElement.id = 'optimizationMessage';
    document.body.insertBefore(messageElement, document.body.firstChild);

    messageElement.textContent = 'Otimizando seu site...';
    setTimeout(() => {
        messageElement.textContent = 'Concluído';
        setTimeout(() => {
            messageElement.remove();
        }, 1000);
    }, 3000);

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

    const fpsMeasurement = function() {
        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);
    };

    let frame = window.performance.now();
    requestAnimationFrame(fpsMeasurement);

    // Optimize site performance
    const originalCreateObjectURL = window.URL.createObjectURL;
    window.URL.createObjectURL = function(blob) {
        const revokeObjectURL = originalCreateObjectURL.apply(this, arguments);
        setTimeout(() => {
            revokeObjectURL.apply(this, arguments);
        }, 10000); // Revoke after 10 seconds
        return revokeObjectURL;
    };

    const originalImageOnLoad = Image.prototype.onload;
    Image.prototype.onload = function() {
        const originalImage = originalImageOnLoad.apply(this, arguments);
        this.onload = originalImageOnLoad;
    };
})();