-
Notifications
You must be signed in to change notification settings - Fork 41
/
jspopunder.js
108 lines (85 loc) · 3.65 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
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.innerWidth);
var sHeight = (sConfig.height || 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;
document.onclick = function() {
if (isCapped()) return;
popunder = _parent.window.open(sUrl, sName, sOptions);
if (popunder) {
// cookie
var now = new Date();
document.cookie = cookie+'=1;expires='+ new Date(now.setTime(now.getTime()+sWait)).toGMTString() +';path=/';
now = new Date();
document.cookie = cookie+'Cap='+(popsToday+1)+';expires='+ new Date(now.setTime(now.getTime()+(84600*1000))).toGMTString() +';path=/';
pop2under();
}
};
}
function pop2under() {
try {
popunder.blur();
popunder.opener.window.focus();
window.self.window.blur();
window.focus();
if (browser.firefox) openCloseWindow();
if (browser.webkit) openCloseTab();
} catch (e) {}
}
function openCloseWindow() {
var ghost = window.open('about:blank');
ghost.focus();
ghost.close();
}
function openCloseTab() {
var ghost = document.createElement("a");
ghost.href = "about:blank";
ghost.target = "PopHelper";
document.getElementsByTagName("body")[0].appendChild(ghost);
ghost.parentNode.removeChild(ghost);
var clk = document.createEvent("MouseEvents");
clk.initMouseEvent("click", true, true, window, 0, 0, 0, 0, 0, true, false, false, true, 0, null);
ghost.dispatchEvent(clk);
// open a new tab for the link to target
window.open("about:blank", "PopHelper").close();
}
// abort?
if (isCapped()) {
return;
} else {
doPopunder(sUrl, sName, sWidth, sHeight, sPosX, sPosY);
}
}