RU AdList JS Fixes

try to take over the world!

As of 2018-11-11. See the latest version.

// ==UserScript==
// @name         RU AdList JS Fixes
// @namespace    ruadlist_js_fixes
// @version      20181111.0
// @description  try to take over the world!
// @author       lainverse & dimisa
// @supportURL   https://greasyfork.org/en/scripts/19993-ru-adlist-js-fixes/feedback
// @match        *://*/*
// @exclude      *://auth.wi-fi.ru/*
// @exclude      *://*.alfabank.ru/*
// @exclude      *://alfabank.ru/*
// @exclude      *://*.unicreditbanking.net/*
// @exclude      *://unicreditbanking.net/*
// @exclude      *://*.telegram.org/*
// @exclude      *://telegram.org/*
// @grant        unsafeWindow
// @grant        window.close
// @run-at       document-start
// ==/UserScript==

(function() {
    'use strict';

    let win = (unsafeWindow || window);

    // MooTools are crazy enough to replace standard browser object window.Document: https://mootools.net/core
    // Occasionally their code runs before my script on some domains and causes all kinds of havoc.
    let _Document = Object.getPrototypeOf(HTMLDocument);
    let _Element = Object.getPrototypeOf(HTMLElement);
    // dTree 2.05 in some cases replaces Node object
    let _Node = Object.getPrototypeOf(_Element);

    // http://stackoverflow.com/questions/9847580/how-to-detect-safari-chrome-ie-firefox-and-opera-browser
    let isOpera = (!!window.opr && !!window.opr.addons) || !!window.opera || navigator.userAgent.indexOf(' OPR/') >= 0,
        isChrome = !!window.chrome && !!window.chrome.webstore,
        isSafari =
        Object.prototype.toString.call(window.HTMLElement).indexOf('Constructor') > 0 ||
        (function (p) { return p.toString() === "[object SafariRemoteNotification]"; })(!window.safari || window.safari.pushNotification);
    let isFirefox = 'InstallTrigger' in win;
    let inIFrame = (win.self !== win.top);
    let _getAttribute = Function.prototype.call.bind(_Element.prototype.getAttribute),
        _setAttribute = Function.prototype.call.bind(_Element.prototype.setAttribute),
        _removeAttribute = Function.prototype.call.bind(_Element.prototype.removeAttribute);
    let _document = win.document,
        _de = _document.documentElement,
        _appendChild = _Document.prototype.appendChild.bind(_de),
        _removeChild = _Document.prototype.removeChild.bind(_de),
        _createElement = _Document.prototype.createElement.bind(_document);

    if (isFirefox && // Exit on image pages in Fx
        _document.constructor.prototype.toString() === '[object ImageDocumentPrototype]')
        return;

    // NodeList and HTMLCollection iterator polyfill
    // required for old versions of Safari and Chrome 49 (last available for WinXP)
    // https://jakearchibald.com/2014/iterators-gonna-iterate/
    if (!NodeList.prototype[Symbol.iterator])
        NodeList.prototype[Symbol.iterator] = Array.prototype[Symbol.iterator];
    if (!HTMLCollection.prototype[Symbol.iterator])
        HTMLCollection.prototype[Symbol.iterator] = Array.prototype[Symbol.iterator];

    // Wrapper to run scripts designed to override objects available to other scripts
    // Required in old versions of Firefox (<58) or when running with Greasemonkey
    let skipLander = true;
    try {
        skipLander = !(isFirefox && ('StopIteration' in win || GM.info.scriptHandler === 'Greasemonkey'));
    } catch(ignore){}
    let batchLand = [];
    let batchPrepend = [];
    let _APIString = 'let win = window, _document = win.document, _Document = Object.getPrototypeOf(HTMLDocument),'+
        ' _Element = Object.getPrototypeOf(HTMLElement), _Node = Object.getPrototypeOf(_Element);';
    let landScript = (f, pre) => {
        let script = _createElement('script');
        script.textContent = `(()=>{${_APIString}${(
            (pre.length > 0 ? pre.join(';') : '')
        )};(${f.join(')();(')})();})();`;
        _appendChild(script);
        _removeChild(script);
    };
    let scriptLander = f => f();
    if (!skipLander) {
        scriptLander = (func, ...prepend) => {
            prepend.forEach(
                x => batchPrepend.includes(x) ? null : batchPrepend.push(x)
            );
            batchLand.push(func);
        };
        _document.addEventListener(
            'DOMContentLoaded', () => void (scriptLander = (f, ...prep) => landScript([f], prep)), false
        );
    }

    function nullTools(opts) {
        let nt = this;
        opts = opts || {};
        let log = (...args) => opts.log && console.log(...args);
        let warn = (...args) => console.warn(...args);
        let trace = (...args) => (opts.log || opts.trace) && warn(...args);

        nt.destroy = function(o, destroy) {
            if (!opts.destroy && !destroy && o instanceof Object)
                return;
            log('cleaning', o);
            try {
                for (let item in o) {
                    if (item instanceof Object)
                        nt.destroy(item);
                    delete o[item];
                }
            } catch (e) {
                log('Error in object destructor', e);
            }
        };

        nt.define = function(obj, prop, val, enumerable = true) {
            try {
                Object.defineProperty(
                    obj, prop, {
                        get: () => val,
                        set: v => {
                            if (v !== val) {
                                log(`set ${prop} of`, obj, 'to', v);
                                nt.destroy(v);
                            }
                        },
                        enumerable: enumerable
                    }
                );
            } catch (err) {
                console.log(`Unable to redefine "${prop}" in `, obj, err);
            }
        };
        nt.proxy = function(obj) {
            return new Proxy(
                obj, {
                    get: (t, p) => p in t ? t[p] : console.warn(`Missing ${p} in`, t),
                    set: (t, p, v) => {
                        if (v !== t[p]) {
                            log(`set ${p} of`, t, 'to', v);
                            nt.destroy(v);
                        }
                        return true;
                    }
                }
            );
        };
        nt.func = (val, name = '', force_log = false) => nt.proxy((...args) => {
            (force_log ? warn : trace)(`call ${name}(`, ...args,`) return`, val);
            return val;
        });
    }

    // Debug function, lists all unusual window properties
    function getStrangeObjectsList() {
        console.warn('Strangers list start');
        let _toString = Function.prototype.call.bind(Function.prototype.toString);
        let _skip = 'frames/self/window/webkitStorageInfo'.split('/');
        for (let n in win) {
            let val = win[n];
            if (val && !_skip.includes(n) && (win !== window && val !== window[n] || win === window) &&
                (!(val instanceof Function) ||
                 val instanceof Function &&
                 !(new RegExp (`^function\\s(${n})?\\(\\)[\\s\\r\\n]*\\{[\\s\\r\\n]*\\[native\\scode\\][\\s\\r\\n]*\\}$`)).test(_toString(val))))
                console.log(`${n} =`, val);
        }
        console.warn('Strangers list end');
    }

    // Fake objects of advertisement networks to break their workflow
    scriptLander(
        function() {
            let nt = new nullTools();
            // Popular adblock detector
            if (!('fuckAdBlock' in win)) {
                let FuckAdBlock = function(options) {
                    let self = this;
                    self._options = {
                        checkOnLoad: false,
                        resetOnEnd: false,
                        checking: false
                    };
                    self.setOption = function(opt, val) {
                        if (val)
                            self._options[opt] = val;
                        else
                            Object.assign(self._options, opt);
                    };
                    if (options)
                        self.setOption(options);

                    self._var = { event: {} };
                    self.clearEvent = function() {
                        self._var.event.detected = [];
                        self._var.event.notDetected = [];
                    };
                    self.clearEvent();

                    self.on = function(detected, fun) {
                        self._var.event[detected?'detected':'notDetected'].push(fun);
                        return self;
                    };
                    self.onDetected = function(cb) {
                        return self.on(true, cb);
                    };
                    self.onNotDetected = function(cb) {
                        return self.on(false, cb);
                    };
                    self.emitEvent = function() {
                        for (let fun of self._var.event.notDetected)
                            fun();
                        if (self._options.resetOnEnd)
                            self.clearEvent();
                        return self;
                    };
                    self._creatBait = () => null;
                    self._destroyBait = () => null;
                    self._checkBait = function() {
                        setTimeout((() => self.emitEvent()), 1);
                    };
                    self.check = function() {
                        self._checkBait();
                        return true;
                    };

                    let callback = function() {
                        if (self._options.checkOnLoad)
                            setTimeout(self.check, 1);
                    };
                    window.addEventListener('load', callback, false);
                };
                nt.define(win, 'FuckAdBlock', FuckAdBlock);
                nt.define(win, 'fuckAdBlock', new FuckAdBlock({
                    checkOnLoad: true,
                    resetOnEnd: true
                }));
            }

            // CoinHive miner stub. Continuous 100% CPU load can easily kill some CPU with overheat.
            if (!('CoinHive' in win))
                if (location.hostname !== 'cnhv.co') {
                    // CoinHive stub for cases when it doesn't affect site functionality
                    let CoinHiveConstructor = function() {
                        console.warn('Fake CoinHive miner created.');
                        this.setThrottle = nt.func(null);
                        this.start = nt.func(null);
                        this.on = nt.func(null);
                        this.getHashesPerSecond = nt.func(Infinity);
                        this.getTotalHashes = nt.func(Infinity);
                        this.getAcceptedHashes = nt.func(Infinity);
                    };
                    let CoinHiveStub = nt.proxy({
                        Anonymous: CoinHiveConstructor,
                        User: CoinHiveConstructor,
                        Token: CoinHiveConstructor,
                        JobThread: nt.func(null),
                        Res: nt.func(null),
                        IF_EXCLUSIVE_TAB: false,
                        CONFIG: nt.proxy({})
                    });
                    nt.define(win, 'CoinHive', CoinHiveStub);
                } else {
                    // CoinHive wrapper to fool sites which expect it to actually work and return results
                    let CoinHiveObject;
                    Object.defineProperty(win, 'CoinHive', {
                        set: function(obj) {
                            if ('Token' in obj) {
                                console.log('[CoinHive] Token wrapper applied.');
                                let _Token = obj.Token.bind(obj);
                                obj.Token = function(siteKey, goal, params) {
                                    let _goal = goal;
                                    goal = goal > 256 ? 256 : goal;
                                    console.log(`[CoinHive] Original goal: ${_goal}, new smaller goal ${goal}.`);
                                    console.log(`With smaller goals server may return 'invalid_goal' error and stop working.`);
                                    let miner = _Token(siteKey, goal, params);
                                    miner.setThrottle(0.99);
                                    miner.setThrottle = () => null;
                                    let _start = miner.start.bind(miner);
                                    miner.start = function() {
                                        let res = _start(window.CoinHive.FORCE_EXCLUSIVE_TAB);
                                        return res;
                                    };
                                    let _getTotalHashes = miner.getTotalHashes;
                                    miner.getTotalHashes = function() {
                                        return Math.trunc(_getTotalHashes.call(this) / goal * _goal);
                                    };
                                    let __emit = miner._emit;
                                    miner._emit = function(state, props) {
                                        let _self = this;
                                        console.log('[CoinHive] state:', state, props);
                                        if (state === 'job')
                                            setTimeout(() => {
                                                _self.stop();
                                                _self._emit('accepted', { hashes: goal });
                                            }, 1000);
                                        return __emit.apply(_self, arguments);
                                    };
                                    let _on = miner.on.bind(miner);
                                    miner.on = function(type, callback) {
                                        if (type === 'accepted') {
                                            console.log('[CoinHive] "accepted" callback wrapper applied.');
                                            let _callback = callback;
                                            callback = function(params) {
                                                console.log('[CoinHive] "accepted" callback is called, imitating original goal being reached.');
                                                params.hashes = _goal;
                                                return _callback.apply(this, arguments);
                                            };
                                            miner.stop();
                                        }
                                        return _on(type, callback);
                                    };
                                    return miner;
                                };
                            }
                            CoinHiveObject = obj;
                        },
                        get: () => CoinHiveObject
                    });
                }

            // VideoJS player wrapper
            VideoJS: {
                let _videojs = win.videojs || void 0;
                Object.defineProperty(win, 'videojs', {
                    get: () => _videojs,
                    set: f => {
                        if (f === _videojs)
                            return true;
                        console.log('videojs =', f);
                        _videojs = new Proxy(f, {
                            apply: (tgt, ths, args) => {
                                console.log('videojs(', ...args, ')');
                                let params = args[1];
                                if (params) {
                                    if (params.hasAd)
                                        params.hasAd = false;
                                    if (params.plugins && params.plugins.vastClient)
                                        delete params.plugins.vastClient;
                                }
                                let res = tgt.apply(ths, args);
                                if (res && res.seed)
                                    res.seed = () => null;
                                console.log('player = ', res);
                                return res;
                            }
                        });
                    }
                });
            }

            // piguiqproxy-like script loaded from random RU domains
            let fab_application_define = nt.func(null, 'fab_application.define', true);
            fab_application_define.amd = nt.func(null, 'fab_application.define.amd');
            let fab_application = {
                requirejs: nt.func(null, 'fab_application.requirejs'),
                require: nt.func(null, 'fab_application.require'),
                define: nt.proxy(fab_application_define)
            };
            nt.define(win, 'fab_application', nt.proxy(fab_application));

            // Set a little trap for BodyClick ads
            Object.defineProperty(win, '__BC_domain', {
                set: () => { throw 'BodyClick trap' }
            });

            // Yandex API (ADBTools, Metrika)
            let hostname = location.hostname;
            if (// Thank you, Greasemonkey, now I have to check for this. -_-
                location.protocol === 'about:' ||
                // Google likes to define odd global variables like Ya
                hostname.startsWith('google.') || hostname.includes('.google.') ||
                // Also, Yandex uses their Ya object for a lot of things on their pages and
                // wrapping it may cause problems. It's better to skip it in some cases.
                ((hostname.startsWith('yandex.') || hostname.includes('.yandex.')) &&
                 /^\/((yand)?search|images)/i.test(location.pathname) && !hostname.startsWith('news.')) ||
                // Also skip on these following sites since they use
                // code minification which generated global Ya variable.
                hostname.endsWith('chatango.com') || hostname.endsWith('github.io') ||
                hostname.endsWith('grimtools.com') || hostname.endsWith('poeplanner.com'))
                return;

            let YaProps = new Set();
            function onAdvManager (Ya, rootProp, obj) {
                return new Proxy(obj, {
                    set: (tgt, prop, val) => {
                        if (prop === 'AdvManager') {
                            console.log(`Ya.${rootProp} = Ya.Context`);
                            nt.define(Ya, rootProp, Ya.Context);
                            YaProps.add(rootProp);
                        }
                        tgt[prop] = val;
                        return true;
                    },
                    get: (tgt, prop) => {
                        if (prop === 'AdvManager' && !(prop in tgt)) {
                            console.log(`Injected missing ${prop} in Ya.${rootProp}.`);
                            tgt[prop] = Ya.Context[prop];
                        }
                        return tgt[prop];
                    }
                });
            }
            let Rum = {};
            [
                '__timeMarks', '_timeMarks', '__deltaMarks', '_deltaMarks',
                '__defRes', '_defRes', '__defTimes', '_defTimes', '_vars',
                'commonVars'
            ].forEach(name => void(Rum[name] = []));
            [
                'send', 'sendRaf', 'sendTimeMark', 'sendResTiming', 'sendTTI',
                'sendHeroElement', 'time', 'timeEnd', 'getTime', 'mark', 'init',
                'makeSubPage', 'observeDOMNode', 'isVisibilityChanged'
            ].forEach(name => void(Rum[name] = nt.func(null, `Ya.Rum.${name}`)));
            [
                'getSettings', 'getVarsList'
            ].forEach(name => void(Rum[name] = nt.func([], `Ya.Rum.${name}`)));
            [
                ['ajaxStart', 0], ['ajaxComplete', 0],
                ['enabled', true], ['_tti', null],
                ['vsChanged', false], ['vsStart', 'visible']
            ].forEach(([prop, val]) => void(Rum[prop] = val));
            Rum = nt.proxy(Rum);
            let Ya = new Proxy({}, {
                set: function(tgt, prop, val) {
                    if (val === tgt[prop])
                        return true;
                    if (prop === 'Rum') {
                        nt.define(tgt, prop, Rum);
                        YaProps.add(prop);
                        Object.assign(val, Rum);
                    }
                    if (YaProps.has(prop)) {
                        console.log(`Ya.${prop} \u2260`, val);
                        return true;
                    }
                    if (val instanceof Object && prop !== '__inline_params__')
                        val = onAdvManager(Ya, prop, val);
                    tgt[prop] = val;
                    console.log(`Ya.${prop} =`, val);
                    return true;
                },
                get: (tgt, prop) => tgt[prop]
            });
            let callWithParams = function(f) {
                f.call(this, Ya.__inline_params__ || {});
                Ya.__inline_params__ = null;
            };
            nt.define(Ya, 'callWithParams', callWithParams);
            nt.define(Ya, 'PerfCounters', nt.proxy({
                addCacheEvent: nt.func(null),
                getTime: nt.func(null),
                sendCacheEvents: nt.func(null),
                sendResTiming: nt.func(null),
                sendTimeMark: nt.func(null),
                __cacheEvents: []
            }));
            nt.define(Ya, '__isSent', true);
            nt.define(Ya, 'confirmUrl', '');
            nt.define(Ya, 'Direct', nt.proxy({
                insertInto: nt.func(null)
            }));
            nt.define(Ya, 'mediaCode', nt.proxy({
                create: function() {
                    if (inIFrame) {
                        console.log('Removed body of ad-frame.');
                        _document.documentElement.removeChild(_document.body);
                    }
                },
                moduleLoad: nt.func(null),
                setModule: nt.func(null)
            }));
            let extra = nt.proxy({
                extra: nt.proxy({ match: 0, confirm: '', src: '' }),
                id: 0, percent: 100, threshold: 1
            });
            nt.define(Ya, '_exp', nt.proxy({
                id: 0, coin: 0,
                choose: nt.func(extra),
                get: (prop) => extra.hasOwnProperty(prop) ? extra[prop] : null,
                getId: nt.func(0),
                defaultVersion: extra,
                getExtra: nt.func(extra.extra),
                getDefaultExtra: nt.func(extra.extra),
                versions: [extra]
            }));
            nt.define(Ya, 'c', nt.func(null));
            nt.define(Ya, 'ADBTools', function(){
                ['loadContext', 'testAdbStyle'].forEach(name => void(this[name] = nt.func(null)));
                this.getCurrentState = nt.func(true);
                return nt.proxy(this);
            });
            nt.define(Ya, 'AdDetector', nt.proxy({}));

            if (!location.hostname.includes('kinopoisk.ru')) {
                // kinopoisk uses adfox wrapped in Yandex code to display self-ads
                let adfoxCode = {};
                [
                    'create', 'reload', 'destroy',
                    'createAdaptive', 'moduleLoad',
                    'createScroll', 'pr'
                ].forEach(name => void(adfoxCode[name] = nt.func(null, `Ya.adfoxCode.${name}`)));
                nt.define(adfoxCode, 'xhrExperiment', nt.proxy({ isXhr: true, isControl: true }));
                nt.define(adfoxCode, 'isXhr', true);
                nt.define(adfoxCode, 'isExp', false);
                nt.define(adfoxCode, 'forcedDirectLoadingExp', false);
                nt.define(adfoxCode, 'isLoadingTurnedOn', false);
                nt.define(Ya, 'adfoxCode', nt.proxy(adfoxCode));
            } else
                nt.define(win, 'abExperiments', nt.proxy([]));
            let managerForAdfox = {
                loaderVersion: 1,
                isCurrrencyExp: true,
                isReady: nt.func(true, 'Ya.headerBidding.managerForAdfox.isReady'),
                getRequestTimeout: nt.func(300 + Math.floor(Math.random()*100), 'Ya.headerBidding.managerForAdfox.getRequestTimeout')
            };
            [
                'render', 'requestBids', 'requestBidForContainer',
                'getBidsForAdfoxByContainerId', 'addAdfoxCallback'
            ].forEach(name => void(managerForAdfox[name] = nt.func(null, `Ya.headerBidding.managerForAdfox.${name}`)));
            let headerBidding = nt.proxy({
                setSettings: opts => {
                    if (!(opts && opts.adUnits))
                        return null;
                    let ids = [];
                    for (let unit of opts.adUnits)
                        ids.push(unit.code);
                    createStyle(`#${ids.join(', #')} { display: none !important }`);
                },
                managerForAdfox: nt.proxy(managerForAdfox)
            });
            nt.define(Ya, 'headerBidding', headerBidding);

            let AdvManager = function() {
                [
                    'renderDirect',
                    'getBid', 'releaseBid',
                    'getSkipToken',
                    'getAdSessionId'
                ].forEach(name => void(this[name] = nt.func(null, `Ya.AdvManager.${name}`)));
                this.render = function(o) {
                    if (!o.renderTo)
                        return;
                    let placeholder = _document.getElementById(o.renderTo);
                    let parent = placeholder.parentNode;
                    placeholder.style = 'display:none!important';
                    parent.style = (parent.getAttribute('style')||'') + 'height:auto!important';
                    // fix for Yandex TV pages
                    if (location.hostname.startsWith('tv.yandex.')) {
                        let sibling = placeholder.previousSibling;
                        if (sibling && sibling.classList && sibling.classList.contains('tv-spin'))
                            sibling.style.display = 'none';
                    }
                };
                this.constructor = Object;
                return nt.proxy(this);
            };
            nt.define(Ya, 'Context', nt.proxy({
                __longExperiment: null,
                _callbacks: nt.proxy([]),
                _asyncModeOn: true,
                _init: nt.func(null),
                isAllowedRepeatAds: nt.func(null),
                isNewLoader: nt.func(false),
                AdvManager: new AdvManager(),
                AdvManagerStatic: nt.func({})
            }));
            let Metrika = function Metrika(x) {
                [
                    'reachGoal', 'replacePhones',
                    'trackLinks', 'userParams',
                    'hit', 'params', 'clickmap', 'then'
                ].forEach(name => void(this[name] = nt.func(null, `Ya.Metrika.${name}`)));
                this._ecommerce = '';
                if (x && 'id' in x)
                    this.id = x.id;
                else
                    this.id = 0;
                return nt.proxy(this);
            };
            Metrika.counters = () => Ya._metrika.counters;
            nt.define(Ya, 'Metrika', Metrika);
            nt.define(Ya, 'Metrika2', Metrika);
            let counter = new Ya.Metrika();
            nt.define(Ya, '_metrika', nt.proxy({
                counter: counter,
                counters: [counter],
                hitParam: {},
                counterNum: 0,
                hitId: 0,
                v: 1,
                i: 0,
                _globalMetrikaHitId: 0,
                getCounters: null,
                dataLayer: null,
                f1: null
            }));
            nt.define(Ya, '_globalMetrikaHitId', 0);
            counter = {};
            [
                'cpHitDelayed','wHitDelayed',
                'hitDelayed','makeReq',
                'redir','hit','log','cp','w'
            ].forEach(name => void(counter[name] = nt.func(null, `Ya.counter.${name}`)));
            [
                'stringifyParams','_getVars',
                'getUid','getUrl','getHash'
            ].forEach(name => void(counter[name] = nt.func('')));
            nt.define(Ya, 'counter', nt.proxy(counter));
            nt.define(Ya, 'jserrors', []);
            nt.define(Ya, 'onerror', nt.func(null, 'Ya.onerror'));
            if (win.Ya) {
                console.log('Found existing Ya object:', win.Ya);
                for (let prop in win.Ya)
                    Ya[prop] = win.Ya[prop];
            }
            for (let prop in Ya)
                if (prop !== '__inline_params__')
                    YaProps.add(prop);
            nt.define(win, 'Ya', Ya);
            // Yandex.Metrika callbacks
            let yandex_metrika_callbacks = [];
            _document.addEventListener(
                'DOMContentLoaded', () => {
                    yandex_metrika_callbacks.forEach((f) => f && f.call(window));
                    yandex_metrika_callbacks.length = 0;
                    yandex_metrika_callbacks.push = (f) => setTimeout(f, 0);
                }, false
            );
            nt.define(win, 'yandex_metrika_callbacks', yandex_metrika_callbacks);
        }, nullTools, createStyle
    );

    // Creates and return protected style (unless protection is manually disabled).
    // Protected style will re-add itself on removal and remaind enabled on attempt to disable it.
    function createStyle(rules, props, skip_protect) {
        props = props || {};
        props.type = 'text/css';

        function _protect(style) {
            if (skip_protect)
                return;

            Object.defineProperty(style, 'sheet', {
                value: null,
                enumerable: true
            });
            Object.defineProperty(style, 'disabled', {
                get: () => true, //pretend to be disabled
                set: () => undefined,
                enumerable: true
            });
            (new MutationObserver(
                (ms) => _removeChild(ms[0].target)
            )).observe(style, { childList: true });
        }


        function _create() {
            let style = _appendChild(_createElement('style'));
            Object.assign(style, props);

            function insertRules(rule) {
                if (rule.forEach)
                    rule.forEach(insertRules);
                else try {
                    style.sheet.insertRule(rule, 0);
                } catch (e) {
                    console.error(e);
                }
            }

            insertRules(rules);
            _protect(style);

            return style;
        }

        let style = _create();
        if (skip_protect)
            return style;

        (new MutationObserver(
            function(ms) {
                let m, node;
                let createStyleInANewThread = resolve => setTimeout(
                    resolve => resolve(_create()),
                    0, resolve
                );
                let setStyle = st => void(style = st);
                for (m of ms) for (node of m.removedNodes)
                    if (node === style)
                        (new Promise(createStyleInANewThread))
                            .then(setStyle);
            }
        )).observe(_de, { childList: true });

        return style;
    }

    if (!isFirefox) {
        // scripts for non-Firefox browsers
        // https://greasyfork.org/scripts/14720-it-s-not-important
        unimptt: {
            // BigInt were implemented in Chrome 67 which also support
            // proper user styles and doesn't need this fix anymore.
            if ((isChrome || isOpera) && 'BigInt' in win)
                break unimptt;

            let imptt = /((display|(margin|padding)(-top|-bottom)?)\s*:[^;!]*)!\s*important/ig,
                ret_b = (a,b) => b,
                _toLowerCase = String.prototype.toLowerCase,
                protectedNodes = new WeakSet(),
                log = false;

            let logger = function() {
                if (log)
                    console.log('Some page elements became a bit less important.');
                log = false;
            };

            let unimportanter = function(node) {
                let style = (node.nodeType === _Node.ELEMENT_NODE) ?
                    _getAttribute(node, 'style') : null;

                if (!style || !imptt.test(style) || node.style.display === 'none' ||
                    (node.src && node.src.startsWith('chrome-extension:'))) // Web of Trust IFRAME and similar
                    return false; // get out if we have nothing to do here

                protectedNodes.add(node);
                _setAttribute(node, 'style', style.replace(imptt, ret_b));
                log = true;
            };

            (new MutationObserver(
                function(mutations) {
                    setTimeout(
                        function(ms) {
                            let m, node;
                            for (m of ms) for (node of m.addedNodes)
                                unimportanter(node);
                            logger();
                        }, 0, mutations
                    );
                }
            )).observe(_document, {
                childList : true,
                subtree : true
            });

            _Element.prototype.setAttribute = function setAttribute(name, value) {
                '[native code]';
                let replaced = value;
                if (name && _toLowerCase.call(name) === 'style' && protectedNodes.has(this))
                    replaced = value.replace(imptt, ret_b);
                log = (replaced !== value);
                logger();
                return _setAttribute(this, ...arguments);
            };

            win.addEventListener (
                'load', () => {
                    for (let imp of _document.querySelectorAll('[style*="!"]'))
                        unimportanter(imp);
                    logger();
                }, false
            );
        }

        // Naive ABP Style protector
        {
            let _querySelector = _Document.prototype.querySelector.bind(_document);
            let _removeChild = Function.prototype.call.bind(_Node.prototype.removeChild);
            let _appendChild = Function.prototype.call.bind(_Node.prototype.appendChild);
            let createShadow = () => _createElement('shadow');
            // Prevent adding fake content entry point
            _Node.prototype.appendChild = function appendChild(child) {
                if (this instanceof ShadowRoot &&
                    child instanceof HTMLContentElement)
                    return _appendChild(this, createShadow());
                return _appendChild(this, ...arguments);
            };
            {
                let _shadowSelector = Function.prototype.call.bind(ShadowRoot.prototype.querySelector);
                let _innerHTML = Object.getOwnPropertyDescriptor(ShadowRoot.prototype, 'innerHTML');
                let _parentNode = Object.getOwnPropertyDescriptor(_Node.prototype, 'parentNode');
                if (_innerHTML && _parentNode) {
                    let _set = Function.prototype.call.bind(_innerHTML.set);
                    let _getParent = Function.prototype.call.bind(_parentNode.get);
                    _innerHTML.configurable = false;
                    _innerHTML.set = function() {
                        _set(this, ...arguments);
                        let content = _shadowSelector(this, 'content');
                        if (content) {
                            let parent = _getParent(content);
                            _removeChild(parent, content);
                            _appendChild(parent, createShadow());
                        }
                    };
                }
                Object.defineProperty(ShadowRoot.prototype, 'innerHTML', _innerHTML);
            }
            // Locate and apply extra protection to a style on top of what ABP does
            let style;
            (new Promise(
                function(resolve, reject) {
                    let getStyle = () => _querySelector('::shadow style');
                    style = getStyle();
                    if (style)
                        return resolve(style);
                    let intv = setInterval(
                        function() {
                            style = getStyle();
                            if (!style)
                                return;
                            intv = clearInterval(intv);
                            return resolve(style);
                        }, 0
                    );
                    _document.addEventListener(
                        'DOMContentLoaded', () => {
                            if (intv)
                                clearInterval(intv);
                            style = getStyle();
                            return style ? resolve(style) : reject();
                        }, false
                    );
                }
            )).then(
                function(style) {
                    let emptyArr = [],
                        nullStr = {
                            get: () => '',
                            set: () => undefined
                        };
                    let shadow = style.parentNode;
                    Object.defineProperties(shadow, {
                        childElementCount: { value: 0 },
                        styleSheets: { value: emptyArr },
                        firstChild: { value: null },
                        firstElementChild: { value: null },
                        lastChild: { value: null },
                        lastElementChild: { value: null },
                        childNodes: { value: emptyArr },
                        children: { value: emptyArr },
                        innerHTML: { value: nullStr },
                    });
                    Object.defineProperties(style, {
                        innerHTML: { value: nullStr },
                        textContent: { value: nullStr },
                        ownerDocument: { value: null },
                        parentNode: {value: null },
                        previousElementSibling: { value: null },
                        previousSibling: { value: null },
                        disabled: { get: () => true, set: () => null }
                    });
                    Object.defineProperties(style.sheet, {
                        deleteRule: { value: () => null },
                        disabled: { get: () => true, set: () => null },
                        cssRules: { value: emptyArr },
                        rules: { value: emptyArr }
                    });
                }
            ).catch(()=>null);
            _Node.prototype.removeChild = function removeChild(child) {
                if (child === style)
                    return;
                return _removeChild(this, ...arguments);
            };
        }
    }

    if (/^https?:\/\/(mail\.yandex\.|music\.yandex\.|news\.yandex\.|(www\.)?yandex\.[^/]+\/(yand)?search[/?])/i.test(win.location.href) ||
        /^https?:\/\/tv\.yandex\./i.test(win.location.href)) {
        // https://greasyfork.org/en/scripts/809-no-yandex-ads
        let yadWord = /Яндекс.Директ/i,
            adWords = /Реклама|Ad/i;
        let _querySelector = _document.querySelector.bind(_document),
            _querySelectorAll = _document.querySelectorAll.bind(_document),
            _getAttribute = Function.prototype.call.bind(_Element.prototype.getAttribute),
            _setAttribute = Function.prototype.call.bind(_Element.prototype.setAttribute);
        // Function to attach an observer to monitor dynamic changes on the page
        let pageUpdateObserver = (func, obj, params) => {
            if (obj)
                (new MutationObserver(func))
                    .observe(obj, (params || { childList:true, subtree:true }));
        };
        // Short name for parentNode.removeChild and setAttribute style to display:none
        let remove = (node) => {
            if (!node || !node.parentNode)
                return false;
            console.log('Removed node.');
            node.parentNode.removeChild(node);
        };
        let hide = (node) => {
            if (!node)
                return false;
            console.log('Hid node.');
            _setAttribute(node, 'style', 'display:none!important');
        };
        // Yandex search ads in Google Chrome
        if ('attachShadow' in _Element.prototype) {
            let _attachShadow = _Element.prototype.attachShadow;
            _Element.prototype.attachShadow = function() {
                let node = this,
                    root = _attachShadow.apply(node, arguments);
                pageUpdateObserver(
                    (ms) => {
                        for (let m of ms) if (m.addedNodes.length)
                            if (adWords.test(root.textContent))
                                remove(node.closest('.serp-item'));
                    }, root
                );
                return root;
            };
        }
        // prevent/defuse adblock detector
        setInterval(()=>{
            localStorage.ic = '';
            localStorage._mt__data = '';
        },100);
        let yp_keepCookieParts = /\.(sp|ygo|ygu)\./; // ygo = city id; ygu = detect city automatically
        let _doc_proto = ('cookie' in _Document.prototype) ? _Document.prototype : Object.getPrototypeOf(_document);
        let _cookie = Object.getOwnPropertyDescriptor(_doc_proto, 'cookie');
        if (_cookie) {
            let _set_cookie = Function.prototype.call.bind(_cookie.set);
            _cookie.set = function(value) {
                if (/^(mda=|yp=|ys=|yabs-|__|bltsr=)/.test(value))
                    // remove value, set expired
                    if (!value.startsWith('yp=')) {
                        value = value.replace(/^([^=]+=)[^;]+/,'$1').replace(/(expires=)[\w\s\d,]+/,'$1Thu, 01 Jan 1970 00');
                        console.log('expire cookie', value.match(/^[^=]+/)[0]);
                    } else {
                        let parts = value.split(';');
                        let values = parts[0].split('#').filter(part => yp_keepCookieParts.test(part));
                        if (values.length)
                            values[0] = values[0].replace(/^yp=/, '');
                        let res = `yp=${values.join('#')}`;
                        console.log(`set cookie ${res}, dropped ${parts[0].replace(res,'')}`);
                        parts[0] = res;
                        value = parts.join(';');
                    }
                return _set_cookie(this, value);
            };
            Object.defineProperty(_doc_proto, 'cookie', _cookie);
        }
        // other ads
        _document.addEventListener(
            'DOMContentLoaded', () => {
                {
                    // Generic ads removal and fixes
                    let node = _querySelector('.serp-header');
                    if (node)
                        node.style.marginTop = '0';
                    for (node of _querySelectorAll(
                        '.serp-adv__head + .serp-item,'+
                        '#adbanner,'+
                        '.serp-adv,'+
                        '.b-spec-adv,'+
                        'div[class*="serp-adv__"]:not(.serp-adv__found):not(.serp-adv__displayed)'
                    )) remove(node);
                }
                // Search ads
                function removeSearchAds() {
                    for (let node of _querySelectorAll('.serp-item'))
                        if (_getAttribute(node, 'role') === 'complementary' ||
                            adWords.test((node.querySelector('.label')||{}).textContent))
                            hide(node);
                }
                // News ads
                function removeNewsAds() {
                    let node, block, items, mask, classes,
                        masks = [
                            { class: '.ads__wrapper', regex: /[^,]*?,[^,]*?\.ads__wrapper/ },
                            { class: '.ads__pool', regex: /[^,]*?,[^,]*?\.ads__pool/ }
                        ];
                    for (node of _querySelectorAll('style[nonce]')) {
                        classes = node.innerText.replace(/\{[^}]+\}+/ig, '|').split('|');
                        for (block of classes) for (mask of masks)
                            if (block.includes(mask.class)) {
                                block = block.match(mask.regex)[0];
                                items = _querySelectorAll(block);
                                for (item of items)
                                    remove(items[0]);
                            }
                    }
                }
                // Music ads
                function removeMusicAds() {
                    for (let node of _querySelectorAll('.ads-block'))
                        remove(node);
                }
                // Mail ads
                function removeMailAds() {
                    let slice = Array.prototype.slice,
                        nodes = slice.call(_querySelectorAll('.ns-view-folders')),
                        node, len, cls;

                    for (node of nodes)
                        if (!len || len > node.classList.length)
                            len = node.classList.length;

                    node = nodes.pop();
                    while (node) {
                        if (node.classList.length > len)
                            for (cls of slice.call(node.classList))
                                if (cls.indexOf('-') === -1) {
                                    remove(node);
                                    break;
                                }
                        node = nodes.pop();
                    }
                }
                // News fixes
                function removePageAdsClass() {
                    if (_document.body.classList.contains("b-page_ads_yes")) {
                        _document.body.classList.remove("b-page_ads_yes");
                        console.log('Page ads class removed.');
                    }
                }
                // TV fixes
                function removeTVAds() {
                    for (let node of _querySelectorAll('div[class^="_"][data-reactid] > div'))
                        if (yadWord.test(node.textContent) || node.querySelector('iframe:not([src])')) {
                            if (node.offsetWidth) {
                                let pad = _document.createElement('div');
                                _setAttribute(pad, 'style', `width:${node.offsetWidth}px`);
                                node.parentNode.appendChild(pad);
                            }
                            remove(node);
                        }
                }

                if (location.hostname.startsWith('mail.')) {
                    pageUpdateObserver(
                        function(ms, o) {
                            let aside = _querySelector('.mail-Layout-Aside');
                            if (aside) {
                                o.disconnect();
                                pageUpdateObserver(removeMailAds, aside);
                            }
                        }, _document.body
                    );
                    removeMailAds();
                } else if (location.hostname.startsWith('music.')) {
                    pageUpdateObserver(removeMusicAds, _querySelector('.sidebar'));
                    removeMusicAds();
                } else if (location.hostname.startsWith('news.')) {
                    pageUpdateObserver(removeNewsAds, _document.body);
                    pageUpdateObserver(removePageAdsClass, _document.body, { attributes:true, attributesFilter:['class'] });
                    removeNewsAds();
                    removePageAdsClass();
                } else if (location.hostname.startsWith('tv.')) {
                    pageUpdateObserver(removeTVAds, _document.body);
                    removeTVAds();
                } else {
                    pageUpdateObserver(removeSearchAds, _querySelector('.main__content'));
                    removeSearchAds();
                }
            }
        );
    }

    // Generic Yandex Scripts
    if (/^https?:\/\/([^.]+\.)*yandex\.[^/]+/i.test(win.location.href)) {
        // remove banner on the start page
        scriptLander(() => {
            let nt = new nullTools({log: false, trace: true});
            let AwapsJsonAPI_Json = function(...args) {
                console.log('>> new AwapsJsonAPI.Json(', ...args, ')');
            };
            [
                'setID', 'addImageContent',
                'sendCounts', 'expand', 'refreshAd'
            ].forEach(name => void(AwapsJsonAPI_Json.prototype[name] = nt.func(null, `AwapsJsonAPI.Json.${name}`)));
            AwapsJsonAPI_Json.prototype.checkBannerVisibility = nt.func(true, 'AwapsJsonAPI.Json.checkBannerVisibility');
            AwapsJsonAPI_Json.prototype.addIframeContent = nt.proxy(function(...args) {
                try {
                    let frame = args[1][0].parentNode;
                    frame.parentNode.removeChild(frame);
                    console.log(`Removed banner placeholder.`);
                } catch(ignore) {
                    console.log(`Can't locate frame object to remove.`);
                }
            });
            AwapsJsonAPI_Json.prototype.getHTML = nt.func('', 'AwapsJsonAPI.Json.getHTML');
            AwapsJsonAPI_Json.prototype = nt.proxy(AwapsJsonAPI_Json.prototype);
            AwapsJsonAPI_Json = nt.proxy(AwapsJsonAPI_Json);
            if ('AwapsJsonAPI' in win) {
                console.log('Oops! AwapsJsonAPI already defined.');
                let f = win.AwapsJsonAPI.Json;
                win.AwapsJsonAPI.Json = AwapsJsonAPI_Json;
                if (f && f.prototype)
                    f.prototype = AwapsJsonAPI_Json.prototype;
            } else
                nt.define(win, 'AwapsJsonAPI', nt.proxy({
                    Json: AwapsJsonAPI_Json
                }));

            let parseExport = x => {
                if (!x)
                    return x;
                // remove banner placeholder
                if (x.banner && x.banner.cls) {
                    let _parent = `.${x.banner.cls.banner__parent}`;
                    _document.addEventListener('DOMContentLoaded', () => {
                        for (let banner of _document.querySelectorAll(_parent)) {
                            _setAttribute(banner, 'style', 'display:none!important');
                            console.log('Hid banner placeholder.');
                        }
                    }, false);
                }

                // remove banner data and some other stuff
                delete x.banner;
                delete x.consistency;
                delete x['i-bannerid'];
                delete x['i-counter'];
                delete x['promo-curtain'];

                // remove parts of ga-counter (complete removal break "ТВ Онлайн")
                if (x['ga-counter'] && x['ga-counter'].data) {
                    x['ga-counter'].data.id = 0;
                    delete x['ga-counter'].data.ether;
                    delete x['ga-counter'].data.iframeSrc;
                    delete x['ga-counter'].data.iframeSrcEx;
                }

                return x;
            };
            let _home = win.home;
            Object.defineProperty(win, 'home', {
                get: () => _home,
                set: vl => {
                    if (vl === _home)
                        return;
                    console.log('home =', vl);
                    let _home_export = parseExport(vl.export);
                    Object.defineProperty(vl, 'export', {
                        get: () => _home_export,
                        set: vl => {
                            _home_export = parseExport(vl);
                        }
                    });
                    _home = vl;
                }
            });
        }, nullTools, 'let _setAttribute = Function.prototype.call.bind(_Element.prototype.setAttribute)');

        if ('attachShadow' in _Element.prototype) {
            let fakeRoot = () => ({
                firstChild: null,
                appendChild: ()=>null,
                querySelector: ()=>null,
                querySelectorAll: ()=>null
            });
            _Element.prototype.createShadowRoot = fakeRoot;
            let shadows = new WeakMap();
            let _attachShadow = Object.getOwnPropertyDescriptor(_Element.prototype, 'attachShadow');
            _attachShadow.value = function() {
                return shadows.set(this, fakeRoot()).get(this);
            };
            Object.defineProperty(_Element.prototype, 'attachShadow', _attachShadow);
            let _shadowRoot = Object.getOwnPropertyDescriptor(_Element.prototype, 'shadowRoot');
            _shadowRoot.set = () => null;
            _shadowRoot.get = function() {
                return shadows.has(this) ? shadows.get(this) : void 0;
            };
            Object.defineProperty(_Element.prototype, 'shadowRoot', _shadowRoot);
        }

        // Disable banner styleSheet (on a main page)
        document.addEventListener('DOMContentLoaded', () => {
            for (let sheet of document.styleSheets)
                try {
                    for (let rule of sheet.cssRules)
                        if (rule.cssText.includes(' 728px 90px')) {
                            rule.parentStyleSheet.disabled = true;
                            console.log('Disabled banner styleSheet:', rule.parentStyleSheet);
                        }
                } catch(ignore) {}
        }, false);

        // Partially based on https://greasyfork.org/en/scripts/22737-remove-yandex-redirect
        let selectors = (
            'A[onmousedown*="/jsredir"],'+
            'A[data-vdir-href],'+
            'A[data-counter]'
        );
        let removeTrackingAttributes = function(link) {
            link.removeAttribute('onmousedown');
            if (link.hasAttribute('data-vdir-href')) {
                link.removeAttribute('data-vdir-href');
                link.removeAttribute('data-orig-href');
            }
            if (link.hasAttribute('data-counter')) {
                link.removeAttribute('data-counter');
                link.removeAttribute('data-bem');
            }
        };
        let removeTracking = function(scope) {
            if (scope instanceof Element)
                for (let link of scope.querySelectorAll(selectors))
                    removeTrackingAttributes(link);
        };
        _document.addEventListener('DOMContentLoaded', (e) => removeTracking(e.target));
        (new MutationObserver(
            function(ms) {
                let m, node;
                for (m of ms) for (node of m.addedNodes)
                    if (node instanceof HTMLAnchorElement && node.matches(selectors))
                        removeTrackingAttributes(node);
                    else
                        removeTracking(node);
            }
        )).observe(_de, { childList: true, subtree: true });
    }

    // https://greasyfork.org/en/scripts/21937-moonwalk-hdgo-kodik-fix v0.8 (adapted)
    _document.addEventListener(
        'DOMContentLoaded', function() {
            function log (name) {
                console.log(`Player FIX: Detected ${name} player in ${location.href}`);
            }
            function removeVast (data) {
                if (data && (data.vast || data.reserve_vast || data.vast_button)) {
                    console.log('Removed:\ndata.vast', data.vast, '\ndata.reserve_vast', data.reserve_vast, '\ndata.vast_button', data.vast_button);
                    delete data.vast;
                    delete data.reserve_vast;
                    delete data.vast_button;
                    if (data.chain) {
                        let need = [],
                            drop = [],
                            links = data.chain.split('.');
                        for (let link of links)
                            if (!/^vast_|_vast_|_vast$/.test(link))
                                need.push(link);
                            else
                                drop.push(link);
                        console.log('Dropped from the chain:', ...drop);
                        data.chain = need.join('.');
                    }
                }
                return data;
            }
            if (win.video_balancer !== void 0 && win.event_callback !== void 0) {
                log('Moonwalk');
                if (video_balancer.adv_loader)
                    removeVast(video_balancer.adv_loader.options);
                if ('_mw_adb' in win)
                    Object.defineProperty(win, '_mw_adb', {
                        get: () => false,
                        set: () => true
                    });
            /*} else if (win.stat_url !== undefined && win.is_html5 !== undefined && win.is_wp8 !== undefined) {
                log('HDGo');
                _document.body.onclick = null;
                let tmp = _document.querySelector('#swtf');
                if (tmp)
                    tmp.style.display = 'none';
                if (win.banner_second !== void 0)
                    win.banner_second = 0;
                if (win.$banner_ads !== void 0)
                    win.$banner_ads = false;
                if (win.$new_ads !== void 0)
                    win.$new_ads = false;
                if (win.createCookie !== void 0)
                    win.createCookie('popup', 'true', '999');
                if (win.canRunAds !== void 0 && win.canRunAds !== true)
                    win.canRunAds = true;*/
            } else if (win.startKodikPlayer !== void 0) {
                log('Kodik');
                // skip attempt to block access to HD resolutions
                let chainCall = new Proxy({}, { get: () => () => chainCall });
                if ($ && $.prototype && $.prototype.addClass) {
                    let $addClass = $.prototype.addClass;
                    $.prototype.addClass = function (className) {
                        if (className === 'blocked')
                            return chainCall;
                        return $addClass.apply(this, arguments);
                    };
                }
                // remove ad links from the metadata
                let _ajax = win.$.ajax;
                win.$.ajax = (params, ...args) => {
                    if (params.success) {
                        let _s = params.success;
                        params.success = (data, ...args) => _s(removeVast(data), ...args);
                    }
                    return _ajax(params, ...args);
                }
            } else if (win.getnextepisode && win.uppodEvent) {
                log('Share-Serials.net');
                scriptLander(
                    function() {
                        let _setInterval = win.setInterval,
                            _setTimeout = win.setTimeout,
                            _toString = Function.prototype.call.bind(Function.prototype.toString);
                        win.setInterval = function(func) {
                            if (func instanceof Function && _toString(func).includes('_delay')) {
                                let intv = _setInterval.call(
                                    this, function() {
                                        _setTimeout.call(
                                            this, function(intv) {
                                                clearInterval(intv);
                                                let timer = _document.querySelector('#timer');
                                                if (timer)
                                                    timer.click();
                                            }, 100, intv);
                                        func.call(this);
                                    }, 5
                                );

                                return intv;
                            }
                            return _setInterval.apply(this, arguments);
                        };
                        win.setTimeout = function(func) {
                            if (func instanceof Function && _toString(func).includes('adv_showed'))
                                return _setTimeout.call(this, func, 0);
                            return _setTimeout.apply(this, arguments);
                        };
                    }
                );
            } else if ('ADC' in win) {
                log('vjs-creatives plugin in');
                let replacer = (obj) => {
                    for (let name in obj)
                        if (obj[name] instanceof Function)
                            obj[name] = () => null;
                };
                replacer(win.ADC);
                replacer(win.currentAdSlot);
            }
            UberVK: {
                if (!inIFrame)
                    break UberVK;
                let oddNames = 'HD' in win &&
                    !Object.getOwnPropertyNames(win).every(n => !n.startsWith('_0x'));
                if (!oddNames)
                    break UberVK;
                log('UberVK');
                XMLHttpRequest.prototype.open = () => {
                    throw 404;
                };
            }
        }, false
    );

    // Applies wrapper function on the current page and all newly created same-origin iframes
    // This is used to prevent trick which allows to get fresh page API through newly created same-origin iframes
    function deepWrapAPI(wrapper) {
        let wrapped = new WeakSet(),
            _get_contentWindow = () => null,
            log = (...args) => false && console.log(...args);
        let wrapAPI = root => {
            if (!root || wrapped.has(root))
                return;
            wrapped.add(root);
            try {
                wrapper(root instanceof HTMLIFrameElement ? _get_contentWindow(root) : root);
                log('Wrapped API in', (root === win) ? "main window." : root);
            } catch(e) {
                log('Failed to wrap API in', (root === win) ? "main window." : root, '\n', e);
            }
        };

        // wrap API on contentWindow access
        let _contentWindow = Object.getOwnPropertyDescriptor(HTMLIFrameElement.prototype, 'contentWindow');
        _get_contentWindow = Function.prototype.apply.bind(_contentWindow.get);
        _contentWindow.get = function() {
            wrapAPI(this);
            return _get_contentWindow(this);;
        };
        Object.defineProperty(HTMLIFrameElement.prototype, 'contentWindow', _contentWindow);

        // wrap API on contentDocument access
        let _contentDocument = Object.getOwnPropertyDescriptor(HTMLIFrameElement.prototype, 'contentDocument');
        let _get_contentDocument = Function.prototype.apply.bind(_contentDocument.get);
        _contentDocument.get = function() {
            wrapAPI(this);
            return _get_contentDocument(this);
        };
        Object.defineProperty(HTMLIFrameElement.prototype, 'contentDocument', _contentDocument);

        // manual children objects traverser to avoid issues
        // with calling querySelectorAll on wrong types of objects
        let _call = Function.prototype.call;
        let _nodeType = _call.bind(Object.getOwnPropertyDescriptor(_Node.prototype, 'nodeType').get);
        let _childNodes = _call.bind(Object.getOwnPropertyDescriptor(_Node.prototype, 'childNodes').get);
        let _ELEMENT_NODE = _Node.prototype.ELEMENT_NODE;
        let _DOCUMENT_FRAGMENT_NODE = _Node.prototype.DOCUMENT_FRAGMENT_NODE
        let wrapFrames = root => {
            if (_nodeType(root) !== _ELEMENT_NODE && _nodeType(root) !== _DOCUMENT_FRAGMENT_NODE)
                return; // only process nodes which may contain an IFRAME or be one
            if (root instanceof HTMLIFrameElement) {
                wrapAPI(root);
                return;
            }
            for (let child of _childNodes(root))
                wrapFrames(child);
        };

        // wrap API in a newly appended iframe objects
        let _appendChild = Function.prototype.apply.bind(Node.prototype.appendChild);
        Node.prototype.appendChild = function appendChild() {
            let res = _appendChild(this, arguments);
            wrapFrames(res);
            return res;
        };

        // wrap API in iframe objects created with innerHTML of element on page
        let _innerHTML = Object.getOwnPropertyDescriptor(_Element.prototype, 'innerHTML');
        let _set_innerHTML = Function.prototype.apply.bind(_innerHTML.set);
        _innerHTML.set = function() {
            _set_innerHTML(this, arguments);
            if (_document.contains(this))
                wrapFrames(this);
        };
        Object.defineProperty(_Element.prototype, 'innerHTML', _innerHTML);

        wrapAPI(win);
    }

    // piguiqproxy.com / zmctrack.net circumvention and onerror callback prevention
    scriptLander(
        () => {
            // onerror callback blacklist
            let masks = [],
                blockAll = /(^|\.)(((a-dot-)?kinozal-tv|rutracker-org)\.appspot\.com)$/,
                isBlocked = url => masks.some(mask => mask.test(url)) || blockAll.test(location.hostname);
            for (let filter of [// blacklist
                '/fuckadblock/', '/fuckadblock.',
                '||185.87.50.147^',
                '||10root25.website^', '||24video.xxx^',
                '||adlabs.ru^', '||adspayformymortgage.win^', '||amgload.net^', '||aviabay.ru^',
                '||bgrndi.com^', '||brokeloy.com^',
                '||cnamerutor.ru^',
                '||directadvert.ru^', '||dsn-fishki.ru^', '||docfilms.info^', '||dreadfula.ru^',
                '||et-code.ru^', '||etcodes.com^',
                '||franecki.net^', '||film-doma.ru^',
                '||free-torrent.org^', '||free-torrent.pw^',
                '||free-torrents.org^', '||free-torrents.pw^',
                '||game-torrent.info^', '||gocdn.ru^',
                '||hdkinoshka.com^', '||hghit.com^', '||hindcine.net^',
                '||kiev.ua^', '||kinotochka.net^', '||kinott.com^', '||kinott.ru^',
                '||klcheck.com^', '||kuveres.com^',
                '||lepubs.com^', '||luxadv.com^', '||luxup.ru^', '||luxupcdna.com^',
                '||marketgid.com^', '||mebablo.com^', '||mixadvert.com^', '||mxtads.com^',
                '||nickhel.com^',
                '||oconner.biz^', '||oconner.link^', '||octoclick.net^', '||octozoon.org^',
                '||piguiqproxy.com^', '||pkpojhc.com^',
                '||psma01.com^', '||psma02.com^', '||psma03.com^',
                '||rcdn.pro^', '||recreativ.ru^', '||redtram.com^', '||regpole.com^',
                '||rootmedia.ws^', '||ruttwind.com^', '||rutvind.com^',
                '||skidl.ru^', '||smi2.net^', '||smcheck.org^',
                '||torvind.com^', '||traffic-media.co^', '||trafmag.com^', '||ttarget.ru^',
                '||utarget.ru^',
                '||webadvert-gid.ru^', '||webadvertgid.ru^',
                '||xxuhter.ru^',
                '||yuiout.online^',
                '||zmctrack.net^', '||zoom-film.ru^'])
                masks.push(new RegExp(
                    filter.replace(/([\\/[\].+?(){}$])/g, '\\$1')
                    .replace(/\*/g, '.*?')
                    .replace(/\^(?!$)/g,'\\.?[^\\w%._-]')
                    .replace(/\^$/,'\\.?([^\\w%._-]|$)')
                    .replace(/^\|\|/,'^(ws|http)s?:\\/+([^/.]+\\.)*?'),
                    'i'));
            // main script
            deepWrapAPI(root => {
                let _call = root.Function.prototype.call,
                    _defineProperty = root.Object.defineProperty,
                    _getOwnPropertyDescriptor = root.Object.getOwnPropertyDescriptor;
                onerror: {
                    // 'onerror' handler for scripts from blacklisted sources
                    let scriptMap = new WeakMap();
                    let _Reflect_apply = root.Reflect.apply,
                        _HTMLScriptElement = root.HTMLScriptElement,
                        _HTMLImageElement = root.HTMLImageElement;
                    let _get_tagName = _call.bind(_getOwnPropertyDescriptor(root.Element.prototype, 'tagName').get),
                        _get_scr_src = _call.bind(_getOwnPropertyDescriptor(_HTMLScriptElement.prototype, 'src').get),
                        _get_img_src = _call.bind(_getOwnPropertyDescriptor(_HTMLImageElement.prototype, 'src').get);
                    let _get_src = node => {
                        if (node instanceof _HTMLScriptElement)
                            return _get_scr_src(node);
                        if (node instanceof _HTMLImageElement)
                            return _get_img_src(node);
                        return void 0
                    };
                    let _onerror = _getOwnPropertyDescriptor(root.HTMLElement.prototype, 'onerror'),
                        _set_onerror = _call.bind(_onerror.set);
                    _onerror.get = function() {
                        return scriptMap.get(this) || null;
                    };
                    _onerror.set = function(callback) {
                        if (typeof callback !== 'function') {
                            scriptMap.delete(this);
                            _set_onerror(this, callback);
                            return;
                        }
                        scriptMap.set(this, callback);
                        _set_onerror(this, function() {
                            let src = _get_src(this);
                            if (isBlocked(src)) {
                                console.warn(`Blocked "onerror" callback from ${_get_tagName(this)}: ${src}`);
                                return;
                            }
                            _Reflect_apply(scriptMap.get(this), this, arguments);
                        });
                    };
                    _defineProperty(root.HTMLElement.prototype, 'onerror', _onerror);
                }
                // Simplistic WebSocket wrapper for Maxthon and Firefox before v58
                WSWrap: { // once again seems required in Google Chrome and similar browsers due to zmctrack.net -_-
                    if (true /*/Maxthon/.test(navigator.appVersion) ||
                        'InstallTrigger' in win && 'StopIteration' in win*/) {
                        let _ws = _getOwnPropertyDescriptor(root, 'WebSocket');
                        if (!_ws)
                            break WSWrap;
                        _ws.value = new Proxy(_ws.value, {
                            construct: (ws, args) => {
                                if (isBlocked(args[0])) {
                                    console.log('Blocked WS connection:', args[0]);
                                    return {};
                                }
                                return new ws(...args);
                            }
                        });
                        _defineProperty(root, 'WebSocket', _ws);
                    }
                }
                untrustedClick: {
                    // Block popular method to open a new window in Google Chrome by dispatching a custom click
                    // event on a newly created anchor with _blank target. Untrusted events must not open new windows.
                    let _dispatchEvent = _call.bind(root.EventTarget.prototype.dispatchEvent);
                    root.EventTarget.prototype.dispatchEvent = function dispatchEvent(e) {
                        if (!e.isTrusted && e.type === 'click' && e.constructor.name === 'MouseEvent' &&
                            !this.parentNode && this.tagName === 'A' && this.target[0] === '_') {
                            console.log('Blocked dispatching a click event on a parentless anchor:', this);
                            return;
                        }
                        return _dispatchEvent(this, ...arguments);
                    };
                }
                // XHR Wrapper
                let _proto = void 0;
                try {
                    _proto = root.XMLHttpRequest.prototype;
                } catch(ignore) {
                    return;
                };
                // blacklist of domains where all third-party requests are ignored
                let ondomains = /(^|[/.@])oane\.ws($|[:/])/i;
                // highly suspicious URLs
                let suspicious = /^https?:\/\/(csp-)?([a-z0-9]{6}){1,2}\.ru\//i;
                let on_get_ban = /^https?:\/\/(csp-)?([a-z0-9]{6}){1,2}\.ru\/([a-z0-9/]{40,}|[a-z0-9]{8,}|ad\/banner\/.+)$/i;
                let on_post_ban = /^https?:\/\/(csp-)?([a-z0-9]{6}){1,2}\.ru\/([a-z0-9]{6,})$/i;
                let yandex_direct = /^https?:\/\/(yandex(\.[a-z]{2,3}){1,2}\/(images\/[a-z0-9/_-]{40,}|j?clck\/.*)|[^.]+\.yandex\.net\/static\/main\.js(\?.*)?)$/i;

                function checkRequest(fname, method, url) {
                    if (isBlocked(url) ||
                        ondomains.test(location.hostname) && !ondomains.test(url) ||
                        method === 'GET' && on_get_ban.test(url) ||
                        method === 'POST' && on_post_ban.test(url) ||
                        yandex_direct.test(url)) {
                        console.log(`Blocked ${fname} ${method} request:`, url);
                        return true;
                    }
                    if (suspicious.test(url))
                        console.warn(`Suspicious ${fname} ${method} request:`, url);
                    return false;
                }

                let xhrStopList = new WeakSet();
                let _open = root.Function.prototype.apply.bind(_proto.open);
                _proto.open = function open() {
                    '[native code]';
                    if (checkRequest('xhr', ...arguments)) {
                        xhrStopList.add(this);
                        return;
                    }
                    return _open(this, arguments);
                };
                ['send', 'setRequestHeader', 'getAllResponseHeaders'].forEach(
                    name => {
                        let func = _proto[name];
                        _proto[name] = function(...args) {
                            return xhrStopList.has(this) ? null : func.apply(this, args);
                        };
                    }
                );

                let _fetch = root.Function.prototype.apply.bind(root.fetch);
                root.fetch = function fetch() {
                    '[native code]';
                    let url = arguments[0];
                    let method = arguments[1] ? arguments[1].method : void 0;
                    if (arguments[0] instanceof Request) {
                        method = url.method;
                        url = url.url;
                    }
                    if (checkRequest('fetch', method, url))
                        return new Promise(() => null);
                    return _fetch(root, arguments);
                };
            });

            win.stop = () => {
                console.warn('window.stop() ...y tho?');
                for (let sheet of _document.styleSheets)
                    if (sheet.disabled) {
                        sheet.disabled = false;
                        console.log('Re-enabled:', sheet);
                    }
            }
        }, deepWrapAPI
    );

    // === Helper functions ===

    // function to search and remove nodes by content
    // selector - standard CSS selector to define set of nodes to check
    // words - regular expression to check content of the suspicious nodes
    // params - object with multiple extra parameters:
    //   .log - display log in the console
    //   .hide - set display to none instead of removing from the page
    //   .parent - parent node to remove if content is found in the child node
    //   .siblings - number of simling nodes to remove (excluding text nodes)
    let scRemove = (node) => node.parentNode.removeChild(node);
    let scHide = function(node) {
        let style = _getAttribute(node, 'style') || '',
            hide = ';display:none!important;';
        if (style.indexOf(hide) < 0)
            _setAttribute(node, 'style', style + hide);
    };

    function scissors (selector, words, scope, params) {
        let logger = (...args) => { if (params.log) console.log(...args) };
        if (!scope.contains(_document.body))
            logger('[s] scope', scope);
        let remFunc = (params.hide ? scHide : scRemove),
            iterFunc = (params.siblings > 0 ? 'nextElementSibling' : 'previousElementSibling'),
            toRemove = [],
            siblings;
        for (let node of scope.querySelectorAll(selector)) {
            // drill up to a parent node if specified, break if not found
            if (params.parent) {
                let old = node;
                node = node.closest(params.parent);
                if (node === null || node.contains(scope)) {
                    logger('[s] went out of scope with', old);
                    continue;
                }
            }
            logger('[s] processing', node);
            if (toRemove.includes(node))
                continue;
            if (words.test(node.innerHTML)) {
                // skip node if already marked for removal
                logger('[s] marked for removal');
                toRemove.push(node);
                // add multiple nodes if defined more than one sibling
                siblings = Math.abs(params.siblings) || 0;
                while (siblings) {
                    node = node[iterFunc];
                    if (!node) break; // can't go any further - exit
                    logger('[s] adding sibling node', node);
                    toRemove.push(node);
                    siblings -= 1;
                }
            }
        }
        let toSkip = [];
        for (let node of toRemove)
            if (!toRemove.every(other => other === node || !node.contains(other)))
                toSkip.push(node);
        if (toRemove.length)
            logger(`[s] proceeding with ${params.hide?'hide':'removal'} of`, toRemove, `skip`, toSkip);
        for (let node of toRemove) if (!toSkip.includes(node))
            remFunc(node);
    }

    // function to perform multiple checks if ads inserted with a delay
    // by default does 30 checks withing a 3 seconds unless nonstop mode specified
    // also does 1 extra check when a page completely loads
    // selector and words - passed dow to scissors
    // params - object with multiple extra parameters:
    //   .log - display log in the console
    //   .root - selector to narrow down scope to scan;
    //   .observe - if true then check will be performed continuously;
    // Other parameters passed down to scissors.
    function gardener(selector, words, params) {
        let logger = (...args) => { if (params.log) console.log(...args) };
        params = params || {};
        logger(`[gardener] selector: '${selector}' detector: ${words} options: ${JSON.stringify(params)}`);
        let scope;
        let globalScope = [_de];
        let domLoaded = false;
        let getScope = root => root ? _de.querySelectorAll(root) : globalScope;
        let onevent = e => {
            logger(`[gardener] cleanup on ${Object.getPrototypeOf(e)} "${e.type}"`);
            for (let node of scope)
                scissors(selector, words, node, params);
        };
        let repeater = n => {
            if (!domLoaded && n) {
                setTimeout(repeater, 500, n - 1);
                scope = getScope(params.root);
                if (!scope) // exit if the root element is not present on the page
                    return 0;
                onevent({type: 'Repeater'});
            }
        };
        repeater(20);
        _document.addEventListener(
            'DOMContentLoaded', (e) => {
                domLoaded = true;
                // narrow down scope to a specific element
                scope = getScope(params.root);
                if (!scope) // exit if the root element is not present on the page
                    return 0;
                logger('[g] scope', scope);
                // add observe mode if required
                if (params.observe) {
                    let params = { childList:true, subtree: true };
                    let observer = new MutationObserver(
                        function(ms) {
                            for (let m of ms)
                                if (m.addedNodes.length)
                                    onevent(m);
                        }
                    );
                    for (let node of scope)
                        observer.observe(node, params);
                    logger('[g] observer enabled');
                }
                onevent(e);
            }, false);
        // wait for a full page load to do one extra cut
        win.addEventListener('load', onevent, false);
    }

    // wrap popular methods to open a new tab to catch specific behaviours
    function createWindowOpenWrapper(openFunc) {
        let _createElement = _Document.prototype.createElement,
            _appendChild = _Element.prototype.appendChild,
            fakeNative = (f) => (f.toString = () => `function ${f.name}() { [native code] }`);

        let nt = new nullTools();
        fakeNative(openFunc);

        let parser = _createElement.call(_document, 'a');
        let openWhitelist = (url, parent) => {
            parser.href = url;
            return parser.hostname === 'www.imdb.com' || parser.hostname === 'www.kinopoisk.ru' ||
                parent.hostname === 'radikal.ru' && url === void 0;
        };

        let redefineOpen = (root) => {
            if ('open' in root) {
                let _open = root.open.bind(root);
                nt.define(root, 'open', (...args) => {
                    if (openWhitelist(args[0], location)) {
                        console.log('Whitelisted popup:', ...args);
                        return _open(...args);
                    }
                    return openFunc(...args);
                });
            }
        };
        redefineOpen(win);

        function createElement() {
            '[native code]';
            let el = _createElement.apply(this, arguments);
            // redefine window.open in first-party frames
            if (el instanceof HTMLIFrameElement || el instanceof HTMLObjectElement)
                el.addEventListener('load', (e) => {
                    try {
                        redefineOpen(e.target.contentWindow);
                    } catch(ignore) {}
                }, false);
            return el;
        }
        fakeNative(createElement);

        let redefineCreateElement = (obj) => {
            for (let root of [obj.document, _Document.prototype]) if ('createElement' in root)
                nt.define(root, 'createElement', createElement);
        };
        redefineCreateElement(win);

        // wrap window.open in newly added first-party frames
        _Element.prototype.appendChild = function appendChild() {
            '[native code]';
            let el = _appendChild.apply(this, arguments);
            if (el instanceof HTMLIFrameElement)
                try {
                    redefineOpen(el.contentWindow);
                    redefineCreateElement(el.contentWindow);
                } catch(ignore) {}
            return el;
        };
        fakeNative(_Element.prototype.appendChild);
    }

    // Function to catch and block various methods to open a new window with 3rd-party content.
    // Some advertisement networks went way past simple window.open call to circumvent default popup protection.
    // This funciton blocks window.open, ability to restore original window.open from an IFRAME object,
    // ability to perform an untrusted (not initiated by user) click on a link, click on a link without a parent
    // node or simply a link with piece of javascript code in the HREF attribute.
    function preventPopups() {
        // call sandbox-me if in iframe and not whitelisted
        if (inIFrame) {
            win.top.postMessage({ name: 'sandbox-me', href: win.location.href }, '*');
            return;
        }

        scriptLander(() => {
            let nt = new nullTools({log:true});
            let open = (...args) => {
                '[native code]';
                console.warn('Site attempted to open a new window', ...args);
                return {
                    document: nt.proxy({
                        write: nt.func({}, 'write'),
                        writeln: nt.func({}, 'writeln')
                    }),
                    location: nt.proxy({})
                };
            };

            createWindowOpenWrapper(open);

            console.log('Popup prevention enabled.');
        }, nullTools, createWindowOpenWrapper);
    }

    // Helper function to close background tab if site opens itself in a new tab and then
    // loads a 3rd-party page in the background one (thus performing background redirect).
    function preventPopunders() {
        // create "close_me" event to call high-level window.close()
        let eventName = `close_me_${Math.random().toString(36).substr(2)}`;
        let callClose = () => {
            console.log('close call');
            window.close();
        };
        window.addEventListener(eventName, callClose, true);

        scriptLander(() => {
            // get host of a provided URL with help of an anchor object
            // unfortunately new URL(url, window.location) generates wrong URL in some cases
            let parseURL = _document.createElement('A');
            let getHost = url => {
                parseURL.href = url;
                return parseURL.hostname
            };
            // site went to a new tab and attempts to unload
            // call for high-level close through event
            let closeWindow = () => window.dispatchEvent(new CustomEvent(eventName, {}));
            // check is URL local or goes to different site
            let isLocal = (url) => {
                if (url === location.pathname || url === location.href)
                    return true; // URL points to current pathname or full address
                let host = getHost(url);
                let site = location.hostname;
                return host !== '' && // URLs with unusual protocol may have empty 'host'
                    (site === host || site.endsWith(`.${host}`) || host.endsWith(`.${site}`));
            };

            let _open = window.open.bind(window);
            let open = (...args) => {
                '[native code]';
                let url = args[0];
                if (url && isLocal(url))
                    window.addEventListener('beforeunload', closeWindow, true);
                return _open(...args);
            };

            createWindowOpenWrapper(open);

            console.log("Background redirect prevention enabled.");
        }, `let eventName="${eventName}"`, nullTools, createWindowOpenWrapper);
    }

    // Mix between check for popups and popunders
    // Significantly more agressive than both and can't be used as universal solution
    function preventPopMix() {
        if (inIFrame) {
            win.top.postMessage({ name: 'sandbox-me', href: win.location.href }, '*');
            return;
        }

        // create "close_me" event to call high-level window.close()
        let eventName = `close_me_${Math.random().toString(36).substr(2)}`;
        let callClose = () => {
            console.log('close call');
            window.close();
        };
        window.addEventListener(eventName, callClose, true);

        scriptLander(() => {
            let _open = window.open,
                parseURL = _document.createElement('A');
            // get host of a provided URL with help of an anchor object
            // unfortunately new URL(url, window.location) generates wrong URL in some cases
            let getHost = (url) => {
                parseURL.href = url;
                return parseURL.host;
            };
            // site went to a new tab and attempts to unload
            // call for high-level close through event
            let closeWindow = () => {
                _open(window.location,'_self');
                window.dispatchEvent(new CustomEvent(eventName, {}));
            };
            // check is URL local or goes to different site
            function isLocal(url) {
                let loc = window.location;
                if (url === loc.pathname || url === loc.href)
                    return true; // URL points to current pathname or full address
                let host = getHost(url),
                    site = loc.host;
                if (host === '')
                    return false; // URLs with unusual protocol may have empty 'host'
                if (host.length > site.length)
                    [site, host] = [host, site];
                return site.includes(host, site.length - host.length);
            }

            // add check for redirect for 5 seconds, then disable it
            function checkRedirect() {
                window.addEventListener('beforeunload', closeWindow, true);
                setTimeout(closeWindow=>window.removeEventListener('beforeunload', closeWindow, true), 5000, closeWindow);
            }

            function open(url, name) {
                '[native code]';
                if (url && isLocal(url) && (!name || name === '_blank')) {
                    console.warn('Suspicious local new window', arguments);
                    checkRedirect();
                    return _open.apply(this, arguments);
                }
                console.warn('Blocked attempt to open a new window', arguments);
                return {
                    document: {
                        write: () => {},
                        writeln: () => {}
                    }
                };
            }

            function clickHandler(e) {
                let link = e.target,
                    url = link.href||'';
                if (e.targetParentNode && e.isTrusted || link.target !== '_blank') {
                    console.log('Link', link, 'were created dinamically, but looks fine.');
                    return true;
                }
                if (isLocal(url) && link.target === '_blank') {
                    console.log('Suspicious local link', link);
                    checkRedirect();
                    return;
                }
                console.log('Blocked suspicious click on a link', link);
                e.stopPropagation();
                e.preventDefault();
            }

            createWindowOpenWrapper(open, clickHandler);

            console.log("Mixed popups prevention enabled.");
        }, `let eventName="${eventName}"`, createWindowOpenWrapper);
    }
    // External listener for case when site known to open popups were loaded in iframe
    // It will sandbox any iframe which will send message 'forbid.popups' (preventPopups sends it)
    // Some sites replace frame's window.location with data-url to run in clean context
    if (!inIFrame) window.addEventListener(
        'message', function(e) {
            if (!e.data || e.data.name !== 'sandbox-me' || !e.data.href)
                return;
            let src = e.data.href;
            for (let frame of _document.querySelectorAll('iframe'))
                if (frame.contentWindow === e.source) {
                    if (frame.hasAttribute('sandbox')) {
                        if (!frame.sandbox.contains('allow-popups'))
                            return; // exit frame since it's already sandboxed and popups are blocked
                        // remove allow-popups if frame already sandboxed
                        frame.sandbox.remove('allow-popups');
                    } else
                        // set sandbox mode for troublesome frame and allow scripts, forms and a few other actions
                        // technically allowing both scripts and same-origin allows removal of the sandbox attribute,
                        // but to apply content must be reloaded and this script will re-apply it in the result
                        frame.setAttribute('sandbox','allow-forms allow-scripts allow-presentation allow-top-navigation allow-same-origin');
                    console.log('Disallowed popups from iframe', frame);

                    // reload frame content to apply restrictions
                    if (!src) {
                        src = frame.src;
                        console.log('Unable to get current iframe location, reloading from src', src);
                    } else
                        console.log('Reloading iframe with URL', src);
                    frame.src = 'about:blank';
                    frame.src = src;
                }
        }, false
    );

    function selectiveEval(extra) {
        scriptLander(() => {
            let _eval_def = Object.getOwnPropertyDescriptor(win, 'eval');
            if (!_eval_def || !_eval_def.value) {
                console.log('Unable to wrap window.eval.', _eval_def);
                return;
            }
            let genericPatterns = /_0x|location\s*?=|location.href\s*?=|location.assign\(|open\(/i;
            let _eval_val = _eval_def.value;
            _eval_def.value = function(...args) {
                if (genericPatterns.test(args[0]) || extra && extra.test(args[0])) {
                    console.log(`Skipped eval of ${args[0].slice(0, 512)}\u2026`);
                    return null;
                }
                return _eval_val.apply(this, args);
            };
            Object.defineProperty(win, 'eval', _eval_def);
        }, `let extra = ${extra}`);
    }

    // hides cookies by pattern and attempts to remove them if they already set
    // also prevents setting new versions of such cookies
    function selectiveCookies(scPattern, scPaths = []) {
        scriptLander(() => {
            let ga = '_g(at?|id)|__utm[a-z]'; // Google Analytics Cookies
            scPattern = new RegExp(`(^|;\\s?)(${scPattern}|${ga})($|=)`);
            if (isFirefox && scPaths.length)
                scPaths.forEach((path, id) => scPaths[id] = `${path}/`);
            scPaths.push('/');
            let _doc_proto = ('cookie' in _Document.prototype) ? _Document.prototype : Object.getPrototypeOf(_document);
            let _cookie = Object.getOwnPropertyDescriptor(_doc_proto, 'cookie');
            if (_cookie) {
                let _set_cookie = Function.prototype.call.bind(_cookie.set);
                let _get_cookie = Function.prototype.call.bind(_cookie.get);
                let expireDate = 'Thu, 01 Jan 1970 00:00:01 UTC';
                let expireAge = '-99999999';
                let expireBase = `=;expires=${expireDate};Max-Age=${expireAge}`;
                let expireAttempted = {};
                // expire is called from cookie getter and doesn't know exact parameters used to set cookies present there
                // so, it will use path=/ by default if scPaths wasn't set and attempt to set cookies on all parent domains
                let expire = (cookie, that) => {
                    let domain = that.location.hostname.split('.'),
                        name = cookie.replace(/=.*/,'');
                    scPaths.forEach(path =>_set_cookie(that, `${name}${expireBase};path=${path}`));
                    while (domain.length > 1) {
                        try {
                            scPaths.forEach(
                                path => _set_cookie(that, `${name}${expireBase};domain=${domain.join('.')};path=${path}`)
                            );
                        } catch(e) { console.warn(e); }
                        domain.shift();
                    }
                    expireAttempted[name] = true;
                    console.log('Removing existing cookie:', cookie);
                };
                // skip setting unwanted cookies
                _cookie.set = function(value) {
                    if (scPattern.test(value)) {
                        console.warn('Ignored cookie:', value);
                        // try to remove same cookie if it already exists using exact values from the set string
                        if (scPattern.test(_get_cookie(this))) {
                            let parts = value.split(/;\s?/),
                                name = parts[0].replace(/=.*/,''),
                                newParts = [`${name}=`, `expires=${expireDate}`, `Max-Age=${expireAge}`],
                                skip = [name, 'expires', 'Max-Age'];
                            for (let part of parts)
                                if (!skip.includes(part.replace(/=.*/,'')))
                                    newParts.push(part);
                            try {
                                _set_cookie(this, newParts.join(';'));
                            } catch(e) { console.warn(e); }
                            console.log('Removing existing cookie:', name);
                        }
                        return;
                    }
                    return _set_cookie(this, value);
                };
                // hide unwanted cookies from site
                _cookie.get = function() {
                    let res = _get_cookie(this);
                    if (scPattern.test(res)) {
                        let stack = [];
                        for (let cookie of res.split(/;\s?/))
                            if (!scPattern.test(cookie))
                                stack.push(cookie);
                            else {
                                let name = cookie.replace(/=.*/,'');
                                if (expireAttempted[name]) {
                                    console.log('Unable to expire:', cookie);
                                    expireAttempted[name] = false;
                                }
                                if (!(name in expireAttempted))
                                    expire(cookie, this);
                            }
                        res = stack.join('; ');
                    }
                    return res;
                };
                Object.defineProperty(_doc_proto, 'cookie', _cookie);
            }
            console.log('Active cookies:', document.cookie);
        }, `let scPattern = "${scPattern}", scPaths = ${scPaths}, isFirefox = ${isFirefox};`);
    }

    /*{ // simple toString wrapper, might be useful to prevent detection
        '[native code]';
        let _toString = Function.prototype.apply.bind(Function.prototype.toString);
        let baseText = Function.prototype.toString.toString();
        let protect = new WeakSet();
        protect.add(_Document.prototype.createElement);
        protect.add(_Node.prototype.appendChild);
        protect.add(_Node.prototype.removeChild);
        win.Function.prototype.toString = function() {
            if (protect.has(this))
                return baseText.replace('toString', this.name);
            return _toString(this);
        };
        protect.add(Function.prototype.toString);
    }*/

    // === Scripts for specific domains ===

    let scripts = {};
    // prevent popups and redirects block
    // Popups
    scripts.preventPopups = {
        other: [
            'biqle.ru',
            'chaturbate.com',
            'dfiles.ru',
            'eporner.eu',
            'hentaiz.org',
            'mirrorcreator.com',
            'online-multy.ru',
            'radikal.ru', 'rumedia.ws',
            'tapehub.tech', 'thepiratebay.org',
            'unionpeer.com',
            'zippyshare.com'
        ],
        now: preventPopups
    };
    // Popunders (background redirect)
    scripts.preventPopunders = {
        other: [
            'lostfilm-online.ru',
            'mediafire.com', 'megapeer.org', 'megapeer.ru',
            'perfectgirls.net'
        ],
        now: preventPopunders
    };
    // PopMix (both types of popups encountered on site)
    scripts['openload.co'] = {
        other: ['oload.tv', 'oload.info'],
        now: () => {
            let nt = new nullTools();
            nt.define(win, 'CNight', win.CoinHive);
            if (location.pathname.startsWith('/embed/')) {
                nt.define(win, 'BetterJsPop', {
                    add: ((a, b) => console.warn('BetterJsPop.add', a, b)),
                    config: ((o) => console.warn('BetterJsPop.config', o)),
                    Browser: { isChrome: true }
                });
                nt.define(win, 'isSandboxed', nt.func(null));
                nt.define(win, 'adblock', false);
                nt.define(win, 'adblock2', false);
            } else preventPopMix();
        }
    };
    scripts['turbobit.net'] = preventPopMix;

    scripts['tapochek.net'] = () => {
        // workaround for moradu.com/apu.php load error handler script, not sure which ad network is this
        let _appendChild = Object.getOwnPropertyDescriptor(_Node.prototype, 'appendChild');
        let _appendChild_value = _appendChild.value;
        _appendChild.value = function appendChild(node) {
            if (this === _document.body)
                if ((node instanceof HTMLScriptElement || node instanceof HTMLStyleElement) &&
                    /^https?:\/\/[0-9a-f]{15}\.com\/\d+(\/|\.css)$/.test(node.src) ||
                    node instanceof HTMLDivElement && node.style.zIndex > 900000 &&
                    node.style.backgroundImage.includes('R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7'))
                    throw '...eenope!';
            return _appendChild_value.apply(this, arguments);
        };
        Object.defineProperty(_Node.prototype, 'appendChild', _appendChild);

        // disable window focus tricks and changing location
        let focusHandlerName = /\WfocusAchieved\(/
        let _toString = Function.prototype.call.bind(Function.prototype.toString);
        let _setInterval = win.setInterval;
        win.setInterval = (...args) => {
            if (args.length && focusHandlerName.test(_toString(args[0]))) {
                console.log('skip setInterval for', ...args);
                return -1;
            }
            return _setInterval(...args);
        };
        let _addEventListener = win.addEventListener;
        win.addEventListener = function(...args) {
            if (args.length && args[0] === 'focus' && focusHandlerName.test(_toString(args[1]))) {
                console.log('skip addEventListener for', ...args);
                return void 0;
            }
            return _addEventListener.apply(this, args);
        };

        // generic popup prevention
        preventPopups();
    };

    scripts['rustorka.com'] = {
        other: ['rustorka.club', 'rustorka.lib', 'rustorka.net'],
        now: () => {
            selectiveEval(/antiadblock/);
            selectiveCookies('adblock|u_count|gophp|st2|st3', ['/forum']);
            scriptLander(() => {
                // wrap window.open to catch a popup if it triggers
                win.open = (...args) => {
                    console.warn(`Site attempted to open "${args[0]}" in a new window.`);
                    location.replace(location.href);
                    return null;
                };
                window.addEventListener('DOMContentLoaded', () => {
                    let link = void 0;
                    _document.body.addEventListener('mousedown', e => {
                        link = e.target.closest('a, select, #fancybox-title-wrap');
                    }, false);
                    let _open = window.open.bind(window);
                    let _getAttribute = Function.prototype.call.bind(_Element.prototype.getAttribute);
                    win.open = (...args) => {
                        let url = args[0];
                        if (link instanceof HTMLAnchorElement) {
                            // third-party post links
                            let href = _getAttribute(link, 'href');
                            if (link.classList.contains('postLink') &&
                                !link.matches(`a[href*="${location.hostname}"]`) &&
                                (href === url || link.href === url))
                                return _open(...args);
                            // onclick # links
                            if (href === '#' && /window\.open/.test(_getAttribute(link, 'onclick')))
                                return _open(...args);
                            // force local links to load in the current window
                            if (href[0] === '/' || href.startsWith('./') || href.includes(`//${location.hostname}/`))
                                location.assign(href);
                        }
                        // list of image hostings under upload picture button (new comment)
                        if (link instanceof HTMLSelectElement &&
                            !url.includes(location.hostname) &&
                            link.value === url)
                            return _open(...args);
                        // open screenshot in a new window
                        if (link instanceof HTMLSpanElement &&
                            link.id === 'fancybox-title-wrap')
                            return _open(...args);
                        // looks like tabunder
                        if (link === null && url === location.href)
                            location.replace(url); // reload current page
                        // other cases
                        console.warn(`Site attempted to open "${url}" in a new window. Source: `, link);
                        return {};
                    };
                }, true);
            }, nullTools)
        }
    };

    // = other ======================================================================================
    scripts['1tv.ru'] = {
        other: ['mediavitrina.ru'],
        now: () => scriptLander(() => {
            let nt = new nullTools();
            nt.define(win, 'EUMPAntiblockConfig', nt.proxy({url: '//www.1tv.ru/favicon.ico'}));
            let disablePlugins = {
                'antiblock': false,
                'stat1tv': false
            };
            let _EUMPConfig = void 0;
            let _EUMPConfig_set = x => {
                if (x.plugins) {
                    x.plugins = x.plugins.filter(plugin => (plugin in disablePlugins) ? !(disablePlugins[plugin] = true) : true);
                    console.warn(`Player plugins: active [${x.plugins}], disabled [${Object.keys(disablePlugins).filter(x => disablePlugins[x])}]`);
                }
                _EUMPConfig = x;
            };
            if ('EUMPConfig' in win)
                _EUMPConfig_set(win.EUMPConfig);
            Object.defineProperty(win, 'EUMPConfig', {
                enumerable: true,
                get: () => _EUMPConfig,
                set: _EUMPConfig_set
            });
        }, nullTools)
    };

    scripts['2picsun.ru'] = {
        other: [
            'pics2sun.ru', '3pics-img.ru'
        ],
        now: () => {
            Object.defineProperty(navigator, 'userAgent', {value: 'googlebot'});
        }
    };

    scripts['4pda.ru'] = {
        now: () => {
            // https://greasyfork.org/en/scripts/14470-4pda-unbrender
            let isForum = location.pathname.startsWith('/forum/'),
                remove = node => (node && node.parentNode.removeChild(node)),
                hide = node => (node && (node.style.display = 'none'));

            // save links to non-overridden functions to use later
            let protectedElems;
            // protect/hide changed attributes in case site attempt to restore them
            function styleProtector(eventMode) {
                let _toLowerCase = String.prototype.toLowerCase,
                    isStyleText = (t) => (_toLowerCase.call(t) === 'style'),
                    protectedElems = new WeakMap();
                function protoOverride(element, functionName, isStyleCheck, returnIfProtected) {
                    let originalFunction = element.prototype[functionName];
                    element.prototype[functionName] = function wrapper() {
                        if (protectedElems.has(this) && isStyleCheck(arguments[0]))
                            return returnIfProtected(this, arguments);
                        return originalFunction.apply(this, arguments);
                    };
                }
                protoOverride(Element, 'removeAttribute', isStyleText, () => undefined);
                protoOverride(Element, 'hasAttribute', isStyleText, (_this) => protectedElems.get(_this) !== null);
                protoOverride(Element, 'setAttribute', isStyleText, (_this, args) => protectedElems.set(_this, args[1]));
                protoOverride(Element, 'getAttribute', isStyleText, (_this) => protectedElems.get(_this));
                if (!eventMode)
                    return protectedElems;
                let e = _document.createEvent('Event');
                e.initEvent('protoOverride', false, false);
                window.protectedElems = protectedElems;
                window.dispatchEvent(e);
            }
            if (!isFirefox)
                protectedElems = styleProtector(false);
            else {
                let script = _document.createElement('script');
                script.textContent = `(${styleProtector.toString()})(true);`;
                window.addEventListener(
                    'protoOverride', function protoOverrideCallback() {
                        if (win.protectedElems) {
                            protectedElems = win.protectedElems;
                            delete win.protectedElems;
                        }
                        _document.removeEventListener('protoOverride', protoOverrideCallback, true);
                    }, true
                );
                _appendChild(script);
                _removeChild(script);
            }

            // clean a page
            window.addEventListener(
                'DOMContentLoaded', function() {
                    let width = () => window.innerWidth || _de.clientWidth || _document.body.clientWidth || 0;
                    let height = () => window.innerHeight || _de.clientHeight || _document.body.clientHeight || 0;

                    HeaderAds: {
                        // hide ads above HEADER
                        let header = _document.querySelector('.drop-search');
                        if (!header) {
                            console.warn('Unable to locate header element');
                            break HeaderAds;
                        }
                        header = header.parentNode.parentNode;
                        for (let itm of header.parentNode.children)
                            if (itm !== header)
                                hide(itm);
                            else break;
                    }

                    if (isForum) {
                        let itm = _document.querySelector('#logostrip');
                        if (itm)
                            remove(itm.parentNode.nextSibling);
                        // clear background in the download frame
                        if (location.pathname.startsWith('/forum/dl/')) {
                            let setBackground = node => _setAttribute(
                                node,
                                'style', (_getAttribute(node, 'style') || '') +
                                ';background-color:#4ebaf6!important'
                            );
                            setBackground(_document.body);
                            for (let itm of _document.querySelectorAll('body > div'))
                                if (!itm.querySelector('.dw-fdwlink, .content') && !itm.classList.contains('footer'))
                                    remove(itm);
                                else
                                    setBackground(itm);
                        }
                        // exist from DOMContentLoaded since the rest is not for forum
                        return;
                    }

                    FixNavMenu: {
                        // restore DevDB link in the navigation
                        let itm = _document.querySelector('#nav li a[href$="/devdb/"]')
                        if (!itm) {
                            console.warn('Unable to locate navigation menu');
                            break FixNavMenu;
                        }
                        itm.closest('li').style.display = 'block';
                        // hide ad link from the navigation
                        hide(_document.querySelector('#nav li a[data-dotrack]'));
                    }
                    SidebarAds: {
                        // remove ads from sidebar
                        let aside = _document.querySelectorAll('[class]:not([id]) > [id]:not([class]) > :first-child + :last-child');
                        if (!aside.length) {
                            console.warn('Unable to locate sidebar');
                            break SidebarAds;
                        }
                        let post;
                        for (let side of aside) {
                            console.log('Processing potential sidebar:', side);
                            for (let itm of Array.from(side.children)) {
                                post = itm.classList.contains('post');
                                if (itm.querySelector('iframe') && !post)
                                    remove(itm);
                                if (itm.querySelector('script, a[target="_blank"] > img') && !post || !itm.children.length)
                                    hide(itm);
                            }
                        }
                    }

                    _document.body.setAttribute('style', (_document.body.getAttribute('style')||'')+';background-color:#E6E7E9!important');

                    let extra = 'background-image:none!important;background-color:transparent!important',
                        fakeStyles = new WeakMap(),
                        styleProxy = {
                            get: (target, prop) => fakeStyles.get(target)[prop] || target[prop],
                            set: function(target, prop, value) {
                                let fakeStyle = fakeStyles.get(target);
                                ((prop in fakeStyle) ? fakeStyle : target)[prop] = value;
                                return true;
                            }
                        };
                    for (let itm of _document.querySelectorAll('[id]:not(A), A')) {
                        if (!(itm.offsetWidth > 0.95 * width() &&
                              itm.offsetHeight > 0.85 * height()))
                            continue;
                        if (itm.tagName !== 'A') {
                            fakeStyles.set(itm.style, {
                                'backgroundImage': itm.style.backgroundImage,
                                'backgroundColor': itm.style.backgroundColor
                            });

                            try {
                                Object.defineProperty(itm, 'style', {
                                    value: new Proxy(itm.style, styleProxy),
                                    enumerable: true
                                });
                            } catch (e) {
                                console.log('Unable to protect style property.', e);
                            }

                            if (protectedElems)
                                protectedElems.set(itm, _getAttribute(itm, 'style'));

                            _setAttribute(itm, 'style', `${(_getAttribute(itm, 'style') || '')};${extra}`);
                        }
                        if (itm.tagName === 'A') {
                            if (protectedElems)
                                protectedElems.set(itm, _getAttribute(itm, 'style'));
                            _setAttribute(itm, 'style', 'display:none!important');
                        }
                    }
                }
            );
        }
    };

    scripts['adhands.ru'] = () => scriptLander(() => {
        let nt = new nullTools();
        try {
            let _adv;
            Object.defineProperty(win, 'adv', {
                get: () => _adv,
                set: (v) => {
                    console.log('Blocked advert on adhands.ru.');
                    nt.define(v, 'advert', '');
                    _adv = v;
                }
            });
        } catch (ignore) {
            if (!win.adv)
                console.log('Unable to locate advert on adhands.ru.');
            else {
                console.log('Blocked advert on adhands.ru.');
                nt.define(win.adv, 'advert', '');
            }
        }
    }, nullTools);

    scripts['all-episodes.tv'] = () => {
        let nt = new nullTools();
        nt.define(win, 'perX1', 2);
        createStyle('#advtss, #ad3, a[href*="/ad.admitad.com/"] { display:none!important }');
    };

    scripts['allhentai.ru'] = () => {
        selectiveEval();
        preventPopups();
        scriptLander(() => {
            let _onerror = Object.getOwnPropertyDescriptor(HTMLElement.prototype, 'onerror');
            if (!_onerror)
                return;
            _onerror.set = (...args) => console.log(args[0].toString());
            Object.defineProperty(HTMLElement.prototype, 'onerror', _onerror);
        });
    };

    scripts['allmovie.pro'] = {
        other: ['rufilmtv.org'],
        dom: function() {
            // pretend to be Android to make site use different played for ads
            if (isSafari)
                return;
            Object.defineProperty(navigator, 'userAgent', {
                get: function(){
                    return 'Mozilla/5.0 (Linux; Android 4.1.1; Nexus 7 Build/JRO03D) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.166 Safari/535.19';
                },
                enumerable: true
            });
        }
    };

    scripts['anidub-online.ru'] = {
        other: ['anime.anidub.com', 'online.anidub.com'],
        dom: function() {
            if (win.ogonekstart1)
                win.ogonekstart1 = () => console.log("Fire in the hole!");
        },
        now: () => createStyle([
            '.background {background: none!important;}',
            '.background > script + div,'+
            '.background > script ~ div:not([id]):not([class]) + div[id][class]'+
            '{display:none!important}'
        ])
    };

    scripts['avito.ru'] = () => selectiveCookies('abp|bltsr|cmtchd|crookie|is_adblock');

    scripts['di.fm'] = () => scriptLander(() => {
        let log = false;
        // wrap global app object to catch registration of specific modules
        let _di = void 0;
        Object.defineProperty(win, 'di', {
            get: () => _di,
            set: vl => {
                if (vl === _di)
                    return;
                log && console.log('di =', vl);
                _di = new Proxy(vl, {
                    set: (di, name, vl) => {
                        if (vl === di[name])
                            return true;
                        if (name === 'app') {
                            log && console.log('di.app =', vl);
                            if ('module' in vl)
                                vl.module = new Proxy(vl.module, {
                                    apply: (module, that, args) => {
                                        if (/Wall|Banner|Detect/.test(args[0])) {
                                            let name = args[0];
                                            log && console.warn('wrap', name, 'module');
                                            if (typeof args[1] === 'function')
                                                args[1] = new Proxy(args[1], {
                                                    apply: (fun, that, args) => {
                                                        if (args[0]) // module object
                                                            args[0].start = () => console.log('Skipped start of', name);
                                                        return Reflect.apply(fun, that, args);
                                                    }
                                                });
                                        }
                                        return Reflect.apply(module, that, args);
                                    }
                                });
                        }
                        di[name] = vl;
                        return true;
                    }
                });
            }
        });
        // don't send errorception logs
        Object.defineProperty(win, 'onerror', {
            set: vl => log && console.warn('Skipped global onerror callback', vl)
        });
    });

    scripts['drive2.ru'] = () => {
        gardener('.c-block:not([data-metrika="recomm"]),.o-grid__item', />Реклама<\//i);
        scriptLander(() => {
            let _d2 = void 0;
            Object.defineProperty(win, 'd2', {
                get: () => _d2,
                set: o => {
                    if (o === _d2)
                        return true;
                    _d2 = new Proxy(o, {
                        set: (tgt, prop, val) => {
                            if (['brandingRender', 'dvReveal', '__dv'].includes(prop))
                                val = () => null;
                            tgt[prop] = val;
                            return true;
                        }
                    });
                }
            });
        });
    };

    scripts['fastpic.ru'] = () => {
        let nt = new nullTools();
        // Had to obfuscate property name to avoid triggering anti-obfuscation on greasyfork.org -_- (Exception 403012)
        nt.define(win, `_0x${'4955'}`, []);
    };

    scripts['fishki.net'] = () => {
        scriptLander(() => {
            let nt = new nullTools();
            let fishki = {};
            nt.define(fishki, 'adv', nt.proxy({
                afterAdblockCheck: nt.func(null),
                refreshFloat: nt.func(null)
            }));
            nt.define(fishki, 'is_adblock', false);
            nt.define(win, 'fishki', fishki);
        }, nullTools);
        gardener('.drag_list > .drag_element, .list-view > .paddingtop15, .post-wrap', /543769|Новости\sпартнеров|Полезная\sреклама/);
    };

    scripts['friends.in.ua'] = () => scriptLander(() => {
        Object.defineProperty(win, 'need_warning', {
            get: () => 0, set: () => null
        });
    });

    scripts['gidonline.club'] = () => createStyle('.tray > div[style] {display: none!important}');

    scripts['hdgo.cc'] = {
        other: ['46.30.43.38', 'couber.be'],
        now: () => (new MutationObserver(
            (ms) => {
                let m, node;
                for (m of ms) for (node of m.addedNodes)
                    if (node.tagName instanceof HTMLScriptElement && _getAttribute(node, 'onerror') !== null)
                        node.removeAttribute('onerror');
            }
        )).observe(_document.documentElement, { childList:true, subtree: true })
    };

    scripts['gismeteo.ru'] = {
        other: ['gismeteo.by', 'gismeteo.kz', 'gismeteo.ua'],
        now: () => {
            selectiveCookies('ab_[^=]*|bltsr|redirect|_gab');
            gardener('div > script', /AdvManager/i, { observe: true, parent: 'div' })
        }
    };

    scripts['hdrezka.ag'] = () => {
        Object.defineProperty(win, 'ab', { value: false, enumerable: true });
        gardener('div[id][onclick][onmouseup][onmousedown]', /onmouseout/i);
    };

    scripts['hqq.tv'] = () => scriptLander(() => {
        // disable anti-debugging in hqq.tv player
        let isObfuscated = text => /[^a-z0-9]([a-z0-9]{1,2}\.[a-z0-9]{1,2}\(|[a-z0-9]{4}\.[a-z]\(\d+\)|[a-z0-9]\[[a-z0-9]{1,2}\]\[[a-z0-9]{1,2}\])/i.test(text);
        deepWrapAPI(root => {
            // skip obfuscated stuff and a few other calls
            let _setInterval = root.setInterval,
                _setTimeout = root.setTimeout,
                _toString = root.Function.prototype.call.bind(root.Function.prototype.toString);
            root.setInterval = (...args) => {
                let fun = args[0];
                if (fun instanceof Function) {
                    let text = _toString(fun),
                        skip = text.includes('check();') || isObfuscated(text);
                    console.warn('setInterval', text, 'skip', skip);
                    if (skip) return -1;
                }
                return _setInterval.apply(this, args);
            };
            let wrappedST = new WeakSet();
            root.setTimeout = (...args) => {
                let fun = args[0];
                if (fun instanceof Function) {
                    let text = _toString(fun),
                        skip = fun.name === 'check' || isObfuscated(text);
                    if (!wrappedST.has(fun)) {
                        console.warn('setTimeout', text, 'skip', skip);
                        wrappedST.add(fun);
                    }
                    if (skip) return;
                }
                return _setTimeout.apply(this, args);
            };
            // skip 'debugger' call
            let _eval = root.eval;
            root.eval = text => {
                if (typeof text === 'string' && text.includes('debugger;')) {
                    console.warn('skip eval', text);
                    return;
                }
                _eval(text);
            };
            // Prevent RegExpt + toString trick
            let _proto = void 0;
            try {
                _proto = root.RegExp.prototype;
            } catch(ignore) {
                return;
            }
            let _RE_tS = Object.getOwnPropertyDescriptor(_proto, 'toString');
            let _RE_tSV = _RE_tS.value || _RE_tS.get();
            Object.defineProperty(_proto, 'toString', {
                enumerable: _RE_tS.enumerable,
                configurable: _RE_tS.configurable,
                get: () => _RE_tSV,
                set: val => console.warn('Attempt to change toString for', this, 'with', _toString(val))
            });
        });
    }, deepWrapAPI);

    scripts['hideip.me'] = {
        now: () => scriptLander(() => {
            let _innerHTML = Object.getOwnPropertyDescriptor(_Element.prototype, 'innerHTML');
            let _set_innerHTML = _innerHTML.set;
            let _innerText = Object.getOwnPropertyDescriptor(HTMLElement.prototype, 'innerText');
            let _get_innerText = _innerText.get;
            let div = _document.createElement('div');
            _innerHTML.set = function(...args) {
                _set_innerHTML.call(div, args[0].replace('i','a'));
                if (args[0] && /[рp][еe]кл/.test(_get_innerText.call(div))||
                    /(\d\d\d?\.){3}\d\d\d?:\d/.test(_get_innerText.call(this)) ) {
                    console.log('Anti-Adblock killed.');
                    return true;
                }
                _set_innerHTML.apply(this, args);
            };
            Object.defineProperty(_Element.prototype, 'innerHTML', _innerHTML);
            Object.defineProperty(win, 'adblock', {
                get: () => false,
                set: () => null,
                enumerable: true
            });
            let _$ = {};
            let _$_map = new WeakMap();
            let _gOPD = Object.getOwnPropertyDescriptor(Object, 'getOwnPropertyDescriptor');
            let _val_gOPD = _gOPD.value;
            _gOPD.value = function(...args) {
                let _res = _val_gOPD.apply(this, args);
                if (args[0] instanceof Window && (args[1] === '$' || args[1] === 'jQuery')) {
                    delete _res.get;
                    delete _res.set;
                    _res.value = win[args[1]];
                }
                return _res;
            };
            Object.defineProperty(Object, 'getOwnPropertyDescriptor', _gOPD);
            let getJQWrap = (n) => {
                let name = n;
                return {
                    enumerable: true,
                    get: () => _$[name],
                    set: x => {
                        if (_$_map.has(x)) {
                            _$[name] = _$_map.get(x);
                            return true;
                        }
                        if (x === _$.$ || x === _$.jQuery) {
                            _$[name] = x;
                            return true;
                        }
                        _$[name] = new Proxy(x, {
                            apply: (t, o, args) => {
                                let _res = t.apply(o, args);
                                if (_$_map.has(_res.is))
                                    _res.is = _$_map.get(_res.is);
                                else {
                                    let _is = _res.is;
                                    _res.is = function(...args) {
                                        if (args[0] === ':hidden')
                                            return false;
                                        return _is.apply(this, args);
                                    };
                                    _$_map.set(_is, _res.is);
                                }
                                return _res;
                            }
                        });
                        _$_map.set(x, _$[name]);
                        return true;
                    }
                };
            };
            Object.defineProperty(win, '$', getJQWrap('$'));
            Object.defineProperty(win, 'jQuery', getJQWrap('jQuery'));
            let _dP = Object.defineProperty;
            Object.defineProperty = function(...args) {
                if (args[0] instanceof Window && (args[1] === '$' || args[1] === 'jQuery'))
                    return void 0;
                return _dP.apply(this, args);
            };
        })
    };

    scripts['igra-prestoloff.cx'] = () => scriptLander(() => {
        let nt = new nullTools();
        /*jslint evil: true */ // yes, evil, I know
        let _write = _document.write.bind(_document);
        /*jslint evil: false */
        nt.define(_document, 'write', t => {
            let id = t.match(/jwplayer\("(\w+)"\)/i);
            if (id && id[1])
                return _write(`<div id="${id[1]}"></div>${t}`);
            return _write('');
        });
    });

    scripts['imageban.ru'] = () => { Object.defineProperty(win, 'V7x1J', { get: () => null }); };

    scripts['ivi.ru'] = () => {
        let _xhr_open = win.XMLHttpRequest.prototype.open;
        win.XMLHttpRequest.prototype.open = function(method, url, ...args) {
            if (typeof url === 'string')
                if (url.endsWith('/track'))
                    return;
            return _xhr_open.call(this, method, url, ...args);
        };
        let _responseText = Object.getOwnPropertyDescriptor(XMLHttpRequest.prototype, 'responseText');
        let _responseText_get = _responseText.get;
        _responseText.get = function() {
            if (this.__responseText__)
                return this.__responseText__;
            let res = _responseText_get.apply(this, arguments);
            let o;
            try {
                if (res)
                    o = JSON.parse(res);
            } catch(ignore) {};
            let changed = false;
            if (o && o.result) {
                if (o.result instanceof Array &&
                    'adv_network_logo_url' in o.result[0]) {
                    o.result = [];
                    changed = true;
                }
                if (o.result.show_adv) {
                    o.result.show_adv = false;
                    changed = true;
                }
            }
            if (changed) {
                console.log('changed response >>', o);
                res = JSON.stringify(o);
            }
            this.__responseText__ = res;
            return res;
        };
        Object.defineProperty(XMLHttpRequest.prototype, 'responseText', _responseText);
    };

    scripts['kinopoisk.ru'] = () => {
        selectiveCookies('bltsr|cmtchd|crookie|kpunk');
        // set no-branding body style and adjust other blocks on the page
        createStyle([
            'body:not(#id) { background: #d5d5d5 url(/images/noBrandBg.jpg) 50% 0 no-repeat !important }',
            '#top { height: 150px !important }',
            '#top_superscreen { display: none !important }',
            '#top_3banners { display: block !important }'
        ]);
        // catch branding and other things
        let _KP = void 0;
        Object.defineProperty(win, 'KP', {
            get: () => _KP,
            set: val => {
                if (_KP === val)
                    return true;
                _KP = new Proxy(val, {
                    set: (kp, name, val) => {
                        if (name === 'branding')
                            return true;
                        if (name === 'config')
                            val = new Proxy(val, {
                                set: (cfg, name, val) => {
                                    if (name === 'anContextUrl')
                                        return true;
                                    if (name === 'adfoxEnabled' || name === 'hasBranding')
                                        val = false;
                                    if (name === 'adfoxVideoAdUrls')
                                        val = {flash:{}, html:{}};
                                    cfg[name] = val;
                                    return true;
                                }
                            });
                        kp[name] = val;
                        return true;
                    }
                });
                console.log('KP =', val);
            }
        });
    };

    scripts['kinozal-tv.appspot.com'] = {
        other: ['a-dot-kinozal-tv.appspot.com'],
        now: () => {
            // They check if 'startsWith' in String.prototype
            // and skip parts of ABP detector if it's not there
            delete String.prototype.startsWith;
        }
    };

    scripts['korrespondent.net'] = {
        now: () => scriptLander(() => {
            let nt = new nullTools();
            nt.define(win, 'holder', function(id) {
                let div = _document.getElementById(id);
                if (!div)
                    return;
                if (div.parentNode.classList.contains('col__sidebar')) {
                    div.parentNode.appendChild(div);
                    div.style.height = '300px';
                }
            });
        }, nullTools),
        dom: () => {
            for (let frame of _document.querySelectorAll('.unit-side-informer > iframe'))
                frame.parentNode.style.width = '1px';
        }
    };

    scripts['mail.ru'] = {
        other: ['ok.ru'],
        now: () => scriptLander(() => {
            let nt = new nullTools();
            // Trick to prevent mail.ru from removing 3rd-party styles
            nt.define(Object.prototype, 'restoreVisibility', nt.func(null), false);
            // Disable some of their counters
            nt.define(win, 'rb_counter', nt.func(null, 'rb_counter'));
            if (location.hostname === 'e.mail.ru')
                nt.define(win, 'aRadar', nt.func(null, 'aRadar'));
            else
                nt.define(win, 'createRadar', nt.func(nt.func(null, 'aRadar'), 'createRadar'));

            {
                let missingCheck = {
                    get: (obj, name) => {
                        if (!(name in obj))
                            console.warn(obj, 'missing:', name);
                        return obj[name];
                    }
                };
                let redefiner = {
                    apply: (target, thisArg, args) => {
                        let res = void 0;
                        let skipLog = (name, ret) => (...args) => (console.log(`Skip ${name}(`, ...args, ')'), ret);
                        if (target._name === 'mrg-smokescreen/Welter')
                            res = {
                                isWelter: () => true,
                                wrap: skipLog(`${target._name}.wrap`)
                            };
                        if (target._name === 'mrg-smokescreen/StyleSheets')
                            res = {
                                update: skipLog(`${target._name}.update`),
                                remove: skipLog(`${target._name}.remove`),
                                insert: skipLog(`${target._name}.insert`),
                                setup: skipLog(`${target._name}.setup`)
                            };
                        if (target._name === 'mrg-honeypot/main')
                            res = {
                                check: skipLog(`${target._name}.check`, false)
                            };
                        if (target._name.startsWith('advert/rb/slot'))
                            res = {
                                slot: '0',
                                get: () => null,
                                getHTML: () => null,
                                createBlock: () => null,
                                onRedirect: () => null
                            };
                        if (target._name.startsWith('OK/banners/'))
                            res = {
                                activate: skipLog(`${target._name}.activate`),
                                deactivate: skipLog(`${target._name}.deactivate`)
                            };
                        if (res)
                            res = new Proxy(res, missingCheck);
                        else
                            res = target.apply(thisArg, args);
                        if (target._name === 'advert/RB') {
                            res.getSlots = () => [];
                            res.load._name = target._name + '.load';
                            res.load = new Proxy(res.load, redefiner);
                        }
                        console.log(target._name, '(',...args,') >>', res);
                        return res;
                    }
                };

                let wrapAdFuncs = {
                    apply: (target, thisArg, args) => {
                        let module = args[0];
                        if (typeof module === 'string')
                            if (module.startsWith('mrg-smoke') ||
                                module.startsWith('mrg-context') ||
                                module.startsWith('mrg-honeypot') ||
                                module.startsWith('advert') ||
                                module.startsWith('OK/banner') ||
                                module === 'OK/Smokescreen') {
                                let fun = args[args.length-1];
                                fun._name = module;
                                args[args.length-1] = new Proxy(fun, redefiner);
                            }// else
                        //   console.log('Define:', args[0]);
                        return target.apply(thisArg, args);
                    }
                };
                let wrapDefine = def => {
                    if (!def)
                        return;
                    console.log('define =', def);
                    def = new Proxy(def, wrapAdFuncs);
                    def._name = 'define';
                    return def;
                };
                let _define = wrapDefine(win.define);
                Object.defineProperty(win, 'define', {
                    get: () => _define,
                    set: x => {
                        if (_define === x)
                            return true;
                        _define = wrapDefine(x);
                        return true;
                    }
                });
            }

            // Disable page scrambler on mail.ru to let extensions easily block ads there
            let logger = {
                apply: (target, thisArg, args) => {
                    let res = target.apply(thisArg, args);
                    console.log(`${target._name}(`, ...args, `) >>`, res);
                    return res;
                }
            };

            function defineLocator(root) {
                let _locator;

                function wrapLocator(locator) {
                    if ('setup' in locator) {
                        let _setup = locator.setup;
                        locator.setup = function(o) {
                            if ('enable' in o) {
                                o.enable = false;
                                console.log('Disable mimic mode.');
                            }
                            if ('links' in o) {
                                o.links = [];
                                console.log('Call with empty list of sheets.');
                            }
                            return _setup.call(this, o);
                        };
                        locator.insertSheet = () => false;
                        locator.wrap = () => false;
                    }
                    try {
                        let names = [];
                        for (let name in locator)
                            if (locator[name] instanceof Function && name !== 'transform') {
                                locator[name]._name = "locator." + name;
                                locator[name] = new Proxy(locator[name], logger);
                                names.push(name);
                            }
                        console.log(`[locator] wrapped properties: ${names.length ? names.join(', ') : '[empty]'}`);
                    } catch(e) {
                        console.log(e);
                    }
                    _locator = locator;
                }

                if ('locator' in root && root.locator) {
                    console.log('Found existing "locator" object. :|', root.locator);
                    wrapLocator(root.locator);
                }

                let loc_desc = Object.getOwnPropertyDescriptor(root, 'locator');
                if (!loc_desc || loc_desc.set !== wrapLocator)
                    try {
                        Object.defineProperty(root, 'locator', {
                            set: wrapLocator,
                            get: () => _locator
                        });
                    } catch (err) {
                        console.log('Unable to redefine "locator" object!!!', err);
                    }
            }

            function defineDetector(mr) {
                let _honeyPot;
                let __ = mr._ || {};
                let check = function() {
                    __.STUCK_IN_POT = false;
                    return false;
                };
                check._name = 'honeyPot.check';
                let setHoneyPot = o => {
                    console.log('[honeyPot]', o);
                    o.check = new Proxy(check, logger);
                    _honeyPot = o;
                };
                if ('honeyPot' in mr)
                    setHoneyPot(mr.honeyPot);
                Object.defineProperty(mr, 'honeyPot', {
                    get: () => _honeyPot,
                    set: setHoneyPot
                });

                __ = new Proxy(__, {
                    get: (t, p) => t[p],
                    set: (t, p, v) => {
                        console.log(`mr._.${p} =`, v);
                        t[p] = v;
                        return true;
                    }
                });
                mr._ = __;
            }

            function defineAdd(mr) {
                let _add;
                let addWrapper = {
                    apply: (tgt, that, args) => {
                        let module = args[0];
                        if (module.startsWith('ad')) {
                            console.log('Skip module:', module);
                            return;
                        }
                        return logger.apply(tgt, that, args);
                    }
                };
                let setMrAdd = v => {
                    v._name = 'mr.add';
                    v = new Proxy(v, addWrapper);
                    _add = v;
                };
                if ('add' in mr)
                    setMrAdd(mr.add);
                Object.defineProperty(mr, 'add', {
                    get: () => _add,
                    set: setMrAdd
                });

            }

            try {
                let _mr;
                Object.defineProperty(win, 'mr', {
                    enumerable: true,
                    get: () => _mr,
                    set: (v) => {
                        if (v === _mr)
                            return true;
                        console.log('Trapped new "mr" object.');
                        defineLocator(v.mimic ? v.mimic : v);
                        defineDetector(v);
                        defineAdd(v);
                        _mr = v;
                    }
                });
                if (!('mr' in win))
                    throw 'Wat!?';
            } catch (e) {
                console.log('Found existing "mr" object.', e instanceof TypeError ? '' : e);
                defineLocator(win.mr);
                defineDetector(win.mr);
                defineAdd(win.mr);
            }

            // smokyTools wrapper for news.mail.ru
            nt.define(win, 'smokyTools', nt.proxy({
                Selector: nt.func(null),
                getDict: nt.func(nt.proxy({
                    attrLookup: nt.func(null),
                    classLookup: nt.func(null)
                })),
                CSS: nt.func(nt.proxy({
                    addAllCssToIndex: nt.func(null),
                    addCssToIndex: nt.func(null),
                    smokeClass: nt.func(null)
                }))
            }));
            nt.define(win, 'smoky', nt.func(null));
            nt.define(win, 'smokySingleElement', nt.func(null));
            nt.define(win, 'smokyByClass', nt.func(null));
        }, nullTools)
    };

    scripts['oms.matchat.online'] = () => scriptLander(() => {
        let _rmpGlobals = void 0;
        Object.defineProperty(win, 'rmpGlobals', {
            get: () => _rmpGlobals,
            set: x => {
                if (x === _rmpGlobals)
                    return true;
                _rmpGlobals = new Proxy(x, {
                    get: (obj, name) => {
                        if (name === 'adBlockerDetected')
                            return false;
                        return obj[name];
                    },
                    set: (obj, name, val) => {
                        if (name === 'adBlockerDetected')
                            console.warn('rmpGlobals.adBlockerDetected =', val)
                        else
                            obj[name] = val;
                        return true;
                    }
                });
            }
        });
    });

    scripts['megogo.net'] = {
        now: () => {
            let nt = new nullTools();
            nt.define(win, 'adBlock', false);
            nt.define(win, 'showAdBlockMessage', nt.func(null));
        }
    };

    scripts['naruto-base.su'] = () => gardener('div[id^="entryID"],.block', /href="http.*?target="_blank"/i);

    scripts['newdeaf-online.net'] = {
        dom: () => {
            let adNodes = _document.querySelectorAll('.ads');
            if (!adNodes)
                return;
            let getter = x => {
                let val = x;
                return () => (console.warn('read .ads', name, val), val);
            };
            let setter = x => console.warn('skip write .ads', name, x);
            for (let adNode of adNodes)
                for (let name of ['innerHTML'])
                    Object.defineProperty(adNode, name, {
                        get: getter(ads[name]),
                        set: setter
                    });
        }
    };

    scripts['overclockers.ru'] = {
        dom: () => scriptLander(() => {
            let killed = () => console.warn('Anti-Adblock killed.');
            if ('$' in win)
                win.$ = new Proxy($, {
                    apply: (tgt, that, args) => {
                        let res = tgt.apply(that, args);
                        if (res[0] && res[0] === _document.body) {
                            res.html = killed;
                            res.empty = killed;
                        }
                        return res;
                    }
                });
        })
    };
    scripts['forums.overclockers.ru'] = {
        now: () => {
            createStyle('.needblock {position: fixed; left: -10000px}');
            Object.defineProperty(win, 'adblck', {
                get: () => 'no',
                set: () => undefined,
                enumerable: true
            });
        }
    };

    scripts['pb.wtf'] = {
        other: ['piratbit.org', 'piratbit.ru'],
        dom: () => {
            // line above topic content and images in the slider in the header
            let remove = node => (console.log('removed', node), node.parentNode.removeChild(node));
            for (let el of _document.querySelectorAll('.release-block-img a, #page_content a')) {
                if (location.hostname === el.hostname &&
                    /^\/(\w{3}|exit)\/[\w=/]{20,}$/.test(el.pathname)) {
                    remove(el.closest('div, tr'));
                    continue;
                }
                // ads in the topic header in case filter above wasn't enough
                let parent = el.closest('tr');
                if (parent) {
                    let span = (parent.querySelector('span') || {}).textContent;
                    span && span.startsWith('YO!') && remove(parent);
                }
            }
            // casino ad button in random places
            for (let el of _document.querySelectorAll('.btn-group')) {
                el = el.parentNode;
                if (el.tagName === 'CENTER')
                    remove(el.parentNode);
            }
            // ads in comments
            let el = _document.querySelector('thead + tbody[id^="post_"] + tbody[class*=" "]');
            if (el && el.parentNode.children[2] == el)
                remove(el);
        }
    };

    scripts['pikabu.ru'] = () => gardener('.story', /story__author[^>]+>ads</i, {root: '.inner_wrap', observe: true});

    scripts['peka2.tv'] = () => {
        let bodyClass = 'body--branding';
        let checkNode = node => {
            for (let className of node.classList)
                if (className.includes('banner') || className === bodyClass) {
                    _removeAttribute(node, 'style');
                    node.classList.remove(className);
                    for (let attr of Array.from(node.attributes))
                        if (attr.name.startsWith('advert'))
                            _removeAttribute(node, attr.name);
                }
        };
        (new MutationObserver(ms => {
            let m, node;
            for (m of ms) for (node of m.addedNodes)
                if (node instanceof HTMLElement)
                    checkNode(node);
        })).observe(_de, {childList: true, subtree: true});
        (new MutationObserver(ms => {
            for (let m of ms)
                checkNode(m.target);
        })).observe(_de, {attributes: true, subtree: true, attributeFilter: ['class']});
    };

    scripts['qrz.ru'] = {
        now: () => {
            let nt = new nullTools();
            nt.define(win, 'ab', false);
            nt.define(win, 'tryMessage', nt.func(null));
        }
    };

    scripts['razlozhi.ru'] = {
        now: () => {
            for (let func of ['createShadowRoot', 'attachShadow'])
                if (func in _Element.prototype)
                    _Element.prototype[func] = function(){
                        return this.cloneNode();
                    };
        }
    };

    scripts['rbc.ru'] = {
        other: ['rbcplus.ru', 'sportrbc.ru'],
        now: () => {
            selectiveCookies('adb_on');
            let _RA = void 0;
            let setArgs = {
                'showBanners': true,
                'showAds': true,
                'paywall.user.logined': true,
                'paywall.user.paid': true,
                'banners.staticPath': '',
                'paywall.staticPath': '',
                'banners.dfp.config': [],
                'banners.dfp.pageTargeting': () => null,
            };
            Object.defineProperty(win, 'RA', {
                get: () => _RA,
                set: vl => {
                    console.log('RA =', vl);
                    if ('repo' in vl) {
                        console.log('RA.repo =', vl.repo);
                        vl.repo = new Proxy(vl.repo, {
                            set: (o, name, val) => {
                                if (name === 'banner') {
                                    console.log(`RA.repo.${name} =`, val);
                                    val = new Proxy(val, {
                                        get: (o, name) => {
                                            let res = o[name];
                                            if (typeof o[name] === 'function')
                                                res = () => null;
                                            if (name === 'isInited')
                                                res = true;
                                            console.warn(`get RA.repo.banner.${name}`, res);
                                            return res;
                                        }
                                    });
                                }
                                o[name] = val;
                                return true;
                            }
                        });
                    } else
                        console.log('Unable to locate RA.repo');
                    _RA = new Proxy(vl, {
                        set: (o, name, val) => {
                            if (name === 'config') {
                                console.log('RA.config =', val);
                                if ('set' in val) {
                                    val.set = new Proxy(val.set, {
                                        apply: (set, that, args) => {
                                            let name = args[0];
                                            if (name in setArgs)
                                                args[1] = setArgs[name];
                                            if (name in setArgs || name === 'checkad')
                                                console.log('RA.config.set(', ...args, ')');
                                            return Reflect.apply(set, that, args);
                                        }
                                    });
                                    val.set('showAds', true); // pretend ads already were shown
                                }
                            }
                            o[name] = val;
                            return true;
                        }
                    });
                }
            });
            Object.defineProperty(win, 'bannersConfig', {
                get: () => [], set: () => null
            });
            // pretend there is a paywall landing on screen already
            let pwl = _document.createElement('div');
            pwl.style.display = 'none';
            pwl.className = 'js-paywall-landing';
            _document.documentElement.appendChild(pwl);
            // detect and skip execution of one of the ABP detectors
            let _setTimeout = Function.prototype.apply.bind(win.setTimeout);
            let _toString = Function.prototype.call.bind(Function.prototype.toString);
            win.setTimeout = function setTimeout() {
                if (typeof arguments[0] === 'function') {
                    let fts = _toString(arguments[0]);
                    if (/\.length\s*>\s*0\s*&&/.test(fts) && /:hidden/.test(fts)) {
                        console.log('Skipped setTimout(', fts, arguments[1], ')');
                        return;
                    }
                }
                return _setTimeout(this, arguments);
            };
        }
    };

    scripts['rp5.ru'] = {
        other: ['rp5.by', 'rp5.kz', 'rp5.ua'],
        now: () => {
            Object.defineProperty(win, 'sContentBottom', {
                get: () => '',
                set: () => true
            });
        }
    };

    scripts['rutube.ru'] = () => scriptLander(() => {
        let _parse = JSON.parse;
        let _skip_enabled = false;
        JSON.parse = (...args) => {
            let res = _parse(...args),
                log = false;
            if (!res)
                return res;
            // parse player configuration
            if ('appearance' in res || 'video_balancer' in res) {
                log = true;
                if (res.appearance) {
                    if ('forbid_seek' in res.appearance && res.appearance.forbid_seek)
                        res.appearance.forbid_seek = false;
                    if ('forbid_timeline_preview' in res.appearance && res.appearance.forbid_timeline_preview)
                        res.appearance.forbid_timeline_preview = false;
                }
                _skip_enabled = !!res.remove_unseekable_blocks;
                //res.advert = [];
                delete res.advert;
                //for (let limit of res.limits)
                //    limit.limit = 0;
                delete res.limits;
                //res.yast = null;
                //res.yast_live_online = null;
                delete res.yast;
                delete res.yast_live_online;
                Object.defineProperty(res, 'stat', {
                    get: () => [],
                    set: () => true,
                    enumerable: true
                });
            }

            // parse video configuration
            if ('video_url' in res) {
                log = true;
                if (res.cuepoints && !_skip_enabled)
                    for (let point of res.cuepoints) {
                        point.is_pause = false;
                        point.show_navigation = true;
                        point.forbid_seek = false;
                    }
            }

            if (log)
                console.log('[rutube]', res);
            return res;
        };
    });

    scripts['simpsonsua.com.ua'] = () => scriptLander(() => {
        let _addEventListener = _Document.prototype.addEventListener;
        _document.addEventListener = function(event, callback) {
            if (event === 'DOMContentLoaded' && callback.toString().includes('show_warning'))
                return;
            return _addEventListener.apply(this, arguments);
        };
    });

    scripts['smotret-anime.ru'] = () => scriptLander(() => {
        deepWrapAPI(root => {
            let _pause = root.Function.prototype.call.bind(root.Audio.prototype.pause);
            let _addEventListener = root.Function.prototype.call.bind(root.Element.prototype.addEventListener);
            let stopper = e => _pause(e.target);
            root.Audio = new Proxy(root.Audio, {
                construct: (audio, args) => {
                    let res = new audio(...args);
                    _addEventListener(res, 'play', stopper, true);
                    return res;
                }
            });
            _createElement = root.Document.prototype.createElement;
            root.Document.prototype.createElement = function createElement() {
                let res = _createElement.apply(this, arguments);
                if (res instanceof HTMLAudioElement)
                    _addEventListener(res, 'play', stopper, true);
                return res;
            };
        });
    }, deepWrapAPI);

    scripts['spaces.ru'] = () => {
        gardener('div:not(.f-c_fll) > a[href*="spaces.ru/?Cl="]', /./, { parent: 'div' });
        gardener('.js-banner_rotator', /./, { parent: '.widgets-group' });
    };

    scripts['spam-club.blogspot.co.uk'] = () => {
        let _clientHeight = Object.getOwnPropertyDescriptor(_Element.prototype, 'clientHeight'),
            _clientWidth = Object.getOwnPropertyDescriptor(_Element.prototype, 'clientWidth');
        let wrapGetter = (getter) => {
            let _getter = getter;
            return function() {
                let _size = _getter.apply(this, arguments);
                return _size ? _size : 1;
            };
        };
        _clientHeight.get = wrapGetter(_clientHeight.get);
        _clientWidth.get = wrapGetter(_clientWidth.get);
        Object.defineProperty(_Element.prototype, 'clientHeight', _clientHeight);
        Object.defineProperty(_Element.prototype, 'clientWidth', _clientWidth);
        let _onload = Object.getOwnPropertyDescriptor(HTMLElement.prototype, 'onload'),
            _set_onload = _onload.set;
        _onload.set = function() {
            if (this instanceof HTMLImageElement)
                return true;
            _set_onload.apply(this, arguments);
        };
        Object.defineProperty(HTMLElement.prototype, 'onload', _onload);
    };

    scripts['sport-express.ru'] = () => gardener('.js-relap__item',/>Реклама\s+<\//, {root:'.container', observe: true});

    scripts['sports.ru'] = {
        now: () => {
            gardener('.aside-news-list__item', /aside-news-list__advert/i, {root:'.columns-layout__left', observe: true});
            gardener('.material-list__item', /Реклама/i, {root:'.columns-layout', observe: true});
            // extra functionality: shows/hides panel at the top depending on scroll direction
            createStyle([
                '.user-panel__fixed { transition: top 0.2s ease-in-out!important; }',
                '.user-panel-up { top: -40px!important }'
            ], {id: 'userPanelSlide'}, false);
        },
        dom: () => {
            (function lookForPanel() {
                let panel = _document.querySelector('.user-panel__fixed');
                if (!panel)
                    setTimeout(lookForPanel, 100);
                else
                    window.addEventListener(
                        'wheel', function(e) {
                            if (e.deltaY > 0 && !panel.classList.contains('user-panel-up'))
                                panel.classList.add('user-panel-up');
                            else if (e.deltaY < 0 && panel.classList.contains('user-panel-up'))
                                panel.classList.remove('user-panel-up');
                        }, false
                    );
            })();
        }
    };

    scripts['stealthz.ru'] = {
        dom: () => {
            // skip timeout
            let $ = _document.querySelector.bind(_document);
            let [timer_1, timer_2] = [$('#timer_1'), $('#timer_2')];
            if (!timer_1 || !timer_2)
                return;
            timer_1.style.display = 'none';
            timer_2.style.display = 'block';
        }
    };

    scripts['xittv.net'] = () => scriptLander(() => {
        let logNames = ['setup', 'trigger', 'on', 'off', 'onReady', 'onError', 'getConfig', 'addPlugin', 'getAdBlock'];
        let skipEvents = ['adComplete', 'adSkipped', 'adBlock', 'adRequest', 'adMeta', 'adImpression', 'adError', 'adTime', 'adStarted', 'adClick'];
        let _jwplayer = void 0;
        Object.defineProperty(win, 'jwplayer', {
            get: () => _jwplayer,
            set: x => {
                _jwplayer = new Proxy(x, {
                    apply: (fun, that, args) => {
                        let res = fun.apply(that, args);
                        res = new Proxy(res, {
                            get: (obj, name) => {
                                if (logNames.includes(name) && obj[name] instanceof Function)
                                    return new Proxy(obj[name], {
                                        apply: (fun, that, args) => {
                                            if (name === 'setup') {
                                                let o = args[0];
                                                if (o)
                                                    delete o.advertising;
                                            }
                                            if (name === 'on' || name === 'trigger') {
                                                let events = typeof args[0] === 'string' ? args[0].split(" ") : null;
                                                if (events.length === 1 && skipEvents.includes(events[0]))
                                                    return res;
                                                if (events.length > 1) {
                                                    let names = [];
                                                    for (let event of events)
                                                        if (!skipEvents.includes(event))
                                                            names.push(event);
                                                    if (names.length > 0)
                                                        args[0] = names.join(" ");
                                                    else
                                                        return res;
                                                }
                                            }
                                            let subres = fun.apply(that, args);
                                            console.warn(`jwplayer().${name}(`, ...args, `) >>`, res);
                                            return subres;
                                        }
                                    });
                                return obj[name];
                            }
                        });
                        return res;
                    }
                });
                console.log('jwplayer =', x);
            }
        });
    });

    scripts['yap.ru'] = {
        other: ['yaplakal.com'],
        now: () => {
            gardener('form > table[id^="p_row_"]:nth-of-type(2)', /member1438|Administration/);
            gardener('.icon-comments', /member1438|Administration|\/go\/\?http/, {parent:'tr', siblings:-2});
        }
    };

    scripts['rambler.ru'] = {
        other: ['championat.com', 'gazeta.ru', 'lenta.ru', 'media.eagleplatform.com', 'quto.ru', 'rns.online'],
        now: () => scriptLander(() => {
            // Prevent autoplay
            if (!('EaglePlayer' in win)) {
                let _EaglePlayer = void 0;
                Object.defineProperty(win, 'EaglePlayer', {
                    enumerable: true,
                    get: () => _EaglePlayer,
                    set: x => {
                        if (x === _EaglePlayer)
                            return true;
                        _EaglePlayer = new Proxy(x, {
                            construct: (targ, args) => {
                                let player = new targ(...args);
                                if (!player.options) {
                                    console.log('EaglePlayer: no options', EaglePlayer);
                                    return player;
                                }
                                Object.defineProperty(player.options, 'autoplay', {
                                    get: () => false,
                                    set: () => true
                                });
                                Object.defineProperty(player.options, 'scroll', {
                                    get: () => false,
                                    set: () => true
                                });
                                return player;
                            }
                        });
                    }
                });
                let _setAttribute = Function.prototype.apply.bind(_Element.prototype.setAttribute);
                let isAutoplay = /^autoplay$/i;
                _Element.prototype.setAttribute = function setAttribute(name) {
                    if (!this._stopped && isAutoplay.test(name)) {
                        console.log('Prevented assigning autoplay attribute.');
                        return null;
                    }
                    return _setAttribute(this, arguments);
                };
            } else {
                console.log('EaglePlayer function already exists.');
                if (inIFrame) {
                    let _setAttribute = Function.prototype.apply.bind(_Element.prototype.setAttribute);
                    let isAutoplay = /^autoplay$/i;
                    _Element.prototype.setAttribute = function setAttribute(name) {
                        if (!this._stopped && isAutoplay.test(name)) {
                            console.log('Prevented assigning autoplay attribute.');
                            this._stopped = true;
                            this.play = () => {
                                console.log('Prevented attempt to force-start playback.');
                                delete this.play;
                            };
                            return null;
                        }
                        return _setAttribute(this, arguments);
                    };
                }
            }
            if (location.hostname.endsWith('.media.eagleplatform.com'))
                return;
            // prevent ads from loading
            let blockObfuscated = false;
            let obfuscation = /\[[a-z]{4}\("0x\d+"\)\]/i;
            let _toString = Function.prototype.call.bind(Function.prototype.toString);
            let CSSRuleProto = 'cssText' in CSSRule.prototype ? CSSRule.prototype : CSSStyleRule.prototype;
            let _cssText = Object.getOwnPropertyDescriptor(CSSRuleProto, 'cssText');
            let _cssText_get = _cssText.get;
            _cssText.configurable = false;
            _cssText.get = function() {
                let cssText = _cssText_get.call(this);
                if (cssText.includes('content:')) {
                    console.warn('Blocked access to suspicious cssText:', cssText.slice(0,60), '\u2026', cssText.length);
                    blockObfuscated = true;
                    return null;
                }
                return cssText;
            };
            Object.defineProperty(CSSRuleProto, 'cssText', _cssText);
            let _setTimeout = win.setTimeout;
            win.setTimeout = function(f) {
                if (blockObfuscated && obfuscation.test(_toString(f))) {
                    console.warn('Stopped setTimeout for:', _toString(f).slice(0,100), '\u2026');
                    return null;
                };
                return _setTimeout.apply(this, arguments);
            };
            // fake global Adf object
            let nt = new nullTools();
            let Adf_banner = {};
            [
                'reloadssp', 'sspScroll',
                'sspRich', 'ssp'
            ].forEach(name => void(Adf_banner[name] = nt.proxy(() => new Promise(r => r({status: true})))));
            nt.define(win, 'Adf', nt.proxy({
                banner: nt.proxy(Adf_banner)
            }));
            // extra script to remove partner news on gazeta.ru
            if (!location.hostname.includes('gazeta.ru'))
                return;
            (new MutationObserver(
                (ms) => {
                    let m, node, header;
                    for (m of ms) for (node of m.addedNodes)
                        if (node instanceof HTMLDivElement && node.matches('.sausage')) {
                            header = node.querySelector('.sausage-header');
                            if (header && /новости\s+партн[её]ров/i.test(header.textContent))
                                node.style.display = 'none';
                        }
                }
            )).observe(_document.documentElement, { childList:true, subtree: true });
        }, `let inIFrame = ${inIFrame}`, nullTools)
    };

    scripts['reactor.cc'] = {
        other: ['joyreactor.cc', 'pornreactor.cc'],
        now: () => {
            selectiveEval();
            scriptLander(() => {
                let nt = new nullTools();
                win.open = function(){
                    throw new Error('Redirect prevention.');
                };
                nt.define(win, 'Worker', function(){});
                nt.define(win, 'JRCH', win.CoinHive);
            }, nullTools);
        },
        click: function(e) {
            let node = e.target;
            if (node.nodeType === _Node.ELEMENT_NODE &&
                node.style.position === 'absolute' &&
                node.style.zIndex > 0)
                node.parentNode.removeChild(node);
        },
        dom: function() {
            let words = new RegExp(
                'блокировщик рекламы'
                .split('')
                .map(function(e){
                    return e+'[\u200b\u200c\u200d]*';
                })
                .join('')
                .replace(' ', '\\s*')
                .replace(/[аоре]/g, function(e){
                    return ['[аa]','[оo]','[рp]','[еe]']['аоре'.indexOf(e)];
                }),
                'i'),
                can;
            function deeper(spider) {
                for (let child of spider.childNodes)
                    if (words.test(child.innerText))
                        if (child.offsetHeight >= 750)
                            deeper(child);
                        else
                            can.push(child);
            }
            function probe() {
                can = [];
                deeper(_document.body);
                for (let spider of can)
                    _setAttribute(spider, 'style', 'background:none!important');
            }
            (new MutationObserver(probe))
                .observe(_document, { childList:true, subtree:true });
        }
    };

    scripts['auto.ru'] = () => {
        let words = /Реклама|Яндекс.Директ|yandex_ad_/;
        let userAdsListAds = (
            '.listing-list > .listing-item,'+
            '.listing-item_type_fixed.listing-item'
        );
        let catalogAds = (
            'div[class*="layout_catalog-inline"],'+
            'div[class$="layout_horizontal"]'
        );
        let otherAds = (
            '.advt_auto,'+
            '.sidebar-block,'+
            '.pager-listing + div[class],'+
            '.card > div[class][style],'+
            '.sidebar > div[class],'+
            '.main-page__section + div[class],'+
            '.listing > tbody'
        );
        gardener(userAdsListAds, words, {root:'.listing-wrap', observe:true});
        gardener(catalogAds, words, {root:'.catalog__page,.content__wrapper', observe:true});
        gardener(otherAds, words);
    };

    scripts['rsload.net'] = {
        load: () => {
            let dis = _document.querySelector('label[class*="cb-disable"]');
            if (dis)
                dis.click();
        },
        click: e => {
            let t = e.target;
            if (t && t.href && (/:\/\/\d+\.\d+\.\d+\.\d+\//.test(t.href)))
                t.href = t.href.replace('://','://rsload.net:rsload.net@');
        }
    };

    let domain;
    // add alternative domain names if present and wrap functions into objects
    for (let name in scripts) {
        if (scripts[name] instanceof Function)
            scripts[name] = { now: scripts[name] };
        for (domain of (scripts[name].other||[])) {
            if (domain in scripts)
                console.log('Error in scripts list. Script for', name, 'replaced script for', domain);
            scripts[domain] = scripts[name];
        }
        delete scripts[name].other;
    }
    // look for current domain in the list and run appropriate code
    domain = _document.domain;
    while (domain.includes('.')) {
        if (domain in scripts) for (let when in scripts[domain])
            switch(when) {
                case 'now':
                    scripts[domain][when]();
                    break;
                case 'dom':
                    _document.addEventListener('DOMContentLoaded', scripts[domain][when], false);
                    break;
                default:
                    _document.addEventListener (when, scripts[domain][when], false);
            }
        domain = domain.slice(domain.indexOf('.') + 1);
    }

    // Batch script lander
    if (!skipLander)
        landScript(batchLand, batchPrepend);

    { // JS Fixes Tools Menu
        let openOptions = function() {
            let ovl = _createElement('div'),
                inner = _createElement('div');
            ovl.style = (
                'position: fixed;'+
                'top:0; left:0;'+
                'bottom: 0; right: 0;'+
                'background: rgba(0,0,0,0.85);'+
                'z-index: 2147483647;'+
                'padding: 5em'
            );
            inner.style = (
                'background: whitesmoke;'+
                'font-size: 10pt;'+
                'color: black;'+
                'padding: 1em'
            );
            inner.textContent = 'JS Fixes Tools';
            inner.appendChild(_createElement('br'));
            inner.appendChild(_createElement('br'));
            ovl.addEventListener(
                'click', function(e) {
                    if (e.target === ovl) {
                        ovl.parentNode.removeChild(ovl);
                        e.preventDefault();
                    }
                    e.stopPropagation();
                }, false
            );

            let sObjBtn = _createElement('button');
            sObjBtn.onclick = getStrangeObjectsList;
            sObjBtn.textContent = 'Print (in console) list of unusual window properties';
            inner.appendChild(_createElement('br'));
            inner.appendChild(sObjBtn);

            _document.body.appendChild(ovl);
            ovl.appendChild(inner);
        };

        // monitor keys pressed for Ctrl+Alt+Shift+J > s > f code
        let opPos = 0, opKey = ['KeyJ','KeyS','KeyF'];
        _document.addEventListener(
            'keydown', function(e) {
                if ((e.code === opKey[opPos] || e.location) &&
                    (!!opPos || e.altKey && e.ctrlKey && e.shiftKey)) {
                    opPos += e.location ? 0 : 1;
                    e.stopPropagation();
                    e.preventDefault();
                } else
                    opPos = 0;
                if (opPos === opKey.length) {
                    opPos = 0;
                    openOptions();
                }
            }, false
        );
    }
})();