first-party-breadcrumbs
Version:
BreadCrumbs is an npm library designed to enhance user experience by efficiently managing query parameters and form fields through first-party cookies. This library simplifies data handling by automatically storing query parameters in cookies and allows f
2 lines • 4.19 kB
JavaScript
/*! For license information please see index.esm.js.LICENSE.txt */
var e={905:e=>{e.exports=function(e){for(var t={},r=location.search.substr(1).split("&"),n=0;n<r.length;n++)t[r[n].split("=")[0]]=decodeURIComponent(r[n].split("=")[1]);return e?t.hasOwnProperty(e)?decodeURIComponent(t[e].replace(/\+/g," ")):"":t}}},t={};function r(n){var i=t[n];if(void 0!==i)return i.exports;var o=t[n]={exports:{}};return e[n](o,o.exports,r),o.exports}r.d=(e,t)=>{for(var n in t)r.o(t,n)&&!r.o(e,n)&&Object.defineProperty(e,n,{enumerable:!0,get:t[n]})},r.o=(e,t)=>Object.prototype.hasOwnProperty.call(e,t);var n={};function i(e){for(var t=1;t<arguments.length;t++){var r=arguments[t];for(var n in r)e[n]=r[n]}return e}r.d(n,{A:()=>d});var o=function e(t,r){function n(e,n,o){if("undefined"!=typeof document){"number"==typeof(o=i({},r,o)).expires&&(o.expires=new Date(Date.now()+864e5*o.expires)),o.expires&&(o.expires=o.expires.toUTCString()),e=encodeURIComponent(e).replace(/%(2[346B]|5E|60|7C)/g,decodeURIComponent).replace(/[()]/g,escape);var c="";for(var s in o)o[s]&&(c+="; "+s,!0!==o[s]&&(c+="="+o[s].split(";")[0]));return document.cookie=e+"="+t.write(n,e)+c}}return Object.create({set:n,get:function(e){if("undefined"!=typeof document&&(!arguments.length||e)){for(var r=document.cookie?document.cookie.split("; "):[],n={},i=0;i<r.length;i++){var o=r[i].split("="),c=o.slice(1).join("=");try{var s=decodeURIComponent(o[0]);if(n[s]=t.read(c,s),e===s)break}catch(e){}}return e?n[e]:n}},remove:function(e,t){n(e,"",i({},t,{expires:-1}))},withAttributes:function(t){return e(this.converter,i({},this.attributes,t))},withConverter:function(t){return e(i({},this.converter,t),this.attributes)}},{attributes:{value:Object.freeze(r)},converter:{value:Object.freeze(t)}})}({read:function(e){return'"'===e[0]&&(e=e.slice(1,-1)),e.replace(/(%[\dA-F]{2})+/gi,decodeURIComponent)},write:function(e){return encodeURIComponent(e).replace(/%(2[346BF]|3[AC-F]|40|5[BDE]|60|7[BCD])/g,decodeURIComponent)}},{path:"/"});class c extends EventTarget{constructor(e){super(),this.config=e,this.current=this.hydrate()}options(e){const t=this.config;this.config={...t,...e}}hydrate(){const e=o.get(this.config.cookieName);try{return JSON.parse(atob(e)??"{}")||{}}catch(e){return JSON.parse(o.get(this.config.cookieName)??"{}")||{}}}dump(){return JSON.stringify(this.current)}update(){const e=this.current,t=new CustomEvent("scp.update");return this.dispatchEvent(t),o.set(this.config.cookieName,btoa(JSON.stringify(e)),{expires:this.config.expires||7,path:this.config.path||"",domain:this.config.domain||null,secure:this.config.secure||!1,sameSite:this.config.sameSite||"lax"})}upsert(e,t,r=!1){(void 0===this.current[e]||r)&&(this.current[e]=t);const n=new CustomEvent("scp.upsert",{detail:{key:e,value:t}});return this.dispatchEvent(n),this.update()}get(e){return this.current[e]||null}formfill(e=!1){for(const t in this.current)this.current.hasOwnProperty(t)&&document.querySelectorAll(`[name="${t}"]`).forEach((r=>{const n=this.current[t];e&&r.value||("checkbox"===r.type||"radio"===r.type?Array.isArray(n)?r.checked=n.includes(r.value):r.checked=r.value===String(n):r.value=n)}))}}const s=c,u=r(905),a={path:"/",cookieName:"_hansel",formfill:!1,watching:["utm_term","utm_source","utm_campaign","utm_content","utm_medium","email","gclid"],overwrite:["utm_term","utm_source","utm_campaign","utm_content","utm_medium","email","gclid"]},d={breadcrumbs:null,init:function(e){const t=e||{};this.breadcrumbs=new s({...a,...t}),this.stage();const r=new CustomEvent("breadcrumbs.initialized",{detail:this.breadcrumbs});return document.dispatchEvent(r),this.breadcrumbs},visitor:function(){return`${Date.now().toString(36)}-${Math.random().toString(36).substr(2,9)}`},stage:function(){const e=new CustomEvent("breadcrumbs.before_init",{detail:this.breadcrumbs});document.dispatchEvent(e);const t=this,r=this.breadcrumbs.get("vid")||this.visitor();this.breadcrumbs.upsert("vid",r),this.breadcrumbs.config.watching.forEach(((e,r)=>{const n=u(e);n&&t.breadcrumbs.upsert(e,n,t.breadcrumbs.config.overwrite.indexOf(e)>=0)})),this.breadcrumbs.config.formfill&&this.breadcrumbs.formfill()}}.init();var m=n.A;export{m as default};