1let mappings = {}; 2addEventListener("load", async () => { 3 getCookie("page_url"); 4 setTimeout(() => 5 setCookie("page_url", window.location.href, 1), 3000) 6 if (!JSON.parse(wp_args.is_supported_form_plugin_active)) { 7 mappings = await getMappings(); 8 listenFormSubmit(); 9 } 10}); 11 12function setCookie(cname, cvalue, exdays) { 13 const d = new Date(); 14 d.setTime(d.getTime() + (exdays * 24 * 60 * 60 * 1000)); 15 let expires = "expires=" + d.toUTCString(); 16 document.cookie = cname + "=" + cvalue + ";" + expires + ";path=/"; 17} 18 19function getCookie(cname) { 20 let result = ""; 21 const cookieName = cname + "="; 22 let decodedCookie = decodeURIComponent(document.cookie); 23 let ca = decodedCookie.split(';'); 24 for (let i = 0; i < ca.length; i++) { 25 let c = ca[i]; 26 console.log(c); 27 while (c.charAt(0) == ' ') { 28 c = c.substring(1); 29 } 30 if (c.indexOf(cookieName) == 0) { 31 result = c.substring(cookieName.length, c.length); 32 } 33 } 34 console.log(result) 35 return result; 36} 37 38function listenFormSubmit() { 39 jQuery(document).on('submit', async (e) => { 40 const fields = {}; 41 Object.keys(mappings).forEach(websiteTag => { 42 const elem = jQuery(e.target) 43 .find(`input[name="${websiteTag}"],select[name="${websiteTag}"],textarea[name="${websiteTag}"]`) 44 const value = processValue(elem); 45 if (value) { 46 fields[mappings[websiteTag]] = value; 47 } 48 49 }) 50 if (Object.keys(fields).length) { 51 if (jQuery(e.target).attr('name')) { 52 fields['Form name'] = jQuery(e.target).attr('name'); 53 } 54 // await sendData(fields); 55 } 56 }) 57} 58 59function processValue(element) { 60 const type = element.attr('type'); 61 let value = null; 62 switch (type) { 63 case 'radio': { 64 if (element.is(':checked')) 65 value = element.val(); 66 break; 67 } 68 case 'checkbox': { 69 Array.from((element).filter(':checked')) 70 .forEach(elem => { 71 value = value ? value + ',' + elem.value : elem.value; 72 }) 73 break; 74 } 75 case 'date': { 76 elemValue = element.val(); 77 value = Date.parse(elemValue) ? Date.parse(elemValue) : elemValue; 78 break; 79 } 80 default: { 81 value = element.val(); 82 break; 83 } 84 } 85 return value; 86} 87 88async function getLastLead(secretKey) { 89 try { 90 const data = jQuery.ajax({ 91 type: 'GET', 92 url: `${wp_args.lead_url}${secretKey}`, 93 success: function (response) { 94 let result = response[0].reduce((prevVal, currVal) => { 95 if (!prevVal[currVal.response_code]) { 96 prevVal[currVal.response_code] = { capturedOn: currVal.date_field }; 97 } 98 prevVal[currVal.response_code][currVal.lead_field] = currVal.value; 99 return prevVal; 100 }, {}) 101 console.log(result); 102 } 103 }) 104 } catch (error) { 105 console.log(error); 106 } 107} 108 109async function getMappings() { 110 let response = {}; 111 try { 112 await jQuery.ajax({ 113 type: 'GET', 114 url: `${wp_args.mappings_url}`, 115 success: function (data) { 116 response = data; 117 console.log(response); 118 } 119 }) 120 } catch (error) { 121 console.log(error) 122 } 123 return response; 124} 125 126async function sendData(fields) { 127 try { 128 const data = jQuery.ajax({ 129 type: 'POST', 130 url: wp_args.lead_url, 131 data: { 132 fields, 133 form_url: window.location.href 134 }, 135 success: function (response) { 136 console.log(response); 137 } 138 }) 139 } catch (error) { 140 console.log(error); 141 } 142} 143 144async function setPageUrl() { 145 try { 146 const data = jQuery.ajax({ 147 type: 'POST', 148 url: wp_args.page_url, 149 data: { 150 page_url: window.location.href 151 }, 152 success: function (response) { 153 console.log(response); 154 } 155 }) 156 } catch (error) { 157 console.log(error); 158 } 159}
Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.