Greasy Fork

Greasy Fork is available in English.

页面隐私保护

No Privacy Spys

目前为 2020-12-04 提交的版本。查看 最新版本

// ==UserScript==
// @name         页面隐私保护
// @namespace    No Privacy Spys
// @version      2020.12.04.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;
    let oriTitle;

    const URL_SITE = location.href.match(/^https?:\/\/(.*?)\//)[1];
    const TEXT_PROTECT_TITLE = '后台页面'
    const TEXT_PROTECT_MENU_ADD = '保护' + URL_SITE
    const TEXT_PROTECT_MENU_DEL = '不保护' + URL_SITE
    const TEXT_ID_CANVAS = 'blurUI';

    //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) {
        if (!(protectAnyway === true) && document.visibilityState != 'hidden') {return;};
        // Blur GUI
        blur();
        // Hide title
        oriTitle = document.title != TEXT_PROTECT_TITLE ? 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
        document.head.removeChild(link);
    }

    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';
        canvas.style.display = 'none';
    }

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

    let initGUI = function() {
        if (!link) {
            link = document.createElement('link');
            link.type = 'image/x-icon';
            link.rel = 'shortcut 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)) {
            console.log('add');
            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('visibilitychange', protect);
            window.addEventListener('load', function() {if (Document.visibilityState === 'hidden') {protect();};})
        }
        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();
})();