WebSuckIt!

Ugh, these Webs these Sockets... Nobody need them.

当前为 2016-09-01 提交的版本,查看 最新版本

// ==UserScript==
// @name         WebSuckIt!
// @namespace    lainscripts_websuckit
// @version      3.1
// @description  Ugh, these Webs these Sockets... Nobody need them.
// @author       lainverse
// @match        *://*/*
// @grant        none
// @run-at       document-start
// ==/UserScript==
(function() {
    'use strict';
    function WebSuckIt() {
        // check if the browser supports Proxy and WebSocket
        if (typeof Proxy !== 'function' ||
            typeof WebSocket !== 'function') {
            return;
        }
        var to_block = [
            '||bgrndi.com^',
            '||brokeloy.com^',
            '||dreadfula.ru^',
            '||et-code.ru^',
            '||free-torrent.org^',
            '||free-torrents.org^',
            '||gocdn.ru^',
            '||hghit.com^',
            '||kinotochka.net^',
            '||kuveres.com^',
            '||lepubs.com^',
            '||mail.ru^',
            '||marketgid.com^',
            '||mxtads.com^',
            '||oconner.biz^',
            '||psma01.com^',
            '||psma02.com^',
            '||psma03.com^',
            '||recreativ.ru^',
            '||regpole.com^',
            '||skidl.ru^',
            '||torvind.com^',
            '||trafmag.com^',
            '||xxuhter.ru^',
            '||yuiout.online^'
        ];
        var masks = [];
        to_block.forEach(function(m){
            masks.push(new RegExp(
                m.replace(/([\\\/\[\].*+?(){}$])/g, '\\$1')
                .replace(/\^(?!$)/g,'\\.?[^\\w%._-]')
                .replace(/\^$/,'\\.?([^\\w%._-]|$)')
                .replace(/^\|\|/,'^wss?:\\/+([^\/.]+\\.)*'),
                'i'));
        });
        var ws = window.WebSocket,
            closingFunctions = ['close', 'send'];
        function wsGetter(tgt, nm) {
            console.log('[WSI] Registered call to property "', nm, '"');
            try {
                if (typeof ws.prototype[nm] === 'function') {
                    if (closingFunctions.indexOf(nm) > -1) {
                        tgt.readyState = ws.CLOSED;
                    }
                    return function(){return;};
                }
                if (typeof ws.prototype[nm] === 'number') {
                    return ws[nm];
                }
            } catch(ignore) {}
            return tgt[nm];
        }
        window.WebSocket = (new Proxy(ws, {
            construct: function(target, args) {
                var url = args[0];
                console.log('[WSI] Opening socket on', url, '…');
                var i = masks.length;
                while(i--) {
                    if (masks[i].test(url)) {
                        console.log("[WSI] Blocked.");
                        return new Proxy({url: url, readyState: ws.OPEN}, {
                            get: wsGetter
                        });
                    }
                }
                return new target(args[0], args[1]);
            }
        }));
        if (document.currentScript) {
            document.currentScript.parentNode.removeChild(document.currentScript);
        }
    }

    var script = document.createElement('script');
    script.appendChild(document.createTextNode('('+ WebSuckIt +')();'));
    (document.head || document.body || document.documentElement).appendChild(script);
})();