跳到正文

科技·工程

洛谷网络错误屏蔽器

Jrc2014_dF约 11 分钟
原文

这是 2026年5月24日 存档的第 1 版,不是当前版本。查看当前版本

文章信息与版本

文章信息

发布
2026年5月24日
收录
2026年5月24日
更新
2026年11月10日
篇幅
约 11 分钟

版本

  1. v3当前
  2. v2
  3. v1
JavaScript
(function() {
  'use strict';
  
  console.log('[洛谷弹窗屏蔽] 插件已加载');
  
  // 策略1:阻止特定类型的弹窗显示
  function blockSpecificAlerts() {
    // 检查是否有 SweetAlert2 的容器存在
    const swalContainers = document.querySelectorAll('.swal2-container');
    swalContainers.forEach(container => {
      // 只处理包含特定内容的弹窗(如网络错误)
      const title = container.querySelector('.swal2-title');
      const content = container.querySelector('.swal2-html-container');
      
      if (title && (title.textContent.includes('网络错误') || 
                   title.textContent.includes('Network Error') ||
                   title.textContent.includes('加载失败'))) {
        console.log('[洛谷弹窗屏蔽] 检测到网络错误弹窗,正在移除');
        container.remove();
      }
    });
  }
  
  // 策略2:移除 html 元素上的 swal2-shown 类
  function removeSwalClasses() {
    if (document.documentElement.classList.contains('swal2-shown')) {
      document.documentElement.classList.remove('swal2-shown');
      console.log('[洛谷弹窗屏蔽] 已移除 swal2-shown 类');
    }
  }
  
  // 策略3:使用更温和的 CSS 隐藏而非删除
  function addBlockingCSS() {
    const styleId = 'luogu-swal-blocker';
    if (!document.getElementById(styleId)) {
      const style = document.createElement('style');
      style.id = styleId;
      style.textContent = `
        /* 隐藏网络错误相关的 SweetAlert2 弹窗 */
        .swal2-container:has(.swal2-title:contains("网络错误")),
        .swal2-container:has(.swal2-title:contains("Network Error")),
        .swal2-container:has(.swal2-title:contains("加载失败")) {
          display: none !important;
          opacity: 0 !important;
          visibility: hidden !important;
          pointer-events: none !important;
        }
        
        /* 移除页面遮罩效果 */
        .swal2-shown {
          overflow: auto !important;
        }
        .swal2-shown .swal2-container {
          background-color: transparent !important;
        }
        .swal2-overlay {
          background-color: transparent !important;
        }
      `;
      document.head.appendChild(style);
      console.log('[洛谷弹窗屏蔽] 已添加屏蔽CSS');
    }
  }
  
  // 策略4:监听 DOM 变化,但不直接删除元素
  function setupObserver() {
    const observer = new MutationObserver((mutations) => {
      let shouldCheck = false;
      
      for (const mutation of mutations) {
        // 检查是否有新节点添加
        if (mutation.addedNodes.length > 0) {
          for (const node of mutation.addedNodes) {
            if (node.nodeType === 1) { // 元素节点
              if (node.classList && 
                  (node.classList.contains('swal2-container') || 
                   node.classList.contains('swal2-overlay') ||
                   node.classList.contains('swal2-popup'))) {
                shouldCheck = true;
                break;
              }
            }
          }
        }
        
        // 检查类名变化
        if (mutation.type === 'attributes' && mutation.attributeName === 'class') {
          if (mutation.target.classList.contains('swal2-shown')) {
            removeSwalClasses();
          }
        }
      }
      
      if (shouldCheck) {
        setTimeout(blockSpecificAlerts, 100); // 延迟检查以确保内容已加载
      }
    });
    
    // 开始观察
    observer.observe(document.documentElement, {
      childList: true,
      subtree: true,
      attributes: true,
      attributeFilter: ['class']
    });
    
    console.log('[洛谷弹窗屏蔽] 已启动DOM观察器');
    return observer;
  }
  
  // 策略5:拦截 SweetAlert2 的显示函数(如果存在)
  function interceptSweetAlert() {
    if (window.swal || window.Swal) {
      const originalSwal = window.swal || window.Swal;
      const wrapper = function(...args) {
        // 检查参数中是否包含网络错误相关文本
        const firstArg = args[0];
        if (typeof firstArg === 'object') {
          if (firstArg.title && 
              (firstArg.title.includes('网络错误') || 
               firstArg.title.includes('Network Error') ||
               firstArg.title.includes('加载失败'))) {
            console.log('[洛谷弹窗屏蔽] 拦截了网络错误弹窗调用');
            return Promise.resolve({ isConfirmed: false, isDenied: false, isDismissed: true });
          }
        } else if (typeof firstArg === 'string' && 
                   (firstArg.includes('网络错误') || 
                    firstArg.includes('Network Error'))) {
          console.log('[洛谷弹窗屏蔽] 拦截了网络错误弹窗调用');
          return Promise.resolve({ isConfirmed: false, isDenied: false, isDismissed: true });
        }
        
        // 正常调用
        return originalSwal.apply(this, args);
      };
      
      // 替换全局函数
      window.swal = wrapper;
      window.Swal = wrapper;
      console.log('[洛谷弹窗屏蔽] 已拦截 SweetAlert2 函数');
    }
  }
  
  // 初始化
  function init() {
    // 1. 添加CSS规则
    addBlockingCSS();
    
    // 2. 移除现有swal类
    removeSwalClasses();
    
    // 3. 拦截 SweetAlert2
    interceptSweetAlert();
    
    // 4. 检查并移除现有弹窗
    setTimeout(blockSpecificAlerts, 500);
    
    // 5. 设置观察器
    const observer = setupObserver();
    
    // 6. 添加一个定时检查,以防万一
    const checkInterval = setInterval(() => {
      removeSwalClasses();
      blockSpecificAlerts();
    }, 2000);
    
    // 清理定时器(页面卸载时)
    window.addEventListener('beforeunload', () => {
      clearInterval(checkInterval);
      if (observer) observer.disconnect();
    });
  }
  
  // 等待页面加载完成后初始化
  if (document.readyState === 'loading') {
    document.addEventListener('DOMContentLoaded', init);
  } else {
    init();
  }
})();

AI 写的,功能如题