Skip to content

Commit ef67a63

Browse files
committed
Fix. Integrations. Fixed fetch request fields assignment (NoCookie|EventToken)
1 parent 36f0e42 commit ef67a63

17 files changed

+746
-458
lines changed

js/apbct-public-bundle.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

js/apbct-public-bundle_ext-protection.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

js/apbct-public-bundle_ext-protection_gathering.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

js/apbct-public-bundle_full-protection.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

js/apbct-public-bundle_full-protection_gathering.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

js/apbct-public-bundle_gathering.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

js/apbct-public-bundle_int-protection.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

js/apbct-public-bundle_int-protection_gathering.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

js/prebuild/apbct-public-bundle.js

Lines changed: 82 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -3029,95 +3029,127 @@ class ApbctHandler {
30293029
})
30303030
)
30313031
) {
3032+
/**
3033+
* Select key/value pair depending on botDetectorEnabled flag
3034+
* @param {bool} botDetectorEnabled
3035+
* @return {{key: string, value: string}|false} False on empty gained data.
3036+
*/
3037+
const selectFieldsData = function(botDetectorEnabled) {
3038+
const result = {
3039+
'key': null,
3040+
'value': null,
3041+
};
3042+
if (botDetectorEnabled) {
3043+
result.key = 'ct_bot_detector_event_token';
3044+
result.value = apbctLocalStorage.get('bot_detector_event_token');
3045+
} else {
3046+
result.key = 'ct_no_cookie_hidden_field';
3047+
result.value = getNoCookieData();
3048+
};
3049+
return result.key && result.value ? result : false;
3050+
};
3051+
3052+
/**
3053+
*
3054+
* @param {string} body Fetch request data body.
3055+
* @param {object|bool} fieldPair Key value to inject.
3056+
* @return {string} Modified body.
3057+
*/
3058+
const attachFieldsToBody = function(body, fieldPair = false) {
3059+
if (fieldPair) {
3060+
if (body instanceof FormData || typeof body.append === 'function') {
3061+
body.append(fieldPair.key, fieldPair.value);
3062+
} else {
3063+
let bodyObj = JSON.parse(body);
3064+
if (!bodyObj.hasOwnProperty(fieldPair.key)) {
3065+
bodyObj[fieldPair.key] = fieldPair.value;
3066+
body = JSON.stringify(bodyObj);
3067+
}
3068+
}
3069+
}
3070+
return body;
3071+
};
3072+
30323073
let preventOriginalFetch = false;
30333074

30343075
window.fetch = async function(...args) {
3076+
// if no data set provided - exit
3077+
if (
3078+
!args ||
3079+
!args[0] ||
3080+
!args[1] ||
3081+
!args[1].body
3082+
) {
3083+
return defaultFetch.apply(window, args);
3084+
}
3085+
30353086
// Metform block
30363087
if (
30373088
Array.from(document.forms).some((form) => form.classList.contains('metform-form-content')) &&
3038-
args &&
3039-
args[0] &&
30403089
typeof args[0].includes === 'function' &&
30413090
(args[0].includes('/wp-json/metform/') ||
30423091
(ctPublicFunctions._rest_url && (() => {
30433092
try {
30443093
return args[0].includes(new URL(ctPublicFunctions._rest_url).pathname + 'metform/');
30453094
} catch (e) {
3046-
return false;
3095+
return defaultFetch.apply(window, args);
30473096
}
30483097
})())
30493098
)
30503099
) {
3051-
if (args && args[1] && args[1].body) {
3052-
if (+ctPublic.settings__data__bot_detector_enabled) {
3053-
args[1].body.append(
3054-
'ct_bot_detector_event_token',
3055-
apbctLocalStorage.get('bot_detector_event_token'),
3056-
);
3057-
} else {
3058-
args[1].body.append('ct_no_cookie_hidden_field', getNoCookieData());
3059-
}
3100+
try {
3101+
args[1].body = attachFieldsToBody(
3102+
args[1].body,
3103+
selectFieldsData(+ctPublic.settings__data__bot_detector_enabled),
3104+
);
3105+
} catch (e) {
3106+
return defaultFetch.apply(window, args);
30603107
}
30613108
}
3109+
30623110
// WP Recipe Maker block
30633111
if (
30643112
Array.from(document.forms).some(
30653113
(form) => form.classList.contains('wprm-user-ratings-modal-stars-container'),
30663114
) &&
3067-
args &&
3068-
args[0] &&
30693115
typeof args[0].includes === 'function' &&
30703116
args[0].includes('/wp-json/wp-recipe-maker/')
30713117
) {
3072-
if (args[1] && args[1].body) {
3073-
if (typeof args[1].body === 'string') {
3074-
let bodyObj;
3075-
try {
3076-
bodyObj = JSON.parse(args[1].body);
3077-
} catch (e) {
3078-
bodyObj = {};
3079-
}
3080-
if (+ctPublic.settings__data__bot_detector_enabled) {
3081-
bodyObj.ct_bot_detector_event_token =
3082-
apbctLocalStorage.get('bot_detector_event_token');
3083-
} else {
3084-
bodyObj.ct_no_cookie_hidden_field = getNoCookieData();
3085-
}
3086-
args[1].body = JSON.stringify(bodyObj);
3087-
}
3118+
try {
3119+
args[1].body = attachFieldsToBody(
3120+
args[1].body,
3121+
selectFieldsData(+ctPublic.settings__data__bot_detector_enabled),
3122+
);
3123+
} catch (e) {
3124+
return defaultFetch.apply(window, args);
30883125
}
30893126
}
30903127

30913128
// WooCommerce add to cart request, like:
30923129
// /index.php?rest_route=/wc/store/v1/cart/add-item
3093-
if (args && args[0] &&
3094-
args[0].includes('/wc/store/v1/cart/add-item') &&
3095-
args && args[1] && args[1].body
3130+
if (
3131+
typeof args[0].includes === 'function' &&
3132+
args[0].includes('/wc/store/v1/cart/add-item')
30963133
) {
3097-
if (
3098-
+ctPublic.settings__data__bot_detector_enabled &&
3099-
+ctPublic.settings__forms__wc_add_to_cart
3100-
) {
3101-
try {
3102-
let bodyObj = JSON.parse(args[1].body);
3103-
if (!bodyObj.hasOwnProperty('ct_bot_detector_event_token')) {
3104-
bodyObj.ct_bot_detector_event_token =
3105-
apbctLocalStorage.get('bot_detector_event_token');
3106-
args[1].body = JSON.stringify(bodyObj);
3107-
}
3108-
} catch (e) {
3109-
return false;
3134+
try {
3135+
if (
3136+
+ctPublic.settings__forms__wc_add_to_cart
3137+
) {
3138+
args[1].body = attachFieldsToBody(
3139+
args[1].body,
3140+
selectFieldsData(+ctPublic.settings__data__bot_detector_enabled),
3141+
);
31103142
}
3111-
} else {
3112-
args[1].body.append('ct_no_cookie_hidden_field', getNoCookieData());
3143+
} catch (e) {
3144+
return defaultFetch.apply(window, args);
31133145
}
31143146
}
31153147

31163148
// bitrix24 EXTERNAL form
31173149
if (+ctPublic.settings__forms__check_external &&
3118-
args && args[0] &&
3150+
typeof args[0].includes === 'function' &&
31193151
args[0].includes('bitrix/services/main/ajax.php?action=crm.site.form.fill') &&
3120-
args[1] && args[1].body && args[1].body instanceof FormData
3152+
args[1].body instanceof FormData
31213153
) {
31223154
const currentTargetForm = document.querySelector('.b24-form form');
31233155
let data = {

js/prebuild/apbct-public-bundle_ext-protection.js

Lines changed: 82 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -3029,95 +3029,127 @@ class ApbctHandler {
30293029
})
30303030
)
30313031
) {
3032+
/**
3033+
* Select key/value pair depending on botDetectorEnabled flag
3034+
* @param {bool} botDetectorEnabled
3035+
* @return {{key: string, value: string}|false} False on empty gained data.
3036+
*/
3037+
const selectFieldsData = function(botDetectorEnabled) {
3038+
const result = {
3039+
'key': null,
3040+
'value': null,
3041+
};
3042+
if (botDetectorEnabled) {
3043+
result.key = 'ct_bot_detector_event_token';
3044+
result.value = apbctLocalStorage.get('bot_detector_event_token');
3045+
} else {
3046+
result.key = 'ct_no_cookie_hidden_field';
3047+
result.value = getNoCookieData();
3048+
};
3049+
return result.key && result.value ? result : false;
3050+
};
3051+
3052+
/**
3053+
*
3054+
* @param {string} body Fetch request data body.
3055+
* @param {object|bool} fieldPair Key value to inject.
3056+
* @return {string} Modified body.
3057+
*/
3058+
const attachFieldsToBody = function(body, fieldPair = false) {
3059+
if (fieldPair) {
3060+
if (body instanceof FormData || typeof body.append === 'function') {
3061+
body.append(fieldPair.key, fieldPair.value);
3062+
} else {
3063+
let bodyObj = JSON.parse(body);
3064+
if (!bodyObj.hasOwnProperty(fieldPair.key)) {
3065+
bodyObj[fieldPair.key] = fieldPair.value;
3066+
body = JSON.stringify(bodyObj);
3067+
}
3068+
}
3069+
}
3070+
return body;
3071+
};
3072+
30323073
let preventOriginalFetch = false;
30333074

30343075
window.fetch = async function(...args) {
3076+
// if no data set provided - exit
3077+
if (
3078+
!args ||
3079+
!args[0] ||
3080+
!args[1] ||
3081+
!args[1].body
3082+
) {
3083+
return defaultFetch.apply(window, args);
3084+
}
3085+
30353086
// Metform block
30363087
if (
30373088
Array.from(document.forms).some((form) => form.classList.contains('metform-form-content')) &&
3038-
args &&
3039-
args[0] &&
30403089
typeof args[0].includes === 'function' &&
30413090
(args[0].includes('/wp-json/metform/') ||
30423091
(ctPublicFunctions._rest_url && (() => {
30433092
try {
30443093
return args[0].includes(new URL(ctPublicFunctions._rest_url).pathname + 'metform/');
30453094
} catch (e) {
3046-
return false;
3095+
return defaultFetch.apply(window, args);
30473096
}
30483097
})())
30493098
)
30503099
) {
3051-
if (args && args[1] && args[1].body) {
3052-
if (+ctPublic.settings__data__bot_detector_enabled) {
3053-
args[1].body.append(
3054-
'ct_bot_detector_event_token',
3055-
apbctLocalStorage.get('bot_detector_event_token'),
3056-
);
3057-
} else {
3058-
args[1].body.append('ct_no_cookie_hidden_field', getNoCookieData());
3059-
}
3100+
try {
3101+
args[1].body = attachFieldsToBody(
3102+
args[1].body,
3103+
selectFieldsData(+ctPublic.settings__data__bot_detector_enabled),
3104+
);
3105+
} catch (e) {
3106+
return defaultFetch.apply(window, args);
30603107
}
30613108
}
3109+
30623110
// WP Recipe Maker block
30633111
if (
30643112
Array.from(document.forms).some(
30653113
(form) => form.classList.contains('wprm-user-ratings-modal-stars-container'),
30663114
) &&
3067-
args &&
3068-
args[0] &&
30693115
typeof args[0].includes === 'function' &&
30703116
args[0].includes('/wp-json/wp-recipe-maker/')
30713117
) {
3072-
if (args[1] && args[1].body) {
3073-
if (typeof args[1].body === 'string') {
3074-
let bodyObj;
3075-
try {
3076-
bodyObj = JSON.parse(args[1].body);
3077-
} catch (e) {
3078-
bodyObj = {};
3079-
}
3080-
if (+ctPublic.settings__data__bot_detector_enabled) {
3081-
bodyObj.ct_bot_detector_event_token =
3082-
apbctLocalStorage.get('bot_detector_event_token');
3083-
} else {
3084-
bodyObj.ct_no_cookie_hidden_field = getNoCookieData();
3085-
}
3086-
args[1].body = JSON.stringify(bodyObj);
3087-
}
3118+
try {
3119+
args[1].body = attachFieldsToBody(
3120+
args[1].body,
3121+
selectFieldsData(+ctPublic.settings__data__bot_detector_enabled),
3122+
);
3123+
} catch (e) {
3124+
return defaultFetch.apply(window, args);
30883125
}
30893126
}
30903127

30913128
// WooCommerce add to cart request, like:
30923129
// /index.php?rest_route=/wc/store/v1/cart/add-item
3093-
if (args && args[0] &&
3094-
args[0].includes('/wc/store/v1/cart/add-item') &&
3095-
args && args[1] && args[1].body
3130+
if (
3131+
typeof args[0].includes === 'function' &&
3132+
args[0].includes('/wc/store/v1/cart/add-item')
30963133
) {
3097-
if (
3098-
+ctPublic.settings__data__bot_detector_enabled &&
3099-
+ctPublic.settings__forms__wc_add_to_cart
3100-
) {
3101-
try {
3102-
let bodyObj = JSON.parse(args[1].body);
3103-
if (!bodyObj.hasOwnProperty('ct_bot_detector_event_token')) {
3104-
bodyObj.ct_bot_detector_event_token =
3105-
apbctLocalStorage.get('bot_detector_event_token');
3106-
args[1].body = JSON.stringify(bodyObj);
3107-
}
3108-
} catch (e) {
3109-
return false;
3134+
try {
3135+
if (
3136+
+ctPublic.settings__forms__wc_add_to_cart
3137+
) {
3138+
args[1].body = attachFieldsToBody(
3139+
args[1].body,
3140+
selectFieldsData(+ctPublic.settings__data__bot_detector_enabled),
3141+
);
31103142
}
3111-
} else {
3112-
args[1].body.append('ct_no_cookie_hidden_field', getNoCookieData());
3143+
} catch (e) {
3144+
return defaultFetch.apply(window, args);
31133145
}
31143146
}
31153147

31163148
// bitrix24 EXTERNAL form
31173149
if (+ctPublic.settings__forms__check_external &&
3118-
args && args[0] &&
3150+
typeof args[0].includes === 'function' &&
31193151
args[0].includes('bitrix/services/main/ajax.php?action=crm.site.form.fill') &&
3120-
args[1] && args[1].body && args[1].body instanceof FormData
3152+
args[1].body instanceof FormData
31213153
) {
31223154
const currentTargetForm = document.querySelector('.b24-form form');
31233155
let data = {

0 commit comments

Comments
 (0)