PluginProbe ʕ •ᴥ•ʔ
Cookiebot by Usercentrics – Automatic Cookie Banner for GDPR/CCPA & Google Consent Mode / 4.5.3
Cookiebot by Usercentrics – Automatic Cookie Banner for GDPR/CCPA & Google Consent Mode v4.5.3
4.7.2 4.7.1 trunk 2.3.0 2.4.0 2.4.1 2.4.2 2.5.0 3.0.0 3.0.1 3.1.0 3.10.0 3.10.1 3.11.1 3.11.2 3.11.3 3.2.0 3.3.0 3.3.1 3.4.0 3.4.1 3.4.2 3.5.0 3.6.0 3.6.1 3.6.2 3.6.5 3.6.6 3.7.0 3.7.1 3.8.0 3.9.0 4.0.0 4.0.1 4.0.2 4.0.3 4.1.0 4.1.1 4.2.0 4.2.1 4.2.10 4.2.11 4.2.12 4.2.13 4.2.14 4.2.2 4.2.3 4.2.4 4.2.5 4.2.6 4.2.7 4.2.8 4.2.9 4.3.0 4.3.1 4.3.10 4.3.11 4.3.12 4.3.2 4.3.3 4.3.4 4.3.5 4.3.6 4.3.7 4.3.7.1 4.3.8 4.3.9 4.3.9.1 4.4.0 4.4.1 4.4.2 4.5.0 4.5.1 4.5.10 4.5.11 4.5.2 4.5.3 4.5.4 4.5.5 4.5.6 4.5.7 4.5.8 4.5.9 4.6.0 4.6.1 4.6.2 4.6.3 4.6.4 4.6.5 4.6.6 4.6.7 4.7.0
cookiebot / assets / js / backend / account.js
cookiebot / assets / js / backend Last commit date
gutenberg 3 years ago account-static.js 1 year ago account.js 1 year ago consent-mapping.js 1 year ago cookiebot-admin-script.js 1 year ago dashboard.js 1 year ago debug-page.js 1 year ago jquery.tipTip.js 3 years ago multiple-page.js 3 years ago network-settings-page.js 1 year ago prior-consent-settings.js 3 years ago settings-page.js 1 year ago support-page.js 3 years ago
account.js
447 lines
1 const API_BASE_URL = 'https://api.ea.prod.usercentrics.cloud/v1';
2 let authToken = '';
3
4 // Helper function to create FormData for AJAX calls
5 const createFormData = (action, data = {}) => {
6 const formData = new FormData();
7 formData.append('action', action);
8 formData.append('nonce', cookiebot_account.nonce);
9
10 Object.entries(data).forEach(([key, value]) => {
11 formData.append(key, value);
12 });
13
14 return formData;
15 };
16
17 // Function to check scan status
18 async function checkScanStatus(scanId) {
19 try {
20 const response = await fetch(`${API_BASE_URL}/scan/${scanId}`, {
21 method: 'GET',
22 headers: {
23 'Accept': 'application/json',
24 'Content-Type': 'application/json',
25 'Authorization': `Bearer ${authToken}`
26 }
27 });
28
29 const data = await response.json();
30
31 if (data.status === 'DONE') {
32 await fetch(cookiebot_account.ajax_url, {
33 method: 'POST',
34 body: createFormData('cookiebot_store_scan_details', {
35 scan_id: scanId,
36 scan_status: 'DONE'
37 }),
38 credentials: 'same-origin'
39 });
40 return;
41 }
42
43 // Store both scan ID and status
44 await fetch(cookiebot_account.ajax_url, {
45 method: 'POST',
46 body: createFormData('cookiebot_store_scan_details', {
47 scan_id: scanId,
48 scan_status: 'IN_PROGRESS'
49 }),
50 credentials: 'same-origin'
51 });
52
53 if (!response.ok) {
54 await fetch(cookiebot_account.ajax_url, {
55 method: 'POST',
56 body: createFormData('cookiebot_store_scan_details', {
57 scan_id: scanId,
58 scan_status: 'FAILED'
59 }),
60 credentials: 'same-origin'
61 });
62 }
63 } catch (error) {
64 console.error('Error checking scan status:', error);
65 }
66 }
67
68 const checkUserData = async () => {
69 try {
70 let userData = await fetch(`${API_BASE_URL}/accounts`, {
71 headers: {
72 'Accept': 'application/json',
73 'Authorization': `Bearer ${authToken}`,
74 }
75 }).then(r => r.json()).then(data => data[0]);
76
77 if (!userData) throw new Error('No user data received');
78
79 // Store user data
80 userResponseData = await fetch(cookiebot_account.ajax_url, {
81 method: 'POST',
82 body: createFormData('cookiebot_post_user_data', {
83 data: JSON.stringify(userData)
84 }),
85 credentials: 'same-origin'
86 }).then(r => r.json());
87
88 if (!userResponseData.success) {
89 throw new Error('Failed to store user data');
90 }
91
92 return userResponseData;
93 } catch (error) {
94 console.error('Failed to fetch user data:', error);
95 return;
96 }
97 }
98
99 const isAuthenticated = async () => {
100 try {
101 const response = await fetch(`${API_BASE_URL}/accounts`, {
102 headers: {
103 'Accept': 'application/json',
104 'Authorization': `Bearer ${authToken}`
105 }
106 });
107 if (response.status === 401 && cookiebot_account.has_user_data) {
108 await fetch(cookiebot_account.ajax_url, {
109 method: 'POST',
110 body: createFormData('cookiebot_delete_auth_token'),
111 credentials: 'same-origin'
112 });
113 if (!window.prevent_default) {
114 window.location.reload();
115 }
116 }
117 return response.status !== 401;
118 } catch (error) {
119 console.error('Failed to check authentication:', error);
120 return false;
121 }
122 };
123
124 // Function to fetch configuration details
125 async function fetchConfigurationDetails(configId) {
126 try {
127 const response = await fetch(`${API_BASE_URL}/configurations/${configId}`, {
128 method: 'GET',
129 headers: {
130 'Accept': 'application/json',
131 'Content-Type': 'application/json',
132 'Authorization': `Bearer ${authToken}`
133 }
134 });
135
136 if (!response.ok) {
137 throw new Error('Failed to fetch configuration details');
138 }
139
140 const data = await response.json();
141
142 // Store full configuration data
143 await fetch(cookiebot_account.ajax_url, {
144 method: 'POST',
145 body: createFormData('cookiebot_store_configuration', {
146 configuration: JSON.stringify(data)
147 }),
148 credentials: 'same-origin'
149 });
150
151 return data;
152 } catch (error) {
153 console.error('Error fetching configuration details:', error);
154 throw error;
155 }
156 }
157
158 document.addEventListener('DOMContentLoaded', async () => {
159 const urlParams = new URLSearchParams(window.location.search);
160 const ucApiCode = urlParams.get('uc_api_code');
161 // For existing users, no account registration workflow is needed
162 if (cookiebot_account.has_cbid && !cookiebot_account.has_user_data && !cookiebot_account.was_onboarded) {
163 return;
164 }
165
166 try {
167 authToken = await fetch(cookiebot_account.ajax_url, {
168 method: 'POST',
169 body: createFormData('cookiebot_get_auth_token'),
170 credentials: 'same-origin'
171 }).then(r => r.json()).then(data => data.data);
172
173 if (ucApiCode) {
174
175 const isNewUser = urlParams.get('is_new_user');
176
177 if (cookiebot_account.has_cbid) {
178 try {
179 const response = await fetch(cookiebot_account.ajax_url, {
180 method: 'POST',
181 body: createFormData('cookiebot_process_auth_code', { code: ucApiCode }),
182 credentials: 'same-origin'
183 });
184
185 authToken = await fetch(cookiebot_account.ajax_url, {
186 method: 'POST',
187 body: createFormData('cookiebot_get_auth_token'),
188 credentials: 'same-origin'
189 }).then(r => r.json()).then(data => data.data);
190
191 if (!response.ok) throw new Error(`Auth failed: ${response.status}`);
192 const newUrl = new URL(window.location.href);
193 newUrl.searchParams.delete('uc_api_code');
194 newUrl.searchParams.delete('is_new_user');
195 window.location.href = newUrl;
196 return;
197 } catch (error) {
198 console.error('Failed to process authentication:', error);
199 return;
200 }
201 }
202
203
204 if (isNewUser === 'false' && !cookiebot_account.has_user_data) {
205 await fetch(cookiebot_account.ajax_url, {
206 method: 'POST',
207 body: createFormData('cookiebot_process_auth_code', { code: ucApiCode }),
208 credentials: 'same-origin'
209 });
210 const settingsUrl = window.location.protocol + '//' + window.location.hostname + '/wp-admin/admin.php?page=cookiebot_settings';
211 window.location.href = settingsUrl;
212 return;
213 }
214
215 // Add loading state
216 const loadingOverlay = document.createElement('div');
217 loadingOverlay.className = 'loading-overlay';
218 loadingOverlay.innerHTML = `
219 <div class="loading-content">
220 <div class="loading-spinner"></div>
221 <h2>Creating your account</h2>
222 <p>This should only take about a minute. Keep this window open while we set things up.</p>
223 </div>
224 `;
225 document.body.classList.add('has-loading-overlay');
226 document.body.appendChild(loadingOverlay);
227
228 try {
229 const response = await fetch(cookiebot_account.ajax_url, {
230 method: 'POST',
231 body: createFormData('cookiebot_process_auth_code', { code: ucApiCode }),
232 credentials: 'same-origin'
233 });
234
235 authToken = await fetch(cookiebot_account.ajax_url, {
236 method: 'POST',
237 body: createFormData('cookiebot_get_auth_token'),
238 credentials: 'same-origin'
239 }).then(r => r.json()).then(data => data.data);
240
241 if (!response.ok) throw new Error(`Auth failed: ${response.status}`);
242 } catch (error) {
243 console.error('Failed to process authentication:', error);
244 // Remove loading state on error
245 document.body.classList.remove('has-loading-overlay');
246 loadingOverlay.remove();
247 return;
248 }
249 }
250
251 // Check for existing CBID
252 const cbid = await fetch(cookiebot_account.ajax_url, {
253 method: 'POST',
254 body: createFormData('cookiebot_get_cbid'),
255 credentials: 'same-origin'
256 }).then(r => r.json()).then(data => data.success ? data.data : null);
257
258 // Skip if user data exists and is authenticated
259 const isAuthenticatedCondition = await isAuthenticated();
260 if (cookiebot_account.has_user_data && isAuthenticatedCondition) {
261 // Check scan status before returning
262 await checkScanStatus(cbid);
263 await checkUserData();
264 return;
265 }
266
267 // if (!isAuthenticatedCondition && cookiebot_account.has_user_data) {
268 // const callbackUrl = window.location.protocol + '//' + window.location.hostname + '/wp-admin/admin.php?page=cookiebot';
269 // window.location.href = `${API_BASE_URL}/auth/auth0/authorize?origin=wordpress_plugin&callback_domain=${encodeURIComponent(callbackUrl)}`;
270 // }
271
272 if (!cbid) {
273 try {
274 // Create new configuration
275 const siteDomain = window.location.hostname;
276 const formattedDomain = siteDomain.startsWith('http') ? siteDomain : `https://${siteDomain}`;
277
278 const configResponse = await fetch(`${API_BASE_URL}/configurations`, {
279 method: 'POST',
280 headers: {
281 'Accept': 'application/json',
282 'Content-Type': 'application/json',
283 'Authorization': `Bearer ${authToken}`
284 },
285 body: JSON.stringify({
286 configuration_name: "",
287 data_controller: "",
288 legal_framework_template: "gdpr",
289 domains: [formattedDomain]
290 })
291 });
292
293 if (!configResponse.ok) throw new Error(`Config creation failed: ${configResponse.status}`);
294
295 const configData = await configResponse.json();
296 if (!configData?.configuration_id) throw new Error('No configuration ID');
297
298 // Store CBID
299 await fetch(cookiebot_account.ajax_url, {
300 method: 'POST',
301 body: createFormData('cookiebot_store_cbid', { cbid: configData.configuration_id }),
302 credentials: 'same-origin'
303 });
304
305 // Now that we have a CBID, initiate the scan
306 const scanResponse = await fetch(`${API_BASE_URL}/scan`, {
307 method: 'POST',
308 headers: {
309 'Accept': 'application/json',
310 'Content-Type': 'application/json',
311 'Authorization': `Bearer ${authToken}`
312 },
313 body: JSON.stringify({
314 domains: [formattedDomain],
315 configuration_id: configData.configuration_id
316 })
317 });
318
319 if (!scanResponse.ok) throw new Error(`Scan initiation failed: ${scanResponse.status}`);
320
321 const scanData = await scanResponse.json();
322
323 // Check for scan ID in different possible response structures
324 const scanId = scanData?.scan?.scan_id || scanData?.scan_id || scanData?.id;
325 if (!scanId) {
326 console.error('Scan response structure:', scanData);
327 throw new Error('No scan ID received in response');
328 }
329
330 // Store scan ID in WordPress without status initially
331 await fetch(cookiebot_account.ajax_url, {
332 method: 'POST',
333 body: createFormData('cookiebot_store_scan_details', {
334 scan_id: scanId
335 }),
336 credentials: 'same-origin'
337 });
338
339 // Start checking scan status
340 await checkScanStatus(scanId);
341
342 // fetch configuration details
343 await fetchConfigurationDetails(configData.configuration_id);
344
345 } catch (error) {
346 console.error('Failed to create configuration:', error);
347 return;
348 }
349 } else {
350 // If we already have a CBID, check if there's an ongoing scan
351 const scanDetails = await fetch(cookiebot_account.ajax_url, {
352 method: 'POST',
353 body: createFormData('cookiebot_get_scan_details'),
354 credentials: 'same-origin'
355 }).then(r => r.json());
356
357 if (scanDetails.success && scanDetails.data.scan_id) {
358 await checkScanStatus(scanDetails.data.scan_id);
359 }
360 // And fetch configuration details
361 await fetchConfigurationDetails(cbid);
362 await checkUserData();
363 }
364
365 // Check and fetch user data if needed
366 let userResponseData = await fetch(cookiebot_account.ajax_url, {
367 method: 'POST',
368 body: createFormData('cookiebot_get_user_data'),
369 credentials: 'same-origin'
370 }).then(r => r.json());
371
372 try {
373 let userData = await fetch(`${API_BASE_URL}/accounts`, {
374 headers: {
375 'Accept': 'application/json',
376 'Authorization': `Bearer ${authToken}`,
377 }
378 }).then(r => r.json()).then(data => data[0]);
379
380 if (!userData) throw new Error('No user data received');
381
382 // Store user data
383 userResponseData = await fetch(cookiebot_account.ajax_url, {
384 method: 'POST',
385 body: createFormData('cookiebot_post_user_data', {
386 data: JSON.stringify(userData)
387 }),
388 credentials: 'same-origin'
389 }).then(r => r.json());
390
391 if (!userResponseData.success) {
392 throw new Error('Failed to store user data');
393 }
394
395 // Store onboarding status separately
396 await fetch(cookiebot_account.ajax_url, {
397 method: 'POST',
398 body: createFormData('cookiebot_store_onboarding_status', {
399 onboarded: true
400 }),
401 credentials: 'same-origin'
402 });
403
404 } catch (error) {
405 console.error('Failed to fetch user data:', error);
406 return;
407 }
408
409 if (userResponseData.data) {
410 const newUrl = new URL(window.location.href);
411 newUrl.searchParams.delete('uc_api_code');
412 window.location.href = newUrl;
413 }
414 } catch (error) {
415 console.error('An unexpected error occurred:', error);
416 }
417 });
418
419 // Event Listeners
420 document.getElementById('banner-close-btn')?.addEventListener('click', async () => {
421 try {
422 const response = await fetch(cookiebot_account.ajax_url, {
423 method: 'POST',
424 body: createFormData('cookiebot_dismiss_banner'),
425 credentials: 'same-origin'
426 });
427
428 if (!response.ok) throw new Error(`Failed to dismiss banner: ${response.status}`);
429 const banner = document.getElementById('banner-live-notice')
430 if (banner) banner.remove();
431 } catch (error) {
432 console.error('Failed to dismiss banner:', error);
433 }
434 });
435
436 document.getElementById('get-started-button')?.addEventListener('click', async (e) => {
437 e.preventDefault();
438 try {
439 // If multisite is enabled the url might include also the directory for the site
440 // e.g.: http://domain/site1/wp-admin/admin.php?page=cookiebot
441 const callbackUrl = window.location.protocol + '//' + window.location.hostname + '/wp-admin/admin.php?page=cookiebot';
442 window.location.href = `${API_BASE_URL}/auth/auth0/authorize?origin=wordpress_plugin&callback_domain=${encodeURIComponent(callbackUrl)}`;
443 } catch (error) {
444 console.error('Failed to start authentication process:', error);
445 }
446 });
447