版本比较
洛谷网络错误屏蔽器
正文
增加 52 行删除 30 行
12344566789101112131415161516171819202122232424252627282930313232333435368 行未改动454647484949505152537 行未改动61626364656566676869707071727374757677767778798081828384858686878889909192939494959697989999100101102103104105106107107108109110111111112113114115116117118119120121120121122123124125126127128127128129130131132133133134135136137137138139140141142143144144145146147148149149150151152152153154155155156157158158159160161161162163164165166167167168169170171172173174174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205```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('网络错误') ||
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)) {8 行未改动 opacity: 0 !important; visibility: hidden !important; pointer-events: none !important; }
/* 移除页面遮罩效果 */ .swal2-shown { overflow: auto !important; }7 行未改动 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') || 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('网络错误') || 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('网络错误') || } 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(); }})();```manifest:```javascript{ "manifest_version": 3, "name": "洛谷网络错误弹窗屏蔽器", "version": "1.0", "description": "屏蔽洛谷题目页面的网络错误弹窗和遮罩", "permissions": [], "content_scripts": [ { "matches": ["https://*.luogu.com.cn/problem/*"], "js": ["content.js"], "run_at": "document_end", "all_frames": true } ], "host_permissions": [ "https://*.luogu.com.cn/*" ]}```
AI 写的,功能如题