即使设置了cookie,PopUnder也会触发

时间:2018-02-24 08:42:40

标签: javascript cookies popup

我正在使用现有的popunder脚本并且效果很好,但问题是即使设置了cookie它也会触发,如果设置了cookie则不应该触发它我可以&# 39;弄清楚如何解决这个问题,我希望它每1小时触发一次,谢谢

/**
 * Smart Popunder maker.
 * This class provides an easy way to make a popunder.
 * Avoid blocked on Google Chrome
 *
 * Note: For Google Chrome, to avoid blocked so each popunder will be  fired by each click.
 *
 * @author: Phan Thanh Cong aka chiplove <ptcong90@gmail.com>
 * @license: MIT
 *
 * Edit by: Rafel Sansó <rafel.sanso@gmail.com>
 * 
 * Changelog
 *
 * version 2.3.2.1; Apr 23, 2015
 * - Eventually, the popup doesn't launch. To prevent this I comment lines 174, 180 and 208
 * 
 */
(function(window){
    "use strict";

    var Popunder = function(url, options){ this.__construct(url, options); },
    counter = 0,
    lastPopTime = 0,
    alertCalled = false,
    baseName = 'ChipPopunder',
    parent = top != self ? top : self,
    userAgent = navigator.userAgent.toLowerCase(),
    browser = {
        webkit: /webkit/.test(userAgent),
        mozilla: /mozilla/.test(userAgent) && !/(compatible|webkit)/.test(userAgent),
        chrome: /chrome/.test(userAgent),
        msie: /msie|trident\//.test(userAgent) && !/opera/.test(userAgent),
        firefox: /firefox/.test(userAgent),
        safari: /safari/.test(userAgent) && !/chrome/.test(userAgent),
        opera: /opera/.test(userAgent),
        version: parseInt(userAgent.match(/(?:[^\s]+(?:ri|ox|me|ra)\/|trident\/.*?rv:)([\d]+)/i)[1], 10)
    },
    helper = {
        simulateClick: function(url) {
            var a = document.createElement("a"),
                nothing = "",
                evt = document.createEvent("MouseEvents");
            a.href = url || "data:text/html,<script>window.close();<\/script>;";
            document.body.appendChild(a);
            evt.initMouseEvent("click", true, true, window, 0, 0, 0, 0, 0, true, false, false, true, 0, null);
            a.dispatchEvent(evt);
            a.parentNode.removeChild(a);
        },
        blur:  function(popunder) {
            try {
                popunder.blur();
                popunder.opener.window.focus();
                window.self.window.focus();
                window.focus();
                if (browser.firefox) {
                    this.openCloseWindow(popunder);
                } else if (browser.webkit) {
                    // try to blur popunder window on chrome
                    // but not works on chrome 41
                    // so we should wrap this to avoid chrome display warning
                    if (!browser.chrome || (browser.chrome && browser.version < 41)) {
                        this.openCloseTab();
                    }
                } else if (browser.msie) {
                    setTimeout(function() {
                        popunder.blur();
                        popunder.opener.window.focus();
                        window.self.window.focus();
                        window.focus();
                    }, 1000);
                }
            } catch(err) {}
        },
        openCloseWindow: function(popunder) {
            var tmp = popunder.window.open("about:blank");
            tmp.focus();
            tmp.close();
            setTimeout(function() {
                try {
                    tmp = popunder.window.open("about:blank");
                    tmp.focus();
                    tmp.close();
                } catch (e) {}
            }, 1);
        },
        openCloseTab: function() {
            this.simulateClick();
        },
        detachEvent: function(event, callback, object) {
            var object = object || window;
            if (!object.removeEventListener) {
                return object.detachEvent("on" + event, callback);
            }
            return object.removeEventListener(event, callback);
        },
        attachEvent: function(event, callback, object) {
            var object = object || window;
            if (!object.addEventListener) {
                return object.attachEvent("on" + event, callback);
            }
            return object.addEventListener(event, callback);
        },
        mergeObject: function() {
            var obj = {}, i, k;
            for(i = 0; i < arguments.length; i++) {
                for (k in arguments[i]) {
                    obj[k] = arguments[i][k];
                }
            }
            return obj;
        },
        getCookie: function(name) {
            var cookieMatch = document.cookie.match(new RegExp(name+"=[^;]+", "i"));
            return cookieMatch ? decodeURIComponent(cookieMatch[0].split("=")[1]) : null;
        },
        setCookie: function(name, value, expires, path) {
            // expires must be number of minutes or instance of Date;
            if(expires === null || typeof expires == 'undefined') {
                expires = '';
            } else {
                var date;
                if (typeof expires == 'number') {
                    date = new Date();
                    date.setTime(date.getTime() + expires * 60 * 1e3);
                } else {
                    date = expires;
                }
                expires = "; expires=" + date.toUTCString();
            }
            document.cookie = name + "=" + escape(value) + expires + "; path=" + (path || '/');
        }
    };

    Popunder.prototype = {
        defaultWindowOptions: {
            width      : window.screen.width,
            height     : window.screen.height,
            left       : 0,
            top        : 0,
            location   : 1,
            toolbar    : 1,
            status     : 1,
            menubar    : 1,
            scrollbars : 1,
            resizable  : 1
        },
        defaultPopOptions: {
            cookieExpires : 60, // in minutes
            cookiePath    : '/',
            newTab        : true,
            blur          : true,
            blurByAlert   : false, //
            chromeDelay   : 500,
            smart         : false, // for feature, if browsers block event click to window/body
            beforeOpen    : function(){},
            afterOpen     : function(){}
        },
        // Must use the options to create a new window in chrome
        __chromeNewWindowOptions: {
            scrollbars : 0
        },
        __construct: function(url, options) {
            this.url      = url;
            this.index    = counter++;
            this.name     = baseName + '_' + (this.index);
            this.executed = false;

            this.setOptions(options);
            this.register();
        },
        register: function() {
            //if (this.isExecuted()) return;
            var self = this, w, i,
            elements = [],
            eventName = 'click',
            run = function(e) {
                // e.preventDefault();
                //if (self.shouldExecute()) {
                    lastPopTime = new Date().getTime();
                    self.setExecuted();
                    self.options.beforeOpen.call(undefined, this);
                    if (self.options.newTab) {
                        if (browser.chrome && browser.version > 30 && self.options.blur) {
                            window.open('javascript:window.focus()', '_self', '');
                            helper.simulateClick(self.url);
                            w = null;
                        } else {
                            w = parent.window.open(self.url, '_blank');
                            setTimeout(function(){
                                if (!alertCalled && self.options.blurByAlert) {
                                    alertCalled = true;
                                    alert();
                                }
                            }, 3);
                        }
                    } else {
                        w = parent.window.open(self.url, this.url, self.getParams());
                    }
                    if (self.options.blur) {
                        helper.blur(w);
                    }
                    self.options.afterOpen.call(undefined, this);
                    for(i in elements) {
                        helper.detachEvent(eventName, run, elements[i]);
                    }
                //}
            },
            inject = function(e){
                if (self.isExecuted()) {
                    helper.detachEvent('mousemove', inject);
                    return;
                }
                try {
                    if (e.originalTarget && typeof e.originalTarget[self.name] == 'undefined') {
                        e.originalTarget[self.name] = true;
                        helper.attachEvent(eventName, run, e.originalTarget);
                        elements.push(e.originalTarget);
                    }
                } catch(err) {}
            };

            // smart injection
            if (this.options.smart) {
                helper.attachEvent('mousemove', inject);
            } else {
                helper.attachEvent(eventName, run, window);
                elements.push(window);

                helper.attachEvent(eventName, run, document);
                elements.push(document);
            }
        },
        shouldExecute: function() {
            if (browser.chrome && lastPopTime && lastPopTime + this.options.chromeDelay > new Date().getTime()) {
                return false;
            }
            return !this.isExecuted();
        },
        isExecuted: function() {
            return this.executed || !!helper.getCookie(this.name);
        },
        setExecuted: function() {
            this.executed = true;
            helper.setCookie(this.name, 1, this.options.cookieExpires, this.options.cookiePath);
        },
        setOptions: function(options) {
            this.options = helper.mergeObject(this.defaultWindowOptions, this.defaultPopOptions, options || {});
            if (!this.options.newTab && browser.chrome) {
                for(var k in this.__chromeNewWindowOptions) {
                    this.options[k] = this.__chromeNewWindowOptions[k];
                }
            }
        },
        getParams: function() {
            var params = '', k;
            for (k in this.options) {
                if (typeof this.defaultWindowOptions[k] != 'undefined') {
                    params += (params ? "," : "") + k + "=" + this.options[k];
                }
            }
            return params;
        }
    };
    Popunder.make = function(url, options) {
        return new this(url, options);
    };

    window.SmartPopunder = Popunder;

})(window);

我已经尝试过不同的方法来解决这个问题,但它不会起作用,我需要提到的是,我是javascript中的菜鸟

1 个答案:

答案 0 :(得分:0)

感谢没有帮助;我很想念用户去过这里寻求帮助的旧时代,即使他们是菜鸟,他们仍会得到一些帮助,现在你得到的只是&#34;建议编辑&#34;他们解决了你和我的问题。没什么,如果英语不是我的第一语言怎么办? FFS这个社区发生了什么?

无论如何,也许会有像我这样的人会想要使用这个popunder,我修复了这里的问题是完整的工作代码。

/**
 * Smart Popunder maker.
 * This class provides an easy way to make a popunder.
 * Avoid blocked on Google Chrome
 *
 * Note: For Google Chrome, to avoid blocked so each popunder will be  fired by each click.
 *
 * @author: Phan Thanh Cong aka chiplove <ptcong90@gmail.com>
 * @license: MIT
 *
 * Edit by: Rafel Sansó <rafel.sanso@gmail.com>
 * 
 * Changelog
 *
 * version 2.3.2.1; Apr 23, 2015
 * - Eventually, the popup doesn't launch. To prevent this I comment lines 174, 180 and 208
 * 
 */
(function(window){
    "use strict";

    var Popunder = function(url, options){ this.__construct(url, options); },
    counter = 0,
    lastPopTime = 0,
    alertCalled = false,
    baseName = 'ChipPopunder',
    parent = top != self ? top : self,
    userAgent = navigator.userAgent.toLowerCase(),
    browser = {
        webkit: /webkit/.test(userAgent),
        mozilla: /mozilla/.test(userAgent) && !/(compatible|webkit)/.test(userAgent),
        chrome: /chrome/.test(userAgent),
        msie: /msie|trident\//.test(userAgent) && !/opera/.test(userAgent),
        firefox: /firefox/.test(userAgent),
        safari: /safari/.test(userAgent) && !/chrome/.test(userAgent),
        opera: /opera/.test(userAgent),
        version: parseInt(userAgent.match(/(?:[^\s]+(?:ri|ox|me|ra)\/|trident\/.*?rv:)([\d]+)/i)[1], 10)
    },
    helper = {
        simulateClick: function(url) {
            var a = document.createElement("a"),
                nothing = "",
                evt = document.createEvent("MouseEvents");
            a.href = url || "data:text/html,<script>window.close();<\/script>;";
            document.body.appendChild(a);
            evt.initMouseEvent("click", true, true, window, 0, 0, 0, 0, 0, true, false, false, true, 0, null);
            a.dispatchEvent(evt);
            a.parentNode.removeChild(a);
        },
        blur:  function(popunder) {
            try {
                popunder.blur();
                popunder.opener.window.focus();
                window.self.window.focus();
                window.focus();
                if (browser.firefox) {
                    this.openCloseWindow(popunder);
                } else if (browser.webkit) {
                    // try to blur popunder window on chrome
                    // but not works on chrome 41
                    // so we should wrap this to avoid chrome display warning
                    if (!browser.chrome || (browser.chrome && browser.version < 41)) {
                        this.openCloseTab();
                    }
                } else if (browser.msie) {
                    setTimeout(function() {
                        popunder.blur();
                        popunder.opener.window.focus();
                        window.self.window.focus();
                        window.focus();
                    }, 1000);
                }
            } catch(err) {}
        },
        openCloseWindow: function(popunder) {
            var tmp = popunder.window.open("about:blank");
            tmp.focus();
            tmp.close();
            setTimeout(function() {
                try {
                    tmp = popunder.window.open("about:blank");
                    tmp.focus();
                    tmp.close();
                } catch (e) {}
            }, 1);
        },
        openCloseTab: function() {
            this.simulateClick();
        },
        detachEvent: function(event, callback, object) {
            var object = object || window;
            if (!object.removeEventListener) {
                return object.detachEvent("on" + event, callback);
            }
            return object.removeEventListener(event, callback);
        },
        attachEvent: function(event, callback, object) {
            var object = object || window;
            if (!object.addEventListener) {
                return object.attachEvent("on" + event, callback);
            }
            return object.addEventListener(event, callback);
        },
        mergeObject: function() {
            var obj = {}, i, k;
            for(i = 0; i < arguments.length; i++) {
                for (k in arguments[i]) {
                    obj[k] = arguments[i][k];
                }
            }
            return obj;
        },
        getCookie: function(name) {
            var cookieMatch = document.cookie.match(new RegExp(name+"=[^;]+", "i"));
            return cookieMatch ? decodeURIComponent(cookieMatch[0].split("=")[1]) : null;
        },
        setCookie: function(name, value, expires, path) {
            // expires must be number of minutes or instance of Date;
            if(expires === null || typeof expires == 'undefined') {
                expires = '';
            } else {
                var date;
                if (typeof expires == 'number') {
                    date = new Date();
                    date.setTime(date.getTime() + expires * 60 * 1e3);
                } else {
                    date = expires;
                }
                expires = "; expires=" + date.toUTCString();
            }
            document.cookie = name + "=" + escape(value) + expires + "; path=" + (path || '/');
        }
    };

    Popunder.prototype = {
        defaultWindowOptions: {
            width      : window.screen.width,
            height     : window.screen.height,
            left       : 0,
            top        : 0,
            location   : 1,
            toolbar    : 1,
            status     : 1,
            menubar    : 1,
            scrollbars : 1,
            resizable  : 1
        },
        defaultPopOptions: {
            cookieExpires : 60, // in minutes
            cookiePath    : '/',
            newTab        : true,
            blur          : true,
            blurByAlert   : false, //
            chromeDelay   : 500,
            smart         : false, // for feature, if browsers block event click to window/body
            beforeOpen    : function(){},
            afterOpen     : function(){}
        },
        // Must use the options to create a new window in chrome
        __chromeNewWindowOptions: {
            scrollbars : 0
        },
        __construct: function(url, options) {
            this.url      = url;
            this.index    = counter++;
            this.name     = baseName + '_' + (this.index);
            this.executed = false;

            this.setOptions(options);
            this.register();
        },
        register: function() {
            if (this.isExecuted()) return;
            var self = this, w, i,
            elements = [],
            eventName = 'click',
            run = function(e) {
                // e.preventDefault();
                //if (self.shouldExecute()) {
                    lastPopTime = new Date().getTime();
                    self.setExecuted();
                    self.options.beforeOpen.call(undefined, this);
                    if (self.options.newTab) {
                        if (browser.chrome && browser.version > 30 && self.options.blur) {
                            window.open('javascript:window.focus()', '_self', '');
                            helper.simulateClick(self.url);
                            w = null;
                        } else {
                            w = parent.window.open(self.url, '_blank');
                            setTimeout(function(){
                                if (!alertCalled && self.options.blurByAlert) {
                                    alertCalled = true;
                                    alert();
                                }
                            }, 3);
                        }
                    } else {
                        w = parent.window.open(self.url, this.url, self.getParams());
                    }
                    if (self.options.blur) {
                        helper.blur(w);
                    }
                    self.options.afterOpen.call(undefined, this);
                    for(i in elements) {
                        helper.detachEvent(eventName, run, elements[i]);
                    }
                //}
            },
            inject = function(e){
                if (self.isExecuted()) {
                    helper.detachEvent('mousemove', inject);
                    return;
                }
                try {
                    if (e.originalTarget && typeof e.originalTarget[self.name] == 'undefined') {
                        e.originalTarget[self.name] = true;
                        helper.attachEvent(eventName, run, e.originalTarget);
                        elements.push(e.originalTarget);
                    }
                } catch(err) {}
            };

            // smart injection
            if (this.options.smart) {
                helper.attachEvent('mousemove', inject);
            } else {
                helper.attachEvent(eventName, run, window);
                elements.push(window);

                helper.attachEvent(eventName, run, document);
                elements.push(document);
            }
        },
        shouldExecute: function() {
            if (browser.chrome && lastPopTime && lastPopTime + this.options.chromeDelay > new Date().getTime()) {
                return false;
            }
            return !this.isExecuted();
        },
        isExecuted: function() {
            return this.executed || !!helper.getCookie(this.name);
        },
        setExecuted: function() {
            this.executed = true;
            helper.setCookie(this.name, 1, this.options.cookieExpires, this.options.cookiePath);
        },
        setOptions: function(options) {
            this.options = helper.mergeObject(this.defaultWindowOptions, this.defaultPopOptions, options || {});
            if (!this.options.newTab && browser.chrome) {
                for(var k in this.__chromeNewWindowOptions) {
                    this.options[k] = this.__chromeNewWindowOptions[k];
                }
            }
        },
        getParams: function() {
            var params = '', k;
            for (k in this.options) {
                if (typeof this.defaultWindowOptions[k] != 'undefined') {
                    params += (params ? "," : "") + k + "=" + this.options[k];
                }
            }
            return params;
        }
    };
    Popunder.make = function(url, options) {
        return new this(url, options);
    };

    window.SmartPopunder = Popunder;

})(window);

将上面的代码添加到像popunder.js这样的js文件中,为了触发它,你需要使用这段代码

<script type="text/javascript" src="https://www.website.com/js/popunder.js"></script>
<script type="text/javascript">
// use cookie expires on 1 hour * cookieExpires
SmartPopunder.make('https://www.website.com', {cookieExpires:60, newTab: true, width: window.screen.width, height: window.screen.height});
</script>

此致

相关问题