| 1 |
/** |
| 2 |
* Frontend functionality for subscribers and tags. |
| 3 |
* |
| 4 |
* @since 1.9.6 |
| 5 |
* |
| 6 |
* @author ConvertKit |
| 7 |
*/ |
| 8 |
|
| 9 |
/** |
| 10 |
* Remove the url subscriber_id url param |
| 11 |
* |
| 12 |
* The 'ck_subscriber_id' should only be set on URLs included on |
| 13 |
* links from a ConvertKit email with no other URL parameters. |
| 14 |
* This function removes the parameters so a customer won't share |
| 15 |
* a URL with their subscriber ID in it. |
| 16 |
* |
| 17 |
* @param {string} url URL. |
| 18 |
*/ |
| 19 |
function convertKitRemoveSubscriberIDFromURL(url) { |
| 20 |
// Parse URL. |
| 21 |
const url_object = new URL(url); |
| 22 |
const ck_subscriber_id = url_object.searchParams.get('ck_subscriber_id'); |
| 23 |
|
| 24 |
// If ck_subscriber_id is null, it's not included in the URL. |
| 25 |
// Don't modify the URL. |
| 26 |
if (ck_subscriber_id === null) { |
| 27 |
return; |
| 28 |
} |
| 29 |
|
| 30 |
// Remove ck_subscriber_id from URL params. |
| 31 |
url_object.searchParams.delete('ck_subscriber_id'); |
| 32 |
|
| 33 |
// Get title and string of parameters. |
| 34 |
const title = document.getElementsByTagName('title')[0].innerHTML; |
| 35 |
let params = url_object.searchParams.toString(); |
| 36 |
|
| 37 |
// Only add '?' if there are parameters. |
| 38 |
if (params.length > 0) { |
| 39 |
params = '?' + params; |
| 40 |
} |
| 41 |
|
| 42 |
// Update history. |
| 43 |
window.history.replaceState( |
| 44 |
null, |
| 45 |
title, |
| 46 |
url_object.pathname + params + url_object.hash |
| 47 |
); |
| 48 |
|
| 49 |
// Emit custom event with the removed subscriber ID. |
| 50 |
convertKitEmitCustomEvent('kit_subscriber_id_removed_from_url', { |
| 51 |
id: ck_subscriber_id, |
| 52 |
}); |
| 53 |
} |
| 54 |
|
| 55 |
/** |
| 56 |
* Emit a custom event with optional detail data. |
| 57 |
* |
| 58 |
* This function creates and dispatches a custom event with the specified |
| 59 |
* event name and detail data. |
| 60 |
* |
| 61 |
* @since 2.5.0 |
| 62 |
* |
| 63 |
* @param {string} eventName The name of the custom event to emit. |
| 64 |
* @param {Object} [detail={}] Optional detail data to include with the event. |
| 65 |
*/ |
| 66 |
function convertKitEmitCustomEvent(eventName, detail) { |
| 67 |
const event = new CustomEvent(eventName, { detail }); |
| 68 |
document.dispatchEvent(event); |
| 69 |
} |
| 70 |
|
| 71 |
/* eslint-disable no-unused-vars */ |
| 72 |
/** |
| 73 |
* Handles form submissions when reCAPTCHA is enabled. |
| 74 |
* |
| 75 |
* @param {string} token reCAPTCHA token. |
| 76 |
*/ |
| 77 |
function convertKitRecaptchaFormSubmit(token) { |
| 78 |
// Find submit button with the data-callback attribute. |
| 79 |
const submitButton = document.querySelector( |
| 80 |
'[type="submit"][data-callback="convertKitRecaptchaFormSubmit"]' |
| 81 |
); |
| 82 |
|
| 83 |
// Get the parent form of the submit button. |
| 84 |
const form = submitButton.closest('form'); |
| 85 |
|
| 86 |
// Submit the form. |
| 87 |
form.submit(); |
| 88 |
} |
| 89 |
|
| 90 |
// Scope the function to the window object as webpack will wrap everything in a closure, |
| 91 |
// resulting in the function not being available globally. |
| 92 |
window.convertKitRecaptchaFormSubmit = convertKitRecaptchaFormSubmit; |
| 93 |
|
| 94 |
/** |
| 95 |
* Handles form submissions when Cloudflare Turnstile is enabled. |
| 96 |
* |
| 97 |
* Turnstile auto-populates a hidden `cf-turnstile-response` input inside the |
| 98 |
* enclosing form once the challenge is solved, so we just need to submit the |
| 99 |
* containing form when the callback fires. |
| 100 |
* |
| 101 |
* @param {string} token Turnstile response token. |
| 102 |
*/ |
| 103 |
function convertKitTurnstileFormSubmit(token) { |
| 104 |
// Find the Turnstile widget div with the data-callback attribute. |
| 105 |
const widget = document.querySelector( |
| 106 |
'.cf-turnstile[data-callback="convertKitTurnstileFormSubmit"]' |
| 107 |
); |
| 108 |
|
| 109 |
if (!widget) { |
| 110 |
return; |
| 111 |
} |
| 112 |
|
| 113 |
// Get the parent form of the widget. |
| 114 |
const form = widget.closest('form'); |
| 115 |
|
| 116 |
if (!form) { |
| 117 |
return; |
| 118 |
} |
| 119 |
|
| 120 |
// Submit the form. |
| 121 |
form.submit(); |
| 122 |
} |
| 123 |
|
| 124 |
// Scope the function to the window object as webpack will wrap everything in a closure, |
| 125 |
// resulting in the function not being available globally. |
| 126 |
window.convertKitTurnstileFormSubmit = convertKitTurnstileFormSubmit; |
| 127 |
/* eslint-enable no-unused-vars */ |
| 128 |
|
| 129 |
/** |
| 130 |
* Register events on frontend. |
| 131 |
* |
| 132 |
* @since 3.2.0 |
| 133 |
*/ |
| 134 |
if (typeof convertkit !== 'undefined') { |
| 135 |
document.addEventListener('DOMContentLoaded', function () { |
| 136 |
// Removes `ck_subscriber_id` from the URI. |
| 137 |
convertKitRemoveSubscriberIDFromURL(window.location.href); |
| 138 |
|
| 139 |
// Set a cookie if any scripts with data-kit-limit-per-session attribute exist. |
| 140 |
if ( |
| 141 |
document.querySelectorAll('script[data-kit-limit-per-session="1"]') |
| 142 |
.length > 0 |
| 143 |
) { |
| 144 |
document.cookie = 'ck_non_inline_form_displayed=1; path=/'; |
| 145 |
if (convertkit.debug) { |
| 146 |
console.log( |
| 147 |
'Set `ck_non_inline_form_displayed` cookie for non-inline form limit' |
| 148 |
); |
| 149 |
} |
| 150 |
} |
| 151 |
}); |
| 152 |
} |
| 153 |
|