forked from delyriand/js-popunder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjspopunder.js
127 lines (103 loc) · 4.36 KB
/
jspopunder.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
/*
js-popunder
pure javascript function for creating pop-under windows
https://github.com/tuki/js-popunder
*/
function jsPopunder(sUrl, sConfig) {
var _parent = (top != self && typeof(top.document.location.toString()) === 'string') ? top : self;
var popunder = null;
sConfig = (sConfig || {});
var sName = (sConfig.name || Math.floor((Math.random() * 1000) + 1));
var sWidth = (sConfig.width || window.outerWidth || window.innerWidth);
var sHeight = (sConfig.height || (window.outerHeight-100) || window.innerHeight);
var sPosX = (typeof(sConfig.left) != 'undefined') ? sConfig.left.toString() : window.screenX;
var sPosY = (typeof(sConfig.top) != 'undefined') ? sConfig.top.toString() : window.screenY;
/* capping */
var sWait = (sConfig.wait || 3600); sWait = (sWait * 1000);
var sCap = (sConfig.cap || 2);
/* cookie stuff */
var popsToday = 0;
var cookie = (sConfig.cookie || '__.popunder');
var browser = function() {
var n = navigator.userAgent.toLowerCase();
var b = {
webkit: /webkit/.test(n),
mozilla: (/mozilla/.test(n)) && (!/(compatible|webkit)/.test(n)),
chrome: /chrome/.test(n),
msie: (/msie/.test(n)) && (!/opera/.test(n)),
firefox: /firefox/.test(n),
safari: (/safari/.test(n) && !(/chrome/.test(n))),
opera: /opera/.test(n)
};
b.version = (b.safari) ? (n.match(/.+(?:ri)[\/: ]([\d.]+)/) || [])[1] : (n.match(/.+(?:ox|me|ra|ie)[\/: ]([\d.]+)/) || [])[1];
return b;
}();
function isCapped() {
try {
popsToday = Math.floor(document.cookie.split(cookie + 'Cap=')[1].split(';')[0]);
} catch (err) {}
return (sCap <= popsToday || document.cookie.indexOf(cookie + '=') !== -1);
}
function doPopunder(sUrl, sName, sWidth, sHeight, sPosX, sPosY) {
if (isCapped()) return;
var sOptions = 'toolbar=no,scrollbars=yes,location=yes,statusbar=yes,menubar=no,resizable=1,width=' + sWidth.toString() + ',height=' + sHeight.toString() + ',screenX=' + sPosX + ',screenY=' + sPosY;
var listenerEvent = function() {
if (isCapped()) return;
popunder = _parent.window.open(sUrl, sName, sOptions);
if (popunder) {
// cookie
var now = new Date();
var next = new Date(now.setTime(now.getTime() + sWait));
document.cookie = cookie + '=1;expires=' + next.toGMTString() + ';path=/';
var tomorrow = new Date(); tomorrow.setHours(24,0,0,0);
document.cookie = cookie + 'Cap=' + (popsToday + 1) + ';expires=' + tomorrow.toGMTString() + ';path=/';
pop2under();
}
};
// trigger on document.click
if (document.addEventListener) {
document.addEventListener("click", listenerEvent, false);
} else {
document.attachEvent("onclick", listenerEvent);
}
}
function pop2under() {
try {
popunder.blur();
popunder.opener.window.focus();
window.self.window.focus();
window.focus();
if (browser.firefox) openCloseWindow();
if (browser.webkit) openCloseTab();
if (browser.msie) {
setTimeout(function() {
popunder.blur();
popunder.opener.window.focus();
window.self.window.focus();
window.focus();
}, 1000);
}
} catch (e) {}
}
function openCloseWindow() {
var ghost = window.open('about:blank');
ghost.focus();
ghost.close();
}
function openCloseTab() {
var nothing = '';
var ghost = document.createElement("a");
ghost.href = "data:text/html,<scr"+nothing+"ipt>window.close();</scr"+nothing+"ipt>";
document.getElementsByTagName("body")[0].appendChild(ghost);
var clk = document.createEvent("MouseEvents");
clk.initMouseEvent("click", false, true, window, 0, 0, 0, 0, 0, true, false, false, true, 0, null);
ghost.dispatchEvent(clk);
ghost.parentNode.removeChild(ghost);
}
// abort?
if (isCapped()) {
return;
} else {
doPopunder(sUrl, sName, sWidth, sHeight, sPosX, sPosY);
}
}