forked from prebid/Prebid.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathidImportLibrary.js
288 lines (259 loc) · 8.19 KB
/
idImportLibrary.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
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
import MD5 from 'crypto-js/md5.js';
import { ACTIVITY_ENRICH_UFPD } from '../src/activities/activities.js';
import { activityParams } from '../src/activities/activityParams.js';
import { MODULE_TYPE_PREBID } from '../src/activities/modules.js';
import { isActivityAllowed } from '../src/activities/rules.js';
import { ajax } from '../src/ajax.js';
import { config } from '../src/config.js';
import { getGlobal } from '../src/prebidGlobal.js';
import { logError, logInfo } from '../src/utils.js';
let email;
let conf;
const LOG_PRE_FIX = 'ID-Library: ';
const CONF_DEFAULT_OBSERVER_DEBOUNCE_MS = 250;
export const CONF_DEFAULT_FULL_BODY_SCAN = false;
export const CONF_DEFAULT_INPUT_SCAN = false;
const OBSERVER_CONFIG = {
subtree: true,
attributes: true,
attributeOldValue: false,
childList: true,
attirbuteFilter: ['value'],
characterData: true,
characterDataOldValue: false
};
const _logInfo = createLogInfo(LOG_PRE_FIX);
const _logError = createLogError(LOG_PRE_FIX);
function createLogInfo(prefix) {
return function (...strings) {
logInfo(prefix + ' ', ...strings);
}
}
function createLogError(prefix) {
return function (...strings) {
logError(prefix + ' ', ...strings);
}
}
function getEmail(value) {
const matched = value.match(/([a-zA-Z0-9._-]+@[a-zA-Z0-9._-]+\.[a-zA-Z0-9._-]+)/gi);
if (!matched) {
return null;
}
_logInfo('Email found: ' + matched[0]);
return matched[0];
}
function bodyAction(mutations, observer) {
_logInfo('BODY observer on debounce called');
// If the email is found in the input element, disconnect the observer
if (email) {
observer.disconnect();
_logInfo('Email is found, body observer disconnected');
return;
}
const body = document.body.innerHTML;
email = getEmail(body);
if (email !== null) {
_logInfo(`Email obtained from the body ${email}`);
observer.disconnect();
_logInfo('Post data on email found in body');
postData();
}
}
function targetAction(mutations, observer) {
_logInfo('Target observer called');
for (const mutation of mutations) {
for (const node of mutation.addedNodes) {
email = node.textContent;
if (email) {
_logInfo('Email obtained from the target ' + email);
observer.disconnect();
_logInfo('Post data on email found in target');
postData();
return;
}
}
}
}
function addInputElementsElementListner() {
if (doesInputElementsHaveEmail()) {
_logInfo('Email found in input elements ' + email);
_logInfo('Post data on email found in target without');
postData();
return;
}
_logInfo('Adding input element listeners');
const inputs = document.querySelectorAll('input[type=text], input[type=email]');
for (var i = 0; i < inputs.length; i++) {
_logInfo(`Original Value in Input = ${inputs[i].value}`);
inputs[i].addEventListener('change', event => processInputChange(event));
inputs[i].addEventListener('blur', event => processInputChange(event));
}
}
function addFormInputElementsElementListner(id) {
_logInfo('Adding input element listeners');
if (doesFormInputElementsHaveEmail(id)) {
_logInfo('Email found in input elements ' + email);
postData();
return;
}
_logInfo('Adding input element listeners');
const input = document.getElementById(id);
input.addEventListener('change', event => processInputChange(event));
input.addEventListener('blur', event => processInputChange(event));
}
function removeInputElementsElementListner() {
_logInfo('Removing input element listeners');
const inputs = document.querySelectorAll('input[type=text], input[type=email]');
for (var i = 0; i < inputs.length; i++) {
inputs[i].removeEventListener('change', event => processInputChange(event));
inputs[i].removeEventListener('blur', event => processInputChange(event));
}
}
function processInputChange(event) {
const value = event.target.value;
_logInfo(`Modified Value of input ${event.target.value}`);
email = getEmail(value);
if (email !== null) {
_logInfo('Email found in input ' + email);
postData();
removeInputElementsElementListner();
}
}
function debounce(func, wait, immediate) {
var timeout;
return function () {
const context = this;
const args = arguments;
const later = function () {
timeout = null;
if (!immediate) func.apply(context, args);
};
var callNow = immediate && !timeout;
clearTimeout(timeout);
if (callNow) {
func.apply(context, args);
} else {
_logInfo('Debounce wait time ' + wait);
timeout = setTimeout(later, wait);
}
};
};
function handleTargetElement() {
const targetObserver = new MutationObserver(debounce(targetAction, conf.debounce, false));
const targetElement = document.getElementById(conf.target);
if (targetElement) {
email = targetElement.textContent;
if (!email) {
_logInfo('Finding the email with observer');
targetObserver.observe(targetElement, OBSERVER_CONFIG);
} else {
_logInfo('Target found with target ' + email);
_logInfo('Post data on email found in target with target');
postData();
}
}
}
function handleBodyElements() {
email = getEmail(document.body.innerHTML);
if (email !== null) {
_logInfo('Email found in body ' + email);
_logInfo('Post data on email found in the body without observer');
postData();
return;
}
if (conf.fullscan === true) {
const bodyObserver = new MutationObserver(debounce(bodyAction, conf.debounce, false));
bodyObserver.observe(document.body, OBSERVER_CONFIG);
}
}
function doesInputElementsHaveEmail() {
const inputs = document.getElementsByTagName('input');
for (let index = 0; index < inputs.length; ++index) {
const curInput = inputs[index];
email = getEmail(curInput.value);
if (email !== null) {
return true;
}
}
return false;
}
function doesFormInputElementsHaveEmail(formElementId) {
const input = document.getElementById(formElementId);
if (input) {
email = getEmail(input.value);
if (email !== null) {
return true;
}
}
return false;
}
function syncCallback() {
return {
success: function () {
_logInfo('Data synced successfully.');
},
error: function () {
_logInfo('Data sync failed.');
}
}
}
function postData() {
(getGlobal()).refreshUserIds();
const userIds = (getGlobal()).getUserIds();
if (Object.keys(userIds).length === 0) {
_logInfo('No user ids');
return;
}
_logInfo('Users' + userIds);
const syncPayload = {};
syncPayload.hid = MD5(email).toString();
syncPayload.uids = userIds;
const payloadString = JSON.stringify(syncPayload);
_logInfo(payloadString);
ajax(conf.url, syncCallback(), payloadString, {method: 'POST', withCredentials: true});
}
function associateIds() {
if (window.MutationObserver || window.WebKitMutationObserver) {
if (conf.target) {
handleTargetElement();
} else if (conf.formElementId) {
addFormInputElementsElementListner(conf.formElementId);
} else if (conf.inputscan) {
addInputElementsElementListner();
} else {
handleBodyElements();
}
}
}
export function setConfig(config) {
if (!config) {
_logError('Required confirguration not provided');
return;
}
if (!config.url) {
_logError('The required url is not configured');
return;
}
if (!isActivityAllowed(ACTIVITY_ENRICH_UFPD, activityParams(MODULE_TYPE_PREBID, 'idImportLibrary'))) {
_logError('Permission for id import was denied by CMP');
return;
}
if (typeof config.debounce !== 'number') {
config.debounce = CONF_DEFAULT_OBSERVER_DEBOUNCE_MS;
_logInfo('Set default observer debounce to ' + CONF_DEFAULT_OBSERVER_DEBOUNCE_MS);
}
if (typeof config.fullscan !== 'boolean') {
config.fullscan = CONF_DEFAULT_FULL_BODY_SCAN;
_logInfo('Set default fullscan ' + CONF_DEFAULT_FULL_BODY_SCAN);
}
if (typeof config.inputscan !== 'boolean') {
config.inputscan = CONF_DEFAULT_INPUT_SCAN;
_logInfo('Set default input scan ' + CONF_DEFAULT_INPUT_SCAN);
}
if (typeof config.formElementId == 'string') {
_logInfo('Looking for formElementId ' + config.formElementId);
}
conf = config;
associateIds();
}
config.getConfig('idImportLibrary', config => setConfig(config.idImportLibrary));