// ==UserScript== // @name Via 浏览器 404 和网络连接失败页面美化 // @namespace https://viayoo.com/ // @version 2.1 // @license MIT // @description 为 Via 浏览器错误页面注入美化,404页面采用幻境风格,网络连接失败页面采用生命美学与科幻风格,支持日夜模式自动切换 // @author 是小白呀 & Grok & DeepSeek // @match *://*/* // @grant none // @run-at document-start // @downloadURL none // ==/UserScript== (function() { 'use strict'; // 配置参数 const CONFIG = { maxAttempts: 20, checkInterval: 50, injectionTimeout: 2000 }; // 页面错误类型检测 function detectErrorType() { const bodyText = document.body?.innerText || ''; const htmlContent = document.documentElement.innerHTML; // 404 页面检测标准 const has404Title = /404/.test(document.title) || /not found/i.test(document.title); const has404Body = /404/.test(bodyText) || /not found/i.test(bodyText); const hasNginx = /nginx/i.test(bodyText); const is404 = has404Title && has404Body && hasNginx; // 网络连接失败页面检测标准 const isNetworkError = isStrictChromeNetworkError(bodyText, htmlContent); if (is404 && isNetworkError) return 'both'; if (is404) return '404'; if (isNetworkError) return 'network'; return 'none'; } // Chrome 标准网络错误页面格式验证 function isStrictChromeNetworkError(bodyText, htmlContent) { // 验证标准错误页面文本结构 const hasExactFormat = /网页无法打开/.test(bodyText) && /位于\s*(https?:\/\/[^\s]+)\s*的网页无法加载,因为:/.test(bodyText) && /net::ERR_[A-Z_]+/.test(bodyText); // 验证错误页面结构特征 const isMinimalErrorPage = htmlContent.trim() === '' || document.body?.innerHTML.trim() === '' || (document.body && document.body.children.length <= 3) || /]*>\s*\s*<\/head>\s*/.test(htmlContent); return hasExactFormat && isMinimalErrorPage; } // 页面错误信息提取 function extractErrorInfo() { const bodyText = document.body?.innerText || ''; // 错误代码提取 const errorCodeMatch = bodyText.match(/(net::ERR_[A-Z_]+)/); // 目标地址提取 let url = '未知地址'; const urlMatch = bodyText.match(/位于\s*(https?:\/\/[^\s]+)\s*的网页无法加载/); if (urlMatch && urlMatch[1]) { url = urlMatch[1]; } else { const fallbackUrlMatch = bodyText.match(/(https?:\/\/[^\s]+)/i); url = fallbackUrlMatch ? fallbackUrlMatch[0] : (window.location.href || '未知地址'); } return { errorCode: errorCodeMatch?.[0] || (document.title.includes('404') ? '404 Not Found' : '网络连接错误'), server: /nginx/i.test(bodyText) ? 'nginx' : '', url: url }; } // 404 错误页面替换(幻境主题) function replace404Page() { const { errorCode, server } = extractErrorInfo(); document.documentElement.innerHTML = ` 迷失幻境

迷失幻境

${errorCode}
${server}

路径已断,迷雾笼罩,尝试返回现实吧。

`; } // 网络连接失败页面替换(科幻主题) function replaceConnectionErrorPage() { const { errorCode, url } = extractErrorInfo(); document.documentElement.innerHTML = ` 连接中断
⚡️

连接中断

无法感知星辰脉动,请检查网络或稍后重试。

目标地址: ${url}
错误代码: ${errorCode}
`; } // 脚本初始化流程 function initialize() { let injected = false; let attempts = 0; const tryInject = () => { if (injected) return true; const errorType = detectErrorType(); if (errorType === 'both') return true; if (errorType === '404') { replace404Page(); injected = true; return true; } if (errorType === 'network') { replaceConnectionErrorPage(); injected = true; return true; } return false; }; // 立即执行检测 if (tryInject()) return; // 早期检测循环 const interval = setInterval(() => { if (tryInject() || attempts >= CONFIG.maxAttempts) { clearInterval(interval); } attempts++; }, CONFIG.checkInterval); // 检测超时保护 setTimeout(() => clearInterval(interval), CONFIG.injectionTimeout); // DOM 变化监听 const observer = new MutationObserver(() => tryInject()); observer.observe(document.documentElement, { childList: true, subtree: true }); // DOM 加载完成检测 document.addEventListener('DOMContentLoaded', () => tryInject(), { once: true }); } // 脚本启动 initialize(); })();