Greasy Fork

修复Snapchat网络版:禁用焦点追踪和屏幕截图防护

通过禁用无法阻止屏幕截图但却大大降低可用性的防护功能,改善Snapchat网络版的体验。

目前为 2023-07-17 提交的版本。查看 最新版本

// ==UserScript==
// @name         Unbreak Snapchat web. Disable focus tracking and screenshot prevention
// @name:zh-CN   修复Snapchat网络版:禁用焦点追踪和屏幕截图防护
// @namespace    http://tampermonkey.net/
// @version      0.1.2
// @description  Improve the Snapchat web experience by disabling screenshot prevention features which don't prevent screenshots but do actively harm the usability.
// @description:zh-CN  通过禁用无法阻止屏幕截图但却大大降低可用性的防护功能,改善Snapchat网络版的体验。
// @homepage     https://gist.github.com/varenc/20e4dbfe8e7a2cc305043ffcbc5454d0
// @author       https://github.com/varenc
// @match        https://web.snapchat.com/*
// @icon         http://snapchat.com/favicon.ico
// @license      MIT
// @run-at       document-idle
// @grant        none
// ==/UserScript==
 
(function() {
    'use strict';
 
    function __unblockControlKeyEvents() {
        const events = ["keydown", "keyup", "keypress"];
        const modifyKeys = ["Control", "Meta", "Alt","Shift"];
        for (var i = 0; i < events.length; i++) {
            var event_type = events[i];
            document.addEventListener(
                event_type,
                function (e) {
                    // console.log(`${event_type}[${i}]=`, e.key);
                    if (modifyKeys.includes(e.key)) {
                        e.preventDefault();
                        e.stopPropagation();
                        console.log(`'${event_type}' event for '${e.key}' received and prevented:`, e);
                        e.stopImmediatePropagation();
                    }
                },
                true
            );
        }
    }

    function __unblockEvent() {
        for (var i = 0; i < arguments.length; i++) {
            var event_type = arguments[i];
            document.addEventListener(
                arguments[i],
                function (e) {
                    // e.preventDefault();
                    e.stopPropagation();
                    console.log(`'${event_type}' event received and prevented:`, e);
                    // e.stopImmediatePropagation();
                },
                true
            );
        }
    }

    function __fixConsole() {
        // snapchat tries to disable console.log.. how mean. So we copy the real Console object from a new iframe
        const iframe = document.createElement("iframe");
        iframe.style.display = "none";
        document.body.appendChild(iframe);
        const nativeConsole = iframe.contentWindow.console;
        window.console=nativeConsole;
    }

    function __setupUnblocker() {
        __fixConsole();
        __unblockControlKeyEvents();

        ///////// NOTE /////////
        // The below makes right-click work like normal, but it also disables Snapchat's special right click behavior in certain places.
        __unblockEvent("contextmenu")
    }
    
    console.dir("Snapchat unbreaker running!")  // Snapchat doesn't disable `console.dir`... silly Snapchat.
    __setupUnblocker();
    // Run a few extra times to ensure our event listeners take priority.
    setTimeout(__setupUnblocker, 1000);
    setTimeout(__setupUnblocker, 5000);
    setTimeout(__setupUnblocker, 10000);
 
    // Works 90% of the time, but they also use some other method to check focus I haven't tracked down yet.
    document.hasFocus = function(){return true}
 
 
})();