-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
mass.js
184 lines (150 loc) · 4.09 KB
/
mass.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
/**
* This module adds MASS support to Prebid.js.
*/
import {config} from '../src/config.js';
import {getHook} from '../src/hook.js';
import {auctionManager} from '../src/auctionManager.js';
import {timedBidResponseHook} from '../src/utils/perfMetrics.js';
const defaultCfg = {
dealIdPattern: /^MASS/i
};
let cfg;
export let listenerAdded = false;
export let isEnabled = false;
const matchedBids = {};
let renderers;
config.getConfig('mass', config => init(config.mass));
/**
* Module init.
*/
export function init(userCfg) {
cfg = Object.assign({}, defaultCfg, window.massConfig && window.massConfig.mass, userCfg);
if (cfg.enabled === false) {
if (isEnabled) {
getHook('addBidResponse').getHooks({hook: addBidResponseHook}).remove();
isEnabled = false;
}
} else {
if (!isEnabled) {
getHook('addBidResponse').before(addBidResponseHook);
isEnabled = true;
}
}
if (isEnabled) {
updateRenderers();
}
}
/**
* Update the list of renderers based on current config.
*/
export function updateRenderers() {
renderers = [];
// official MASS renderer:
if (cfg.dealIdPattern && cfg.renderUrl) {
renderers.push({
match: isMassBid,
render: useDefaultRender(cfg.renderUrl, 'mass')
});
}
// add any custom renderer defined in the config:
(cfg.custom || []).forEach(renderer => {
if (!renderer.match && renderer.dealIdPattern) {
renderer.match = useDefaultMatch(renderer.dealIdPattern);
}
if (!renderer.render && renderer.renderUrl && renderer.namespace) {
renderer.render = useDefaultRender(renderer.renderUrl, renderer.namespace);
}
if (renderer.match && renderer.render) {
renderers.push(renderer);
}
});
return renderers;
}
/**
* Before hook for 'addBidResponse'.
*/
export const addBidResponseHook = timedBidResponseHook('mass', function addBidResponseHook(next, adUnitCode, bid, reject, {index = auctionManager.index} = {}) {
let renderer;
for (let i = 0; i < renderers.length; i++) {
if (renderers[i].match(bid)) {
renderer = renderers[i];
break;
}
}
if (renderer) {
const bidRequest = index.getBidRequest(bid);
matchedBids[bid.requestId] = {
renderer,
payload: {
bidRequest,
bid,
adm: bid.ad
}
};
bid.ad = '<script>window.parent.postMessage({massBidId: "' + bid.requestId + '"}, "*");\x3c/script>';
addListenerOnce();
}
next(adUnitCode, bid, reject);
});
/**
* Add listener for the "message" event sent by the winning bid
*/
export function addListenerOnce() {
if (!listenerAdded) {
window.addEventListener('message', e => {
if (!e || !e.data || !e.data.massBidId) {
return;
}
const matchedBid = matchedBids[e.data.massBidId];
if (matchedBid) {
const payload = {
type: 'prebid',
event: e,
...matchedBid.payload
};
delete payload.bid.ad;
matchedBid.renderer.render(payload);
}
});
listenerAdded = true;
}
}
/**
* Check if a bid is MASS.
*/
export function isMassBid(bid) {
// either bid.meta.mass is set or deal ID matches a publisher specified pattern:
if (!((bid.meta && bid.meta.mass) || (cfg.dealIdPattern && cfg.dealIdPattern.test(bid.dealId)))) {
return false;
}
// there must be a 'mass://' or 'pcreative?' in the ad markup:
return /mass:\/\/|\/pcreative\?/i.test(bid.ad);
}
/**
* Default match (factory).
*/
export function useDefaultMatch(dealIdPattern) {
return function(bid) {
return dealIdPattern.test(bid.dealId);
};
}
/**
* Default render (factory).
*/
export function useDefaultRender(renderUrl, namespace) {
return function render(payload) {
const ns = window[namespace] = window[namespace] || {};
ns.queue = ns.queue || [];
ns.queue.push(payload);
if (!ns.loaded) {
const s = document.createElement('script');
s.type = 'text/javascript';
s.async = true;
s.src = renderUrl;
const x = document.getElementsByTagName('script')[0];
x.parentNode.insertBefore(s, x);
ns.loaded = true;
}
};
}
init();