// ==UserScript== // @name WebView 错误美化 // @namespace https://viayoo.com/h88v22 // @version 1.1 // @description 基于MIUIX设计语言重绘的 WebView 错误页面,并且给出一定程度上的解决方案。 // @author Aloazny && Gemini // @run-at document-start // @match *://*/* // @license MIT // @grant none // ==/UserScript== (function() { 'use strict'; const ERROR_PATTERNS = [ /ERR_CONNECTION_REFUSED/i, /ERR_CONNECTION_TIMED_OUT/i, /ERR_INTERNET_DISCONNECTED/i, /ERR_CONNECTION_CLOSED/i, /ERR_NAME_NOT_RESOLVED/i, /ERR_SSL_PROTOCOL_ERROR/i, /ERR_PROXY_CONNECTION_FAILED/i, /ERR_CONNECTION_RESET/i, /ERR_CONNECTION_ABORTED/i, /ERR_NETWORK_CHANGED/i, /ERR_ADDRESS_UNREACHABLE/i, /ERR_ADDRESS_INVALID/i, /ERR_DNS_TIMED_OUT/i, /ERR_DNS_SERVER_FAILED/i, /ERR_SSL_VERSION_OR_CIPHER_MISMATCH/i, /ERR_CERT_AUTHORITY_INVALID/i, /ERR_CERT_DATE_INVALID/i, /ERR_CERT_COMMON_NAME_INVALID/i, /ERR_EMPTY_RESPONSE/i, /ERR_INVALID_RESPONSE/i, /ERR_CONTENT_LENGTH_MISMATCH/i, /ERR_TUNNEL_CONNECTION_FAILED/i, /ERR_TIMED_OUT/i, /ERR_FAILED/i, /ERR_ACCESS_DENIED/i, /ERR_BLOCKED_BY_CLIENT/i, /ERR_BLOCKED_BY_RESPONSE/i, /ERR_TOO_MANY_REDIRECTS/i, /ERR_UNSAFE_PORT/i, /ERR_UNSAFE_REDIRECT/i, /DNS_PROBE_FINISHED_NO_INTERNET/i, /DNS_PROBE_FINISHED_NXDOMAIN/i, /DNS_PROBE_STARTED/i, /PR_CONNECT_RESET_ERROR/i, /PR_END_OF_FILE_ERROR/i, /NS_ERROR_NET_TIMEOUT/i, /NS_ERROR_CONNECTION_REFUSED/i, /NS_ERROR_NET_RESET/i, /NS_ERROR_PROXY_CONNECTION_REFUSED/i ]; let isApplied = false; function detect() { if (!document.body || isApplied) return false; const url = window.location.href; const text = document.body.textContent; const isSearchPage = (function() { const searchParams = ["q", "s", "p", "wd", "word", "keyword", "text", "query", "key", "result", "searchWord", "search-result"]; const searchPaths = ['/search', '/s', '/query', '/google', '/bing', '/baidu']; const hasQueryParam = searchParams.some(p => new RegExp(`[?&]${p}=`, 'i').test(url)); const hasSearchPath = searchPaths.some(p => url.includes(p)); const hasGeneralSearchPattern = /[?&](q|word|query|wd)=/.test(url); return hasQueryParam || hasSearchPath || hasGeneralSearchPattern; })(); if (isSearchPage && text.length > 1000) return false; const isInternalError = url.startsWith('chrome-error://') || url.includes('chromewebdata') || window.location.protocol === 'chrome-error:'; const hasErrorElement = !!document.querySelector('#main-frame-error, .error-code, .neterror, #main-message'); const hasErrorCode = ERROR_PATTERNS.some(p => p.test(text)); const isSimplePage = text.length < 1500; return isInternalError || (hasErrorElement && isSimplePage) || (hasErrorCode && isSimplePage && !isSearchPage); } function getInfo() { const text = document.body ? document.body.textContent : ""; const match = text.match(/(ERR_[A-Z_]+|DNS_[A-Z_]+|SSL_[A-Z_]+|CERT_[A-Z_]+|PROXY_[A-Z_]+|NS_ERROR_[A-Z_]+|PR_[A-Z_]+)/i); const code = match ? match[0].toUpperCase() : "ERR_FAILED"; let type = '网络错误', desc = '无法访问此网站,请检查网络连接', help = '
  • 检查数据流量或 Wi-Fi 连接
  • 尝试关闭并重新开启飞行模式
  • '; if (/TIMED_OUT|TIMEOUT/.test(code)) { type = '连接超时'; desc = '服务器响应时间过长'; help = '
  • 检查网络信号是否稳定
  • 尝试刷新页面或稍后再试
  • 检查防火墙或代理服务器设置
  • '; } else if (/REFUSED/.test(code)) { type = '连接被拒绝'; desc = '目标服务器拒绝了连接请求'; help = '
  • 核对网址拼写是否正确
  • 该网站可能暂时关闭或维护
  • 检查本地防火墙拦截记录
  • '; } else if (/DISCONNECTED|NO_INTERNET/.test(code)) { type = '网络已断开'; desc = '当前未连接到互联网'; help = '
  • 检查网线、调制解调器和路由器
  • 重新连接 Wi-Fi 或移动数据
  • 检查是否欠费停机
  • '; } else if (/CLOSED|RESET|ABORTED/.test(code)) { type = '连接中断'; desc = '与服务器的连接意外丢失'; help = '
  • 网络环境切换可能导致此问题
  • 尝试重新加载网页
  • 检查 VPN 或加速器连接状态
  • '; } else if (/NAME_NOT_RESOLVED|NXDOMAIN|DNS_/.test(code)) { type = 'DNS 解析失败'; desc = '找不到服务器的 IP 地址'; help = '
  • 检查网址是否拼写错误
  • 尝试修改 DNS 为 223.5.5.5 或 8.8.8.8
  • 清除浏览器 DNS 缓存
  • '; } else if (/SSL_|CERT_|PROTOCOL/.test(code)) { type = '安全连接失败'; desc = '网页使用了不安全的证书或协议'; help = '
  • 检查系统日期和时间是否准确
  • 该网站证书可能已过期或不可信
  • 避免在公共网络输入敏感信息
  • '; } else if (/PROXY_/.test(code)) { type = '代理错误'; desc = '代理服务器连接异常'; help = '
  • 检查系统或浏览器的代理设置
  • 尝试禁用 VPN 或第三方代理工具
  • 联系网络管理员获取正确配置
  • '; } else if (/ACCESS_DENIED|BLOCKED/.test(code)) { type = '访问受阻'; desc = '请求被客户端或服务器拦截'; help = '
  • 检查广告过滤插件设置
  • 该页面可能需要特定的访问权限
  • 尝试清除 Cookie 后重新登录
  • '; } return { code, type, desc, help }; } function render() { if (isApplied) return; isApplied = true; const data = getInfo(); const html = ` ${data.type}

    ${data.type}

    页面加载失败

    ${data.desc}
    建议操作:
    CODE: ${data.code}
    TIME: ${new Date().toLocaleString()}
    `; document.open(); document.write(html); document.close(); } const main = () => { if (detect()) render(); }; const obs = new MutationObserver(main); if (document.documentElement) obs.observe(document.documentElement, { childList: true, subtree: true }); window.addEventListener('load', main); setTimeout(main, 150); })();