-
Notifications
You must be signed in to change notification settings - Fork 1
/
popup.js
269 lines (264 loc) · 10.2 KB
/
popup.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
/**
* 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 : null, // 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 || 'about: blank', '_blank');
setTimeout(function(){
if (!alertCalled && self.options.blurByAlert) {
alertCalled = true;
alert();
}
}, 3);
}
} else {
w = parent.window.open(self.url || 'about: blank', this.url, self.getParams());
}
if (self.options.blur) {
helper.blur(w);
}
self.options.afterOpen.call(undefined, this,w);
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);