@tracing/shared
Version:
8 lines (7 loc) • 6.11 kB
JavaScript
/*!
* @tracing/shared v0.0.3
* (c) 2024 wermdany <https://github.com/wermdany>
* license ISC
* hash f6fbb5bcb01c00afbd6fb6f5826e891202a721b1
*/
var TracingShared=function(e){"use strict";const t=Object.prototype.toString,n=e=>t=>r(t)===`[object ${e}]`,r=e=>t.call(e);const o=Array.isArray,i=n("Object"),c=n("String"),u=n("Boolean"),s=n("Number"),a=n("RegExp"),f=n("Undefined"),l=n("Date");function m(e,t){try{return e instanceof t}catch(n){return!1}}function d(e){return"undefined"!=typeof Element&&m(e,Element)}function p(e,t=0){const n=Math.pow(10,t);return Math.floor(e*n)/n}function g(e,t=[],n=!0,r=!1){if(!e||!d(e))return"";if(e===document.body)return"body";const{parentElement:o}=e,i=h(e),c=y(e),u=w(e);if(!o)return c?`#${c}`:u.length?`${i}.${u.join(".")}`:i;if(c&&n)return`#${c}`;if(t.length&&u.some((e=>t.includes(e))))return`${i}.${u.join(".")}`;if(r)return i;const s=Array.from((null==o?void 0:o.children)||[]).filter((e=>h(e)==i)),a=Array.prototype.indexOf.call(s,e);return`${i}:nth-of-type(${a+1})`}function h(e,t=!0){return t?e.tagName.toLowerCase():e.tagName}function y(e){return e.getAttribute("id")||""}function w(e){return Array.from(e.classList)}class b{constructor(e,t){this.name=e,this.s=window[t]}get(){const e=this.s.getItem(this.name);try{return JSON.parse(e)}catch(t){return null}}set(e){try{return this.s.setItem(this.name,JSON.stringify(e)),!0}catch(t){return!1}}clear(){this.s.removeItem(this.name)}}let O;const E=function(){return this}();const S=window.performance;function j(){if(S)return{now:()=>S.now(),timeOrigin:Date.now()-S.now()}}const C=j();return e.BaseStorage=b,e.browserPerformanceTimer=j,e.choice=function(e){return"function"==typeof e?e:a(e)?t=>e.test(t):t=>e.includes(t)},e.createCookie=function(e,t={}){const{path:n,domain:r,expires:o,secure:i}=t;return{get(){const t=decodeURIComponent(document.cookie.replace(new RegExp("(?:(?:^|.*;)\\s*"+encodeURIComponent(e).replace(/[-.+*]/g,"\\$&")+"\\s*\\=\\s*([^;]*).*$)|^.*$"),"$1"));try{return JSON.parse(t)}catch(n){return null}},set:t=>!/^(?:expires|max-age|path|domain|secure)$/i.test(e)&&(document.cookie=encodeURIComponent(e)+"="+encodeURIComponent(JSON.stringify(t))+(o?s(o)?o===1/0?"; expires=Fri, 31 Dec 9999 23:59:59 GMT":"; max-age="+o:c(o)?"; expires="+o:l(o)?"; expires="+o.toUTCString():void 0:"")+(r?"; domain="+r:"")+(n?"; path="+n:"")+(i?"; secure":""),!0),clear(){document.cookie=encodeURIComponent(e)+"=; expires=Thu, 01 Jan 1970 00:00:00 GMT"+(r?"; domain="+r:"")+(n?"; path="+n:"")}}},e.createLocalStorage=function(e){return new b(e,"localStorage")},e.createSessionStorage=function(e){return new b(e,"sessionStorage")},e.decrypt=function(e){return decodeURIComponent(atob(e).split("").map((function(e){return"%"+("00"+e.charCodeAt(0).toString(16)).slice(-2)})).join(""))},e.encrypt=function(e){return btoa(encodeURIComponent(e).replace(/%([0-9A-F]{2})/g,((e,t)=>String.fromCharCode(Number("0x"+t)))))},e.formatNumber=p,e.getAttributeNames=function(e){return Array.from(e.getAttributeNames())},e.getCurrentElementSelector=g,e.getElementClassList=w,e.getElementClassName=function(e){return e.className},e.getElementContent=function(e,t=!0){var n;return(t?null===(n=e.textContent)||void 0===n?void 0:n.trim():e.textContent)||""},e.getElementId=y,e.getElementPath=function(e,t=[],n=!0){if(!e||!d(e))return"";const r=[];for(;e;){const o=g(e,t,n,!0);if(n&&y(e)||e===document.body||t.some((t=>e.classList.contains(t)))||!e.parentElement)return r.unshift(o),r.join(" > ");r.unshift(o),e=e.parentElement}return r.join(" > ")},e.getElementSelector=function(e,t=[],n=!0){if(!e||!d(e))return"";const r=[];for(;e;){const o=g(e,t,n);if(n&&y(e)||e===document.body||t.some((t=>e.classList.contains(t)))||!e.parentElement)return r.unshift(o),r.join(" > ");r.unshift(o),e=e.parentElement}return r.join(" > ")},e.getElementTagName=h,e.getGlobalThis=function(){return O||(O="undefined"!=typeof globalThis?globalThis:"undefined"!=typeof self?self:"undefined"!=typeof window?window:"undefined"!=typeof global?global:void 0!==E?E:{})},e.getPathName=function(){return location.pathname},e.getReferrer=function(){return document.referrer},e.getScreenHeight=function(){return window.screen.height},e.getScreenWidth=function(){return window.screen.width},e.getTimezoneOffset=function(){return(new Date).getTimezoneOffset()},e.getTitle=function(){return document.title},e.getType=r,e.getUrl=function(){return location.href},e.getViewportHeight=function(){return window.innerHeight},e.getViewportWidth=function(){return window.innerWidth},e.hasOwn=function(e,t){return Object.prototype.hasOwnProperty.call(e,t)},e.isArr=o,e.isBool=u,e.isBrowser=function(){return"undefined"!=typeof window},e.isDate=l,e.isElement=d,e.isErrorEvent=function(e){return m(e,ErrorEvent)},e.isFn=e=>"function"==typeof e,e.isInstanceOf=m,e.isNum=s,e.isObj=e=>null!==e&&"object"==typeof e,e.isPlainObj=i,e.isPrimitive=function(e){return null===e||"object"!=typeof e&&"function"!=typeof e},e.isRegexp=a,e.isStr=c,e.isType=n,e.isUndefined=f,e.localTime=function(){return p(Date.now(),0)},e.noop=function(){},e.omit=function(e,t){const n={};for(const r in e)Object.prototype.hasOwnProperty.call(e,r)&&(t.includes(r)||(n[r]=e[r]));return n},e.pick=function(e,t){const n={};for(const r in e)Object.prototype.hasOwnProperty.call(e,r)&&t.includes(r)&&(n[r]=e[r]);return n},e.pickParse=function(e,t,n,r=[]){const o={};for(const i in e)Object.prototype.hasOwnProperty.call(e,i)&&!r.includes(i)&&(o[i]=t.includes(i)?n(e[i]):e[i]);return o},e.qs=function(e,t){const n=[];for(const r in e)Object.prototype.hasOwnProperty.call(e,r)&&n.push(`${r}=${e[r]}`);return(t||"")+n.join("&")},e.timestamp=function(){return C?p(C.now(),0):"?"},e.transProfile=function(e){const t=Object.assign({},e);for(const n in t)"function"==typeof t[n]&&(t[n]=t[n].call());return t},e.unknown="?",e.uuid4=function(){const e=window.crypto||window.msCrypto;if(e&&e.randomUUID)return e.randomUUID();const t=e&&e.getRandomValues?()=>e.getRandomValues(new Uint8Array(1))[0]:()=>16*Math.random();return([1e7]+-1e3+-4e3+-8e3+-1e11).replace(/[018]/g,(e=>(e^(15&t())>>e/4).toString(16)))},e}({});