Greasy Fork

Greasy Fork is available in English.

网页加载分析(改)

测试网页加载速度并显示加载最慢的三个网址的域名,二改添加了对Via浏览器toast的调用。

当前为 2025-04-15 提交的版本,查看 最新版本

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         网页加载分析(改)
// @version      1.11
// @description  测试网页加载速度并显示加载最慢的三个网址的域名,二改添加了对Via浏览器toast的调用。
// @description:en Test the webpage loading speed and display the domain names of the three slowest loading URLs.
// @match        *://*/*
// @run-at       document-start
// @author       yzcjd & nobody
// @author2      Lama AI 辅助
// @namespace    https://scriptcat.org/zh-CN/users/157252
// @exclude      *://*.cloudflare.com/*
// @exclude      *://*.recaptcha.net/*
// @license      MIT
// @grant        GM_setValue
// @grant        GM_getValue
// @grant        GM_registerMenuCommand
// ==/UserScript==

(function() {
    'use strict';

    if (typeof GM_registerMenuCommand === 'function') {
        // 记录网站是否开启测试
        const currentDomain = location.hostname || 'unknown';
        const domainSettings = GM_getValue('domainSettings', {});
        const isEnabled = domainSettings[currentDomain] !== false;
        GM_registerMenuCommand(`${isEnabled ? '禁用' : '启用'} 网页加载测试(${currentDomain})`, () => {
            domainSettings[currentDomain] = !isEnabled;
            GM_setValue('domainSettings', domainSettings);
            alert(`网页加载测试已${!isEnabled ? '启用' : '禁用'}(${currentDomain}),刷新页面生效。`);
        });
        if (!isEnabled) {
            return;
        }
    }

    const loadTimeElement = document.createElement('div');
    loadTimeElement.id = 'loadTimeDisplay';
    loadTimeElement.style.cssText = `
        position: fixed;
        top: 90%;
        left: 50%;
        transform: translate(-50%, -50%);
        background: rgba(255, 255, 255, 0.3); /* 半透明毛玻璃背景 */
        backdrop-filter: blur(16px); /* 高斯模糊 */
        -webkit-backdrop-filter: blur(16px); /* 兼容 Safari */
        padding: 12px 20px; /* 舒适内边距 */
        border-radius: 22px; /* iOS 风格圆角 */
        box-shadow: 0 6px 24px rgba(0, 0, 0, 0.15); /* 柔和阴影 */
        white-space: nowrap;
        width: auto; /* 自适应宽度 */
        max-width: 90%; /* 防止溢出 */
        z-index: 9999;
        color: #1C2526; /* 深色文字,确保可读 */
        font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif; /* iOS 字体 */
        font-size: 15px; /* 优雅字体大小 */
        font-weight: 500; /* 适中字重 */
        text-align: center; /* 文字居中 */
        border: 1px solid rgba(255, 255, 255, 0.25); /* 微弱边框增加质感 */
        opacity: 0; /* 初始透明 */
        transition: opacity 0.3s ease-in-out; /* 淡入动画 */
    `;

    // 降级兼容:如果不支持 backdrop-filter,使用纯色背景
    if (!CSS.supports('backdrop-filter', 'blur(12px)')) {
        loadTimeElement.style.background = 'rgba(240, 240, 240, 0.85)';
    }

    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 = '';
        let networkInfoHTML = '';
        if (slowestRequests.length > 0) {
            networkInfo = slowestRequests.map(req => {
                try {
                    const url = new URL(req.name);
                    return `Slow: ${url.hostname} (${req.duration.toFixed(2)}ms)`;
                } catch (error) {
                    return `Slow: Invalid URL (${req.duration.toFixed(2)}ms)`;
                }
            }).join('\n');
            networkInfoHTML = 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]';
            networkInfoHTML = '[none]';
        }

        if (window.via && typeof window.via.toast === 'function') {
            const message = `Time: ${timeElapsed.toFixed(2)}ms\n${networkInfo}`;
            window.via.toast(message);
        } else {
            loadTimeElement.innerHTML = `
                <h2 style="margin: 0; font-size: 16px; font-weight: 600;">Time: ${timeElapsed.toFixed(2)}ms</h2>
                ${networkInfoHTML}
            `;
            document.body.appendChild(loadTimeElement);
            setTimeout(() => {
                loadTimeElement.style.opacity = '1';
            }, 100);
            setTimeout(() => {
                loadTimeElement.style.opacity = '0';
                setTimeout(() => {
                    loadTimeElement.remove();
                }, 300);
            }, 2600);
        }
    });
})();