UNPKG

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.44 kB
/*! For license information please see index.cjs.js.LICENSE.txt */ !function(e,t){"object"==typeof exports&&"object"==typeof module?module.exports=t():"function"==typeof define&&define.amd?define([],t):"object"==typeof exports?exports.BreadCrumbs=t():e.BreadCrumbs=t()}(this,(()=>(()=>{var e={905:e=>{e.exports=function(e){"use strict";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={};return(()=>{"use strict";function e(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,{default:()=>u});var t=function t(r,n){function i(t,i,o){if("undefined"!=typeof document){"number"==typeof(o=e({},n,o)).expires&&(o.expires=new Date(Date.now()+864e5*o.expires)),o.expires&&(o.expires=o.expires.toUTCString()),t=encodeURIComponent(t).replace(/%(2[346B]|5E|60|7C)/g,decodeURIComponent).replace(/[()]/g,escape);var s="";for(var c in o)o[c]&&(s+="; "+c,!0!==o[c]&&(s+="="+o[c].split(";")[0]));return document.cookie=t+"="+r.write(i,t)+s}}return Object.create({set:i,get:function(e){if("undefined"!=typeof document&&(!arguments.length||e)){for(var t=document.cookie?document.cookie.split("; "):[],n={},i=0;i<t.length;i++){var o=t[i].split("="),s=o.slice(1).join("=");try{var c=decodeURIComponent(o[0]);if(n[c]=r.read(s,c),e===c)break}catch(e){}}return e?n[e]:n}},remove:function(t,r){i(t,"",e({},r,{expires:-1}))},withAttributes:function(r){return t(this.converter,e({},this.attributes,r))},withConverter:function(r){return t(e({},this.converter,r),this.attributes)}},{attributes:{value:Object.freeze(n)},converter:{value:Object.freeze(r)}})}({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 i 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=t.get(this.config.cookieName);try{return JSON.parse(atob(e)??"{}")||{}}catch(e){return JSON.parse(t.get(this.config.cookieName)??"{}")||{}}}dump(){return JSON.stringify(this.current)}update(){const e=this.current,r=new CustomEvent("scp.update");return this.dispatchEvent(r),t.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 o=i,s=r(905),c={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"]},u={breadcrumbs:null,init:function(e){const t=e||{};this.breadcrumbs=new o({...c,...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=s(e);n&&t.breadcrumbs.upsert(e,n,t.breadcrumbs.config.overwrite.indexOf(e)>=0)})),this.breadcrumbs.config.formfill&&this.breadcrumbs.formfill()}}.init()})(),n.default})()));