页面隐私保护

No Privacy Spys

// ==UserScript==
// @name         页面隐私保护
// @namespace    No Privacy Spys
// @version      2021.05.01.1
// @description  No Privacy Spys
// @author       PY-DNG
// @include      *
// @grant        GM_getValue
// @grant        GM_setValue
// @grant        GM_deleteValue
// @grant        GM_registerMenuCommand
// @grant        GM_unregisterMenuCommand
// @run-at       document-start
// ==/UserScript==
(function() {
    let protectSites;
    let canvas, link;
    let menuCmdId, timeoutId;
    let oriTitle;

    const URL_SITE = location.href.match(/^(?:\w+):\/{2,3}(.*?)\//)[1];
    const TEXT_PROTECT_TITLE = '后台页面';
    const TEXT_PROTECT_TITLE_TIP = '单击页面解除保护';
    const TEXT_PROTECT_MENU_ADD = '保护' + URL_SITE;
    const TEXT_PROTECT_MENU_DEL = '不保护' + URL_SITE;
    const TEXT_ID_CANVAS = 'blurUI';
    const PIC_ICON = 'data:image/gif;base64,R0lGODlhgACAAHD/ACH+DGdpZjRqZXZsUCAgIAAh+QQJAAD/ACwAAAAAgACAAIcAaaalusa81OHq6uvSzs336+YAfMHJycrOz9AAbbTe3+Dk29fw7/C0y9jX19gAca0Ad7gAgMTx5N660d7/8+2tw8/z8vPn4N3P0NEAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA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A/tt9aWHXW+OB9nPHB52AB+w5zWTGcwIYXnJlj3vmM4c5zvcM60+rPDEBCHoAKkD0ohO9AUhPutIbMIGmO/3pExCA1KdO9aZT/epVd7oAJsB0py/96w0wetGFTi0CZDazd3LAjntcz7c0LyMWysg52d7jLi8APmY3u5TOB2EycpEnC9DJBQjD2LaXk/AKGPxooVLC67aweEpMux8Nr0z2dDWd6syqh7Obu0vWJ8SGN2UjIXlCSQrTgIi0pVN1iUvQXx6TTXSlin0E+chXcpSEZ/1wcY9W+CDP8b2b5l47z/vip9KZ+Cy9Ui8bS3E+8VvGx70W43jQ5ZsW8sN/PhqjxT9gGz+RmNazvm2rKEvne/E/n3JjIl9nRlb+3vQXnebxYohEHrb//l2U5fvhv9TuwTArP2RD4nRIoeREs2R+NeQ6QORNBRRGwhRLByBEF7SAHKQUe2WBrMRgPVRdOERMC2RF1qNCwGcU+AM/F1SBJVSBZZRBKtiCKDiBFzQ+iVFBEoRZuzNbgsRAGzSBH+hAYGREkdQ+98NCj3c/VQRBRRQ/D0Q/QRg/KYSERmhBqhGFVFiFVkg+E3QX+nGFXNiFUUiECxEQADs=';
    //GM_setValue('protectSites', ['www.lianaiyx.com']);

    // delete the item we want from the array we choose
    function delItem(array, item) {
        array.forEach(function(arritem, index, arr) {
            if(arritem === item) {
                arr.splice(index, 1);
            }
        })
    }

    let protect = function(protectAnyway=false) {
        // When switching to this tab, show the tip for 3s
        if (!(protectAnyway === true) && document.visibilityState !== 'hidden') {
            document.title = TEXT_PROTECT_TITLE_TIP;
            // Clear previous title changing task
            if (timeoutId) {clearTimeout(timeoutId);};
            timeoutId = setTimeout(function() {
                // After 3s: If still protecting then hide the tip; If not then just don't change anything
                document.title = document.title === TEXT_PROTECT_TITLE_TIP ? TEXT_PROTECT_TITLE : document.title;
            }, 3000);
            return;
        };
        // Blur GUI
        blur();
        // Hide title
        oriTitle = (document.title !== TEXT_PROTECT_TITLE) && (document.title !== TEXT_PROTECT_TITLE_TIP) ? document.title : oriTitle
        document.title = TEXT_PROTECT_TITLE;
        // Hide icon
        document.head.appendChild(link);
    }

    let recover = function() {
        // Cancel blur GUI
        cancelBlur();
        // Recover title
        document.title = oriTitle;
        // Recover icon
        if (link.parentElement === document.head) {document.head.removeChild(link);};
        // Clear title changing task
        if (timeoutId) {clearTimeout(timeoutId);};
    }

    let config = function() {
        // Initialize protectSites
        protectSites = GM_getValue('protectSites', '');
        if (protectSites === '') {
            protectSites = [];
            GM_setValue('protectSites', protectSites);
        }
    }

    function cancelBlur() {
        document.body.style.filter = 'none';
        document.body.style.pointerEvents = 'auto';
        document.removeEventListener('blur', GUIonblur);
        canvas.style.display = 'none';
    }

    function blur() {
        if (!canvas) {
            setTimeout(blur, 100);
            return;
        }
        initGUI();
        let b = 0;
        document.body.style.filter = 'blur(40px)';
        document.body.style.pointerEvents = 'none';
        document.addEventListener('blur', GUIonblur);
        canvas.style.display = '';
    }

    function GUIonblur() {
        canvas.focus();
    }

    let initGUI = function() {
        if (!link) {
            link = document.createElement('link');
            link.type = 'image/x-icon';
            link.rel = 'shortcut icon';
            link.href = PIC_ICON;
            //link.href = 'https://www.luogu.com.cn/favicon.ico';
        }
        if (!canvas) {
            canvas = document.createElement('canvas');
            canvas.addEventListener('click', recover);
            canvas.id = TEXT_ID_CANVAS;
            canvas.style.position = 'fixed';
            canvas.style.left = 0;
            canvas.style.top = 0;
            canvas.width = window.innerWidth;
            canvas.height = window.innerHeight;
            canvas.style.display = 'none';
        }
        if (!document.getElementById(TEXT_ID_CANVAS)) {
            document.lastChild.appendChild(canvas);
        }
    }

    let addThisSite = function() {
        protectSites.push(URL_SITE);
        GM_setValue('protectSites', protectSites);
        initGUI();
        protect(true);
        window.addEventListener('visibilitychange', protect);
        GM_unregisterMenuCommand(menuCmdId);
        menuCmdId = GM_registerMenuCommand('不保护' + URL_SITE, delThisSite);
    }

    let delThisSite = function() {
        delItem(protectSites, URL_SITE);
        GM_setValue('protectSites', protectSites);
        recover();
        window.removeEventListener('visibilitychange', protect);
        GM_unregisterMenuCommand(menuCmdId);
        menuCmdId = GM_registerMenuCommand(TEXT_PROTECT_MENU_ADD, addThisSite);
    }

    let init = function() {
        config();
        if (protectSites.indexOf(URL_SITE) != -1) {
            window.addEventListener('load', initGUI);
            window.addEventListener('load', function() {window.addEventListener('visibilitychange', protect);});
            window.addEventListener('load', function() {if (document.visibilityState !== 'visible') {protect(true);};})
        }
        menuCmdId = protectSites.indexOf(URL_SITE) === -1 ? GM_registerMenuCommand(TEXT_PROTECT_MENU_ADD, addThisSite) : GM_registerMenuCommand(TEXT_PROTECT_MENU_DEL, delThisSite);
        //menuCmdId = GM_registerMenuCommand((protectSites.indexOf(URL_SITE) === -1 ? TEXT_PROTECT_MENU_ADD : TEXT_PROTECT_MENU_DEL), protectSites.indexOf(URL_SITE) === -1 ? addThisSite : delThisSite);
    }

    init();
})();

// @version 2020.12.05.1