Greasy Fork

Greasy Fork is available in English.

自己的互联网世界

根据自定义的规则屏蔽网页元素

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey,才能安装此脚本。

您需要先安装用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         自己的互联网世界
// @namespace    http://tampermonkey.net/
// @version      0.1.1
// @description  根据自定义的规则屏蔽网页元素
// @author       jahv
// @match        *://*/*
// @grant        none
// @license     MIT
// ==/UserScript==

(function () {
    "use strict";

    // 第一部分:定义
    const ruleSet = {
        "blog.csdn.net": {
            hideElement: [
                ".passport-login-container",
                ".wap-shadowbox",
                ".feed-Sign-weixin",
                "#operate",
                ".btn_open_app_prompt_div",
                ".open_app_channelCode",
                ".csdn-toolbar",
                "#recommend",
                ".aside-header-fixed"
            ],
        },
        "another-example.com": {
            hideElement: ".banner",
            customAction: () => {
                console.log("执行自定义操作");
            },
        },
    };

    // 第二部分:实现
    function applyRules() {
        const currentUrl = window.location.hostname;

        for (let urlPattern in ruleSet) {
            if (currentUrl.includes(urlPattern)) {
                const actions = ruleSet[urlPattern];
                for (let action in actions) {
                    if (action in actionHandlers) {
                        actionHandlers[action](actions[action]);
                    }
                }
                break;
            }
        }
    }

    // 操作集定义
    const actionHandlers = {
        hideElement: (selectors) => {
            selectors = typeof selectors === "string" ? [selectors] : selectors;
            const style = document.createElement("style");
            style.innerHTML = `${selectors.join(",")} { display: none !important; }`;
            document.head.appendChild(style);
        },
        customAction: (callback) => {
            if (typeof callback === "function") {
                callback();
            }
        },
    };

    // 执行规则匹配和操作
    applyRules();
})();