Ping Counter

Ping Counter for Bloxd.io

// ==UserScript==
// @name         Ping Counter
// @namespace    http://tampermonkey.net/
// @version      1.0
// @description  Ping Counter for Bloxd.io
// @author       Ankit
// @match        https://bloxd.io
// @icon         data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==
// @grant        none
// ==/UserScript==

class PingCounter {
    constructor(url) {
        this.url = url;
        this.pingCount = 0;

        // Create an element to display ping time
        this.pingTimeDisplay = document.createElement('div');
        this.pingTimeDisplay.id = 'pingTimeDisplay';
        this.pingTimeDisplay.innerText = 'Ping : ';

        // Style the ping time display
        this.pingTimeDisplay.style.position = 'fixed';  // Fix position
        this.pingTimeDisplay.style.top = '10px';       // Distance from the top
        this.pingTimeDisplay.style.left = '50%';       // Center horizontally
        this.pingTimeDisplay.style.transform = 'translateX(-50%)'; // Adjust for center alignment
        this.pingTimeDisplay.style.padding = '10px';   // Padding for better spacing
        this.pingTimeDisplay.style.backgroundColor = 'rgba(0, 0, 0, 0.7)'; // Semi-transparent background
        this.pingTimeDisplay.style.color = 'white';     // Text color
        this.pingTimeDisplay.style.borderRadius = '5px'; // Rounded corners
        this.pingTimeDisplay.style.fontSize = '20px';   // Font size
        this.pingTimeDisplay.style.zIndex = '1000';     // Ensure it's above other content

        // Append the element to the body
        document.body.appendChild(this.pingTimeDisplay);
    }

    ping() {
        const start = new Date().getTime();
        fetch(this.url, { method: 'HEAD', mode: 'no-cors' })
            .then(() => {
                const end = new Date().getTime();
                const pingTime = end - start;

                // Update ping time on screen
                this.pingTimeDisplay.innerText = `Ping : ${pingTime} ms`;
                this.pingCount++;
            })
            .catch((error) => {
                console.error('Ping failed:', error);
            });
    }

    startPinging(interval) {
        this.ping();
        setInterval(() => this.ping(), interval);
    }
}

// Usage
const pingCounter = new PingCounter('https://bloxd.io');
pingCounter.startPinging(1500); // Ping every 1.5 seconds