Greasy Fork

Unbreak Snapchat web. Disable focus tracking and screenshot prevention

This userscript improves the Snapchat web experience by disabling screenshot prevention features which don't prevent screenshots but do actively harm the usability.

目前为 2023-06-27 提交的版本。查看 最新版本

// ==UserScript==
// @name         Unbreak Snapchat web. Disable focus tracking and screenshot prevention
// @namespace    http://tampermonkey.net/
// @version      0.1.1
// @description  This userscript improves the Snapchat web experience by disabling screenshot prevention features which don't prevent screenshots but do actively harm the usability.
// @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}
 
 
})();