-
Notifications
You must be signed in to change notification settings - Fork 0
/
openxjs.js
177 lines (154 loc) · 5.12 KB
/
openxjs.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
// Generated by CoffeeScript 1.6.1
/*
OpenXJS
Copyright(c) 2013 Softgames GmbH <scotty@softgames.de>
*/
(function() {
window.OpenXJS = (function() {
OpenXJS.prototype.spcScript = "spc.php";
function OpenXJS(options) {
this.deliveryUrl = options.deliveryUrl;
this.defaultParameters = options.parameters;
}
OpenXJS.prototype.displayAds = function(zonesMapping, parameters, callback) {
var _this = this;
return this.receiveAdCodes(zonesMapping, parameters, function(codes) {
var code, target;
for (target in codes) {
code = codes[target];
_this._displayAd(target, code);
}
return typeof callback === "function" ? callback() : void 0;
});
};
OpenXJS.prototype.receiveAdCodes = function(zonesMapping, parameters, callback) {
var openXParameters, scriptUrl,
_this = this;
openXParameters = this._openxParameters(zonesMapping, parameters);
scriptUrl = this.deliveryUrl + this.spcScript + "?" + this._queryString(openXParameters);
return this._loadScript(scriptUrl, function() {
return _this._parseResponse(zonesMapping, callback);
});
};
OpenXJS.prototype._parseResponse = function(zonesMapping, callback) {
var codes, target, zone;
codes = {};
if (typeof window.OA_output !== "object") {
window.OA_output = null;
if (typeof callback === "function") {
callback(codes);
}
return;
}
for (target in zonesMapping) {
zone = zonesMapping[target];
if (this._emptyResponse(window.OA_output[target])) {
continue;
}
codes[target] = window.OA_output[target];
}
window.OA_output = null;
return typeof callback === "function" ? callback(codes) : void 0;
};
OpenXJS.prototype._displayAd = function(target, code) {
var targetElement;
targetElement = document.getElementById(target);
if (targetElement === null) {
return;
}
return document.getElementById(target).innerHTML = code;
};
OpenXJS.prototype._openxParameters = function(zonesMapping, parameters) {
var key, mappingString, mergedParameters, openXParameters, target, value, zone;
mergedParameters = this._mergeObjects(this.defaultParameters, parameters);
mappingString = "|";
for (target in zonesMapping) {
zone = zonesMapping[target];
mappingString += "" + target + "=" + zone + "|";
}
openXParameters = {
zones: mappingString,
nz: 1,
blockcampaign: 1,
charset: this._documentCharset(),
cb: this._randomNumber(),
r: this._randomNumber(),
loc: this._location(),
referer: this._referrer()
};
for (key in mergedParameters) {
value = mergedParameters[key];
openXParameters[key] = value;
}
return openXParameters;
};
OpenXJS.prototype._loadScript = function(url, callback) {
var called, s,
_this = this;
s = document.createElement('script');
s.async = "async";
called = false;
s.onload = s.onreadystatechange = function() {
if ((s.readyState && !(/complete|loaded/.test(s.readyState))) || called) {
return;
}
called = true;
s.onload = s.onreadystatechange = null;
return typeof callback === "function" ? callback() : void 0;
};
s.src = url;
return this._appendToHead(s);
};
OpenXJS.prototype._randomNumber = function() {
return Math.floor(Math.random() * 99999999999);
};
OpenXJS.prototype._location = function() {
return window.location;
};
OpenXJS.prototype._referrer = function() {
return document.referrer;
};
OpenXJS.prototype._documentCharset = function() {
if (document.charset) {
return document.charset;
} else {
if (document.characterSet) {
return document.characterSet;
} else {
return "";
}
}
};
OpenXJS.prototype._emptyResponse = function(response) {
return typeof response !== "string" || response === "" || response === "<a href=\'F\' target=\'_blank\'><img src=\'F\' border=\'0\' alt=\'\'></a>\n";
};
OpenXJS.prototype._queryString = function(parameters) {
var elements, name, value;
elements = [];
for (name in parameters) {
value = parameters[name];
if (value != null) {
elements.push("" + name + "=" + (encodeURIComponent(value)));
}
}
return elements.join('&');
};
OpenXJS.prototype._appendToHead = function(element) {
var head;
head = document.head || document.getElementsByTagName("head")[0] || document.documentElement;
return head.insertBefore(element, head.firstChild);
};
OpenXJS.prototype._mergeObjects = function(obj1, obj2) {
var attrname, obj3;
obj3 = {};
for (attrname in obj1) {
obj3[attrname] = obj1[attrname];
}
for (attrname in obj2) {
obj3[attrname] = obj2[attrname];
}
return obj3;
};
return OpenXJS;
})();
}).call(this);