-
Notifications
You must be signed in to change notification settings - Fork 3
/
stub.js
74 lines (69 loc) · 2.41 KB
/
stub.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
/**
* == STUB ==
* Stock les messages envoyés au CMP en attendant son chargement
* Doc : https://github.com/InteractiveAdvertisingBureau/GDPR-Transparency-and-Consent-Framework
**/
(function () {
var gdprAppliesGlobally = false;
function addFrame() {
if (!window.frames['__cmpLocator']) {
if (document.body) {
var body = document.body,
iframe = document.createElement('iframe');
iframe.style = 'display:none';
iframe.name = '__cmpLocator';
body.appendChild(iframe);
} else {
// In the case where this stub is located in the head,
// this allows us to inject the iframe more quickly than
// relying on DOMContentLoaded or other events.
setTimeout(addFrame, 5);
}
}
}
addFrame();
function stubCMP(command, parameter, callback) {
__cmp.a = __cmp.a || [];
if (command === 'ping') {
callback({"gdprAppliesGlobally": gdprAppliesGlobally, "cmpLoaded": false}, true);
} else {
__cmp.a.push({
command: command,
parameter: parameter,
callback: callback
});
}
}
function cmpMsgHandler(event) {
var msgIsString = typeof event.data === "string";
var json;
if (msgIsString) {
json = event.data.indexOf("__cmpCall") !== -1 ? JSON.parse(event.data) : {};
} else {
json = event.data;
}
if (json.__cmpCall) {
var i = json.__cmpCall;
window.__cmp(i.command, i.parameter, function (retValue, success) {
var returnMsg = {
"__cmpReturn": {
"returnValue": retValue,
"success": success,
"callId": i.callId
}
};
try {
event.source.postMessage(msgIsString ? JSON.stringify(returnMsg) : returnMsg, '*');
} catch (e) {
}
});
}
}
// if (typeof (__cmp) !== 'function') {
window.__cmp = stubCMP;
__cmp.msgHandler = cmpMsgHandler;
if (window.addEventListener)
window.addEventListener('message', cmpMsgHandler, false);
else window.attachEvent('onmessage', cmpMsgHandler);
// }
})();