FPS and Ping Counter and Site Optimization (Fully Automated FPS Unlock)

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

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

// ==UserScript==

// @name         FPS and Ping Counter and Site Optimization (Fully Automated FPS Unlock)

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

// @version      1.8

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

// @author       Kyu

// @match        *://*/*

// @license      MIT License

// @grant        none



// ==/UserScript==

(function () {

  'use strict';

  // Site Optimization Balloon

  const optimizationBalloon = document.createElement('div');

  optimizationBalloon.id = 'optimizationBalloon';

  optimizationBalloon.style.position = 'fixed';

  optimizationBalloon.style.top = '50px';

  optimizationBalloon.style.left = '10px';

  const isBackgroundLight = isLightBackground();

  optimizationBalloon.style.backgroundColor = isBackgroundLight ? 'rgba(0, 0, 0, 0.7)' : 'rgba(255, 255, 255, 0.7)';

  optimizationBalloon.style.color = isBackgroundLight ? 'white' : 'black';

  optimizationBalloon.style.padding = '10px';

  optimizationBalloon.textContent = 'Optimizing site...';

  document.body.appendChild(optimizationBalloon);

  // Improved Site Optimization

  const optimizeSite = () => {

    // Remove specific elements that may impact performance

    const elementsToRemove = document.querySelectorAll('.unnecessary-element');

    elementsToRemove.forEach(element => element.remove());

    // Minimize DOM operations and other optimizations as needed

    // Example: const someElement = document.getElementById('someId');

    //          someElement.style.display = 'none';

    optimizationBalloon.textContent = 'Site optimized!';

    setTimeout(() => {

      optimizationBalloon.style.display = 'none';

    }, 2000); // 2 seconds delay

  };

  // Invoke site optimization

  optimizeSite();

  // # FPS Counter

  const fpsElement = document.createElement('div');

  fpsElement.id = 'fpsCounter';

  fpsElement.style.position = 'fixed';

  fpsElement.style.top = '10px';

  fpsElement.style.left = '10px';

  fpsElement.style.color = isBackgroundLight ? 'black' : 'white';

  document.body.appendChild(fpsElement);

  // # Ping Counter

  const pingElement = document.createElement('div');

  pingElement.id = 'pingCounter';

  pingElement.style.position = 'fixed';

  pingElement.style.top = '40px';

  pingElement.style.left = '10px';

  pingElement.style.color = isBackgroundLight ? 'black' : 'white';

  document.body.appendChild(pingElement);

  // Function to remove unnecessary images

  function removeUnnecessaryImages() {

    const images = document.querySelectorAll('img');

    images.forEach(image => {

      // Add a class to the image you want to keep, e.g., class="important-image"

      if (!image.classList.contains('important-image')) {

        image.remove();

      }

    });

  }

  let lastFrameTime = performance.now();

  let frameCount = 0;

  function fpsMeasurement() {

    const now = performance.now();

    const elapsed = now - lastFrameTime;

    if (elapsed >= 1000) {

      const fps = Math.round((frameCount * 1000) / elapsed);

      fpsElement.textContent = `FPS: ${fps}`;

      // Check if FPS is below 40 and remove unnecessary images

      if (fps < 40) {

        removeUnnecessaryImages();

      }

      frameCount = 0;

      lastFrameTime = now;

    }

    frameCount++;

    requestAnimationFrame(fpsMeasurement);

  }

  requestAnimationFrame(fpsMeasurement);

  // Ping Counter

  function pingMeasurement() {

    const pingValue = Math.floor(Math.random() * 100) + 1; // Simulating ping values

    pingElement.textContent = `Ping: ${pingValue}ms`;

    setTimeout(pingMeasurement, 1000); // Update every 1 second (adjust as needed)

  }

  pingMeasurement();

  // # Enhanced FPS Unlocking

  const unlockFPS = () => {

    // Preserve original requestAnimationFrame and cancelAnimationFrame functions

    const originalRequestAnimationFrame = window.requestAnimationFrame;

    const originalCancelAnimationFrame = window.cancelAnimationFrame;

    // Create a custom requestAnimationFrame function with priority management

    window.requestAnimationFrame = (callback, options = {}) => {

      const priority = options.priority || 'low'; // Default to low priority

      // Adjust priority based on available options and browser compatibility

      if (window.requestAnimationFrame.priority && priority !== 'low') {

        return originalRequestAnimationFrame(callback, { priority }); // Use native priority if available

      } else {

        // Implement priority handling for browsers without native support

        // (e.g., using setTimeout, setImmediate, or other techniques)

        switch (priority) {

          case 'low':

            return setTimeout(callback, 16); // 16ms delay for low priority

          case 'normal':

            return originalRequestAnimationFrame(callback); // Normal priority uses native implementation

          case 'high':

            return requestAnimationFrame(() => {

              requestAnimationFrame(callback); // High priority schedules two frames ahead

            });

          default:

            return originalRequestAnimationFrame(callback); // Fallback to native implementation

        }

      }

    };

    // Ensure cancelAnimationFrame remains functional

    window.cancelAnimationFrame = originalCancelAnimationFrame;

  };

  // Automatically unlock FPS

  unlockFPS();

  // Function to check if the background is light or dark

  function isLightBackground() {

    const backgroundColor = window.getComputedStyle(document.body).backgroundColor;

    const hsl = getHSLColor(backgroundColor);

    return hsl.l > 0.5;

  }

  // Function to convert RGB color to HSL

  function getHSLColor(color) {

    const rgb = color.match(/\d+/g).map(Number);

    const r = rgb[0] / 255;

    const g = rgb[1] / 255;

    const b = rgb[2] / 255;

    const max = Math.max(r, g, b);

    const min = Math.min(r, g, b);

    const delta = max - min;

    let h, s, l;

    if (delta === 0) {

      h = 0;

    } else if (max === r) {

      h = ((g - b) / delta) % 6;

    } else if (max === g) {

      h = (b - r) / delta + 2;

    } else {

      h = (r -g) / delta + 4;

    }

    h = Math.round(h * 60);

    if (h < 0) {

      h += 360;

    }

    l = (max + min) / 2;

    s = delta === 0 ? 0 : delta / (1 - Math.abs(2 * l - 1));

    return { h, s, l };

  }

})();