forked from appnexus/cmp
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
executable file
·237 lines (212 loc) · 6.64 KB
/
Copy pathindex.js
File metadata and controls
executable file
·237 lines (212 loc) · 6.64 KB
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
import 'core-js/fn/array/reduce';
import 'core-js/fn/array/fill';
import 'core-js/fn/array/map';
import 'core-js/fn/array/for-each';
import 'core-js/fn/array/filter';
import 'core-js/fn/array/includes';
import 'core-js/fn/object/values';
import 'core-js/fn/map';
import 'core-js/fn/set';
import 'core-js/fn/number/is-integer';
import 'core-js/fn/symbol';
import 'core-js/fn/string/repeat';
import { init as initializeStore } from './lib/init';
import log from './lib/log';
import config from './lib/config';
import { decodeConsentData, readConsentCookie, applyDecodeFix } from './lib/cookie/cookie';
import { fetchGlobalVendorList } from './lib/vendor';
import { translations } from './lib/translations';
import Promise from 'promise-polyfill';
const TCF_CONFIG = '__tcfConfig';
const queryStringToObject = str => {
return ((str || '') + '').split('&').reduce((acc, curr) => {
const pair = curr.split('=');
if (pair.length > 1) {
acc[decodeURIComponent(pair[0])] = decodeURIComponent(pair[1]);
}
return acc;
}, {});
};
const parsePubConsent = pubConsent => queryStringToObject(decodeURIComponent(pubConsent));
const handleConsentResult = (...args) => {
if (args.length < 2 || !config.autoDisplay) {
return { display: false };
}
const [vendorList = {}, consentData = {}, pubConsent = {}] = args;
const {
publicationVersion: pubVersion,
vendorListVersion: listVersion,
tcfPolicyVersion: listPolicyVersion = 1
} = vendorList;
const {
created,
vendorListVersion,
policyVersion: consentPolicyVersion = 1
} = consentData;
const { publicationVersion } = pubConsent;
if (!created) {
log.debug('No consent data found. Showing consent tool');
return { display: true, command: 'showConsentTool' };
}
if (!listVersion && !pubVersion) {
log.debug('Could not determine vendor list version. Not showing consent tool');
return { display: false };
}
if (pubVersion && parseInt(pubVersion, 10) !== parseInt(publicationVersion, 10)) {
log.debug(`Consent found for publication version ${publicationVersion}, but received vendor list version ${pubVersion}. Showing consent tool`);
return { display: true, command: 'showConsentTool' };
}
if (vendorListVersion !== listVersion) {
log.debug(`Consent found for version ${vendorListVersion}, but received vendor list version ${listVersion}. Showing consent tool`);
return { display: true, command: 'showConsentTool' };
}
if (consentPolicyVersion !== listPolicyVersion) {
log.debug(`Consent found for policy ${consentPolicyVersion}, but received vendor list with policy ${consentPolicyVersion}. Showing consent tool`);
return { display: true, command: 'showConsentTool' };
}
log.debug('Consent found. Not showing consent tool. Show footer when not all consents set to true');
return { display: false, command: 'showFooter' };
};
const shouldDisplay = () => {
return new Promise((resolve) => {
let translationFetched = false;
if (!window.navigator.cookieEnabled) {
const msg = 'Cookies are disabled. Ignoring CMP consent check';
log.warn(msg);
const result = handleConsentResult();
resolve({ ...result, translationFetched });
} else {
const finish = (timeout, vendorList, consentData, pubConsent) => {
clearTimeout(timeout);
const result = handleConsentResult(vendorList, consentData, pubConsent);
resolve({ ...result, translationFetched });
};
const { getVendorList, getConsentData, getConsentDataTimeout } = config;
if (getVendorList) {
getVendorList((err, vendorList) => {
if (err) {
log.error('Failed to get vendor list');
const result = handleConsentResult();
resolve(result);
} else {
translations.initConfig(vendorList.translations);
translations.fetchTranslation()
.then(() => {
translationFetched = true;
const timeout = setTimeout(() => {
resolve({ display: false });
}, getConsentDataTimeout);
if (getConsentData) {
getConsentData((err, data) => {
if (err) {
finish(timeout, vendorList);
} else {
try {
const tcStringDecoded = decodeConsentData(data.consent);
const pubConsent = parsePubConsent(data.pubConsent);
finish(timeout, vendorList, tcStringDecoded, pubConsent);
} catch (e) {
finish(timeout, vendorList);
}
}
});
}
})
.catch(() => {
const result = handleConsentResult();
resolve({ ...result, translationFetched });
});
}
});
} else {
fetchGlobalVendorList().then((vendorList) => {
const timeout = setTimeout(() => {
resolve({ display: false });
}, getConsentDataTimeout);
readConsentCookie().then((cookie) => {
if (cookie) {
try {
const tcStringDecoded = decodeConsentData(cookie);
finish(timeout, vendorList, tcStringDecoded);
} catch (e) {
finish(timeout, vendorList);
}
} else {
finish(timeout, vendorList);
}
});
}).catch(() => {
const result = handleConsentResult();
resolve(result);
});
}
}
});
};
const displayUI = (tcfApi, result) => {
if (!tcfApi) {
return;
}
const { shouldDisplayFooter } = config;
let { display, command } = result;
const tcfApiCall = (command) => {
tcfApi(command, 2, () => log.debug(`${command} command was called`));
};
if (display) {
tcfApiCall(command);
} else if (command === 'showFooter') {
if (shouldDisplayFooter) {
shouldDisplayFooter((result) => {
if (result) {
tcfApiCall(command);
}
});
}
}
};
function readExternalConsentData(config) {
return new Promise((resolve, reject) => {
try {
config.getConsentData((err, data) => {
if (err) {
reject(err);
} else {
try {
const result = {
consent: data && data.consent,
pubConsent: parsePubConsent(data && data.pubConsent)
};
resolve(result);
} catch (err) {
reject(err);
}
}
});
} catch (err) {
reject(err);
}
});
}
function start() {
applyDecodeFix();
// Preserve any config options already set
const tcfConfig = window[TCF_CONFIG] || {};
const configUpdates = {
globalConsentLocation: 'https://rasp.mgr.consensu.org/portal.html',
...tcfConfig
};
config.update(configUpdates);
Promise.all([
shouldDisplay(),
config.getConsentData ? readExternalConsentData(config) : readConsentCookie()
]).then(([displayOptions, consents]) => {
initializeStore(consents, displayOptions).then(() => {
displayUI(window.__tcfapi, displayOptions);
}).catch(err => {
log.error('Failed to initialize CMP store', err);
});
}).catch(err => {
log.error('Failed to load CMP', err);
});
}
start();