// ==UserScript== // @name Via 浏览器 404 和网络连接失败页面美化 // @namespace https://viayoo.com/ // @version 1.12 // @description 为 Via 浏览器错误页面(404 Not Found 和网络连接失败)注入美化,404 页面采用幻境风格,网络连接失败页面采用生命美学与科幻风格,支持日夜模式自动切换 // @author 是小白呀 & Grok // @match *://*/* // @license MIT // @grant none // @downloadURL none // ==/UserScript== (function() { 'use strict'; // 提取错误页面中的 URL(用于网络连接失败页面) function extractErrorUrl() { const bodyText = document.body ? document.body.innerText : ''; const urlMatch = bodyText.match(/(https?:\/\/[^\s]+)/i); return urlMatch ? urlMatch[0] : '未知地址'; } // 提取错误代码和服务器信息(用于 404 页面和网络连接失败页面) function extractErrorInfo() { const bodyText = document.body ? document.body.innerText : ''; const errorCodeMatch = bodyText.match(/net::ERR_[A-Z_]+/) || document.title.match(/404 Not Found/) || ['未知错误']; const serverMatch = bodyText.match(/nginx/i) ? 'nginx' : ''; return { errorCode: errorCodeMatch[0], server: serverMatch }; } // 检查是否为 404 错误页面(严格匹配“404 Not Found”和“nginx”) function checkFor404Page() { let has404 = false; let hasNginx = false; // 检查标题 if (/404 Not Found/i.test(document.title)) { has404 = true; } // 检查 body 内容 if (document.body) { const text = document.body.innerText || ''; if (/404 Not Found/i.test(text)) { has404 = true; } if (/nginx/i.test(text)) { hasNginx = true; } } // 必须同时满足“404 Not Found”和“nginx” return has404 && hasNginx; } // 检查是否为网络连接失败页面(不排除 404,用于混合检查) function isConnectionError() { // 快速检查标题 if (/无法打开|Error/i.test(document.title)) { return true; } // 检查 body 内容 if (document.body) { const text = document.body.innerText || ''; if (/net::ERR_|网页无法打开|无法连接|ERR_NAME_NOT_RESOLVED/i.test(text)) { return true; } // 空页面 if (document.body.innerHTML.trim() === '') { return true; } } return false; } // 检查是否同时包含 404 和网络连接失败内容 function checkForBothErrors() { return checkFor404Page() && isConnectionError(); } // 替换 404 错误页面(幻境风格) function replace404Page() { const { errorCode, server } = extractErrorInfo(); document.documentElement.innerHTML = ` 迷失幻境

迷失幻境

${errorCode}
${server}

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

`; } // 替换网络连接失败页面(科幻风格,生命美学日间模式) function replaceConnectionErrorPage() { const errorUrl = extractErrorUrl(); const { errorCode } = extractErrorInfo(); document.documentElement.innerHTML = ` 连接中断
⚡️

连接中断

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

目标地址: ${errorUrl}
错误代码: ${errorCode}
`; } // 快速注入机制 function initInjection() { if (checkForBothErrors()) { return false; } if (checkFor404Page()) { replace404Page(); return true; } if (isConnectionError()) { replaceConnectionErrorPage(); return true; } return false; } // 立即检查 initInjection(); // 早期高频检查 let attempts = 0; const maxAttempts = 20; // 最多检查 1 秒(50ms x 20) const earlyCheckInterval = setInterval(() => { if (initInjection()) { clearInterval(earlyCheckInterval); } else if (attempts >= maxAttempts) { clearInterval(earlyCheckInterval); } attempts++; }, 50); // 监听 DOM 变化 const observer = new MutationObserver((mutations, obs) => { if (initInjection()) { obs.disconnect(); } }); // 尽早观察 documentElement observer.observe(document.documentElement, { childList: true, subtree: true }); // 页面加载完成时再检查 document.addEventListener('DOMContentLoaded', () => { initInjection(); }, { once: true }); })();