// ==UserScript== // @name 屏蔽所有网站的图片和图标 (支持 Alt+P 切换及状态保存) // @namespace http://tampermonkey.net/ // @version 1.3 // @description 强制隐藏所有网站图片和图标。按 Alt+P 快捷键可随时开启或关闭,状态全局保存。 // @author Panda-Young // @license MIT // @match *://*/* // @run-at document-start // @grant GM_setValue // @grant GM_getValue // @grant GM_addValueChangeListener // @grant GM_info // @downloadURL none // ==/UserScript== (function() { 'use strict'; const STORAGE_KEY = 'global_image_block_state'; // 双重存储读取逻辑:优先使用油猴 API,如果由于某种原因 API 失效,退化为 localStorage let isBlocked = true; try { if (typeof GM_getValue === 'function') { isBlocked = GM_getValue(STORAGE_KEY, true); } else { let localVal = localStorage.getItem(STORAGE_KEY); isBlocked = localVal !== null ? (localVal === 'true') : true; } } catch (e) { console.warn("读取状态失败,采用默认值(开启屏蔽)"); } // 定义要注入的全局 CSS const blockStyle = ` img, picture, svg, canvas, video, image { display: none !important; opacity: 0 !important; visibility: hidden !important; width: 0 !important; height: 0 !important; max-width: 0 !important; max-height: 0 !important; } * { background-image: none !important; } [class*="icon" i], [id*="icon" i], [class*="logo" i], [id*="logo" i], [class*="avatar" i], [id*="avatar" i], [class*="img" i], [id*="img" i], [class*="fa-" i], [class^="el-icon" i], [class^="glyphicon" i] { display: none !important; background-image: none !important; } *::before, *::after { background-image: none !important; } `; // 注入或更新样式的函数 function injectOrUpdateStyles() { let styleNode = document.getElementById('no-images-strict-style'); if (!styleNode) { styleNode = document.createElement('style'); styleNode.id = 'no-images-strict-style'; styleNode.type = 'text/css'; (document.head || document.documentElement).appendChild(styleNode); } styleNode.textContent = isBlocked ? blockStyle : ''; } // 屏幕中央顶部提示框(Toast) function showToast(message) { let toast = document.getElementById('no-images-toast'); if (!toast) { toast = document.createElement('div'); toast.id = 'no-images-toast'; toast.style.cssText = ` position: fixed; top: 20px; left: 50%; transform: translateX(-50%); padding: 10px 20px; background: rgba(0, 0, 0, 0.75); color: #fff; font-size: 14px; font-family: sans-serif; border-radius: 6px; z-index: 2147483647; pointer-events: none; transition: opacity 0.3s ease; `; document.documentElement.appendChild(toast); } toast.textContent = message; toast.style.opacity = '1'; clearTimeout(toast.timer); toast.timer = setTimeout(() => { toast.style.opacity = '0'; }, 2000); } // 保存状态到本地的函数 function saveState(state) { isBlocked = state; try { if (typeof GM_setValue === 'function') { GM_setValue(STORAGE_KEY, state); } localStorage.setItem(STORAGE_KEY, state); } catch (e) {} } // 初始化执行 injectOrUpdateStyles(); document.addEventListener('DOMContentLoaded', injectOrUpdateStyles); const observer = new MutationObserver(() => { if (!document.getElementById('no-images-strict-style')) { injectOrUpdateStyles(); } }); observer.observe(document.documentElement, { childList: true, subtree: false }); // 监听键盘按键,捕获 Alt + P window.addEventListener('keydown', (e) => { if (e.altKey && e.key.toLowerCase() === 'p') { e.preventDefault(); let newState = !isBlocked; saveState(newState); // 永久保存新状态 injectOrUpdateStyles(); showToast(newState ? '🚫 已开启:屏蔽所有图片和图标' : '🖼️ 已关闭:恢复显示图片和图标'); } }, true); // 跨标签页状态同步 if (typeof GM_addValueChangeListener === 'function') { GM_addValueChangeListener(STORAGE_KEY, function(name, old_value, new_value, remote) { if (remote && isBlocked !== new_value) { isBlocked = new_value; injectOrUpdateStyles(); showToast(isBlocked ? '🔄 同步开启:屏蔽所有图片' : '🔄 同步关闭:恢复显示图片'); } }); } })();