WebSuckIt!

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

As of 2016-06-28. See the latest version.

// ==UserScript==
// @name         WebSuckIt!
// @namespace    lainscripts_websuckit
// @version      2.5
// @description  Ugh, these Webs these Sockets... Nobody need them.
// @author       lainverse
// @match        *://*/*
// @grant        none
// @run-at       document-start
// ==/UserScript==
(function() {
    'use strict';
    var to_block = [
        '||bgrndi.com^',
        '||brokeloy.com^',
        '||et-code.ru^',
        '||hghit.com^',
        '||lepubs.com^',
        '||mail.ru^',
        '||marketgid.com^',
        '||mxtads.com^',
        '||regpole.com^',
        '||torvind.com^',
        '||trafmag.com^'
    ], masks = [];
    to_block.forEach(function(m){
        masks.push(new RegExp(
            (m.substr(0,2) === '||' ? '^wss?:\/+([^/.]+\\.)*' : '') +
            m.replace(/\|\||\^/g,'').replace(/[./]/g, function(ch){
                return '\\' + ch;
            }) +
            (m.substr(-1) === '^' ? '[:/]' : ''), 'i'));
    });
    var ws = window.WebSocket;
    window.WebSocket = new Proxy(ws, {
        construct: function(e,i) {
            var url = i[0];
            console.log('[WSI] Opening socket on', url, '…');
            var j = masks.length;
            while(j--)
                if (masks[j].test(url)) {
                    console.log("[WSI] Blocked.");
                    return new Proxy({url: url, readyState: ws.OPEN}, {
                        get: function(tgt, nm) {
                            console.log('[WSI] Registered call to property "', nm, '"');
                            try {
                                if (typeof ws.prototype[nm] === 'function') {
                                    if (['close', 'send'].indexOf(nm) > -1)
                                        tgt.readyState = ws.CLOSED;
                                    return function(){};
                                }
                                if (typeof ws.prototype[nm] === 'number') {
                                    return ws[nm];
                                }
                            } catch(e) {}
                            return tgt[nm];
                        }
                    });
                }
            return new e(i[0],i[1]);
        }
    });
})();