网页加载分析

测试网页加载延迟,显示延迟最高的三个域名。Test the webpage loading speed and display the domain names of the three slowest loading URLs.

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         网页加载分析
// @version      1.08
// @description  测试网页加载延迟,显示延迟最高的三个域名。Test the webpage loading speed and display the domain names of the three slowest loading URLs.
// @match        *://*/*
// @run-at       document-start
// @author         yzcjd
// @author2       Lama AI 辅助
// @namespace    https://greasyfork.org/users/1171320
// @exclude      *://*.cloudflare.com/*
// @exclude      *://*.recaptcha.net/*
// @license      MIT
// ==/UserScript==

(function() {
    'use strict';

    const loadTimeElement = document.createElement('div');
    loadTimeElement.id = 'loadTimeDisplay';
    loadTimeElement.style.cssText = `
            position: fixed;
            top: 90%;
            left: 50%;
            transform: translate(-50%, -50%);
            background: grey;
            padding: 5px;
            border-radius: 8px;
            box-shadow: 0 0 15px rgba(0,0,0,0.3);
            white-space: nowrap;
            width: 400px;
            z-index: 9999;
            background-color: #f5f5f5;
            color: black;
    `;

    const startTime = performance.now();
    let slowestRequests = [];

    const networkObserver = new PerformanceObserver((list, observer) => {
        const entries = list.getEntries();
        entries.forEach(entry => {
            slowestRequests.push({ name: entry.name, duration: entry.duration });
            slowestRequests.sort((a, b) => b.duration - a.duration);
            slowestRequests = slowestRequests.slice(0, 3);
        });
    });

    networkObserver.observe({ entryTypes: ['resource'] });

    window.addEventListener('load', () => {
        const endTime = performance.now();
        const timeElapsed = endTime - startTime;

        let networkInfo = '';
        if (slowestRequests.length > 0) {
            networkInfo = slowestRequests.map(req => {
                try {
                    const url = new URL(req.name);
                    return `slow: ${url.hostname} (${req.duration.toFixed(2)}ms)<br>`;
                } catch (error) {
                    return `slow: Invalid URL (${req.duration.toFixed(2)}ms)<br>`;
                }
            }).join('');
        } else {
            networkInfo = '[none]';
        }

        loadTimeElement.innerHTML = `
            <h2>Time: ${timeElapsed.toFixed(2)}ms</h2>
            ${networkInfo}
        `;

        document.body.appendChild(loadTimeElement);

        setTimeout(() => {
            loadTimeElement.remove();
        }, 5000);
    });
})();