版本比较
洛谷网络错误屏蔽器
正文
增加 52 行删除 30 行
1234567891011121314151617181920212223242526272829303132333435368 行未改动4546474849505152537 行未改动616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183```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)) {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') ||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. 拦截 SweetAlert2interceptSweetAlert();// 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 写的,功能如题1234567891011121314151617181920212223242526272829303132333435368 行未改动4546474849505152537 行未改动616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205```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)) {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') ||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. 拦截 SweetAlert2interceptSweetAlert();// 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 写的,功能如题