UNPKG

@elbwalker/utils

Version:

Shared utils for walkerOS packages

1 lines 15.6 kB
"use strict";function _array_like_to_array(e,t){(null==t||t>e.length)&&(t=e.length);for(var r=0,n=new Array(t);r<t;r++)n[r]=e[r];return n}function _array_with_holes(e){if(Array.isArray(e))return e}function _array_without_holes(e){if(Array.isArray(e))return _array_like_to_array(e)}function asyncGeneratorStep(e,t,r,n,o,i,a){try{var u=e[i](a),s=u.value}catch(e){return void r(e)}u.done?t(s):Promise.resolve(s).then(n,o)}function _async_to_generator(e){return function(){var t=this,r=arguments;return new Promise((function(n,o){var i=e.apply(t,r);function a(e){asyncGeneratorStep(i,n,o,a,u,"next",e)}function u(e){asyncGeneratorStep(i,n,o,a,u,"throw",e)}a(void 0)}))}}function _define_property(e,t,r){return t in e?Object.defineProperty(e,t,{value:r,enumerable:!0,configurable:!0,writable:!0}):e[t]=r,e}function _iterable_to_array(e){if("undefined"!=typeof Symbol&&null!=e[Symbol.iterator]||null!=e["@@iterator"])return Array.from(e)}function _iterable_to_array_limit(e,t){var r=null==e?null:"undefined"!=typeof Symbol&&e[Symbol.iterator]||e["@@iterator"];if(null!=r){var n,o,i=[],a=!0,u=!1;try{for(r=r.call(e);!(a=(n=r.next()).done)&&(i.push(n.value),!t||i.length!==t);a=!0);}catch(e){u=!0,o=e}finally{try{a||null==r.return||r.return()}finally{if(u)throw o}}return i}}function _non_iterable_rest(){throw new TypeError("Invalid attempt to destructure non-iterable instance.\\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}function _non_iterable_spread(){throw new TypeError("Invalid attempt to spread non-iterable instance.\\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}function _object_spread(e){for(var t=1;t<arguments.length;t++){var r=null!=arguments[t]?arguments[t]:{},n=Object.keys(r);"function"==typeof Object.getOwnPropertySymbols&&(n=n.concat(Object.getOwnPropertySymbols(r).filter((function(e){return Object.getOwnPropertyDescriptor(r,e).enumerable})))),n.forEach((function(t){_define_property(e,t,r[t])}))}return e}function ownKeys(e,t){var r=Object.keys(e);if(Object.getOwnPropertySymbols){var n=Object.getOwnPropertySymbols(e);t&&(n=n.filter((function(t){return Object.getOwnPropertyDescriptor(e,t).enumerable}))),r.push.apply(r,n)}return r}function _object_spread_props(e,t){return t=null!=t?t:{},Object.getOwnPropertyDescriptors?Object.defineProperties(e,Object.getOwnPropertyDescriptors(t)):ownKeys(Object(t)).forEach((function(r){Object.defineProperty(e,r,Object.getOwnPropertyDescriptor(t,r))})),e}function _sliced_to_array(e,t){return _array_with_holes(e)||_iterable_to_array_limit(e,t)||_unsupported_iterable_to_array(e,t)||_non_iterable_rest()}function _to_consumable_array(e){return _array_without_holes(e)||_iterable_to_array(e)||_unsupported_iterable_to_array(e)||_non_iterable_spread()}function _type_of(e){return e&&"undefined"!=typeof Symbol&&e.constructor===Symbol?"symbol":typeof e}function _unsupported_iterable_to_array(e,t){if(e){if("string"==typeof e)return _array_like_to_array(e,t);var r=Object.prototype.toString.call(e).slice(8,-1);return"Object"===r&&e.constructor&&(r=e.constructor.name),"Map"===r||"Set"===r?Array.from(r):"Arguments"===r||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(r)?_array_like_to_array(e,t):void 0}}function _ts_generator(e,t){var r,n,o,i,a={label:0,sent:function(){if(1&o[0])throw o[1];return o[1]},trys:[],ops:[]};return i={next:u(0),throw:u(1),return:u(2)},"function"==typeof Symbol&&(i[Symbol.iterator]=function(){return this}),i;function u(i){return function(u){return function(i){if(r)throw new TypeError("Generator is already executing.");for(;a;)try{if(r=1,n&&(o=2&i[0]?n.return:i[0]?n.throw||((o=n.return)&&o.call(n),0):n.next)&&!(o=o.call(n,i[1])).done)return o;switch(n=0,o&&(i=[2&i[0],o.value]),i[0]){case 0:case 1:o=i;break;case 4:return a.label++,{value:i[1],done:!1};case 5:a.label++,n=i[1],i=[0];continue;case 7:i=a.ops.pop(),a.trys.pop();continue;default:if(!(o=a.trys,(o=o.length>0&&o[o.length-1])||6!==i[0]&&2!==i[0])){a=0;continue}if(3===i[0]&&(!o||i[1]>o[0]&&i[1]<o[3])){a.label=i[1];break}if(6===i[0]&&a.label<o[1]){a.label=o[1],o=i;break}if(o&&a.label<o[2]){a.label=o[2],a.ops.push(i);break}o[2]&&a.ops.pop(),a.trys.pop();continue}i=t.call(e,a)}catch(e){i=[6,e],n=0}finally{r=o=0}if(5&i[0])throw i[1];return{value:i[0]?i[1]:void 0,done:!0}}([i,u])}}}var Utils=function(){var e=function(e){return e.language},t=function(){return Intl.DateTimeFormat().resolvedOptions().timeZone},r=function(e){return"".concat(e.screen.width,"x").concat(e.screen.height)},n=function(e,t){return(e.getAttribute(t)||"").trim()},o=function(e){return Array.isArray(e)},i=function(e){return void 0!==e},a=function(e){return E.apply(this,arguments)},u=function(e,t){return T.apply(this,arguments)},s=function(e){var t,r=getComputedStyle(e);if("none"===r.display)return!1;if("visible"!==r.visibility)return!1;if(r.opacity&&Number(r.opacity)<.1)return!1;var n=window.innerHeight,o=e.getBoundingClientRect(),i=o.height,a=o.y,u=a+i,s={x:o.x+e.offsetWidth/2,y:o.y+e.offsetHeight/2};if(i<=n){if(e.offsetWidth+o.width===0||e.offsetHeight+o.height===0)return!1;if(s.x<0)return!1;if(s.x>(document.documentElement.clientWidth||window.innerWidth))return!1;if(s.y<0)return!1;if(s.y>(document.documentElement.clientHeight||window.innerHeight))return!1;t=document.elementFromPoint(s.x,s.y)}else{var c=n/2;if(a<0&&u<c)return!1;if(u>n&&a>c)return!1;t=document.elementFromPoint(s.x,n/2)}if(t)do{if(t===e)return!0}while(t=t.parentElement);return!1},c=function(e){var t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{},r=arguments.length>2&&void 0!==arguments[2]?arguments[2]:{};r=_object_spread({},W,r);var n=Object.entries(t).reduce((function(t,n){var o=_sliced_to_array(n,2),i=o[0],a=o[1],u=e[i];return r.merge&&Array.isArray(u)&&Array.isArray(a)?t[i]=a.reduce((function(e,t){return e.includes(t)?e:_to_consumable_array(e).concat([t])}),_to_consumable_array(u)):(r.extend||i in e)&&(t[i]=a),t}),{});return r.shallow?_object_spread({},e,n):(Object.assign(e,n),e)},l=function(e){return void 0===e?e:(r="",(void 0===(t=e)?"undefined":_type_of(t))===(void 0===r?"undefined":_type_of(r))?e:JSON.stringify(e));var t,r},f=function(){return c({"Content-Type":"application/json; charset=utf-8"},arguments.length>0&&void 0!==arguments[0]?arguments[0]:{})},d=function(e,t,r){return function(){for(var n=arguments.length,o=new Array(n),i=0;i<n;i++)o[i]=arguments[i];try{return e.apply(void 0,_to_consumable_array(o))}catch(e){if(!t)return;return t(e)}finally{null==r||r()}}},y=function(e,t){var r=arguments.length>2&&void 0!==arguments[2]?arguments[2]:{transport:"fetch"};switch(r.transport||"fetch"){case"beacon":return p(e,t);case"xhr":return g(e,t,r);default:return _(e,t,r)}},_=function(e,t){return L.apply(this,arguments)},p=function(e,t){var r=l(t),n=navigator.sendBeacon(e,r);return{ok:n,error:n?void 0:"Failed to send beacon"}},g=function(e,t){var r=arguments.length>2&&void 0!==arguments[2]?arguments[2]:{},n=f(r.headers),o=r.method||"POST",i=l(t);return d((function(){var t=new XMLHttpRequest;for(var r in t.open(o,e,!1),n)t.setRequestHeader(r,n[r]);t.send(i);var a=t.status>=200&&t.status<300;return{ok:a,data:d(JSON.parse,(function(){return t.response}))(t.response),error:a?void 0:"".concat(t.status," ").concat(t.statusText)}}),(function(e){return{ok:!1,error:e.message}}))()},v=function(){for(var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:6,t="";t.length<e;)t+=(36*Math.random()|0).toString(36);return t},b=function(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{},t=e.cb,r=e.consent,n=e.instance,i=e.storage,a=(null==n?void 0:n.push)||C;if(!r)return h((i?k:j)(e),n,t);var u=m(e,t);a("walker on","consent",(o(r)?r:[r]).reduce((function(e,t){return _object_spread_props(_object_spread({},e),_define_property({},t,u))}),{}))},h=function(e,t,r){return!1===r?e:(r||(r=H),r(e,t,H))},m=function(e,t){var r;return function(n,a){if(!i(r)||r!==(null==n?void 0:n.group)){r=null==n?void 0:n.group;var u=function(){return j(e)};if(e.consent)(function(e){var t=_object_spread({},arguments.length>1&&void 0!==arguments[1]?arguments[1]:{},arguments.length>2&&void 0!==arguments[2]?arguments[2]:{}),r={},n=void 0===e;return Object.keys(t).forEach((function(o){t[o]&&(r[o]=!0,e&&e[o]&&(n=!0))})),!!n&&r})((o(e.consent)?e.consent:[e.consent]).reduce((function(e,t){return _object_spread_props(_object_spread({},e),_define_property({},t,!0))}),{}),a)&&(u=function(){return k(e)});return h(u(),n,t)}}},w=function(e){var t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:D.Utils.Storage.Session;switch(t){case D.Utils.Storage.Cookie:O(e,"",0,t);break;case D.Utils.Storage.Local:window.localStorage.removeItem(e);break;case D.Utils.Storage.Session:window.sessionStorage.removeItem(e)}},S=function(e){var t,r,n=arguments.length>1&&void 0!==arguments[1]?arguments[1]:D.Utils.Storage.Session;function o(e){try{return JSON.parse(e||"")}catch(n){var t=1,r="";return e&&(t=0,r=e),{e:t,v:r}}}switch(n){case D.Utils.Storage.Cookie:var i;t=decodeURIComponent((null===(i=document.cookie.split("; ").find((function(t){return t.startsWith(e+"=")})))||void 0===i?void 0:i.split("=")[1])||"");break;case D.Utils.Storage.Local:r=o(window.localStorage.getItem(e));break;case D.Utils.Storage.Session:r=o(window.sessionStorage.getItem(e))}return r&&(t=r.v,0!=r.e&&r.e<Date.now()&&(w(e,n),t="")),function(e){if("true"===e)return!0;if("false"===e)return!1;var t=Number(e);return e==t&&""!==e?t:String(e)}(t||"")},O=function(e,t){var r=arguments.length>2&&void 0!==arguments[2]?arguments[2]:30,n=arguments.length>3&&void 0!==arguments[3]?arguments[3]:D.Utils.Storage.Session,o=arguments.length>4?arguments[4]:void 0,i={e:Date.now()+6e4*r,v:String(t)},a=JSON.stringify(i);switch(n){case D.Utils.Storage.Cookie:t="object"===(void 0===t?"undefined":_type_of(t))?JSON.stringify(t):t;var u="".concat(e,"=").concat(encodeURIComponent(t),"; max-age=").concat(60*r,"; path=/; SameSite=Lax; secure");o&&(u+="; domain="+o),document.cookie=u;break;case D.Utils.Storage.Local:window.localStorage.setItem(e,a);break;case D.Utils.Storage.Session:window.sessionStorage.setItem(e,a)}return S(e,n)},k=function(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{},t=Date.now(),r=e.length,n=void 0===r?30:r,o=e.deviceKey,i=void 0===o?"elbDeviceId":o,a=e.deviceStorage,u=void 0===a?"local":a,s=e.deviceAge,c=void 0===s?30:s,l=e.sessionKey,f=void 0===l?"elbSessionId":l,y=e.sessionStorage,_=void 0===y?"local":y,p=e.pulse,g=void 0!==p&&p,b=j(e),h=!1,m=d((function(e,t,r){var n=S(e,r);return n||(n=v(8),O(e,n,1440*t,r)),String(n)}))(i,c,u),w=d((function(e,r){var o=JSON.parse(String(S(e,r)));return g||(o.isNew=!1,b.marketing&&(Object.assign(o,b),h=!0),h||o.updated+6e4*n<t?(delete o.id,delete o.referrer,o.start=t,o.count++,o.runs=1,h=!0):o.runs++),o}),(function(){h=!0}))(f,_)||{},k={id:v(12),start:t,isNew:!0,count:1,runs:1},A=Object.assign(k,b,w,{device:m},{isStart:h,storage:!0,updated:t},e.data);return O(f,JSON.stringify(A),2*n,_),A},j=function(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{},t=e.isStart||!1,r={isStart:t,storage:!1};if(!1===e.isStart)return r;if(!t&&"navigate"!==_sliced_to_array(performance.getEntriesByType("navigation"),1)[0].type)return r;var n=new URL(e.url||window.location.href),o=e.referrer||document.referrer,i=o&&new URL(o).hostname,a=function(e){var t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{},r="clickId",n={},o={utm_campaign:"campaign",utm_content:"content",utm_medium:"medium",utm_source:"source",utm_term:"term",dclid:r,fbclid:r,gclid:r,msclkid:r,ttclid:r,twclid:r,igshid:r,sclid:r};return Object.entries(c(o,t)).forEach((function(t){var o=_sliced_to_array(t,2),i=o[0],a=o[1],u=e.searchParams.get(i);u&&(a===r&&(a=i,n[r]=i),n[a]=u)})),n}(n,e.parameters);if(Object.keys(a).length&&(a.marketing||(a.marketing=!0),t=!0),!t){var u=e.domains||[];u.push(n.hostname),t=!u.includes(i)}return t?Object.assign({isStart:t,storage:!1,start:Date.now(),id:v(12),referrer:i},a,e.data):r},A=Object.defineProperty,x=Object.getOwnPropertyDescriptor,P=Object.getOwnPropertyNames,U=Object.prototype.hasOwnProperty,I={};!function(e,t){for(var r in t)A(e,r,{get:t[r],enumerable:!0})}(I,{elb:function(){return C},getAttribute:function(){return n},getHashWeb:function(){return u},getLanguage:function(){return e},getScreenSize:function(){return r},getTimezone:function(){return t},isVisible:function(){return s},sendWeb:function(){return y},sendWebAsBeacon:function(){return p},sendWebAsFetch:function(){return _},sendWebAsXhr:function(){return g},sessionStart:function(){return b},sessionStorage:function(){return k},sessionWindow:function(){return j},storageDelete:function(){return w},storageRead:function(){return S},storageWrite:function(){return O}});var C=function(){var e=window;(e.elbLayer=e.elbLayer||[]).push(arguments)},D={Commands:{Action:"action",Config:"config",Consent:"consent",Context:"context",Custom:"custom",Destination:"destination",Elb:"elb",Globals:"globals",Hook:"hook",Init:"init",Link:"link",On:"on",Prefix:"data-elb",Ready:"ready",Run:"run",Session:"session",User:"user",Walker:"walker"},Utils:{Storage:{Cookie:"cookie",Local:"local",Session:"session"}}};function E(){return(E=_async_to_generator((function(e){var t,r,n,o;return _ts_generator(this,(function(a){switch(a.label){case 0:return(t=i(window)&&window.crypto?window.crypto:void 0)&&t.subtle&&TextEncoder?(r=(new TextEncoder).encode(e),[4,t.subtle.digest("SHA-256",r)]):[2];case 1:return n=a.sent(),o=Array.from(new Uint8Array(n)),[2,o.map((function(e){return e.toString(16).padStart(2,"0")})).join("")]}}))}))).apply(this,arguments)}function T(){return(T=_async_to_generator((function(e,t){return _ts_generator(this,(function(r){switch(r.label){case 0:return[4,a(e)];case 1:return[2,(r.sent()||"").slice(0,t)]}}))}))).apply(this,arguments)}var W={merge:!0,shallow:!0,extend:!0};function L(){return L=_async_to_generator((function(e,t){var r,n,o,i=arguments;return _ts_generator(this,(function(a){return n=f((r=i.length>2&&void 0!==i[2]?i[2]:{}).headers),o=l(t),[2,(u=_async_to_generator((function(){var t,i,a;return _ts_generator(this,(function(u){switch(u.label){case 0:return[4,fetch(e,{method:r.method||"POST",headers:n,keepalive:!0,credentials:r.credentials||"same-origin",mode:r.noCors?"no-cors":"cors",body:o})];case 1:return t=u.sent(),r.noCors?(a="",[3,4]):[3,2];case 2:return[4,t.text()];case 3:a=u.sent(),u.label=4;case 4:return i=a,[2,{ok:t.ok,data:i,error:t.ok?void 0:t.statusText}]}}))})),s=function(e){return{ok:!1,error:e.message}},_async_to_generator((function(){var e,t,r,n,o=arguments;return _ts_generator(this,(function(i){switch(i.label){case 0:for(e=o.length,t=new Array(e),r=0;r<e;r++)t[r]=o[r];i.label=1;case 1:return i.trys.push([1,3,5,7]),[4,u.apply(void 0,_to_consumable_array(t))];case 2:return[2,i.sent()];case 3:return n=i.sent(),s?[4,s(n)]:[2];case 4:return[2,i.sent()];case 5:return[4,null==c?void 0:c()];case 6:return i.sent(),[7];case 7:return[2]}}))})))()];var u,s,c}))})),L.apply(this,arguments)}var N,H=function(e,t){var r=(null==t?void 0:t.push)||C,n={};return e.id&&(n.session=e.id),e.storage&&e.device&&(n.device=e.device),r("walker user",n),e.isStart&&r("session start",e),e};return N=I,function(e,t,r,n){if(t&&"object"===(void 0===t?"undefined":_type_of(t))||"function"==typeof t){var o=!0,i=!1,a=void 0;try{for(var u,s=function(){var o=u.value;U.call(e,o)||o===r||A(e,o,{get:function(){return t[o]},enumerable:!(n=x(t,o))||n.enumerable})},c=P(t)[Symbol.iterator]();!(o=(u=c.next()).done);o=!0)s()}catch(e){i=!0,a=e}finally{try{o||null==c.return||c.return()}finally{if(i)throw a}}}return e}(A({},"__esModule",{value:!0}),N)}();