Greasy Fork

图片样式屏蔽器

隐藏所有图片元素,可以用来看网页小说和视频,脚本菜单启用/禁用脚本

目前为 2024-03-03 提交的版本。查看 最新版本

// ==UserScript==
// @name         图片样式屏蔽器
// @version      1.4
// @description  隐藏所有图片元素,可以用来看网页小说和视频,脚本菜单启用/禁用脚本
// @author       ChatGPT
// @match        *://*/*
// @grant        GM_registerMenuCommand
// @grant        GM_unregisterMenuCommand
// @grant        GM_setValue
// @grant        GM_getValue
// @run-at       document-start
// @namespace https://greasyfork.org/users/452911
// ==/UserScript==

(function() {
    'use strict';

    const currentHostname = window.location.hostname;

    // 在初始化时检查是否应用样式屏蔽器
    checkAndApplyImageStyleBlocker();

    // 动态创建或更新菜单项
    function updateMenu() {
        const useImageStyleBlocker = GM_getValue(`use_image_style_blocker_${currentHostname}`, false);
        if (useImageStyleBlocker) {
            GM_registerMenuCommand("禁用图片样式屏蔽器", () => {
                GM_setValue(`use_image_style_blocker_${currentHostname}`, false);
                window.location.reload(); // 禁用后立即刷新页面以移除效果
            });
        } else {
            GM_registerMenuCommand("启用图片样式屏蔽器", () => {
                GM_setValue(`use_image_style_blocker_${currentHostname}`, true);
                applyImageStyleBlocker();
                updateMenu(); // 更新菜单项
            });
        }
    }

    function checkAndApplyImageStyleBlocker() {
        const useImageStyleBlocker = GM_getValue(`use_image_style_blocker_${currentHostname}`, false);
        updateMenu(); // 初始时更新菜单项
        if (useImageStyleBlocker) {
            applyImageStyleBlocker();
        }
    }

    function applyImageStyleBlocker() {
        let style = document.createElement('style');
        style.innerHTML = `img,[style*='height:'][style*='width:'] {display: none !important;visibility: hidden; opacity: 0; z-index: -999; width: 0; height: 0; pointer-events: none; position: absolute; left: -9999px; top: -9999px;}`;
        document.head.appendChild(style);
    }
})();