立刻警戒

一键将该标签页的内容隐藏,并用其他页面覆盖以伪装。

// ==UserScript==
// @name         立刻警戒
// @namespace    https://ez118.github.io/
// @version      0.2.4
// @description  一键将该标签页的内容隐藏,并用其他页面覆盖以伪装。
// @author       ZZY_WISU
// @match        *://*/*
// @license      GPLv3
// @icon         data:image/svg+xml;charset=utf-8;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHZpZXdCb3g9IjAgMCAyNCAyNCI+PHBhdGggZD0iTTEwLjUgMTNIOHYtM2gyLjVWNy41aDNWMTBIMTZ2M2gtMi41djIuNWgtM3pNMTIgMiA0IDV2Ni4wOWMwIDUuMDUgMy40MSA5Ljc2IDggMTAuOTEgNC41OS0xLjE1IDgtNS44NiA4LTEwLjkxVjV6IiBmaWxsPSIjMWRiNDU5Ij48L3BhdGg+PC9zdmc+
// @run-at       document-end
// @grant        GM_addValueChangeListener
// @grant        GM_addStyle
// @grant        GM_setValue
// @grant        GM_getValue
// @grant        window.onurlchange
// @require      https://unpkg.com/jquery@3.7.1/dist/jquery.min.js
// ==/UserScript==



/* =====[ 用户自定义 ]===== */

const CompatibilityMode = false;

const DICT = [
    {
        "title":"腾讯网-QQ.COM",
        "url":"https://xw.qq.com/"
    },
    {
        "title":"IT之家",
        "url":"https://www.ithome.com/"
    },
    {
        "title":"w3school 在线教程",
        "url":"https://www.w3school.com.cn/"
    },
    {
        "title":"jQuery css() 方法 | 菜鸟教程",
        "url":"https://www.runoob.com/jquery/jquery-css.html"
    },
    {
        "title":"出师表(诸葛亮作品)_百度百科",
        "url":"https://baike.baidu.com/item/%E5%87%BA%E5%B8%88%E8%A1%A8/82086"
    },
    {
        "title":"一九八四(乔治·奥威尔著长篇小说)_百度百科",
        "url":"https://baike.baidu.com/item/%E4%B8%80%E4%B9%9D%E5%85%AB%E5%9B%9B/2672"
    },
    {
        "title":"一网通办",
        "url":"https://zwdt.sh.gov.cn/"
    },
    {
        "title":"哔哩哔哩热门",
        "url":"https://www.bilibili.com/v/popular/all/"
    },
    {
        "title":"网页新闻",
        "url":"https://news.163.com/"
    },
    {
        "title":"精华区 - 博客园",
        "url":"https://www.cnblogs.com/pick/"
    },
    {
        "title":"一九八四 (豆瓣)",
        "url":"https://movie.douban.com/subject/1301849/"
    },
    {
        "title":"少数派编辑部 的创作 - 少数派",
        "url":"https://sspai.com/u/ee0vj778/posts"
    },
    {
        "title":"36氪 让一部分人先看到未来",
        "url":"https://www.36kr.com/"
    },
];


/* =====[ 变量存储 ]===== */
const ICONS = {
    'safety': '<svg viewBox="0 0 24 24" width="20px" height="20px"><path d="M10.5 13H8v-3h2.5V7.5h3V10H16v3h-2.5v2.5h-3zM12 2 4 5v6.09c0 5.05 3.41 9.76 8 10.91 4.59-1.15 8-5.86 8-10.91V5z" fill="#FFF"></path></svg>'
};

var origTitle = "";


function randomInt(min, max) {
    /* 固定范围内的随机整数 */

    min = Math.ceil(min);
    max = Math.floor(max);
    return Math.floor(Math.random() * (max - min + 1)) + min;
}

function showToast(message, duration) {
    /* 内建Toast显示函数 */

    duration = duration || 3000;
    var toast = $('<div id="userscript-instantToast"></div>');
    toast.text(message);

    $('body').append(toast);
    toast.fadeIn(100);

    setTimeout(function () {
        toast.fadeOut(100, function () {
            toast.remove();
        });
    }, duration);
}

function showCover() {
    /* 显示伪装遮罩 */

    /* 若遮罩已存在,则不再创建 */
    if($("#userscript-superMask").length > 0) { return; }

    /* 创建元素 */
    var $superMask = $('<iframe>', {
        class: 'userscript-superMask',
        id: 'userscript-superMask',
        style: 'display:none;'
    }).appendTo('body');

    $superMask.show();

    /* 随机选取伪装页面,设置iframe的src、页面标题 */
    var randIndex = randomInt(1, DICT.length) - 1;
    $superMask.attr("src", DICT[randIndex].url);
    $("title").html(DICT[randIndex].title);
}


function initEle() {
    /* 基本元素初始化 */

    /* 创建元素 */
    var $toggleMask = $('<div>', {
        class: 'userscript-toggleMask',
        id: 'userscript-toggleMask',
        'toggle-status': 'idle'
    }).appendTo('body');
    $toggleMask.html(ICONS.safety);

    /* 加载时判断当前是否处于警戒状态 */
    if(GM_getValue('aid_broadcast').status == 'active') {
        $toggleMask.attr("toggle-status", "active");
        $toggleMask.css("right", "calc(50vw - 30px)");
        origTitle = $("title").html();
        showCover();
    }

    /* 点击事件 */
    $toggleMask.click(function(e) {
        if($toggleMask.attr("toggle-status") == 'idle'){
            $toggleMask.attr("toggle-status", "active");
            $toggleMask.css("right", "calc(50vw - 30px)");
            origTitle = $("title").html();
            showCover();
        } else {
            $toggleMask.attr("toggle-status", "idle");
            $toggleMask.css("right", "10px");
            $("title").html(origTitle);
            $("#userscript-superMask").remove();
        }
    });

    /* 右键/长按事件 */
    $toggleMask.on("contextmenu", function(e) {
        e.preventDefault();

        if(GM_getValue('aid_broadcast').status == 'idle'){
            $toggleMask.attr("toggle-status", "active");
            GM_setValue('aid_broadcast', {"status":"active","url":window.location.href, "type":"cover"});

            showToast("开始警戒", 500);
        } else {
            $toggleMask.attr("toggle-status", "idle");
            GM_setValue('aid_broadcast', {"status":"idle","url":window.location.href, "type":"cover"});

            showToast("警戒取消", 1500);
        }
    });
}


(function () {
    'use strict';

    /* 不作用于iframe中 */
    if(window.self !== window.top){ return; }

    /* 初始化 全局脚本广播信息(type可选:cover(遮罩), close(关闭窗口)) */
    if(!GM_getValue('aid_broadcast')){ GM_setValue('aid_broadcast', {"status":"idle","url":"", "type":"cover"}); }


    /* 向页面插入CSS */
    GM_addStyle(`
        body{ -webkit-appearance:none!important; }

        #userscript-instantToast{ display:none; position:fixed; bottom:40px; left:50%; transform:translateX(-50%); background-color:#333333; color:#FFF; padding:8px 20px; border-radius:5px; z-index:999999; box-shadow:0px 2px 5px #555; }

        .userscript-toggleMask{ position:fixed; bottom:10px; right:10px; z-index:999999; padding:6px; background-color:#3b82f690; height:18px; width:18px; border-radius:15px; opacity:0.9; box-shadow:0 0 5px #3b82f6; line-height:initial; display:inline-block; cursor:pointer; box-sizing:initial; user-select:none; }
        .userscript-toggleMask:hover{ opacity:1; }
        .userscript-toggleMask svg{ height:18px; width:18px; vertical-align:initial; }
        .userscript-superMask{ position:fixed; top:0; left:0; right:0; bottom:0; width:100vw; height:100vh; z-index:999998; background-color:#FFF; border:none; padding:none; box-sizing:initial; }
    `);


    /* 初始化 */
    initEle();
    /* 如果在页面加载时未完成初始化 */
    setTimeout(() => { if($("#userscript-toggleMask").length == 0){ initEle(); } }, 1000);
    /* 页面局部刷新时确保元素不被删除 */
    window.addEventListener('urlchange', (info) => { if($("#userscript-toggleMask").length == 0){ setTimeout(() => { initEle(); }, 1000); } });



    /* 全局开启广播监听 */
    if (typeof GM_addValueChangeListener === 'function' && CompatibilityMode == false) {
        GM_addValueChangeListener('aid_broadcast', function(name, old_value, new_value, remote) {
            if(!new_value.url || !new_value.type){ return; }

            var $toggleMask = $("#userscript-toggleMask");
            if(new_value.status == "idle"){
                $toggleMask.attr("toggle-status", "idle");
                $toggleMask.css("right", "10px");
                $("title").html(origTitle);
                $("#userscript-superMask").remove();
            } else {
                $toggleMask.attr("toggle-status", "active");
                $toggleMask.css("right", "calc(50vw - 30px)");
                origTitle = $("title").html();
                showCover();
            }
        });
    } else {
        console.log("[立即警戒] 提示:浏览器不支持或用户禁用了GM_addValueChangeListener,已启用替代方案。");

        function addValueChangeListener(key, callback) {
            let oldValue = GM_getValue(key);

            setInterval(function() {
                let newValue = GM_getValue(key);

                // 如果 oldValue 和 newValue 是对象或数组,进行深度比较
                if (JSON.stringify(oldValue) !== JSON.stringify(newValue)) {
                    callback(key, oldValue, newValue, false);
                    oldValue = newValue;
                }
            }, 1500);
        }

        addValueChangeListener('aid_broadcast', function(name, old_value, new_value, remote) {
            if(!new_value.url || !new_value.type){ return; }

            var $toggleMask = $("#userscript-toggleMask");
            if(new_value.status == "idle"){
                $toggleMask.attr("toggle-status", "idle");
                $toggleMask.css("right", "10px");
                $("title").html(origTitle);
                $("#userscript-superMask").remove();
            } else {
                $toggleMask.attr("toggle-status", "active");
                $toggleMask.css("right", "calc(50vw - 30px)");
                origTitle = $("title").html();
                showCover();
            }

        });
    }

})();