Greasy Fork

Greasy Fork is available in English.

网络延时监控|附:外区苹果ID获取|科学上网|点击获取(可查看付费的苹果ID|免费获取小火箭|免费获取clash订阅|免费获取v2ray订阅|获取小火箭IOS订阅|目前订阅9TB流量/月)

实用工具-实时监控网络波动,显示在页面底部中心位置,可编辑监控自定义网址。点击获取(可查看付费的苹果ID|免费获取小火箭|免费获取clash订阅|免费获取v2ray订阅|获取小火箭IOS订阅|目前订阅9TB流量/月)

当前为 2023-08-05 提交的版本,查看 最新版本

// ==UserScript==
// @name         网络延时监控|附:外区苹果ID获取|科学上网|点击获取(可查看付费的苹果ID|免费获取小火箭|免费获取clash订阅|免费获取v2ray订阅|获取小火箭IOS订阅|目前订阅9TB流量/月)
// @namespace    http://tampermonkey.net/
// @version      1.2
// @description  实用工具-实时监控网络波动,显示在页面底部中心位置,可编辑监控自定义网址。点击获取(可查看付费的苹果ID|免费获取小火箭|免费获取clash订阅|免费获取v2ray订阅|获取小火箭IOS订阅|目前订阅9TB流量/月)
// @author       Nikita
// @match        *://*/*
// @grant        GM_addStyle
// @license      
// @note         23-08-05 1.1 小改动
// @note         23-08-05 1.0 初版发布
// ==/UserScript==

(function() {
    'use strict';
///////////////////////////////  编辑监控自定义网址  ///////////////////////////////////////////////

    const defaultUrls = [
        { name: '腾讯', url: 'www.qq.com' },          //编辑监控自定义网址,复制一行重新编辑即可;
        { name: 'GitHub', url: 'api.github.com' },    //编辑监控自定义网址,复制一行重新编辑即可;
        { name: '谷歌', url: 'www.google.com' },      //编辑监控自定义网址,复制一行重新编辑即可;
    ];

////////////////////////////////////////////////////////////////////////////////////////////////////

    const usageInstructions = "说明: \n 1. 打开脚本编辑监控自定义网址;\n 2. 实时延时监控刷新 频率3秒/次,数据消耗不到2KB/次;\n 3. 延时信息将显示在底部中心位置,支持左右拖动; \n 4. 0-600ms为绿色,600-1000为橙色,大于1000 红色; \n 5. 点击【获取】(可查看付费ID|获取小火箭|获取clash订阅|获取v2ray订阅)\n 6. 更多 待续更新...\n";
    let latencyData = {};
    let isDragging = false;
    let offsetX;

    let expanded = true;
    let displayPosition = 'bottom';

    function calculateLatency(url, callback) {
        const startTime = new Date().getTime();
        fetch('https://' + url, { method: 'HEAD', mode: 'no-cors', cache: 'no-store' })
            .then(response => {
                const endTime = new Date().getTime();
                const latency = endTime - startTime;
                callback(url, latency);
            })
            .catch(error => {
                callback(url, 'Timeout');
            });
    }

    function updateLatency() {
        calculateLatency(window.location.host, (url, latency) => {
            latencyData['当前'] = latency;
            updateDisplay();
        });
        defaultUrls.forEach(item => {
            calculateLatency(item.url, (url, latency) => {
                latencyData[item.name] = latency;
                updateDisplay();
            });
        });
    }

    function updateDisplay() {
        const displayContainer = document.getElementById('latency-display-container');
        if (displayContainer) {
            displayContainer.setAttribute('title', usageInstructions);

            let displayHtml = `当前: <span style="color: ${getLatencyColor(latencyData['当前'])}; font-weight: bold;">${formatLatency(latencyData['当前'])}</span> | `;
            if (expanded) {
                for (const item of defaultUrls) {
                    const name = item.name;
                    const latency = latencyData[name];
                    displayHtml += `${name}: <span style="color: ${getLatencyColor(latency)}; font-weight: bold;">${formatLatency(latency)}</span> | `;
                }
            }
            displayHtml += `<a href="https://free.dabai.in"><span style="color: ${getLatencyColor(latencyData['当前'])}; font-weight: bold;">获取►</span></a>`;

            displayContainer.innerHTML = displayHtml;
        }
    }

    function formatLatency(latency) {
        return latency === 'Timeout' ? '超时' : latency + 'ms';
    }

    function getLatencyColor(latency) {
        if (latency === 'Timeout') {
            return 'gray';
        } else if (latency < 600) {
            return 'green';
        } else if (latency <= 1000) {
            return 'orange';
        } else {
            return 'red';
        }
    }

    function updateDisplayPosition() {
        const displayContainer = document.getElementById('latency-display-container');
        if (displayContainer) {
            if (displayPosition === 'bottom') {
                displayContainer.style.position = 'fixed';
                displayContainer.style.bottom = '10px';
                displayContainer.style.left = '50%';
                displayContainer.style.transform = 'translateX(-50%)';
                displayContainer.style.flexDirection = 'row';
            } else if (displayPosition === 'right') {
                displayContainer.style.position = 'fixed';
                displayContainer.style.top = '50%';
                displayContainer.style.right = '10px';
                displayContainer.style.transform = 'translateY(-50%)';
                displayContainer.style.flexDirection = 'column';
            } else if (displayPosition === 'left') {
                displayContainer.style.position = 'fixed';
                displayContainer.style.top = '50%';
                displayContainer.style.left = '10px';
                displayContainer.style.transform = 'translateY(-50%)';
                displayContainer.style.flexDirection = 'column';
            }
                displayContainer.addEventListener('mousedown', (event) => {
            isDragging = true;
            offsetX = event.clientX - displayContainer.getBoundingClientRect().left;
        });
                window.addEventListener('mousemove', (event) => {
            if (isDragging) {
                const left = event.clientX - offsetX;
                displayContainer.style.left = left + 'px';
            }
        });

        window.addEventListener('mouseup', () => {
            isDragging = false;
        });
        }
    }

    GM_addStyle(`
        #latency-display-container {
            display: flex;
            align-items: center;
            justify-content: center;
            background-color: rgba(255, 255, 255, 0.3);
            padding: 5px 10px;
            border: 1px solid black;
            border-radius: 5px;
            color: black;
            font-size: 14px;
            z-index: 9999;
            cursor: help; /* Show help cursor on hover */
        }
    #latency-display-container::before {
        content: attr(title);
        position: absolute;
        bottom: 100%;
        left: 50%;
        transform: translateX(-50%);
        background-color: rgba(255, 255, 255, 0);
        color: black;
        padding: 5px;
        border: 1px solid black;
        border-radius: 5px;
        font-size: 12px;
        white-space: nowrap;
        opacity: 0;
        pointer-events: none;
        transition: opacity 0.3s;
    }
`);

    const displayContainer = document.createElement('div');
    displayContainer.id = 'latency-display-container';
    document.body.appendChild(displayContainer);

    if (window.self === window.top) {
        updateDisplayPosition();
        updateLatency();
        setInterval(updateLatency, 3000);
    }
})();