// ==UserScript== // @name China ISP P2P Speed Test // @namespace http://yoursite.com // @version 1.0 // @description 测试三大运营商的P2P延迟与带宽(WebRTC) // @match *://*/* // @grant none // ==/UserScript== (function() { 'use strict'; // 仅在特定测速页面运行(可改成你自己的测速站) if (!location.hostname.includes('speedtest.net') && !location.hostname.includes('fast.com')) { return; } console.log('P2P测速脚本已启动...'); // WebRTC P2P测速逻辑 async function testP2PSpeed() { try { const pc = new RTCPeerConnection({ iceServers: [{ urls: 'stun:stun.l.google.com:19302' }] }); const channel = pc.createDataChannel('speedTest'); let startTime, receivedBytes = 0; channel.onopen = () => { console.log('P2P连接已建立,开始测速...'); startTime = performance.now(); // 模拟发送数据 const data = new Uint8Array(65535); for (let i = 0; i < 100; i++) { channel.send(data); } }; channel.onmessage = (event) => { receivedBytes += event.data.length; }; pc.oniceconnectionstatechange = () => { if (pc.iceConnectionState === 'disconnected' || pc.iceConnectionState === 'failed') { const duration = (performance.now() - startTime) / 1000; const speedMbps = (receivedBytes * 8 / duration / 1e6).toFixed(2); console.log(`P2P测速完成:${speedMbps} Mbps`); alert(`P2P测速完成:${speedMbps} Mbps`); pc.close(); } }; const offer = await pc.createOffer(); await pc.setLocalDescription(offer); } catch (err) { console.error('测速出错:', err); } } // 延迟执行,确保页面加载完成 setTimeout(testP2PSpeed, 3000); })();