-
Notifications
You must be signed in to change notification settings - Fork 1
/
tagModified.html
300 lines (275 loc) · 9.43 KB
/
tagModified.html
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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
<!-- Begin tab-cmp -->
<!-- MaxMind for determining user country location. -->
<script src="//geoip-js.com/js/apis/geoip2/v2.1/geoip2.js" type="text/javascript"></script>
<!-- Quantcast Choice. Consent Manager Tag v2.0 (for TCF 2.0) -->
<script type="text/javascript" async=true>
// Gladly modified: add try/catch.
try {
(function() {
// Gladly modified: create window object.
window.tabCMP = window.tabCMP || {
uspStubFunction: undefined
}
// Gladly modified: don't load the QC Choice script.
// We'll import it ourselves.
// var host = window.location.hostname;
// var element = document.createElement('script');
// var firstScript = document.getElementsByTagName('script')[0];
// var url = 'https://cmp.quantcast.com'
// .concat('/choice/', 'FPBLJYpJgR9Zu', '/', host, '/choice.js?tag_version=V2');
var uspTries = 0;
var uspTriesLimit = 3;
// element.async = true;
// element.type = 'text/javascript';
// element.src = url;
// firstScript.parentNode.insertBefore(element, firstScript);
// Gladly modified.
// Add debug logging.
let debug = false
try {
const urlStr = window.location.href
const url = new URL(urlStr)
const tabCMPDebug = url.searchParams.get('tabCMPDebug')
debug = tabCMPDebug === 'true'
} catch (e) {}
const logPrefix = [
'%ctab-cmp',
'background: #7c7c7c; color: #fff; border-radius: 2px; padding: 2px 6px',
'[head script]'
]
const logDebugging = (...args) => {
if (debug) {
console.log(...logPrefix, ...args)
}
}
function makeStub() {
var TCF_LOCATOR_NAME = '__tcfapiLocator';
var queue = [];
var win = window;
var cmpFrame;
function addFrame() {
var doc = win.document;
var otherCMP = !!(win.frames[TCF_LOCATOR_NAME]);
if (!otherCMP) {
if (doc.body) {
var iframe = doc.createElement('iframe');
iframe.style.cssText = 'display:none';
iframe.name = TCF_LOCATOR_NAME;
doc.body.appendChild(iframe);
} else {
setTimeout(addFrame, 5);
}
}
return !otherCMP;
}
function tcfAPIHandler() {
var gdprApplies;
var args = arguments;
// Gladly modified.
try {
if (!args.length) {
return queue
}
// Our modified code should handle some API calls for TCF v2.
const cmd = args[0]
const shouldHandle =
['getTCData', 'ping', 'addEventListener', 'removeEventListener'].indexOf(cmd) > -1 &&
args[1] === 2 &&
typeof args[2] === 'function'
if (shouldHandle) {
if (cmd === 'removeEventListener') {
// Our stubbed "addEventListener" logic doesn't add a
// listener, so we don't need to remove anything.
logDebugging(`Handled TCF API call "removeEventListener" by taking no action.`)
return
} else {
// This item is set and updated in tab-cmp.
const storedTCFData = JSON.parse(localStorage.getItem('tabCMP.tcfv2.data'))
if (storedTCFData) {
const cb = args[2]
const data = {
...storedTCFData,
// Google Ad Manager will consider the CMP failed if the
// "addEventListener" response doesn't contain a listenerId
// value, which is null in the response to "getTCData".
...(cmd === 'addEventListener' && {
listenerId: 1, // a fake ID our stub won't use
})
}
logDebugging(`Responding to modified TCF API call "${cmd}" with TCF data:`, data)
cb(data, true)
return
} else {
logDebugging(`No stored TCF data. Modified TCF stub is not handling a call to "${cmd}"`)
}
}
} else {
logDebugging(`Modified TCF stub is not handling a call to "${cmd}".`)
}
} catch (e) {
console.error('[tab-cmp]', e)
}
if (!args.length) {
return queue;
} else if (args[0] === 'setGdprApplies') {
if (
args.length > 3 &&
args[2] === 2 &&
typeof args[3] === 'boolean'
) {
gdprApplies = args[3];
if (typeof args[2] === 'function') {
args[2]('set', true);
}
}
} else if (args[0] === 'ping') {
var retr = {
gdprApplies: gdprApplies,
cmpLoaded: false,
cmpStatus: 'stub'
};
if (typeof args[2] === 'function') {
args[2](retr);
}
} else {
if(args[0] === 'init' && typeof args[3] === 'object') {
args[3] = Object.assign(args[3], { tag_version: 'V2' });
}
queue.push(args);
}
}
function postMessageEventHandler(event) {
var msgIsString = typeof event.data === 'string';
var json = {};
try {
if (msgIsString) {
json = JSON.parse(event.data);
} else {
json = event.data;
}
} catch (ignore) {}
var payload = json.__tcfapiCall;
if (payload) {
window.__tcfapi(
payload.command,
payload.version,
function(retValue, success) {
var returnMsg = {
__tcfapiReturn: {
returnValue: retValue,
success: success,
callId: payload.callId
}
};
if (msgIsString) {
returnMsg = JSON.stringify(returnMsg);
}
if (event && event.source && event.source.postMessage) {
event.source.postMessage(returnMsg, '*');
}
},
payload.parameter
);
}
}
// Gladly modified:
// Don't try to use the parent frame, which may be the
// new tab page.
try {
if (win.frames[TCF_LOCATOR_NAME]) {
cmpFrame = win;
}
} catch (ignore) {}
// while (win) {
// try {
// if (win.frames[TCF_LOCATOR_NAME]) {
// cmpFrame = win;
// break;
// }
// } catch (ignore) {}
//
// if (win === window.top) {
// break;
// }
// win = win.parent;
// }
if (!cmpFrame) {
addFrame();
win.__tcfapi = tcfAPIHandler;
win.addEventListener('message', postMessageEventHandler, false);
}
};
makeStub();
var uspStubFunction = function() {
var arg = arguments;
// Gladly modified.
try {
if (!arg.length) {
return queue
}
// Our modified code should handle some API calls for USP v1.
const cmd = arg[0]
const shouldHandle =
['getUSPData', 'uspPing'].indexOf(cmd) > -1 &&
arg[1] === 1 &&
typeof arg[2] === 'function'
if (shouldHandle) {
// These items are set and updated in tab-cmp.
if (cmd === 'getUSPData') {
const storedUSPData = JSON.parse(localStorage.getItem('tabCMP.usp.data'))
if (storedUSPData) {
const cb = arg[2]
logDebugging(`Responding to modified USP API call "${cmd}" with stored USP data:`, storedUSPData)
cb(storedUSPData, true)
return
} else {
logDebugging(`No stored USP data. Modified USP stub is not handling a call to "${cmd}"`)
}
} else if (cmd === 'uspPing') {
const storedUSPPingData = JSON.parse(localStorage.getItem('tabCMP.uspPing.data'))
if (storedUSPPingData) {
const cb = arg[2]
logDebugging(`Responding to modified USP API call "${cmd}" with stored USP ping data:`, storedUSPPingData)
cb(storedUSPPingData, true)
return
} else {
logDebugging(`No stored USP data. Modified USP stub is not handling a call to "${cmd}"`)
}
}
} else {
logDebugging(`Modified USP stub is not handling a call to "${cmd}".`)
}
} catch (e) {
console.error('[tab-cmp]', e)
}
if (typeof window.__uspapi !== uspStubFunction) {
setTimeout(function() {
if (typeof window.__uspapi !== 'undefined') {
window.__uspapi.apply(window.__uspapi, arg);
}
}, 500);
}
};
// Gladly modified: store the stub function so we know when
// Quantcast Choice has finished initializing.
window.tabCMP.uspStubFunction = uspStubFunction
var checkIfUspIsReady = function() {
uspTries++;
if (window.__uspapi === uspStubFunction && uspTries < uspTriesLimit) {
// Gladly modified: add [tab-cmp] prefix.
console.error(...logPrefix, 'USP is not accessible.');
} else {
clearInterval(uspInterval);
}
};
if (typeof window.__uspapi === 'undefined') {
window.__uspapi = uspStubFunction;
var uspInterval = setInterval(checkIfUspIsReady, 6000);
}
})();
} catch (e) {
console.error('[tab-cmp] Head tag errored:', e);
}
</script>
<!-- End Quantcast Choice. Consent Manager Tag v2.0 (for TCF 2.0) -->
<!-- End tab-cmp -->