PluginProbe
Kit (formerly ConvertKit) – Email Newsletter, Email Marketing, Membership, Subscribers and Landing Pages / trunk
Kit (formerly ConvertKit) – Email Newsletter, Email Marketing, Membership, Subscribers and Landing Pages vtrunk
3.4.1 3.4.0 3.3.9 3.3.8 3.3.7 3.3.6 3.3.5 3.3.4 3.3.3 3.3.2 3.3.1 2.2.0 2.2.1 2.2.2 2.2.3 2.2.4 2.2.5 2.2.6 2.2.7 2.2.8 2.2.9 2.3.0 2.3.1 2.3.2 2.3.3 All 194 releases
convertkit / resources / frontend / js / convertkit.js

convertkit.js in Kit (formerly ConvertKit) – Email Newsletter, Email Marketing, Membership, Subscribers and Landing Pages trunk, at resources/frontend/js/convertkit.js

153 lines 4.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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