Disarm Adblock Detectors

Few utils which help to bypass Adblock detectors

As of 13.01.2015. See апошняя версія.

This script should not be not be installed directly. It is a library for other scripts to include with the meta directive // @require https://update.greasyfork.org/scripts/7465/31790/Disarm%20Adblock%20Detectors.js

You will need to install an extension such as Tampermonkey, Greasemonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey or Violentmonkey to install this script.

You will need to install an extension such as Tampermonkey or Userscripts to install this script.

You will need to install an extension such as Tampermonkey to install this script.

You will need to install a user script manager extension to install this script.

(I already have a user script manager, let me install it!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(I already have a user style manager, let me install it!)

/*jslint browser: true, devel: true */
/*global unsafeWindow, exportFunction */

var DAD = (function (window, unsafeWindow) {
    "use strict";
    
    var utils, debug,
        slice = Array.prototype.slice;
    
    function noop() {
    }
    
    function injectCSS(css) {
        var style,
            head = document.getElementsByTagName("head")[0];
        
        if (head) {
            style = document.createElement("style");
                
            style.textContent = css;
            style.type = "text/css";
                
            head.appendChild(style);
        }
    }
    
    function wrapValue(value, writable) {
        var wrapper;
        
        writable = !!writable;
        
        if (value instanceof Function) {
            wrapper = exportFunction(value, unsafeWindow);
        } else if (value instanceof Array) {
            wrapper = exportProperties(new unsafeWindow.Array(), value, writable);
        } else if (value instanceof Object) {
            wrapper = exportProperties(new unsafeWindow.Object(), value, writable);
        }
        
        return wrapper || value;
    }
    
    function wrapArgs(args) {
        var index, value;
        
        args =  wrapValue(args, true);
        
        for (index = args.length - 1; index >= 0; --index) {
            value = args[index];
            
            if (typeof value === "function" && !value.isProxy) {
                createProxy(args, index, true);
            }
        }
        
        return args;
    }
    
    function exportProperty(target, name, value, writable) {
        writable = !!writable;
        
        if (debug) {
            console.log("export property " + name);
        }
        
        var descriptor = {
            configurable: false,
            enumerable: true,
            value: wrapValue(value, writable),
            writable: writable
        };
        
        try {
            Object.defineProperty(target, name, descriptor);
        } catch (exception) {
            console.error(exception);
        }
        
        return target[name];
    }
    
    function exportProperties(target, object, writable) {
        writable = !!writable;
        
        var index, key;
        
        if (object instanceof Array) {
            for (index = object.length - 1; index >= 0; --index) {
                exportProperty(target, index, object[index], writable);
            }
        } else if (object instanceof Object) {
            for (key in object) {
                if (object.hasOwnProperty(key)) {
                    exportProperty(target, key, object[key], writable);
                }
            }
        }
        
        return target;
    }
    
    function createProxy(target, name, fn, writable) {
        if (debug) {
            console.log("createProxy", arguments);
        }
        
        var caller, handler, proxy,
            native = target[name];

        if (native.isProxy) {
            console.warn("method " + name + " of", target, "is already a proxy");
        } else {
            caller = function (args) {
                return native.apply(target, wrapArgs(args));
            };
            
            if (typeof fn === "function") {
                writable = !!writable;
                
                handler = function () {
                    return fn(caller, wrapArgs(slice.call(arguments)));
                };
            } else {
                writable = !!fn;
                
                handler = function () {
                    return caller(slice.call(arguments));
                };
            }
            
            proxy = exportProperty(target, name, handler, writable);
            proxy.isProxy = true;
        }
        
        return target[name];
    }
    
    function extractString(script, options) {
        var endIndex, string,
            startIndex = 0;

        options = options || {};
        
        if (options.hasOwnProperty("before")) {
            startIndex = script.indexOf(options.before) + options.before.length;
        }
        
        if (!options.hasOwnProperty("separator")) {
            options.separator = "'";
        }

        if (!options.hasOwnProperty("start")) {
            options.start = "";
        }
        
        if (startIndex > -1) {
            startIndex = script.indexOf(options.separator + options.start, startIndex) + 1;
            endIndex = script.indexOf(options.separator, startIndex);

            string = script.substring(startIndex, endIndex);
        } else {
            string = null;
        }
        
        return string;
    }
    
    function DAD(config) {
        if (typeof config === "function") {
            config = config(unsafeWindow, utils);
        }
        
        debug = !!config.debug;
        
        if (typeof config.css === "string") {
            injectCSS(config.css);
        }
        
        if (typeof config.exports === "object") {
            exportProperties(unsafeWindow, config.exports);
        }
        
        if (typeof config.init === "function") {
            window.addEventListener("DOMContentLoaded", config.init, true);
        }
    }
    
    utils = {
        noop: noop,
        createProxy: createProxy,
        extractString: extractString
    };
    
    return DAD;
}(this, unsafeWindow));